From be065bce297cc2dd2773caf4346c218d04492913 Mon Sep 17 00:00:00 2001 From: Adam Balogh Date: Thu, 23 Nov 2017 12:43:20 +0000 Subject: [PATCH] [ASTMatchers] Matchers for new[] operators Two new matchers for `CXXNewExpr` are added which may be useful e.g. in `clang-tidy` checkers. One of them is `isArray` which matches `new[]` but not plain `new`. The other one, `hasArraySize` matches `new[]` for a given size. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@318909 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/LibASTMatchersReference.html | 20 +++++++++++++++ include/clang/ASTMatchers/ASTMatchers.h | 25 +++++++++++++++++++ lib/ASTMatchers/Dynamic/Registry.cpp | 2 ++ .../ASTMatchers/ASTMatchersNarrowingTest.cpp | 10 ++++++++ 4 files changed, 57 insertions(+) diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index e82bc3e5dc..bcd236adf5 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -2276,6 +2276,16 @@ Given +Matcher<CXXNewExpr>isArray +
Matches array new expressions.
+
+Given:
+  MyClass *p1 = new MyClass[10];
+cxxNewExpr(isArray())
+  matches the expression 'new MyClass[10]'.
+
+ + Matcher<CXXOperatorCallExpr>hasOverloadedOperatorNameStringRef Name
Matches overloaded operator names.
 
@@ -4476,6 +4486,16 @@ Example matches A() in the last line
 
+Matcher<CXXNewExpr>hasArraySizeMatcher<Expr> InnerMatcher +
Matches array new expressions with a given array size.
+
+Given:
+  MyClass *p1 = new MyClass[10];
+cxxNewExpr(hasArraySize(intgerLiteral(equals(10))))
+  matches the expression 'new MyClass[10]'.
+
+ + Matcher<CXXNewExpr>hasDeclarationMatcher<Decl> InnerMatcher
Matches a node if the declaration associated with that node
 matches the given matcher.
diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h
index 1906a57014..93fe9fbb9d 100644
--- a/include/clang/ASTMatchers/ASTMatchers.h
+++ b/include/clang/ASTMatchers/ASTMatchers.h
@@ -5828,6 +5828,31 @@ AST_MATCHER(ParmVarDecl, hasDefaultArgument) {
   return Node.hasDefaultArg(); 
 }
 
+/// \brief Matches array new expressions.
+///
+/// Given:
+/// \code
+///   MyClass *p1 = new MyClass[10];
+/// \endcode
+/// cxxNewExpr(isArray())
+///   matches the expression 'new MyClass[10]'.
+AST_MATCHER(CXXNewExpr, isArray) {
+  return Node.isArray();
+}
+
+/// \brief Matches array new expressions with a given array size.
+///
+/// Given:
+/// \code
+///   MyClass *p1 = new MyClass[10];
+/// \endcode
+/// cxxNewExpr(hasArraySize(intgerLiteral(equals(10))))
+///   matches the expression 'new MyClass[10]'.
+AST_MATCHER_P(CXXNewExpr, hasArraySize, internal::Matcher, InnerMatcher) {
+  return Node.isArray() &&
+    InnerMatcher.matches(*Node.getArraySize(), Finder, Builder);
+}
+
 } // namespace ast_matchers
 } // namespace clang
 
diff --git a/lib/ASTMatchers/Dynamic/Registry.cpp b/lib/ASTMatchers/Dynamic/Registry.cpp
index 27891b8e6a..52b8d3a6c9 100644
--- a/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -234,6 +234,7 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(hasAnyUsingShadowDecl);
   REGISTER_MATCHER(hasArgument);
   REGISTER_MATCHER(hasArgumentOfType);
+  REGISTER_MATCHER(hasArraySize);
   REGISTER_MATCHER(hasAttr);
   REGISTER_MATCHER(hasAutomaticStorageDuration);
   REGISTER_MATCHER(hasBase);
@@ -317,6 +318,7 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(isAnonymous);
   REGISTER_MATCHER(isAnyCharacter);
   REGISTER_MATCHER(isAnyPointer);
+  REGISTER_MATCHER(isArray);
   REGISTER_MATCHER(isArrow);
   REGISTER_MATCHER(isBaseInitializer);
   REGISTER_MATCHER(isBitField);
diff --git a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index 9c9304a6f8..f6b217c0cb 100644
--- a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -1998,5 +1998,15 @@ TEST(HasDefaultArgument, Basic) {
                       parmVarDecl(hasDefaultArgument())));
 }
 
+TEST(IsArray, Basic) {
+  EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];",
+                      cxxNewExpr(isArray())));
+}
+
+TEST(HasArraySize, Basic) {
+  EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];",
+                      cxxNewExpr(hasArraySize(integerLiteral(equals(10))))));
+}
+
 } // namespace ast_matchers
 } // namespace clang
-- 
2.40.0