[PATCH] D44729: [ASTMatchers] Add hasSubExpr() matcher.

Clement Courbet via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 21 03:52:49 PDT 2018


courbet created this revision.
courbet added reviewers: aaron.ballman, alexfh.
Herald added a subscriber: klimek.

This is needed to implement more checks in https://reviews.llvm.org/D38455.


Repository:
  rC Clang

https://reviews.llvm.org/D44729

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


Index: unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
===================================================================
--- unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -1232,6 +1232,13 @@
     cxxMethodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
 }
 
+TEST(HasSubExpr, MatchesSimpleCase) {
+  EXPECT_TRUE(matches("int i = 0.0;",
+                      implicitCastExpr(hasSubExpr(floatLiteral()))));
+  EXPECT_TRUE(notMatches("int i = '0';",
+                      implicitCastExpr(hasSubExpr(floatLiteral()))));
+}
+
 TEST(HasDestinationType, MatchesSimpleCase) {
   EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
                       cxxStaticCastExpr(hasDestinationType(
Index: include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchers.h
+++ include/clang/ASTMatchers/ASTMatchers.h
@@ -4109,6 +4109,21 @@
   return InnerMatcher.matches(Node.getType(), Finder, Builder);
 }
 
+/// \brief Matches casts whose subexpr matches a given matcher.
+///
+/// Example matches the first cast
+///     (matcher = implicitCastExpr(hasSubExpr(floatLiteral())))))
+///   int i = 0.0;
+///   int j = '0';
+AST_POLYMORPHIC_MATCHER_P(
+    hasSubExpr,
+    AST_POLYMORPHIC_SUPPORTED_TYPES(CastExpr),
+    internal::Matcher<Expr>, InnerMatcher) {
+  const Expr *const SubExpr = Node.getSubExpr();
+  return (SubExpr != nullptr &&
+          InnerMatcher.matches(*SubExpr, Finder, Builder));
+}
+
 /// \brief Matches RecordDecl object that are spelled with "struct."
 ///
 /// Example matches S, but not C or U.
Index: docs/LibASTMatchersReference.html
===================================================================
--- docs/LibASTMatchersReference.html
+++ docs/LibASTMatchersReference.html
@@ -4781,6 +4781,16 @@
 </pre></td></tr>
 
 
+<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1CastExpr.html">CastExpr</a>></td><td class="name" onclick="toggle('hasSubExpr0')"><a name="hasSubExpr0Anchor">hasSubExpr</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>> InnerMatcher</td></tr>
+<tr><td colspan="4" class="doc" id="hasSubExpr0"><pre>Matches casts whose subexpr matches a given matcher.
+
+Example matches the first cast
+    (matcher = implicitCastExpr(hasSubExpr(floatLiteral())))))
+  int i = 0.0;
+  int j = '0';
+</pre></td></tr>
+
+
 <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ClassTemplateSpecializationDecl.html">ClassTemplateSpecializationDecl</a>></td><td class="name" onclick="toggle('hasAnyTemplateArgument0')"><a name="hasAnyTemplateArgument0Anchor">hasAnyTemplateArgument</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1TemplateArgument.html">TemplateArgument</a>> InnerMatcher</td></tr>
 <tr><td colspan="4" class="doc" id="hasAnyTemplateArgument0"><pre>Matches classTemplateSpecializations, templateSpecializationType and
 functionDecl that have at least one TemplateArgument matching the given


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44729.139273.patch
Type: text/x-patch
Size: 3123 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180321/0cd7dda5/attachment.bin>


More information about the cfe-commits mailing list