[PATCH] D26032: [ASTMatcher] Add operatorNew matcher for cxxNewExpr

Malcolm Parsons via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 27 06:15:18 PDT 2016


malcolm.parsons created this revision.
malcolm.parsons added reviewers: aaron.ballman, klimek.
malcolm.parsons added a subscriber: cfe-commits.

https://reviews.llvm.org/D26032

Files:
  docs/LibASTMatchersReference.html
  include/clang/ASTMatchers/ASTMatchers.h
  lib/ASTMatchers/Dynamic/Registry.cpp
  unittests/ASTMatchers/ASTMatchersTraversalTest.cpp


Index: unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
===================================================================
--- unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -2189,5 +2189,14 @@
                                        functionDecl(hasName("bar"))))));
 }
 
+TEST(CXXNewExpr, OperatorNew) {
+  std::string Fragment = "int *A = new int();";
+  EXPECT_TRUE(matches(
+      Fragment, cxxNewExpr(operatorNew(functionDecl(parameterCountIs(1))))));
+
+  EXPECT_TRUE(notMatches(
+      Fragment, cxxNewExpr(operatorNew(functionDecl(parameterCountIs(2))))));
+}
+
 } // namespace ast_matchers
 } // namespace clang
Index: lib/ASTMatchers/Dynamic/Registry.cpp
===================================================================
--- lib/ASTMatchers/Dynamic/Registry.cpp
+++ lib/ASTMatchers/Dynamic/Registry.cpp
@@ -362,6 +362,7 @@
   REGISTER_MATCHER(on);
   REGISTER_MATCHER(onImplicitObjectArgument);
   REGISTER_MATCHER(opaqueValueExpr);
+  REGISTER_MATCHER(operatorNew);
   REGISTER_MATCHER(parameterCountIs);
   REGISTER_MATCHER(parenExpr);
   REGISTER_MATCHER(parenListExpr);
Index: include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchers.h
+++ include/clang/ASTMatchers/ASTMatchers.h
@@ -4028,6 +4028,24 @@
   return Node.isVirtualAsWritten();
 }
 
+/// \brief Matches the operator new declaration that the given new expression
+/// calls
+///
+/// Example matches new expression for B in the last line
+///     (matcher = cxxNewExpr(operatorNew(functionDecl(parameterCountIs(2)))))
+/// \code
+///   void *operator new(size_t SZ, void *P) noexcept;
+///   int *A = new int();
+///   char Buffer[sizeof(int)];
+///   int *B = new (&Buffer[0]) int();
+/// \endcode
+AST_MATCHER_P(CXXNewExpr, operatorNew,
+              internal::Matcher<FunctionDecl>, InnerMatcher) {
+  const FunctionDecl *OperatorNew = Node.getOperatorNew();
+  return (OperatorNew != nullptr &&
+          InnerMatcher.matches(*OperatorNew, Finder, Builder));
+}
+
 /// \brief Matches if the given method or class declaration is final.
 ///
 /// Given:
Index: docs/LibASTMatchersReference.html
===================================================================
--- docs/LibASTMatchersReference.html
+++ docs/LibASTMatchersReference.html
@@ -4170,6 +4170,18 @@
 </pre></td></tr>
 
 
+<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1CXXNewExpr.html">CXXNewExpr</a>></td><td class="name" onclick="toggle('operatorNew')"><a name="operatorNewAnchor">operatorNew</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>> InnerMatcher</td></tr>
+<tr><td colspan="4" class="doc" id="operatorNew"><pre>Matches the operator new function declaration that the given new expression calls.
+
+Example matches new expression for B in the last line
+    (matcher = cxxNewExpr(operatorNew(functionDecl(parameterCountIs(2)))))
+  void *operator new(size_t SZ, void *P) noexcept;
+  int *A = new int();
+  char Buffer[sizeof(int)];
+  int *B = new (&Buffer[0]) int();
+</pre></td></tr>
+
+
 <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html">CXXRecordDecl</a>></td><td class="name" onclick="toggle('hasMethod0')"><a name="hasMethod0Anchor">hasMethod</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1CXXMethodDecl.html">CXXMethodDecl</a>> InnerMatcher</td></tr>
 <tr><td colspan="4" class="doc" id="hasMethod0"><pre>Matches the first method of a class or struct that satisfies InnerMatcher.
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D26032.76014.patch
Type: text/x-patch
Size: 3682 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161027/6d9096e1/attachment-0001.bin>


More information about the cfe-commits mailing list