[clang] 6f8f5cb - [libTooling] Add function to Transformer to create a no-op edit.

Yitzhak Mandelbaum via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 22 14:32:35 PDT 2020


Author: Yitzhak Mandelbaum
Date: 2020-10-22T21:29:03Z
New Revision: 6f8f5cb77efd100e5d4916db871b18c88cf49ed0

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

LOG: [libTooling] Add function to Transformer to create a no-op edit.

This functionality is commonly needed in clang tidy checks (based on
transformer) that only print warnings, without suggesting any edits. The no-op
edit allows the user to associate a diagnostic message with a source location.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Tooling/Transformer/RewriteRule.h b/clang/include/clang/Tooling/Transformer/RewriteRule.h
index 4bdcc8d5c329..13d0edaf11b3 100644
--- a/clang/include/clang/Tooling/Transformer/RewriteRule.h
+++ b/clang/include/clang/Tooling/Transformer/RewriteRule.h
@@ -107,7 +107,7 @@ struct ASTEdit {
   TextGenerator Replacement;
   TextGenerator Note;
   // Not all transformations will want or need to attach metadata and therefore
-  // should not be requierd to do so.
+  // should not be required to do so.
   AnyGenerator Metadata = [](const ast_matchers::MatchFinder::MatchResult &)
       -> llvm::Expected<llvm::Any> {
     return llvm::Expected<llvm::Any>(llvm::Any());
@@ -131,6 +131,11 @@ EditGenerator editList(llvm::SmallVector<ASTEdit, 1> Edits);
 /// Generates no edits.
 inline EditGenerator noEdits() { return editList({}); }
 
+/// Generates a single, no-op edit anchored at the start location of the
+/// specified range. A `noopEdit` may be preferred over `noEdits` to associate a
+/// diagnostic `Explanation` with the rule.
+EditGenerator noopEdit(RangeSelector Anchor);
+
 /// Version of `ifBound` specialized to `ASTEdit`.
 inline EditGenerator ifBound(std::string ID, ASTEdit TrueEdit,
                              ASTEdit FalseEdit) {

diff  --git a/clang/lib/Tooling/Transformer/RewriteRule.cpp b/clang/lib/Tooling/Transformer/RewriteRule.cpp
index 03921e0ea7de..e0467d2149cc 100644
--- a/clang/lib/Tooling/Transformer/RewriteRule.cpp
+++ b/clang/lib/Tooling/Transformer/RewriteRule.cpp
@@ -73,6 +73,24 @@ EditGenerator transformer::edit(ASTEdit Edit) {
   };
 }
 
+EditGenerator transformer::noopEdit(RangeSelector Anchor) {
+  return [Anchor = std::move(Anchor)](const MatchResult &Result)
+             -> Expected<SmallVector<transformer::Edit, 1>> {
+    Expected<CharSourceRange> Range = Anchor(Result);
+    if (!Range)
+      return Range.takeError();
+    // In case the range is inside a macro expansion, map the location back to a
+    // "real" source location.
+    SourceLocation Begin =
+        Result.SourceManager->getSpellingLoc(Range->getBegin());
+    Edit E;
+    // Implicitly, leave `E.Replacement` as the empty string.
+    E.Kind = EditKind::Range;
+    E.Range = CharSourceRange::getCharRange(Begin, Begin);
+    return SmallVector<Edit, 1>{E};
+  };
+}
+
 EditGenerator
 transformer::flattenVector(SmallVector<EditGenerator, 2> Generators) {
   if (Generators.size() == 1)

diff  --git a/clang/unittests/Tooling/TransformerTest.cpp b/clang/unittests/Tooling/TransformerTest.cpp
index a8d6d3dd851d..ff735cd73198 100644
--- a/clang/unittests/Tooling/TransformerTest.cpp
+++ b/clang/unittests/Tooling/TransformerTest.cpp
@@ -426,6 +426,14 @@ TEST_F(TransformerTest, NoEdits) {
   testRule(makeRule(returnStmt().bind("return"), noEdits()), Input, Input);
 }
 
+TEST_F(TransformerTest, NoopEdit) {
+  using transformer::node;
+  using transformer::noopEdit;
+  std::string Input = "int f(int x) { return x; }";
+  testRule(makeRule(returnStmt().bind("return"), noopEdit(node("return"))),
+           Input, Input);
+}
+
 TEST_F(TransformerTest, IfBound2Args) {
   using transformer::ifBound;
   std::string Input = "int f(int x) { return x; }";


        


More information about the cfe-commits mailing list