diff options
author | Malcolm Parsons <malcolm.parsons@gmail.com> | 2016-12-24 13:35:14 +0000 |
---|---|---|
committer | Malcolm Parsons <malcolm.parsons@gmail.com> | 2016-12-24 13:35:14 +0000 |
commit | 8614176ac530460aaaa4e9f593fa173f12cb760d (patch) | |
tree | 321a9a16ef9c059103551ce1272466dca16a0c95 /include/clang/ASTMatchers | |
parent | 1083e52e5a3e3c5d410816ba75077f49e96dfe1c (diff) |
[ASTMatchers] Add hasInClassInitializer traversal matcher for FieldDecl.
Summary:
I needed to know whether a FieldDecl had an in-class
initializer for D26453. I used a narrowing matcher there, but a
traversal matcher might be generally useful.
Reviewers: sbenza, bkramer, klimek, aaron.ballman
Subscribers: aaron.ballman, Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D28034
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@290492 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/clang/ASTMatchers')
-rw-r--r-- | include/clang/ASTMatchers/ASTMatchers.h | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index efa606262b..6a5224feba 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -551,6 +551,27 @@ AST_MATCHER_P(FieldDecl, hasBitWidth, unsigned, Width) { Node.getBitWidthValue(Finder->getASTContext()) == Width; } +/// \brief Matches non-static data members that have an in-class initializer. +/// +/// Given +/// \code +/// class C { +/// int a = 2; +/// int b = 3; +/// int c; +/// }; +/// \endcode +/// fieldDecl(hasInClassInitializer(integerLiteral(equals(2)))) +/// matches 'int a;' but not 'int b;'. +/// fieldDecl(hasInClassInitializer(anything())) +/// matches 'int a;' and 'int b;' but not 'int c;'. +AST_MATCHER_P(FieldDecl, hasInClassInitializer, internal::Matcher<Expr>, + InnerMatcher) { + const Expr *Initializer = Node.getInClassInitializer(); + return (Initializer != nullptr && + InnerMatcher.matches(*Initializer, Finder, Builder)); +} + /// \brief Matches a declaration that has been implicitly added /// by the compiler (eg. implicit default/copy constructors). AST_MATCHER(Decl, isImplicit) { |