From: Gabor Horvath Date: Tue, 8 May 2018 11:53:32 +0000 (+0000) Subject: [ASTMatchers] Overload isConstexpr for ifStmts X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3c266eaebab0bb81d8d8fb2def71fc565431b190;p=clang [ASTMatchers] Overload isConstexpr for ifStmts Differential Revision: https://reviews.llvm.org/D46233 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@331759 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index 78ddb14f95..bc58e3d9a6 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -2789,15 +2789,19 @@ auto Y() -> int {} Matcher<FunctionDecl>isConstexpr -
Matches constexpr variable and function declarations.
+
Matches constexpr variable and function declarations,
+       and if constexpr.
 
 Given:
   constexpr int foo = 42;
   constexpr int bar();
+  void baz() { if constexpr(1 > 0) {} }
 varDecl(isConstexpr())
   matches the declaration of foo.
 functionDecl(isConstexpr())
   matches the declaration of bar.
+ifStmt(isConstexpr())
+  matches the if statement in baz.
 
@@ -3039,6 +3043,23 @@ functionProtoType(parameterCountIs(3))
+Matcher<IfStmt>isConstexpr +
Matches constexpr variable and function declarations,
+       and if constexpr.
+
+Given:
+  constexpr int foo = 42;
+  constexpr int bar();
+  void baz() { if constexpr(1 > 0) {} }
+varDecl(isConstexpr())
+  matches the declaration of foo.
+functionDecl(isConstexpr())
+  matches the declaration of bar.
+ifStmt(isConstexpr())
+  matches the if statement in baz.
+
+ + Matcher<IntegerLiteral>equalsbool Value

 
@@ -3803,15 +3824,19 @@ int a;
 
 
 Matcher<VarDecl>isConstexpr
-
Matches constexpr variable and function declarations.
+
Matches constexpr variable and function declarations,
+       and if constexpr.
 
 Given:
   constexpr int foo = 42;
   constexpr int bar();
+  void baz() { if constexpr(1 > 0) {} }
 varDecl(isConstexpr())
   matches the declaration of foo.
 functionDecl(isConstexpr())
   matches the declaration of bar.
+ifStmt(isConstexpr())
+  matches the if statement in baz.
 
diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index 321cec13b5..99dbbe166c 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -3763,20 +3763,25 @@ AST_POLYMORPHIC_MATCHER(isNoThrow, return FnTy->isNothrow(); } -/// \brief Matches constexpr variable and function declarations. +/// \brief Matches constexpr variable and function declarations, +/// and if constexpr. /// /// Given: /// \code /// constexpr int foo = 42; /// constexpr int bar(); +/// void baz() { if constexpr(1 > 0) {} } /// \endcode /// varDecl(isConstexpr()) /// matches the declaration of foo. /// functionDecl(isConstexpr()) /// matches the declaration of bar. +/// ifStmt(isConstexpr()) +/// matches the if statement in baz. AST_POLYMORPHIC_MATCHER(isConstexpr, AST_POLYMORPHIC_SUPPORTED_TYPES(VarDecl, - FunctionDecl)) { + FunctionDecl, + IfStmt)) { return Node.isConstexpr(); } diff --git a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp index ad90f75409..ef385e7f18 100644 --- a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -894,6 +894,10 @@ TEST(isConstexpr, MatchesConstexprDeclarations) { varDecl(hasName("foo"), isConstexpr()))); EXPECT_TRUE(matches("constexpr int bar();", functionDecl(hasName("bar"), isConstexpr()))); + EXPECT_TRUE(matchesConditionally("void baz() { if constexpr(1 > 0) {} }", + ifStmt(isConstexpr()), true, "-std=c++17")); + EXPECT_TRUE(matchesConditionally("void baz() { if (1 > 0) {} }", + ifStmt(isConstexpr()), false, "-std=c++17")); } TEST(TemplateArgumentCountIs, Matches) {