r271927 - Adding an AST matcher to ignore parenthesis in *types* (rather than expressions). This is required for traversing certain types (like function pointer types).
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 6 11:52:18 PDT 2016
Author: aaronballman
Date: Mon Jun 6 13:52:17 2016
New Revision: 271927
URL: http://llvm.org/viewvc/llvm-project?rev=271927&view=rev
Log:
Adding an AST matcher to ignore parenthesis in *types* (rather than expressions). This is required for traversing certain types (like function pointer types).
Modified:
cfe/trunk/docs/LibASTMatchersReference.html
cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h
cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp
cfe/trunk/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
Modified: cfe/trunk/docs/LibASTMatchersReference.html
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/LibASTMatchersReference.html?rev=271927&r1=271926&r2=271927&view=diff
==============================================================================
--- cfe/trunk/docs/LibASTMatchersReference.html (original)
+++ cfe/trunk/docs/LibASTMatchersReference.html Mon Jun 6 13:52:17 2016
@@ -3521,6 +3521,10 @@ Example matches X, Y
ChildT must be an AST base type.
Usable as: Any Matcher
+Note that has is direct matcher, so it also matches things like implicit
+casts and paren casts. If you are matching with expr then you should
+probably consider using ignoringParenImpCasts like:
+has(ignoringParenImpCasts(expr())).
</pre></td></tr>
@@ -4897,6 +4901,17 @@ Usable as: Matcher<<a href="http://cl
</pre></td></tr>
+<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1QualType.html">QualType</a>></td><td class="name" onclick="toggle('ignoringParens0')"><a name="ignoringParens0Anchor">ignoringParens</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1QualType.html">QualType</a>> InnerMatcher</td></tr>
+<tr><td colspan="4" class="doc" id="ignoringParens0"><pre>Matches types that match InnerMatcher after any parens are stripped.
+
+Given
+ void (*fp)(void);
+The matcher
+ varDecl(hasType(pointerType(pointee(ignoringParens(functionType())))))
+would match the declaration for fp.
+</pre></td></tr>
+
+
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1QualType.html">QualType</a>></td><td class="name" onclick="toggle('pointsTo1')"><a name="pointsTo1Anchor">pointsTo</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>> InnerMatcher</td></tr>
<tr><td colspan="4" class="doc" id="pointsTo1"><pre>Overloaded to match the pointee type's declaration.
</pre></td></tr>
Modified: cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h?rev=271927&r1=271926&r2=271927&view=diff
==============================================================================
--- cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h (original)
+++ cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h Mon Jun 6 13:52:17 2016
@@ -625,6 +625,22 @@ AST_MATCHER_P(Expr, ignoringParenImpCast
return InnerMatcher.matches(*Node.IgnoreParenImpCasts(), Finder, Builder);
}
+/// \brief Matches types that match InnerMatcher after any parens are stripped.
+///
+/// Given
+/// \code
+/// void (*fp)(void);
+/// \endcode
+/// The matcher
+/// \code
+/// varDecl(hasType(pointerType(pointee(ignoringParens(functionType())))))
+/// \endcode
+/// would match the declaration for fp.
+AST_MATCHER_P(QualType, ignoringParens,
+ internal::Matcher<QualType>, InnerMatcher) {
+ return InnerMatcher.matches(Node.IgnoreParens(), Finder, Builder);
+}
+
/// \brief Matches classTemplateSpecializations where the n'th TemplateArgument
/// matches the given InnerMatcher.
///
Modified: cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp?rev=271927&r1=271926&r2=271927&view=diff
==============================================================================
--- cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp (original)
+++ cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp Mon Jun 6 13:52:17 2016
@@ -268,6 +268,7 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(ignoringImpCasts);
REGISTER_MATCHER(ignoringParenCasts);
REGISTER_MATCHER(ignoringParenImpCasts);
+ REGISTER_MATCHER(ignoringParens);
REGISTER_MATCHER(implicitCastExpr);
REGISTER_MATCHER(implicitValueInitExpr);
REGISTER_MATCHER(incompleteArrayType);
Modified: cfe/trunk/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/ASTMatchers/ASTMatchersNodeTest.cpp?rev=271927&r1=271926&r2=271927&view=diff
==============================================================================
--- cfe/trunk/unittests/ASTMatchers/ASTMatchersNodeTest.cpp (original)
+++ cfe/trunk/unittests/ASTMatchers/ASTMatchersNodeTest.cpp Mon Jun 6 13:52:17 2016
@@ -1165,6 +1165,13 @@ TEST(TypeMatching, MatchesFunctionTypes)
EXPECT_TRUE(matches("void f(int i) {}", functionType()));
}
+TEST(TypeMatching, IgnoringParens) {
+ EXPECT_TRUE(
+ notMatches("void (*fp)(void);", pointerType(pointee(functionType()))));
+ EXPECT_TRUE(matches("void (*fp)(void);",
+ pointerType(pointee(ignoringParens(functionType())))));
+}
+
TEST(TypeMatching, MatchesFunctionProtoTypes) {
EXPECT_TRUE(matches("int (*f)(int);", functionProtoType()));
EXPECT_TRUE(matches("void f(int i);", functionProtoType()));
More information about the cfe-commits
mailing list