diff options
| author | Samuel Benzaquen <sbenza@google.com> | 2014-10-27 20:58:44 +0000 |
|---|---|---|
| committer | Samuel Benzaquen <sbenza@google.com> | 2014-10-27 20:58:44 +0000 |
| commit | d93fcc1b289457e50e9738ce854bdde1b69012f7 (patch) | |
| tree | 319865a6add4396280a50a5c9b03957ca140b7e8 | |
| parent | e9afaf71f7fb9e2e60519f827a4a7c01d619be37 (diff) | |
| download | bcm5719-llvm-d93fcc1b289457e50e9738ce854bdde1b69012f7.tar.gz bcm5719-llvm-d93fcc1b289457e50e9738ce854bdde1b69012f7.zip | |
Fix segfault in hasDeclContext for nodes that have no decl context.
Summary:
Some declarations do not have a declaration context, like TranslationUnitDecl.
Fix hasDeclContext() to not segfault on these nodes.
Reviewers: klimek
Subscribers: klimek, cfe-commits
Differential Revision: http://reviews.llvm.org/D6003
llvm-svn: 220719
| -rw-r--r-- | clang/include/clang/ASTMatchers/ASTMatchers.h | 5 | ||||
| -rw-r--r-- | clang/unittests/ASTMatchers/ASTMatchersTest.cpp | 2 |
2 files changed, 5 insertions, 2 deletions
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h index fdb32f3ae92..93cb0ef6ec2 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchers.h +++ b/clang/include/clang/ASTMatchers/ASTMatchers.h @@ -3567,8 +3567,9 @@ AST_MATCHER_P(ElaboratedType, namesType, internal::Matcher<QualType>, /// \c recordDecl(hasDeclContext(namedDecl(hasName("M")))) matches the /// declaration of \c class \c D. AST_MATCHER_P(Decl, hasDeclContext, internal::Matcher<Decl>, InnerMatcher) { - return InnerMatcher.matches(*Decl::castFromDeclContext(Node.getDeclContext()), - Finder, Builder); + const DeclContext *DC = Node.getDeclContext(); + if (!DC) return false; + return InnerMatcher.matches(*Decl::castFromDeclContext(DC), Finder, Builder); } /// \brief Matches nested name specifiers. diff --git a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp index b2c2a386e85..5a8f42a87c9 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -375,6 +375,8 @@ TEST(DeclarationMatcher, hasDeclContext) { "}", recordDecl(hasDeclContext(namespaceDecl( hasName("M"), hasDeclContext(namespaceDecl())))))); + + EXPECT_TRUE(matches("class D{};", decl(hasDeclContext(decl())))); } TEST(DeclarationMatcher, LinkageSpecification) { |

