[PATCH] D87048: [libTooling] Restore defaults for matchers in makeRule.

Yitzhak Mandelbaum via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 2 11:48:25 PDT 2020


ymandel created this revision.
ymandel added a reviewer: gribozavr.
Herald added a project: clang.
ymandel requested review of this revision.

This patch restores the default traversal for Transformer's `makeRule` to
`TK_AsIs`. The implicit mode has proven problematic.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D87048

Files:
  clang/lib/Tooling/Transformer/RewriteRule.cpp
  clang/unittests/Tooling/TransformerTest.cpp


Index: clang/unittests/Tooling/TransformerTest.cpp
===================================================================
--- clang/unittests/Tooling/TransformerTest.cpp
+++ clang/unittests/Tooling/TransformerTest.cpp
@@ -878,10 +878,8 @@
 }
 
 // Verifies that a rule with a top-level matcher for an implicit node (like
-// `implicitCastExpr`) does not change the code, when the AST traversal skips
-// implicit nodes. In this test, only the rule with the explicit-node matcher
-// will fire.
-TEST_F(TransformerTest, OrderedRuleImplicitIgnored) {
+// `implicitCastExpr`) works correctly -- the implicit nodes are not skipped.
+TEST_F(TransformerTest, OrderedRuleImplicitMatched) {
   std::string Input = R"cc(
     void f1();
     int f2();
@@ -892,7 +890,7 @@
     void f1();
     int f2();
     void call_f1() { REPLACE_F1; }
-    float call_f2() { return f2(); }
+    float call_f2() { return REPLACE_F2; }
   )cc";
 
   RewriteRule ReplaceF1 =
@@ -904,32 +902,6 @@
   testRule(applyFirst({ReplaceF1, ReplaceF2}), Input, Expected);
 }
 
-// Verifies that explicitly setting the traversal kind fixes the problem in the
-// previous test.
-TEST_F(TransformerTest, OrderedRuleImplicitMatched) {
-  std::string Input = R"cc(
-    void f1();
-    int f2();
-    void call_f1() { f1(); }
-    float call_f2() { return f2(); }
-  )cc";
-  std::string Expected = R"cc(
-    void f1();
-    int f2();
-    void call_f1() { REPLACE_F1; }
-    float call_f2() { return REPLACE_F2; }
-  )cc";
-
-  RewriteRule ReplaceF1 = makeRule(
-      traverse(clang::TK_AsIs, callExpr(callee(functionDecl(hasName("f1"))))),
-      changeTo(cat("REPLACE_F1")));
-  RewriteRule ReplaceF2 =
-      makeRule(traverse(clang::TK_AsIs,
-                        implicitCastExpr(hasSourceExpression(callExpr()))),
-               changeTo(cat("REPLACE_F2")));
-  testRule(applyFirst({ReplaceF1, ReplaceF2}), Input, Expected);
-}
-
 //
 // Negative tests (where we expect no transformation to occur).
 //
Index: clang/lib/Tooling/Transformer/RewriteRule.cpp
===================================================================
--- clang/lib/Tooling/Transformer/RewriteRule.cpp
+++ clang/lib/Tooling/Transformer/RewriteRule.cpp
@@ -345,14 +345,13 @@
   // Each anyOf explicitly controls the traversal kind. The anyOf itself is set
   // to `TK_AsIs` to ensure no nodes are skipped, thereby deferring to the kind
   // of the branches. Then, each branch is either left as is, if the kind is
-  // already set, or explicitly set to `TK_IgnoreUnlessSpelledInSource`. We
-  // choose this setting, because we think it is the one most friendly to
-  // beginners, who are (largely) the target audience of Transformer.
+  // already set, or explicitly set to `TK_AsIs`. We choose this setting because
+  // it is the default interpretation of matchers.
   std::vector<DynTypedMatcher> Matchers;
   for (const auto &Bucket : Buckets) {
     DynTypedMatcher M = DynTypedMatcher::constructVariadic(
         DynTypedMatcher::VO_AnyOf, Bucket.first,
-        taggedMatchers("Tag", Bucket.second, TK_IgnoreUnlessSpelledInSource));
+        taggedMatchers("Tag", Bucket.second, TK_AsIs));
     M.setAllowBind(true);
     // `tryBind` is guaranteed to succeed, because `AllowBind` was set to true.
     Matchers.push_back(M.tryBind(RootID)->withTraversalKind(TK_AsIs));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87048.289524.patch
Type: text/x-patch
Size: 3324 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200902/d945f9f8/attachment.bin>


More information about the cfe-commits mailing list