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
This commit is contained in:
Samuel Benzaquen 2014-10-27 20:58:44 +00:00
parent e9afaf71f7
commit d93fcc1b28
2 changed files with 5 additions and 2 deletions

View File

@ -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.

View File

@ -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) {