]> granicus.if.org Git - clang/commitdiff
Adding new AST matcher: isConstexpr
authorSzabolcs Sipos <szabolcs.sipos@ericsson.com>
Fri, 22 May 2015 11:35:50 +0000 (11:35 +0000)
committerSzabolcs Sipos <szabolcs.sipos@ericsson.com>
Fri, 22 May 2015 11:35:50 +0000 (11:35 +0000)
It matches constexpr variable and function declarations.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@238016 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/ASTMatchers/ASTMatchers.h
unittests/ASTMatchers/ASTMatchersTest.cpp

index 94906d190bff32f3aae5cb965ba2fc42a87672b3..7f9764619c2b2f92911d14b924082bef25bfe42d 100644 (file)
@@ -2709,6 +2709,23 @@ AST_MATCHER(FunctionDecl, isDeleted) {
   return Node.isDeleted();
 }
 
+/// \brief Matches constexpr variable and function declarations.
+///
+/// Given:
+/// \code
+///   constexpr int foo = 42;
+///   constexpr int bar();
+/// \endcode
+/// varDecl(isConstexpr())
+///   matches the declaration of foo.
+/// functionDecl(isConstexpr())
+///   matches the declaration of bar.
+AST_POLYMORPHIC_MATCHER(isConstexpr,
+                        AST_POLYMORPHIC_SUPPORTED_TYPES(VarDecl,
+                                                        FunctionDecl)) {
+  return Node.isConstexpr();
+}
+
 /// \brief Matches the condition expression of an if statement, for loop,
 /// or conditional operator.
 ///
index 6f1cf3fe2c827325787f9ad0bfb6f938ac4dace2..ae363e974b5d4c1284cbf64927a44c34e0b6510f 100644 (file)
@@ -1595,6 +1595,13 @@ TEST(IsDeleted, MatchesDeletedFunctionDeclarations) {
                       functionDecl(hasName("Func"), isDeleted())));
 }
 
+TEST(isConstexpr, MatchesConstexprDeclarations) {
+  EXPECT_TRUE(matches("constexpr int foo = 42;",
+                      varDecl(hasName("foo"), isConstexpr())));
+  EXPECT_TRUE(matches("constexpr int bar();",
+                      functionDecl(hasName("bar"), isConstexpr())));
+}
+
 TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
   EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
       methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));