diff options
author | Hyrum Wright <hwright@google.com> | 2019-01-07 14:14:36 +0000 |
---|---|---|
committer | Hyrum Wright <hwright@google.com> | 2019-01-07 14:14:36 +0000 |
commit | d21770300d16818ce8bd7c5ebbfb79ef323bc5f7 (patch) | |
tree | 1547ccb728170778b588d2324f8ea651129773c2 | |
parent | 1aa1a6f871aca22a9dcca8fef4fb49b41cfd3582 (diff) | |
download | clang-d21770300d16818ce8bd7c5ebbfb79ef323bc5f7.tar.gz |
[clang] Add AST matcher for initializer list members
Summary:
Much like hasArg for various call expressions, this allows LibTooling users to
match against a member of an initializer list.
This is currently being used as part of the abseil-duration-scale clang-tidy
check.
Differential Revision: https://reviews.llvm.org/D56090
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@350523 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | docs/LibASTMatchersReference.html | 11 | ||||
-rw-r--r-- | include/clang/ASTMatchers/ASTMatchers.h | 13 | ||||
-rw-r--r-- | lib/ASTMatchers/Dynamic/Registry.cpp | 1 | ||||
-rw-r--r-- | unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp | 12 |
4 files changed, 36 insertions, 1 deletions
diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index 27cb00b6bd..2af0a7c9e3 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -718,7 +718,7 @@ Example matches a || b <tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>></td><td class="name" onclick="toggle('blockExpr0')"><a name="blockExpr0Anchor">blockExpr</a></td><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1BlockExpr.html">BlockExpr</a>>...</td></tr> -<tr><td colspan="4" class="doc" id="blockExpr0"><pre>MAtches a reference to a block. +<tr><td colspan="4" class="doc" id="blockExpr0"><pre>Matches a reference to a block. Example: matches "^{}": void f() { ^{}(); } @@ -5866,6 +5866,15 @@ FIXME: Unit test this matcher </pre></td></tr> +<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1InitListExpr.html">InitListExpr</a>></td><td class="name" onclick="toggle('hasInit0')"><a name="hasInit0Anchor">hasInit</a></td><td>unsigned N, ast_matchers::Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>> InnerMatcher</td></tr> +<tr><td colspan="4" class="doc" id="hasInit0"><pre>Matches the n'th item of an initializer list expression. + +Example matches y. + (matcher = initListExpr(hasInit(0, expr()))) + int x{y}. +</pre></td></tr> + + <tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1InitListExpr.html">InitListExpr</a>></td><td class="name" onclick="toggle('hasSyntacticForm0')"><a name="hasSyntacticForm0Anchor">hasSyntacticForm</a></td><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>> InnerMatcher</td></tr> <tr><td colspan="4" class="doc" id="hasSyntacticForm0"><pre>Matches the syntactic form of init list expressions (if expression have it). diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index 7e35f0667a..5cdb964a92 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -3514,6 +3514,19 @@ AST_POLYMORPHIC_MATCHER_P2(hasArgument, *Node.getArg(N)->IgnoreParenImpCasts(), Finder, Builder)); } +/// Matches the n'th item of an initializer list expression. +/// +/// Example matches y. +/// (matcher = initListExpr(hasInit(0, expr()))) +/// \code +/// int x{y}. +/// \endcode +AST_MATCHER_P2(InitListExpr, hasInit, unsigned, N, + ast_matchers::internal::Matcher<Expr>, InnerMatcher) { + return N < Node.getNumInits() && + InnerMatcher.matches(*Node.getInit(N), Finder, Builder); +} + /// Matches declaration statements that contain a specific number of /// declarations. /// diff --git a/lib/ASTMatchers/Dynamic/Registry.cpp b/lib/ASTMatchers/Dynamic/Registry.cpp index 0bef326ca5..e6e4846796 100644 --- a/lib/ASTMatchers/Dynamic/Registry.cpp +++ b/lib/ASTMatchers/Dynamic/Registry.cpp @@ -273,6 +273,7 @@ RegistryMaps::RegistryMaps() { REGISTER_MATCHER(hasInClassInitializer); REGISTER_MATCHER(hasIncrement); REGISTER_MATCHER(hasIndex); + REGISTER_MATCHER(hasInit); REGISTER_MATCHER(hasInitializer); REGISTER_MATCHER(hasKeywordSelector); REGISTER_MATCHER(hasLHS); diff --git a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp index d1f9495432..fb17d100c5 100644 --- a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -2255,6 +2255,18 @@ TEST(IsAssignmentOperator, Basic) { notMatches("void x() { int a; if(a == 0) return; }", BinAsgmtOperator)); } +TEST(HasInit, Basic) { + EXPECT_TRUE( + matches("int x{0};", + initListExpr(hasInit(0, expr())))); + EXPECT_FALSE( + matches("int x{0};", + initListExpr(hasInit(1, expr())))); + EXPECT_FALSE( + matches("int x;", + initListExpr(hasInit(0, expr())))); +} + TEST(Matcher, isMain) { EXPECT_TRUE( matches("int main() {}", functionDecl(isMain()))); |