[cfe-commits] [PATCH] New matcher for MaterializeTemporaryExpression.

Sam Panzer reviews at llvm-reviews.chandlerc.com
Mon Aug 20 11:36:23 PDT 2012


Hi klimek,

A new matcher which corresponds to the MaterializeTemporaryExpression AST node.

http://llvm-reviews.chandlerc.com/D20

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

Index: include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchers.h
+++ include/clang/ASTMatchers/ASTMatchers.h
@@ -473,6 +473,22 @@
   Stmt,
   CXXBindTemporaryExpr> bindTemporaryExpression;
 
+/// \brief Matches nodes where temporaries are materialized.
+///
+/// Example: Given
+///   struct T {void func()};
+///   T f();
+///   void g(T);
+/// materializeTempExpr() matches 'f()' in these statements
+///   T u(f());
+///   g(f());
+/// but does not match
+///   f();
+///   f().func();
+const internal::VariadicDynCastAllOfMatcher<
+  Stmt,
+  MaterializeTemporaryExpr> materializeTempExpr;
+
 /// \brief Matches new expressions.
 ///
 /// Given
Index: unittests/ASTMatchers/ASTMatchersTest.cpp
===================================================================
--- unittests/ASTMatchers/ASTMatchersTest.cpp
+++ unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -1249,6 +1249,43 @@
                  TempExpression));
 }
 
+TEST(MaterializeTempExpr, MatchesTemporary) {
+  StatementMatcher MaterializeTemp = materializeTempExpr();
+
+  std::string ClassString =
+      "class string { public: string(); int length(); }; ";
+
+  EXPECT_TRUE(
+      matches(ClassString +
+              "string GetStringByValue();"
+              "void FunctionTakesString(string s);"
+              "void run() { FunctionTakesString(GetStringByValue()); }",
+              MaterializeTemp));
+
+  EXPECT_TRUE(
+      notMatches(ClassString +
+                 "string* GetStringPointer(); "
+                 "void FunctionTakesStringPtr(string* s);"
+                 "void run() {"
+                 "  string* s = GetStringPointer();"
+                 "  FunctionTakesStringPtr(GetStringPointer());"
+                 "  FunctionTakesStringPtr(s);"
+                 "}",
+                 MaterializeTemp));
+
+  EXPECT_TRUE(
+      notMatches(ClassString +
+                 "string GetStringByValue();"
+                 "void run() { int k = GetStringByValue().length(); }",
+                 MaterializeTemp));
+
+  EXPECT_TRUE(
+      notMatches(ClassString +
+                 "string GetStringByValue();"
+                 "void run() { GetStringByValue(); }",
+                 MaterializeTemp));
+}
+
 TEST(ConstructorDeclaration, SimpleCase) {
   EXPECT_TRUE(matches("class Foo { Foo(int i); };",
                       constructor(ofClass(hasName("Foo")))));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20.1.patch
Type: text/x-patch
Size: 2451 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120820/346f9a74/attachment.bin>


More information about the cfe-commits mailing list