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

Sam Panzer reviews at llvm-reviews.chandlerc.com
Fri Aug 24 11:08:38 PDT 2012


  Added \code blocks and simplified test case matcher, as suggested.

Hi klimek,

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

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D20?vs=44&id=66#differential-review-toc

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
@@ -552,6 +552,28 @@
   Stmt,
   CXXBindTemporaryExpr> bindTemporaryExpr;
 
+/// \brief Matches nodes where temporaries are materialized.
+///
+/// Example: Given
+/// \code
+///   struct T {void func()};
+///   T f();
+///   void g(T);
+/// \endcode
+/// materializeTempExpr() matches 'f()' in these statements
+/// \code
+///   T u(f());
+///   g(f());
+/// \endcode
+/// but does not match
+/// \code
+///   f();
+///   f().func();
+/// \endcode
+const internal::VariadicDynCastAllOfMatcher<
+  Stmt,
+  MaterializeTemporaryExpr> materializeTempExpr;
+
 /// \brief Matches new expressions.
 ///
 /// Given
Index: unittests/ASTMatchers/ASTMatchersTest.cpp
===================================================================
--- unittests/ASTMatchers/ASTMatchersTest.cpp
+++ unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -1296,6 +1296,41 @@
                  TempExpression));
 }
 
+TEST(MaterializeTempExpr, MatchesTemporary) {
+  std::string ClassString =
+      "class string { public: string(); int length(); }; ";
+
+  EXPECT_TRUE(
+      matches(ClassString +
+              "string GetStringByValue();"
+              "void FunctionTakesString(string s);"
+              "void run() { FunctionTakesString(GetStringByValue()); }",
+              materializeTempExpr()));
+
+  EXPECT_TRUE(
+      notMatches(ClassString +
+                 "string* GetStringPointer(); "
+                 "void FunctionTakesStringPtr(string* s);"
+                 "void run() {"
+                 "  string* s = GetStringPointer();"
+                 "  FunctionTakesStringPtr(GetStringPointer());"
+                 "  FunctionTakesStringPtr(s);"
+                 "}",
+                 materializeTempExpr()));
+
+  EXPECT_TRUE(
+      notMatches(ClassString +
+                 "string GetStringByValue();"
+                 "void run() { int k = GetStringByValue().length(); }",
+                 materializeTempExpr()));
+
+  EXPECT_TRUE(
+      notMatches(ClassString +
+                 "string GetStringByValue();"
+                 "void run() { GetStringByValue(); }",
+                 materializeTempExpr()));
+}
+
 TEST(ConstructorDeclaration, SimpleCase) {
   EXPECT_TRUE(matches("class Foo { Foo(int i); };",
                       constructorDecl(ofClass(hasName("Foo")))));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20.2.patch
Type: text/x-patch
Size: 2485 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120824/a28b6c44/attachment.bin>


More information about the cfe-commits mailing list