[clang-tools-extra] r273910 - clang-rename: add a -export-fixes option

Miklos Vajna via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 27 12:34:48 PDT 2016


Author: vmiklos
Date: Mon Jun 27 14:34:47 2016
New Revision: 273910

URL: http://llvm.org/viewvc/llvm-project?rev=273910&view=rev
Log:
clang-rename: add a -export-fixes option

Use case: a class is declared in a header, and defined in two
translation units. clang-rename is asked to rename a class member that's
referenced in both translation units.

Using -i is not possible, as in case the first clang-rename invocation
touches the header, the second invocation will result in compilation
errors. Using -export-fixes handles this situation, each invocation can
work on the original source, and at the end the user can apply the
replacements with clang-apply-replacements.

Reviewers: klimek

Differential Revision: http://reviews.llvm.org/D21676

Added:
    clang-tools-extra/trunk/test/clang-rename/ClassTestReplacements.cpp
Modified:
    clang-tools-extra/trunk/clang-rename/tool/ClangRename.cpp

Modified: clang-tools-extra/trunk/clang-rename/tool/ClangRename.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-rename/tool/ClangRename.cpp?rev=273910&r1=273909&r2=273910&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/tool/ClangRename.cpp (original)
+++ clang-tools-extra/trunk/clang-rename/tool/ClangRename.cpp Mon Jun 27 14:34:47 2016
@@ -32,6 +32,7 @@
 #include "clang/Rewrite/Core/Rewriter.h"
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/Refactoring.h"
+#include "clang/Tooling/ReplacementsYaml.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/Support/Host.h"
@@ -72,6 +73,12 @@ PrintLocations(
     "pl",
     cl::desc("Print the locations affected by renaming to stderr."),
     cl::cat(ClangRenameCategory));
+static cl::opt<std::string>
+ExportFixes(
+    "export-fixes",
+    cl::desc("YAML file to store suggested fixes in."),
+    cl::value_desc("filename"),
+    cl::cat(ClangRenameCategory));
 
 #define CLANG_RENAME_VERSION "0.0.1"
 
@@ -126,6 +133,26 @@ int main(int argc, const char **argv) {
   } else {
     res = Tool.run(Factory.get());
 
+    if (!ExportFixes.empty()) {
+      std::error_code EC;
+      llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::F_None);
+      if (EC) {
+        llvm::errs() << "Error opening output file: " << EC.message() << '\n';
+        exit(1);
+      }
+
+      // Export replacements.
+      tooling::TranslationUnitReplacements TUR;
+      const tooling::Replacements &Replacements = Tool.getReplacements();
+      TUR.Replacements.insert(TUR.Replacements.end(), Replacements.begin(),
+                              Replacements.end());
+
+      yaml::Output YAML(OS);
+      YAML << TUR;
+      OS.close();
+      exit(0);
+    }
+
     // Write every file to stdout. Right now we just barf the files without any
     // indication of which files start where, other than that we print the files
     // in the same order we see them.

Added: clang-tools-extra/trunk/test/clang-rename/ClassTestReplacements.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-rename/ClassTestReplacements.cpp?rev=273910&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-rename/ClassTestReplacements.cpp (added)
+++ clang-tools-extra/trunk/test/clang-rename/ClassTestReplacements.cpp Mon Jun 27 14:34:47 2016
@@ -0,0 +1,11 @@
+// RUN: mkdir -p %T/fixes
+// RUN: cat %s > %t.cpp
+// RUN: clang-rename -offset=225 -new-name=Hector -export-fixes=%T/fixes.yaml %t.cpp --
+// RUN: clang-apply-replacements %T
+// RUN: sed 's,//.*,,' %t.cpp | FileCheck %s
+class Cla  // CHECK: class Hector
+{
+};
+
+// Use grep -FUbo 'Cla' <file> to get the correct offset of Cla when changing
+// this file.




More information about the cfe-commits mailing list