[clang-tools-extra] r304534 - clang-rename: add new -force option
Miklos Vajna via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 2 02:32:28 PDT 2017
Author: vmiklos
Date: Fri Jun 2 04:32:28 2017
New Revision: 304534
URL: http://llvm.org/viewvc/llvm-project?rev=304534&view=rev
Log:
clang-rename: add new -force option
Summary:
The use-case is when renaming a widely used name, like a lower-level
class in a codebase and clang-rename is simply invoked for each
translation unit based on the compile database. In this case it's not
interesting to show errors: not finding the symbol means there is
simply nothing to do.
Reviewers: klimek
Reviewed By: klimek
Differential Revision: https://reviews.llvm.org/D32403
Added:
clang-tools-extra/trunk/test/clang-rename/InvalidQualifiedName.cpp
Modified:
clang-tools-extra/trunk/clang-rename/USRFindingAction.cpp
clang-tools-extra/trunk/clang-rename/USRFindingAction.h
clang-tools-extra/trunk/clang-rename/tool/ClangRename.cpp
clang-tools-extra/trunk/docs/clang-rename.rst
clang-tools-extra/trunk/unittests/clang-rename/ClangRenameTest.h
Modified: clang-tools-extra/trunk/clang-rename/USRFindingAction.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-rename/USRFindingAction.cpp?rev=304534&r1=304533&r2=304534&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/USRFindingAction.cpp (original)
+++ clang-tools-extra/trunk/clang-rename/USRFindingAction.cpp Fri Jun 2 04:32:28 2017
@@ -145,9 +145,9 @@ public:
ArrayRef<std::string> QualifiedNames,
std::vector<std::string> &SpellingNames,
std::vector<std::vector<std::string>> &USRList,
- bool &ErrorOccurred)
+ bool Force, bool &ErrorOccurred)
: SymbolOffsets(SymbolOffsets), QualifiedNames(QualifiedNames),
- SpellingNames(SpellingNames), USRList(USRList),
+ SpellingNames(SpellingNames), USRList(USRList), Force(Force),
ErrorOccurred(ErrorOccurred) {}
private:
@@ -182,6 +182,10 @@ private:
ErrorOccurred = true;
return false;
}
+
+ if (Force)
+ return true;
+
unsigned CouldNotFindSymbolNamed = Engine.getCustomDiagID(
DiagnosticsEngine::Error, "clang-rename could not find symbol %0");
Engine.Report(CouldNotFindSymbolNamed) << QualifiedName;
@@ -218,12 +222,14 @@ private:
ArrayRef<std::string> QualifiedNames;
std::vector<std::string> &SpellingNames;
std::vector<std::vector<std::string>> &USRList;
+ bool Force;
bool &ErrorOccurred;
};
std::unique_ptr<ASTConsumer> USRFindingAction::newASTConsumer() {
return llvm::make_unique<NamedDeclFindingConsumer>(
- SymbolOffsets, QualifiedNames, SpellingNames, USRList, ErrorOccurred);
+ SymbolOffsets, QualifiedNames, SpellingNames, USRList, Force,
+ ErrorOccurred);
}
} // namespace rename
Modified: clang-tools-extra/trunk/clang-rename/USRFindingAction.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-rename/USRFindingAction.h?rev=304534&r1=304533&r2=304534&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/USRFindingAction.h (original)
+++ clang-tools-extra/trunk/clang-rename/USRFindingAction.h Fri Jun 2 04:32:28 2017
@@ -30,9 +30,9 @@ namespace rename {
struct USRFindingAction {
USRFindingAction(ArrayRef<unsigned> SymbolOffsets,
- ArrayRef<std::string> QualifiedNames)
+ ArrayRef<std::string> QualifiedNames, bool Force)
: SymbolOffsets(SymbolOffsets), QualifiedNames(QualifiedNames),
- ErrorOccurred(false) {}
+ ErrorOccurred(false), Force(Force) {}
std::unique_ptr<ASTConsumer> newASTConsumer();
ArrayRef<std::string> getUSRSpellings() { return SpellingNames; }
@@ -45,6 +45,7 @@ private:
std::vector<std::string> SpellingNames;
std::vector<std::vector<std::string>> USRList;
bool ErrorOccurred;
+ bool Force;
};
} // namespace rename
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=304534&r1=304533&r2=304534&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/tool/ClangRename.cpp (original)
+++ clang-tools-extra/trunk/clang-rename/tool/ClangRename.cpp Fri Jun 2 04:32:28 2017
@@ -95,6 +95,9 @@ static cl::opt<std::string>
static cl::opt<std::string>
Input("input", cl::desc("YAML file to load oldname-newname pairs from."),
cl::Optional, cl::cat(ClangRenameOptions));
+static cl::opt<bool> Force("force",
+ cl::desc("Ignore nonexistent qualified names."),
+ cl::cat(ClangRenameOptions));
int main(int argc, const char **argv) {
tooling::CommonOptionsParser OP(argc, argv, ClangRenameOptions);
@@ -157,7 +160,7 @@ int main(int argc, const char **argv) {
auto Files = OP.getSourcePathList();
tooling::RefactoringTool Tool(OP.getCompilations(), Files);
- rename::USRFindingAction FindingAction(SymbolOffsets, QualifiedNames);
+ rename::USRFindingAction FindingAction(SymbolOffsets, QualifiedNames, Force);
Tool.run(tooling::newFrontendActionFactory(&FindingAction).get());
const std::vector<std::vector<std::string>> &USRList =
FindingAction.getUSRList();
@@ -173,6 +176,12 @@ int main(int argc, const char **argv) {
exit(1);
}
+ if (Force && PrevNames.size() < NewNames.size()) {
+ // No matching PrevName for all NewNames. Without Force this is an error
+ // above already.
+ exit(0);
+ }
+
// Perform the renaming.
rename::RenamingAction RenameAction(NewNames, PrevNames, USRList,
Tool.getReplacements(), PrintLocations);
Modified: clang-tools-extra/trunk/docs/clang-rename.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-rename.rst?rev=304534&r1=304533&r2=304534&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-rename.rst (original)
+++ clang-tools-extra/trunk/docs/clang-rename.rst Fri Jun 2 04:32:28 2017
@@ -124,6 +124,7 @@ That way you can avoid spelling out all
-export-fixes=<filename> - YAML file to store suggested fixes in.
-extra-arg=<string> - Additional argument to append to the compiler command line
-extra-arg-before=<string> - Additional argument to prepend to the compiler command line
+ -force - Ignore nonexistent qualified names.
-i - Overwrite edited <file>s.
-input=<string> - YAML file to load oldname-newname pairs from.
-new-name=<string> - The new name to change the symbol to.
Added: clang-tools-extra/trunk/test/clang-rename/InvalidQualifiedName.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-rename/InvalidQualifiedName.cpp?rev=304534&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-rename/InvalidQualifiedName.cpp (added)
+++ clang-tools-extra/trunk/test/clang-rename/InvalidQualifiedName.cpp Fri Jun 2 04:32:28 2017
@@ -0,0 +1,4 @@
+struct S {
+};
+
+// RUN: clang-rename -force -qualified-name S2 -new-name=T %s --
Modified: clang-tools-extra/trunk/unittests/clang-rename/ClangRenameTest.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-rename/ClangRenameTest.h?rev=304534&r1=304533&r2=304534&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-rename/ClangRenameTest.h (original)
+++ clang-tools-extra/trunk/unittests/clang-rename/ClangRenameTest.h Fri Jun 2 04:32:28 2017
@@ -56,7 +56,7 @@ protected:
Context.createInMemoryFile(HeaderName, HeaderContent);
clang::FileID InputFileID = Context.createInMemoryFile(CCName, NewCode);
- rename::USRFindingAction FindingAction({}, {OldName});
+ rename::USRFindingAction FindingAction({}, {OldName}, false);
std::unique_ptr<tooling::FrontendActionFactory> USRFindingActionFactory =
tooling::newFrontendActionFactory(&FindingAction);
More information about the cfe-commits
mailing list