Skip to content

[clang][AST] Fix AST IgnoreUnlessSpelledInSource traversal nullptr dereference #146103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,7 @@ Bug Fixes to AST Handling
- Fixed a malformed printout of certain calling convention function attributes. (#GH143160)
- Fixed dependency calculation for TypedefTypes (#GH89774)
- Fixed the right parenthesis source location of ``CXXTemporaryObjectExpr``. (#GH143711)
- Fixed a crash when performing an ``IgnoreUnlessSpelledInSource`` traversal of ASTs containing ``catch(...)`` statements. (#GH146103)

Miscellaneous Bug Fixes
^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/AST/ASTNodeTraverser.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ASTNodeTraverser
TraversalKind GetTraversalKind() const { return Traversal; }

void Visit(const Decl *D, bool VisitLocs = false) {
if (Traversal == TK_IgnoreUnlessSpelledInSource && D->isImplicit())
if (Traversal == TK_IgnoreUnlessSpelledInSource && D && D->isImplicit())
return;

getNodeDelegate().AddChild([=] {
Expand Down
75 changes: 75 additions & 0 deletions clang/unittests/AST/ASTTraverserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class NodeTreePrinter : public TextTreeStructure {
: TextTreeStructure(OS, /* showColors */ false), OS(OS) {}

void Visit(const Decl *D) {
if (!D) {
OS << "<<<NULL>>>";
return;
}
OS << D->getDeclKindName() << "Decl";
if (auto *ND = dyn_cast<NamedDecl>(D)) {
OS << " '" << ND->getDeclName() << "'";
Expand Down Expand Up @@ -1932,4 +1936,75 @@ CXXRewrittenBinaryOperator
}
}

TEST(Traverse, CatchStatements) {

auto AST = buildASTFromCode(R"cpp(
void test()
{
try
{
int a;
}
catch (...)
{
int b;
}

try
{
int a;
}
catch (const int&)
{
int b;
}
}
)cpp");

auto BN =
ast_matchers::match(cxxCatchStmt().bind("catch"), AST->getASTContext());
EXPECT_EQ(BN.size(), 2u);
const auto *catchWithoutDecl = BN[0].getNodeAs<Stmt>("catch");

llvm::StringRef Expected = R"cpp(
CXXCatchStmt
|-<<<NULL>>>
`-CompoundStmt
`-DeclStmt
`-VarDecl 'b'
)cpp";
EXPECT_EQ(dumpASTString(TK_AsIs, catchWithoutDecl), Expected);

Expected = R"cpp(
CXXCatchStmt
|-<<<NULL>>>
`-CompoundStmt
`-DeclStmt
`-VarDecl 'b'
)cpp";
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, catchWithoutDecl),
Expected);

const auto *catchWithDecl = BN[1].getNodeAs<Stmt>("catch");

Expected = R"cpp(
CXXCatchStmt
|-VarDecl ''
`-CompoundStmt
`-DeclStmt
`-VarDecl 'b'
)cpp";
EXPECT_EQ(dumpASTString(TK_AsIs, catchWithDecl), Expected);

Expected = R"cpp(
CXXCatchStmt
|-VarDecl ''
`-CompoundStmt
`-DeclStmt
`-VarDecl 'b'
)cpp";
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, catchWithDecl),
Expected);
}

} // namespace clang
Loading