]> granicus.if.org Git - clang/commitdiff
clang-rename: let -force handle multiple renames
authorMiklos Vajna <vmiklos@vmiklos.hu>
Mon, 11 Sep 2017 20:18:38 +0000 (20:18 +0000)
committerMiklos Vajna <vmiklos@vmiklos.hu>
Mon, 11 Sep 2017 20:18:38 +0000 (20:18 +0000)
Summary:
The use case is that renaming multiple symbols in a large enough codebase is
much faster if all of these can be done with a single invocation, but
there will be multiple translation units where one or more symbols are
not found.

Old behavior was to exit with an error (default) or exit without
reporting an error (-force). New behavior is that -force results in a
best-effort rename: rename symbols which are found and just ignore the
rest.

The existing help for -force sort of already implies this behavior.

Reviewers: cfe-commits, klimek, arphaman

Reviewed By: klimek

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@312942 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Tooling/Refactoring/Rename/RenamingAction.cpp
lib/Tooling/Refactoring/Rename/USRFindingAction.cpp
test/clang-rename/ForceMulti.cpp [new file with mode: 0644]
tools/clang-rename/ClangRename.cpp

index 9c000a72046ec26b60299abca5c9ad5aa6f90a1a..0aed67f5324cf2bdb80b2cf17c4e5bbbd9267a11 100644 (file)
@@ -84,8 +84,13 @@ public:
         FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {}
 
   void HandleTranslationUnit(ASTContext &Context) override {
-    for (unsigned I = 0; I < NewNames.size(); ++I)
+    for (unsigned I = 0; I < NewNames.size(); ++I) {
+      // If the previous name was not found, ignore this rename request.
+      if (PrevNames[I].empty())
+        continue;
+
       HandleOneRename(Context, NewNames[I], PrevNames[I], USRList[I]);
+    }
   }
 
   void HandleOneRename(ASTContext &Context, const std::string &NewName,
index bac3963e1bd9b5a3360cddf0c500fd9d4a3ed8ad..43287acb95e11889c0bdf4d8a9a7909ddf9f58e4 100644 (file)
@@ -198,8 +198,11 @@ private:
         return false;
       }
 
-      if (Force)
+      if (Force) {
+        SpellingNames.push_back(std::string());
+        USRList.push_back(std::vector<std::string>());
         return true;
+      }
 
       unsigned CouldNotFindSymbolNamed = Engine.getCustomDiagID(
           DiagnosticsEngine::Error, "clang-rename could not find symbol %0");
diff --git a/test/clang-rename/ForceMulti.cpp b/test/clang-rename/ForceMulti.cpp
new file mode 100644 (file)
index 0000000..41983ce
--- /dev/null
@@ -0,0 +1,8 @@
+class B /* Test 1 */ { // CHECK: class B2 /* Test 1 */ {
+};
+
+class D : public B /* Test 1 */ { // CHECK: class D : public B2 /* Test 1 */ {
+};
+
+// Test 1.
+// RUN: clang-rename -force -qualified-name B -new-name B2 -qualified-name E -new-name E2 %s -- | sed 's,//.*,,' | FileCheck %s
index cc18a05bcdbe61c1635d6ce5ad5597bba209e2a4..fca12ca60c4b395cb53c43d8c1a079f52f7cc954 100644 (file)
@@ -175,12 +175,6 @@ int main(int argc, const char **argv) {
     return 1;
   }
 
-  if (Force && PrevNames.size() < NewNames.size()) {
-    // No matching PrevName for all NewNames. Without Force this is an error
-    // above already.
-    return 0;
-  }
-
   // Perform the renaming.
   tooling::RenamingAction RenameAction(NewNames, PrevNames, USRList,
                                        Tool.getReplacements(), PrintLocations);