[clang] 6f0a371 - [libTooling] Restore defaults for matchers in makeRule.

Yitzhak Mandelbaum via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 2 12:37:12 PDT 2020


Author: Yitzhak Mandelbaum
Date: 2020-09-02T19:36:14Z
New Revision: 6f0a3711bc15f8b50ad56d64eee70d9ba62f70c6

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

LOG: [libTooling] Restore defaults for matchers in makeRule.

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

Differential Revision: https://reviews.llvm.org/D87048

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Tooling/Transformer/RewriteRule.cpp b/clang/lib/Tooling/Transformer/RewriteRule.cpp
index fe33f9cf8b0c..594e22f56b87 100644
--- a/clang/lib/Tooling/Transformer/RewriteRule.cpp
+++ b/clang/lib/Tooling/Transformer/RewriteRule.cpp
@@ -345,14 +345,13 @@ transformer::detail::buildMatchers(const RewriteRule &Rule) {
   // 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));

diff  --git a/clang/unittests/Tooling/TransformerTest.cpp b/clang/unittests/Tooling/TransformerTest.cpp
index 26158b1520f9..2c9bd7dfd32d 100644
--- a/clang/unittests/Tooling/TransformerTest.cpp
+++ b/clang/unittests/Tooling/TransformerTest.cpp
@@ -878,10 +878,8 @@ TEST_F(TransformerTest, OrderedRuleMultipleKinds) {
 }
 
 // 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 @@ TEST_F(TransformerTest, OrderedRuleImplicitIgnored) {
     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 @@ TEST_F(TransformerTest, OrderedRuleImplicitIgnored) {
   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).
 //


        


More information about the cfe-commits mailing list