[clang] ef1a416 - [Transformer] Split ForStmt test into two

Stephen Kelly via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 17 10:17:17 PST 2020


Author: Stephen Kelly
Date: 2020-11-17T18:16:10Z
New Revision: ef1a4169e8c7f6cf76e704827f4567b9a1b9b0e1

URL: https://github.com/llvm/llvm-project/commit/ef1a4169e8c7f6cf76e704827f4567b9a1b9b0e1
DIFF: https://github.com/llvm/llvm-project/commit/ef1a4169e8c7f6cf76e704827f4567b9a1b9b0e1.diff

LOG: [Transformer] Split ForStmt test into two

It is apparently not possible to have two rewrites in one gtest function
because atomic changes in the test harness accumulate.

Added: 
    

Modified: 
    clang/unittests/Tooling/TransformerTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/unittests/Tooling/TransformerTest.cpp b/clang/unittests/Tooling/TransformerTest.cpp
index d0ebe5007723..773420c015cd 100644
--- a/clang/unittests/Tooling/TransformerTest.cpp
+++ b/clang/unittests/Tooling/TransformerTest.cpp
@@ -1219,7 +1219,7 @@ void testIt()
 
   auto RewriteOutput =
       CodePrefix + RangeLoop + LoopBody + RangeLoop + LoopBody + CodeSuffix;
-  {
+
     auto MatchedLoop = forStmt(
         has(declStmt(
             hasSingleDecl(varDecl(hasInitializer(integerLiteral(equals(0))))
@@ -1244,8 +1244,55 @@ void testIt()
 
     testRuleFailure(makeRule(traverse(TK_AsIs, MatchedLoop), RewriteRule),
                     RewriteInput);
-  }
-  {
+
+}
+
+TEST_F(TransformerTest, ImplicitNodes_ForStmt2) {
+
+  std::string CodePrefix = R"cpp(
+struct NonTrivial {
+    NonTrivial() {}
+    NonTrivial(NonTrivial&) {}
+    NonTrivial& operator=(NonTrivial const&) { return *this; }
+
+    ~NonTrivial() {}
+};
+
+struct ContainsArray {
+    NonTrivial arr[2];
+    ContainsArray& operator=(ContainsArray const&) = default;
+};
+
+void testIt()
+{
+    ContainsArray ca1;
+    ContainsArray ca2;
+    ca2 = ca1;
+)cpp";
+
+  auto CodeSuffix = "}";
+
+  auto LoopBody = R"cpp(
+    {
+
+    }
+)cpp";
+
+  auto RawLoop = "for (auto i = 0; i != 5; ++i)";
+
+  auto RangeLoop = "for (auto i : boost::irange(5))";
+
+  // Expect to rewrite the raw loop to the ranged loop.
+  // This works in TK_IgnoreUnlessSpelledInSource mode, but TK_AsIs
+  // mode also matches the hidden for loop generated in the copy assignment
+  // operator of ContainsArray. Transformer then fails to transform the code at
+  // all.
+
+  auto RewriteInput =
+      CodePrefix + RawLoop + LoopBody + RawLoop + LoopBody + CodeSuffix;
+
+  auto RewriteOutput =
+      CodePrefix + RangeLoop + LoopBody + RangeLoop + LoopBody + CodeSuffix;
     auto MatchedLoop = forStmt(
         hasLoopInit(declStmt(
             hasSingleDecl(varDecl(hasInitializer(integerLiteral(equals(0))))
@@ -1264,13 +1311,13 @@ void testIt()
                  cat("auto ", name("loopVar"), " : boost::irange(",
                      node("upperBoundExpr"), ")"));
 
-    // testRule(makeRule(traverse(TK_IgnoreUnlessSpelledInSource, MatchedLoop),
-    //                   RewriteRule),
-    //          RewriteInput, RewriteOutput);
+    testRule(makeRule(traverse(TK_IgnoreUnlessSpelledInSource, MatchedLoop),
+                      RewriteRule),
+             RewriteInput, RewriteOutput);
 
     testRuleFailure(makeRule(traverse(TK_AsIs, MatchedLoop), RewriteRule),
                     RewriteInput);
-  }
+
 }
 
 TEST_F(TransformerTest, TemplateInstantiation) {


        


More information about the cfe-commits mailing list