2014-08-20 09:39:05 +08:00
|
|
|
//===--- tools/extra/clang-rename/ClangRename.cpp - Clang rename tool -----===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-08-20 09:39:05 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-09 09:00:01 +08:00
|
|
|
/// This file implements a clang-rename tool that automatically finds and
|
2014-08-20 09:39:05 +08:00
|
|
|
/// renames symbols in C++ code.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-07-26 04:30:13 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2014-08-20 09:39:05 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2016-07-26 04:30:13 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2014-08-20 09:39:05 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2016-07-26 04:30:13 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/TokenKinds.h"
|
2014-08-20 09:39:05 +08:00
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
|
|
|
#include "clang/Rewrite/Core/Rewriter.h"
|
|
|
|
#include "clang/Tooling/CommonOptionsParser.h"
|
|
|
|
#include "clang/Tooling/Refactoring.h"
|
2017-07-01 00:36:09 +08:00
|
|
|
#include "clang/Tooling/Refactoring/Rename/RenamingAction.h"
|
|
|
|
#include "clang/Tooling/Refactoring/Rename/USRFindingAction.h"
|
2016-06-28 03:34:47 +08:00
|
|
|
#include "clang/Tooling/ReplacementsYaml.h"
|
2014-08-20 09:39:05 +08:00
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
2016-07-26 04:30:13 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2016-08-04 17:23:30 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-08-20 09:39:05 +08:00
|
|
|
#include <string>
|
2016-07-26 04:30:13 +08:00
|
|
|
#include <system_error>
|
2014-08-20 09:39:05 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// An oldname -> newname rename.
|
2016-08-10 02:20:41 +08:00
|
|
|
struct RenameAllInfo {
|
2016-08-10 15:13:29 +08:00
|
|
|
unsigned Offset = 0;
|
2016-09-16 16:45:19 +08:00
|
|
|
std::string QualifiedName;
|
2016-08-10 02:20:41 +08:00
|
|
|
std::string NewName;
|
|
|
|
};
|
|
|
|
|
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(RenameAllInfo)
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace yaml {
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Specialized MappingTraits to describe how a RenameAllInfo is
|
2016-08-10 02:20:41 +08:00
|
|
|
/// (de)serialized.
|
|
|
|
template <> struct MappingTraits<RenameAllInfo> {
|
|
|
|
static void mapping(IO &IO, RenameAllInfo &Info) {
|
|
|
|
IO.mapOptional("Offset", Info.Offset);
|
2016-09-16 16:45:19 +08:00
|
|
|
IO.mapOptional("QualifiedName", Info.QualifiedName);
|
2016-08-10 02:20:41 +08:00
|
|
|
IO.mapRequired("NewName", Info.NewName);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace yaml
|
|
|
|
} // end namespace llvm
|
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
static cl::OptionCategory ClangRenameOptions("clang-rename common options");
|
|
|
|
|
|
|
|
static cl::list<unsigned> SymbolOffsets(
|
|
|
|
"offset",
|
|
|
|
cl::desc("Locates the symbol by offset as opposed to <line>:<column>."),
|
|
|
|
cl::ZeroOrMore, cl::cat(ClangRenameOptions));
|
|
|
|
static cl::opt<bool> Inplace("i", cl::desc("Overwrite edited <file>s."),
|
|
|
|
cl::cat(ClangRenameOptions));
|
|
|
|
static cl::list<std::string>
|
|
|
|
QualifiedNames("qualified-name",
|
|
|
|
cl::desc("The fully qualified name of the symbol."),
|
|
|
|
cl::ZeroOrMore, cl::cat(ClangRenameOptions));
|
|
|
|
|
|
|
|
static cl::list<std::string>
|
|
|
|
NewNames("new-name", cl::desc("The new name to change the symbol to."),
|
|
|
|
cl::ZeroOrMore, cl::cat(ClangRenameOptions));
|
|
|
|
static cl::opt<bool> PrintName(
|
|
|
|
"pn",
|
|
|
|
cl::desc("Print the found symbol's name prior to renaming to stderr."),
|
|
|
|
cl::cat(ClangRenameOptions));
|
|
|
|
static cl::opt<bool> PrintLocations(
|
|
|
|
"pl", cl::desc("Print the locations affected by renaming to stderr."),
|
|
|
|
cl::cat(ClangRenameOptions));
|
|
|
|
static cl::opt<std::string>
|
|
|
|
ExportFixes("export-fixes",
|
|
|
|
cl::desc("YAML file to store suggested fixes in."),
|
|
|
|
cl::value_desc("filename"), cl::cat(ClangRenameOptions));
|
|
|
|
static cl::opt<std::string>
|
|
|
|
Input("input", cl::desc("YAML file to load oldname-newname pairs from."),
|
|
|
|
cl::Optional, cl::cat(ClangRenameOptions));
|
2017-06-02 17:32:28 +08:00
|
|
|
static cl::opt<bool> Force("force",
|
|
|
|
cl::desc("Ignore nonexistent qualified names."),
|
|
|
|
cl::cat(ClangRenameOptions));
|
2016-09-14 21:23:14 +08:00
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
int main(int argc, const char **argv) {
|
|
|
|
tooling::CommonOptionsParser OP(argc, argv, ClangRenameOptions);
|
2014-08-20 09:39:05 +08:00
|
|
|
|
2016-08-10 02:20:41 +08:00
|
|
|
if (!Input.empty()) {
|
2016-09-16 16:45:19 +08:00
|
|
|
// Populate QualifiedNames and NewNames from a YAML file.
|
2016-08-10 15:13:29 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
|
|
|
|
llvm::MemoryBuffer::getFile(Input);
|
2016-08-10 02:20:41 +08:00
|
|
|
if (!Buffer) {
|
|
|
|
errs() << "clang-rename: failed to read " << Input << ": "
|
|
|
|
<< Buffer.getError().message() << "\n";
|
2016-08-10 15:13:29 +08:00
|
|
|
return 1;
|
2016-08-10 02:20:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<RenameAllInfo> Infos;
|
|
|
|
llvm::yaml::Input YAML(Buffer.get()->getBuffer());
|
|
|
|
YAML >> Infos;
|
|
|
|
for (const auto &Info : Infos) {
|
2016-09-16 16:45:19 +08:00
|
|
|
if (!Info.QualifiedName.empty())
|
|
|
|
QualifiedNames.push_back(Info.QualifiedName);
|
2016-08-10 02:20:41 +08:00
|
|
|
else
|
|
|
|
SymbolOffsets.push_back(Info.Offset);
|
|
|
|
NewNames.push_back(Info.NewName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 09:39:05 +08:00
|
|
|
// Check the arguments for correctness.
|
2016-08-10 02:20:41 +08:00
|
|
|
if (NewNames.empty()) {
|
2016-09-16 16:45:19 +08:00
|
|
|
errs() << "clang-rename: -new-name must be specified.\n\n";
|
2017-07-01 04:24:32 +08:00
|
|
|
return 1;
|
2016-09-16 16:45:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (SymbolOffsets.empty() == QualifiedNames.empty()) {
|
|
|
|
errs() << "clang-rename: -offset and -qualified-name can't be present at "
|
|
|
|
"the same time.\n";
|
2017-07-01 04:24:32 +08:00
|
|
|
return 1;
|
2016-08-10 02:20:41 +08:00
|
|
|
}
|
|
|
|
|
2016-08-02 17:51:31 +08:00
|
|
|
// Check if NewNames is a valid identifier in C++17.
|
2016-09-16 16:45:19 +08:00
|
|
|
LangOptions Options;
|
|
|
|
Options.CPlusPlus = true;
|
2017-12-05 04:27:34 +08:00
|
|
|
Options.CPlusPlus17 = true;
|
2016-09-16 16:45:19 +08:00
|
|
|
IdentifierTable Table(Options);
|
2016-08-02 17:51:31 +08:00
|
|
|
for (const auto &NewName : NewNames) {
|
|
|
|
auto NewNameTokKind = Table.get(NewName).getTokenID();
|
|
|
|
if (!tok::isAnyIdentifier(NewNameTokKind)) {
|
|
|
|
errs() << "ERROR: new name is not a valid identifier in C++17.\n\n";
|
2017-07-01 04:24:32 +08:00
|
|
|
return 1;
|
2016-08-02 17:51:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
if (SymbolOffsets.size() + QualifiedNames.size() != NewNames.size()) {
|
|
|
|
errs() << "clang-rename: number of symbol offsets(" << SymbolOffsets.size()
|
|
|
|
<< ") + number of qualified names (" << QualifiedNames.size()
|
|
|
|
<< ") must be equal to number of new names(" << NewNames.size()
|
2016-09-14 21:23:14 +08:00
|
|
|
<< ").\n\n";
|
|
|
|
cl::PrintHelpMessage();
|
2017-07-01 04:24:32 +08:00
|
|
|
return 1;
|
2016-09-14 21:23:14 +08:00
|
|
|
}
|
|
|
|
|
2014-08-20 09:39:05 +08:00
|
|
|
auto Files = OP.getSourcePathList();
|
|
|
|
tooling::RefactoringTool Tool(OP.getCompilations(), Files);
|
2017-07-01 00:36:09 +08:00
|
|
|
tooling::USRFindingAction FindingAction(SymbolOffsets, QualifiedNames, Force);
|
2016-09-16 16:45:19 +08:00
|
|
|
Tool.run(tooling::newFrontendActionFactory(&FindingAction).get());
|
|
|
|
const std::vector<std::vector<std::string>> &USRList =
|
|
|
|
FindingAction.getUSRList();
|
|
|
|
const std::vector<std::string> &PrevNames = FindingAction.getUSRSpellings();
|
|
|
|
if (PrintName) {
|
|
|
|
for (const auto &PrevName : PrevNames) {
|
|
|
|
outs() << "clang-rename found name: " << PrevName << '\n';
|
2016-08-02 17:51:31 +08:00
|
|
|
}
|
2016-09-16 16:45:19 +08:00
|
|
|
}
|
2014-08-20 09:39:05 +08:00
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
if (FindingAction.errorOccurred()) {
|
|
|
|
// Diagnostics are already issued at this point.
|
2017-07-01 04:24:32 +08:00
|
|
|
return 1;
|
2016-07-14 17:46:03 +08:00
|
|
|
}
|
2014-08-20 09:39:05 +08:00
|
|
|
|
|
|
|
// Perform the renaming.
|
2017-07-01 00:36:09 +08:00
|
|
|
tooling::RenamingAction RenameAction(NewNames, PrevNames, USRList,
|
|
|
|
Tool.getReplacements(), PrintLocations);
|
2016-09-16 16:45:19 +08:00
|
|
|
std::unique_ptr<tooling::FrontendActionFactory> Factory =
|
|
|
|
tooling::newFrontendActionFactory(&RenameAction);
|
2016-07-15 19:29:16 +08:00
|
|
|
int ExitCode;
|
2014-08-20 09:39:05 +08:00
|
|
|
|
|
|
|
if (Inplace) {
|
2016-07-15 19:29:16 +08:00
|
|
|
ExitCode = Tool.runAndSave(Factory.get());
|
2014-08-20 09:39:05 +08:00
|
|
|
} else {
|
2016-07-15 19:29:16 +08:00
|
|
|
ExitCode = Tool.run(Factory.get());
|
2014-08-20 09:39:05 +08:00
|
|
|
|
2016-06-28 03:34:47 +08:00
|
|
|
if (!ExportFixes.empty()) {
|
|
|
|
std::error_code EC;
|
2019-08-05 13:43:48 +08:00
|
|
|
llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::OF_None);
|
2016-06-28 03:34:47 +08:00
|
|
|
if (EC) {
|
|
|
|
llvm::errs() << "Error opening output file: " << EC.message() << '\n';
|
2017-07-01 04:24:32 +08:00
|
|
|
return 1;
|
2016-06-28 03:34:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Export replacements.
|
|
|
|
tooling::TranslationUnitReplacements TUR;
|
2016-08-01 18:16:39 +08:00
|
|
|
const auto &FileToReplacements = Tool.getReplacements();
|
|
|
|
for (const auto &Entry : FileToReplacements)
|
|
|
|
TUR.Replacements.insert(TUR.Replacements.end(), Entry.second.begin(),
|
|
|
|
Entry.second.end());
|
2016-06-28 03:34:47 +08:00
|
|
|
|
|
|
|
yaml::Output YAML(OS);
|
|
|
|
YAML << TUR;
|
|
|
|
OS.close();
|
2017-07-01 04:24:32 +08:00
|
|
|
return 0;
|
2016-06-28 03:34:47 +08:00
|
|
|
}
|
|
|
|
|
2014-08-20 09:39:05 +08:00
|
|
|
// Write every file to stdout. Right now we just barf the files without any
|
|
|
|
// indication of which files start where, other than that we print the files
|
|
|
|
// in the same order we see them.
|
|
|
|
LangOptions DefaultLangOptions;
|
2016-08-02 17:51:31 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
|
2014-08-20 09:39:05 +08:00
|
|
|
TextDiagnosticPrinter DiagnosticPrinter(errs(), &*DiagOpts);
|
|
|
|
DiagnosticsEngine Diagnostics(
|
2016-08-02 17:51:31 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
|
|
|
|
&DiagnosticPrinter, false);
|
2014-08-20 09:39:05 +08:00
|
|
|
auto &FileMgr = Tool.getFiles();
|
|
|
|
SourceManager Sources(Diagnostics, FileMgr);
|
|
|
|
Rewriter Rewrite(Sources, DefaultLangOptions);
|
|
|
|
|
|
|
|
Tool.applyAllReplacements(Rewrite);
|
|
|
|
for (const auto &File : Files) {
|
2019-08-02 05:31:56 +08:00
|
|
|
auto Entry = FileMgr.getFile(File);
|
|
|
|
const auto ID = Sources.getOrCreateFileID(*Entry, SrcMgr::C_User);
|
2014-08-20 09:39:05 +08:00
|
|
|
Rewrite.getEditBuffer(ID).write(outs());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-01 04:24:32 +08:00
|
|
|
return ExitCode;
|
2014-08-20 09:39:05 +08:00
|
|
|
}
|