Index: clang-rename/tool/ClangRename.cpp =================================================================== --- clang-rename/tool/ClangRename.cpp +++ clang-rename/tool/ClangRename.cpp @@ -56,6 +56,29 @@ static int renameAllMain(int argc, const char *argv[]); static int helpMain(int argc, const char *argv[]); +/// \brief An oldname -> newname rename. +struct RenameAllInfo { + std::string OldName; + std::string NewName; +}; + +LLVM_YAML_IS_SEQUENCE_VECTOR(RenameAllInfo) + +namespace llvm { +namespace yaml { + +/// \brief Specialized MappingTraits to describe how a RenameAllInfo is / +/// (de)serialized. +template <> struct MappingTraits { + static void mapping(IO &IO, RenameAllInfo &Info) { + IO.mapRequired("OldName", Info.OldName); + IO.mapRequired("NewName", Info.NewName); + } +}; + +} // end namespace yaml +} // end namespace llvm + int main(int argc, const char **argv) { if (argc > 1) { using MainFunction = std::function; @@ -91,7 +114,7 @@ cl::list NewNames( "new-name", cl::desc("The new name to change the symbol to."), - (isRenameAll ? cl::OneOrMore : cl::Required), cl::cat(*Category)); + (isRenameAll ? cl::ZeroOrMore : cl::Required), cl::cat(*Category)); cl::list SymbolOffsets( "offset", cl::desc("Locates the symbol by offset as opposed to :."), @@ -114,11 +137,37 @@ cl::opt ExportFixes( "export-fixes", cl::desc("YAML file to store suggested fixes in."), cl::value_desc("filename"), cl::cat(*Category)); + cl::opt Input( + "input", cl::desc("YAML file to load oldname-newname pairs from."), + cl::Optional, cl::cat(ClangRenameAllCategory)); tooling::CommonOptionsParser OP(argc, argv, *Category, Usage); + if (!Input.empty()) { + // Populate OldNames and NewNames from a YAML file. + auto Buffer = llvm::MemoryBuffer::getFile(Input); + if (!Buffer) { + errs() << "clang-rename: failed to read " << Input << ": " + << Buffer.getError().message() << "\n"; + exit(1); + } + + std::vector Infos; + llvm::yaml::Input YAML(Buffer.get()->getBuffer()); + YAML >> Infos; + for (const auto &Info : Infos) { + OldNames.push_back(Info.OldName); + NewNames.push_back(Info.NewName); + } + } + // Check the arguments for correctness. + if (NewNames.empty()) { + errs() << "clang-rename: either -new-name or -input is required.\n\n"; + exit(1); + } + // Check if NewNames is a valid identifier in C++17. for (const auto &NewName : NewNames) { LangOptions Options; Index: docs/clang-rename.rst =================================================================== --- docs/clang-rename.rst +++ docs/clang-rename.rst @@ -50,6 +50,23 @@ $ clang-rename rename-all -old-name=foo -new-name=bar test.cpp +Alternatively, old name / new name pairs can be put into a YAML file: + +.. code-block:: yaml + + --- + - OldName: foo + NewName: bar + ... + + +That way you can avoid spelling out all the names as commandline arguments: + +.. code-block:: console + + $ clang-rename rename-all -input=test.yaml test.cpp + + The tool currently supports renaming actions inside a single Translation Unit only. It is planned to extend the tool's functionality to support multi-TU renaming actions in the future. @@ -125,6 +142,7 @@ -extra-arg= - Additional argument to append to the compiler command line -extra-arg-before= - Additional argument to prepend to the compiler command line -i - Overwrite edited s. + -input= - YAML file to load oldname-newname pairs from. -new-name= - The new name to change the symbol to. -offset= - Locates the symbol by offset as opposed to :. -old-name= - The fully qualified name of the symbol, if -offset is not used. Index: test/clang-rename/ClassTestMultiByNameYAML.cpp =================================================================== --- /dev/null +++ test/clang-rename/ClassTestMultiByNameYAML.cpp @@ -0,0 +1,8 @@ +// RUN: cat %s > %t.cpp +// RUN: clang-rename rename-all -input %s.yaml %t.cpp -i -- +// RUN: sed 's,//.*,,' %t.cpp | FileCheck %s +class Foo1 { // CHECK: class Bar1 +}; + +class Foo2 { // CHECK: class Bar2 +}; Index: test/clang-rename/ClassTestMultiByNameYAML.cpp.yaml =================================================================== --- /dev/null +++ test/clang-rename/ClassTestMultiByNameYAML.cpp.yaml @@ -0,0 +1,6 @@ +--- +- OldName: Foo1 + NewName: Bar1 +- OldName: Foo2 + NewName: Bar2 +...