Skip to content

Commit 3d71b51

Browse files
committedAug 9, 2016
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
1 parent adc688c commit 3d71b51

File tree

5 files changed

+113
-8
lines changed

5 files changed

+113
-8
lines changed
 

Diff for: ‎clang-tools-extra/clang-rename/tool/ClangRename.cpp

+57-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,33 @@ static int renameAtMain(int argc, const char *argv[]);
5656
static int renameAllMain(int argc, const char *argv[]);
5757
static int helpMain(int argc, const char *argv[]);
5858

59+
/// \brief An oldname -> newname rename.
60+
struct RenameAllInfo {
61+
std::string OldName;
62+
unsigned Offset;
63+
std::string NewName;
64+
65+
RenameAllInfo() : Offset(0) {}
66+
};
67+
68+
LLVM_YAML_IS_SEQUENCE_VECTOR(RenameAllInfo)
69+
70+
namespace llvm {
71+
namespace yaml {
72+
73+
/// \brief Specialized MappingTraits to describe how a RenameAllInfo is /
74+
/// (de)serialized.
75+
template <> struct MappingTraits<RenameAllInfo> {
76+
static void mapping(IO &IO, RenameAllInfo &Info) {
77+
IO.mapOptional("OldName", Info.OldName);
78+
IO.mapOptional("Offset", Info.Offset);
79+
IO.mapRequired("NewName", Info.NewName);
80+
}
81+
};
82+
83+
} // end namespace yaml
84+
} // end namespace llvm
85+
5986
int main(int argc, const char **argv) {
6087
if (argc > 1) {
6188
using MainFunction = std::function<int(int, const char *[])>;
@@ -91,7 +118,7 @@ int subcommandMain(bool isRenameAll, int argc, const char **argv) {
91118

92119
cl::list<std::string> NewNames(
93120
"new-name", cl::desc("The new name to change the symbol to."),
94-
(isRenameAll ? cl::OneOrMore : cl::Required), cl::cat(*Category));
121+
(isRenameAll ? cl::ZeroOrMore : cl::Required), cl::cat(*Category));
95122
cl::list<unsigned> SymbolOffsets(
96123
"offset",
97124
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) {
114141
cl::opt<std::string> ExportFixes(
115142
"export-fixes", cl::desc("YAML file to store suggested fixes in."),
116143
cl::value_desc("filename"), cl::cat(*Category));
144+
cl::opt<std::string> Input(
145+
"input", cl::desc("YAML file to load oldname-newname pairs from."),
146+
cl::Optional, cl::cat(ClangRenameAllCategory));
117147

118148
tooling::CommonOptionsParser OP(argc, argv, *Category, Usage);
119149

150+
if (!Input.empty()) {
151+
// Populate OldNames and NewNames from a YAML file.
152+
auto Buffer = llvm::MemoryBuffer::getFile(Input);
153+
if (!Buffer) {
154+
errs() << "clang-rename: failed to read " << Input << ": "
155+
<< Buffer.getError().message() << "\n";
156+
exit(1);
157+
}
158+
159+
std::vector<RenameAllInfo> Infos;
160+
llvm::yaml::Input YAML(Buffer.get()->getBuffer());
161+
YAML >> Infos;
162+
for (const auto &Info : Infos) {
163+
if (!Info.OldName.empty())
164+
OldNames.push_back(Info.OldName);
165+
else
166+
SymbolOffsets.push_back(Info.Offset);
167+
NewNames.push_back(Info.NewName);
168+
}
169+
}
170+
120171
// Check the arguments for correctness.
121172

173+
if (NewNames.empty()) {
174+
errs() << "clang-rename: either -new-name or -input is required.\n\n";
175+
exit(1);
176+
}
177+
122178
// Check if NewNames is a valid identifier in C++17.
123179
for (const auto &NewName : NewNames) {
124180
LangOptions Options;

Diff for: ‎clang-tools-extra/docs/clang-rename.rst

+37-7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ To get an offset of a symbol in a file run
4242
$ grep -FUbo 'foo' file.cpp
4343
4444
45+
The tool currently supports renaming actions inside a single Translation Unit
46+
only. It is planned to extend the tool's functionality to support multi-TU
47+
renaming actions in the future.
48+
49+
:program:`clang-rename` also aims to be easily integrated into popular text
50+
editors, such as Vim and Emacs, and improve the workflow of users.
51+
52+
Although a command line interface exists, it is highly recommended to use the
53+
text editor interface instead for better experience.
54+
4555
You can also identify one or more symbols to be renamed by giving the fully qualified
4656
name:
4757

@@ -50,15 +60,34 @@ name:
5060
$ clang-rename rename-all -old-name=foo -new-name=bar test.cpp
5161
5262
53-
The tool currently supports renaming actions inside a single Translation Unit
54-
only. It is planned to extend the tool's functionality to support multi-TU
55-
renaming actions in the future.
63+
Alternatively, old name / new name pairs can be put into a YAML file:
5664

57-
:program:`clang-rename` also aims to be easily integrated into popular text
58-
editors, such as Vim and Emacs, and improve the workflow of users.
65+
.. code-block:: yaml
5966
60-
Although a command line interface exists, it is highly recommended to use the
61-
text editor interface instead for better experience.
67+
---
68+
- OldName: foo
69+
NewName: bar
70+
...
71+
72+
73+
That way you can avoid spelling out all the names as commandline arguments:
74+
75+
.. code-block:: console
76+
77+
$ clang-rename rename-all -input=test.yaml test.cpp
78+
79+
80+
The YAML file also supports offsets:
81+
82+
.. code-block:: yaml
83+
84+
---
85+
- Offset: 42
86+
NewName: foo
87+
...
88+
89+
90+
:program:`clang-rename` offers the following options:
6291

6392
.. code-block:: console
6493
@@ -125,6 +154,7 @@ text editor interface instead for better experience.
125154
-extra-arg=<string> - Additional argument to append to the compiler command line
126155
-extra-arg-before=<string> - Additional argument to prepend to the compiler command line
127156
-i - Overwrite edited <file>s.
157+
-input=<string> - YAML file to load oldname-newname pairs from.
128158
-new-name=<string> - The new name to change the symbol to.
129159
-offset=<uint> - Locates the symbol by offset as opposed to <line>:<column>.
130160
-old-name=<string> - The fully qualified name of the symbol, if -offset is not used.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Foo1 { // CHECK: class Bar1
2+
};
3+
4+
class Foo2 { // CHECK: class Bar2
5+
};
6+
// RUN: clang-rename rename-all -input %S/Inputs/ClassTestMultiByNameYAMLRenameAll.yaml %s -- | sed 's,//.*,,' | FileCheck %s
7+
// RUN: clang-rename rename-all -input %S/Inputs/ClassTestMultiByNameYAMLRenameAt.yaml %s -- | sed 's,//.*,,' | FileCheck %s
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- OldName: Foo1
3+
NewName: Bar1
4+
- OldName: Foo2
5+
NewName: Bar2
6+
...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- Offset: 6
3+
NewName: Bar1
4+
- Offset: 44
5+
NewName: Bar2
6+
...

0 commit comments

Comments
 (0)
Please sign in to comment.