clang-rename rename-all: support reading old/newname pairs from a YAML file
This is handy in case by the time clang-rename is invoked, an external tool already genereated a list of oldname -> newname pairs to handle. Reviewers: omtcyfz Differential Revision: https://reviews.llvm.org/D23198 llvm-svn: 278145
This commit is contained in:
parent
adc688ce9c
commit
3d71b51bc5
|
@ -56,6 +56,33 @@ static int renameAtMain(int argc, const char *argv[]);
|
||||||
static int renameAllMain(int argc, const char *argv[]);
|
static int renameAllMain(int argc, const char *argv[]);
|
||||||
static int helpMain(int argc, const char *argv[]);
|
static int helpMain(int argc, const char *argv[]);
|
||||||
|
|
||||||
|
/// \brief An oldname -> newname rename.
|
||||||
|
struct RenameAllInfo {
|
||||||
|
std::string OldName;
|
||||||
|
unsigned Offset;
|
||||||
|
std::string NewName;
|
||||||
|
|
||||||
|
RenameAllInfo() : Offset(0) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
LLVM_YAML_IS_SEQUENCE_VECTOR(RenameAllInfo)
|
||||||
|
|
||||||
|
namespace llvm {
|
||||||
|
namespace yaml {
|
||||||
|
|
||||||
|
/// \brief Specialized MappingTraits to describe how a RenameAllInfo is /
|
||||||
|
/// (de)serialized.
|
||||||
|
template <> struct MappingTraits<RenameAllInfo> {
|
||||||
|
static void mapping(IO &IO, RenameAllInfo &Info) {
|
||||||
|
IO.mapOptional("OldName", Info.OldName);
|
||||||
|
IO.mapOptional("Offset", Info.Offset);
|
||||||
|
IO.mapRequired("NewName", Info.NewName);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // end namespace yaml
|
||||||
|
} // end namespace llvm
|
||||||
|
|
||||||
int main(int argc, const char **argv) {
|
int main(int argc, const char **argv) {
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
using MainFunction = std::function<int(int, const char *[])>;
|
using MainFunction = std::function<int(int, const char *[])>;
|
||||||
|
@ -91,7 +118,7 @@ int subcommandMain(bool isRenameAll, int argc, const char **argv) {
|
||||||
|
|
||||||
cl::list<std::string> NewNames(
|
cl::list<std::string> NewNames(
|
||||||
"new-name", cl::desc("The new name to change the symbol to."),
|
"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<unsigned> SymbolOffsets(
|
cl::list<unsigned> SymbolOffsets(
|
||||||
"offset",
|
"offset",
|
||||||
cl::desc("Locates the symbol by offset as opposed to <line>:<column>."),
|
cl::desc("Locates the symbol by offset as opposed to <line>:<column>."),
|
||||||
|
@ -114,11 +141,40 @@ int subcommandMain(bool isRenameAll, int argc, const char **argv) {
|
||||||
cl::opt<std::string> ExportFixes(
|
cl::opt<std::string> ExportFixes(
|
||||||
"export-fixes", cl::desc("YAML file to store suggested fixes in."),
|
"export-fixes", cl::desc("YAML file to store suggested fixes in."),
|
||||||
cl::value_desc("filename"), cl::cat(*Category));
|
cl::value_desc("filename"), cl::cat(*Category));
|
||||||
|
cl::opt<std::string> Input(
|
||||||
|
"input", cl::desc("YAML file to load oldname-newname pairs from."),
|
||||||
|
cl::Optional, cl::cat(ClangRenameAllCategory));
|
||||||
|
|
||||||
tooling::CommonOptionsParser OP(argc, argv, *Category, Usage);
|
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<RenameAllInfo> Infos;
|
||||||
|
llvm::yaml::Input YAML(Buffer.get()->getBuffer());
|
||||||
|
YAML >> Infos;
|
||||||
|
for (const auto &Info : Infos) {
|
||||||
|
if (!Info.OldName.empty())
|
||||||
|
OldNames.push_back(Info.OldName);
|
||||||
|
else
|
||||||
|
SymbolOffsets.push_back(Info.Offset);
|
||||||
|
NewNames.push_back(Info.NewName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check the arguments for correctness.
|
// 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.
|
// Check if NewNames is a valid identifier in C++17.
|
||||||
for (const auto &NewName : NewNames) {
|
for (const auto &NewName : NewNames) {
|
||||||
LangOptions Options;
|
LangOptions Options;
|
||||||
|
|
|
@ -42,14 +42,6 @@ To get an offset of a symbol in a file run
|
||||||
$ grep -FUbo 'foo' file.cpp
|
$ grep -FUbo 'foo' file.cpp
|
||||||
|
|
||||||
|
|
||||||
You can also identify one or more symbols to be renamed by giving the fully qualified
|
|
||||||
name:
|
|
||||||
|
|
||||||
.. code-block:: console
|
|
||||||
|
|
||||||
$ clang-rename rename-all -old-name=foo -new-name=bar test.cpp
|
|
||||||
|
|
||||||
|
|
||||||
The tool currently supports renaming actions inside a single Translation Unit
|
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
|
only. It is planned to extend the tool's functionality to support multi-TU
|
||||||
renaming actions in the future.
|
renaming actions in the future.
|
||||||
|
@ -60,6 +52,43 @@ editors, such as Vim and Emacs, and improve the workflow of users.
|
||||||
Although a command line interface exists, it is highly recommended to use the
|
Although a command line interface exists, it is highly recommended to use the
|
||||||
text editor interface instead for better experience.
|
text editor interface instead for better experience.
|
||||||
|
|
||||||
|
You can also identify one or more symbols to be renamed by giving the fully qualified
|
||||||
|
name:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ 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 YAML file also supports offsets:
|
||||||
|
|
||||||
|
.. code-block:: yaml
|
||||||
|
|
||||||
|
---
|
||||||
|
- Offset: 42
|
||||||
|
NewName: foo
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
:program:`clang-rename` offers the following options:
|
||||||
|
|
||||||
.. code-block:: console
|
.. code-block:: console
|
||||||
|
|
||||||
$ clang-rename -help
|
$ clang-rename -help
|
||||||
|
@ -125,6 +154,7 @@ text editor interface instead for better experience.
|
||||||
-extra-arg=<string> - Additional argument to append to the compiler command line
|
-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
|
-extra-arg-before=<string> - Additional argument to prepend to the compiler command line
|
||||||
-i - Overwrite edited <file>s.
|
-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.
|
-new-name=<string> - The new name to change the symbol to.
|
||||||
-offset=<uint> - Locates the symbol by offset as opposed to <line>:<column>.
|
-offset=<uint> - Locates the symbol by offset as opposed to <line>:<column>.
|
||||||
-old-name=<string> - The fully qualified name of the symbol, if -offset is not used.
|
-old-name=<string> - The fully qualified name of the symbol, if -offset is not used.
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
class Foo1 { // CHECK: class Bar1
|
||||||
|
};
|
||||||
|
|
||||||
|
class Foo2 { // CHECK: class Bar2
|
||||||
|
};
|
||||||
|
// RUN: clang-rename rename-all -input %S/Inputs/ClassTestMultiByNameYAMLRenameAll.yaml %s -- | sed 's,//.*,,' | FileCheck %s
|
||||||
|
// RUN: clang-rename rename-all -input %S/Inputs/ClassTestMultiByNameYAMLRenameAt.yaml %s -- | sed 's,//.*,,' | FileCheck %s
|
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
- OldName: Foo1
|
||||||
|
NewName: Bar1
|
||||||
|
- OldName: Foo2
|
||||||
|
NewName: Bar2
|
||||||
|
...
|
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
- Offset: 6
|
||||||
|
NewName: Bar1
|
||||||
|
- Offset: 44
|
||||||
|
NewName: Bar2
|
||||||
|
...
|
Loading…
Reference in New Issue