[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 12:37:18 PDT 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rG6f0a3711bc15: [libTooling] Restore defaults for matchers in makeRule. (authored by ymandel).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D87048/new/

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.289543.patch
Type: text/x-patch
Size: 3324 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200902/54e9b363/attachment.bin>


More information about the cfe-commits mailing list