2019-03-01 17:52:53 +08:00
|
|
|
//===-- ClangChangeNamespace.cpp - Standalone change namespace ------------===//
|
2016-09-20 01:40:32 +08:00
|
|
|
//
|
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
|
2016-09-20 01:40:32 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// This tool can be used to change the surrounding namespaces of class/function
|
|
|
|
// definitions.
|
|
|
|
//
|
|
|
|
// Example: test.cc
|
|
|
|
// namespace na {
|
|
|
|
// class X {};
|
|
|
|
// namespace nb {
|
|
|
|
// class Y { X x; };
|
|
|
|
// } // namespace nb
|
|
|
|
// } // namespace na
|
|
|
|
// To move the definition of class Y from namespace "na::nb" to "x::y", run:
|
|
|
|
// clang-change-namespace --old_namespace "na::nb" \
|
|
|
|
// --new_namespace "x::y" --file_pattern "test.cc" test.cc --
|
|
|
|
// Output:
|
|
|
|
// namespace na {
|
|
|
|
// class X {};
|
|
|
|
// } // namespace na
|
|
|
|
// namespace x {
|
|
|
|
// namespace y {
|
|
|
|
// class Y { na::X x; };
|
|
|
|
// } // namespace y
|
|
|
|
// } // namespace x
|
|
|
|
|
|
|
|
#include "ChangeNamespace.h"
|
2016-09-20 04:41:39 +08:00
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
2016-09-20 01:40:32 +08:00
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
|
|
|
#include "clang/Rewrite/Core/Rewriter.h"
|
|
|
|
#include "clang/Tooling/CommonOptionsParser.h"
|
|
|
|
#include "clang/Tooling/Refactoring.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2016-10-14 02:56:14 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2017-02-14 01:24:14 +08:00
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2016-09-20 01:40:32 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
cl::OptionCategory ChangeNamespaceCategory("Change namespace.");
|
|
|
|
|
|
|
|
cl::opt<std::string> OldNamespace("old_namespace", cl::Required,
|
|
|
|
cl::desc("Old namespace."),
|
|
|
|
cl::cat(ChangeNamespaceCategory));
|
|
|
|
|
|
|
|
cl::opt<std::string> NewNamespace("new_namespace", cl::Required,
|
|
|
|
cl::desc("New namespace."),
|
|
|
|
cl::cat(ChangeNamespaceCategory));
|
|
|
|
|
|
|
|
cl::opt<std::string> FilePattern(
|
|
|
|
"file_pattern", cl::Required,
|
|
|
|
cl::desc("Only rename namespaces in files that match the given pattern."),
|
|
|
|
cl::cat(ChangeNamespaceCategory));
|
|
|
|
|
|
|
|
cl::opt<bool> Inplace("i", cl::desc("Inplace edit <file>s, if specified."),
|
|
|
|
cl::cat(ChangeNamespaceCategory));
|
|
|
|
|
2017-02-14 01:24:14 +08:00
|
|
|
cl::opt<bool>
|
|
|
|
DumpYAML("dump_result",
|
|
|
|
cl::desc("Dump new file contents in YAML, if specified."),
|
|
|
|
cl::cat(ChangeNamespaceCategory));
|
|
|
|
|
2016-09-20 01:40:32 +08:00
|
|
|
cl::opt<std::string> Style("style",
|
|
|
|
cl::desc("The style name used for reformatting."),
|
|
|
|
cl::init("LLVM"), cl::cat(ChangeNamespaceCategory));
|
|
|
|
|
2020-06-20 14:01:42 +08:00
|
|
|
cl::opt<std::string> AllowedFile(
|
|
|
|
"allowed_file",
|
2017-02-24 19:54:45 +08:00
|
|
|
cl::desc("A file containing regexes of symbol names that are not expected "
|
|
|
|
"to be updated when changing namespaces around them."),
|
|
|
|
cl::init(""), cl::cat(ChangeNamespaceCategory));
|
|
|
|
|
2020-06-20 14:01:42 +08:00
|
|
|
llvm::ErrorOr<std::vector<std::string>> GetAllowedSymbolPatterns() {
|
2017-02-28 18:00:15 +08:00
|
|
|
std::vector<std::string> Patterns;
|
2020-06-20 14:01:42 +08:00
|
|
|
if (AllowedFile.empty())
|
2017-02-28 18:00:15 +08:00
|
|
|
return Patterns;
|
2017-02-24 20:56:51 +08:00
|
|
|
|
2017-02-24 19:54:45 +08:00
|
|
|
llvm::SmallVector<StringRef, 8> Lines;
|
2017-02-24 20:56:51 +08:00
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
|
2020-06-20 14:01:42 +08:00
|
|
|
llvm::MemoryBuffer::getFile(AllowedFile);
|
2017-02-24 20:56:51 +08:00
|
|
|
if (!File)
|
|
|
|
return File.getError();
|
|
|
|
llvm::StringRef Content = File.get()->getBuffer();
|
|
|
|
Content.split(Lines, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
|
2017-02-28 18:00:15 +08:00
|
|
|
for (auto Line : Lines)
|
2020-01-29 03:23:46 +08:00
|
|
|
Patterns.push_back(std::string(Line.trim()));
|
2017-02-28 18:00:15 +08:00
|
|
|
return Patterns;
|
2017-02-24 19:54:45 +08:00
|
|
|
}
|
|
|
|
|
2016-09-20 01:40:32 +08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
int main(int argc, const char **argv) {
|
2019-11-26 10:06:56 +08:00
|
|
|
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
|
2016-09-20 01:40:32 +08:00
|
|
|
tooling::CommonOptionsParser OptionsParser(argc, argv,
|
|
|
|
ChangeNamespaceCategory);
|
|
|
|
const auto &Files = OptionsParser.getSourcePathList();
|
|
|
|
tooling::RefactoringTool Tool(OptionsParser.getCompilations(), Files);
|
2020-06-20 14:01:42 +08:00
|
|
|
llvm::ErrorOr<std::vector<std::string>> AllowedPatterns =
|
|
|
|
GetAllowedSymbolPatterns();
|
|
|
|
if (!AllowedPatterns) {
|
2020-06-20 14:33:37 +08:00
|
|
|
llvm::errs() << "Failed to open allow file " << AllowedFile << ". "
|
2020-06-20 14:01:42 +08:00
|
|
|
<< AllowedPatterns.getError().message() << "\n";
|
2017-02-24 19:54:45 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2016-09-20 01:40:32 +08:00
|
|
|
change_namespace::ChangeNamespaceTool NamespaceTool(
|
2020-06-20 14:01:42 +08:00
|
|
|
OldNamespace, NewNamespace, FilePattern, *AllowedPatterns,
|
2017-02-24 19:54:45 +08:00
|
|
|
&Tool.getReplacements(), Style);
|
2016-09-20 01:40:32 +08:00
|
|
|
ast_matchers::MatchFinder Finder;
|
|
|
|
NamespaceTool.registerMatchers(&Finder);
|
|
|
|
std::unique_ptr<tooling::FrontendActionFactory> Factory =
|
|
|
|
tooling::newFrontendActionFactory(&Finder);
|
|
|
|
|
|
|
|
if (int Result = Tool.run(Factory.get()))
|
|
|
|
return Result;
|
|
|
|
LangOptions DefaultLangOptions;
|
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
|
|
|
|
clang::TextDiagnosticPrinter DiagnosticPrinter(errs(), &*DiagOpts);
|
|
|
|
DiagnosticsEngine Diagnostics(
|
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
|
|
|
|
&DiagnosticPrinter, false);
|
|
|
|
auto &FileMgr = Tool.getFiles();
|
|
|
|
SourceManager Sources(Diagnostics, FileMgr);
|
|
|
|
Rewriter Rewrite(Sources, DefaultLangOptions);
|
|
|
|
|
|
|
|
if (!formatAndApplyAllReplacements(Tool.getReplacements(), Rewrite, Style)) {
|
|
|
|
llvm::errs() << "Failed applying all replacements.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (Inplace)
|
|
|
|
return Rewrite.overwriteChangedFiles();
|
|
|
|
|
2017-02-14 01:24:14 +08:00
|
|
|
std::set<llvm::StringRef> ChangedFiles;
|
|
|
|
for (const auto &it : Tool.getReplacements())
|
|
|
|
ChangedFiles.insert(it.first);
|
|
|
|
|
|
|
|
if (DumpYAML) {
|
|
|
|
auto WriteToYAML = [&](llvm::raw_ostream &OS) {
|
|
|
|
OS << "[\n";
|
|
|
|
for (auto I = ChangedFiles.begin(), E = ChangedFiles.end(); I != E; ++I) {
|
|
|
|
OS << " {\n";
|
|
|
|
OS << " \"FilePath\": \"" << *I << "\",\n";
|
2019-08-02 05:32:01 +08:00
|
|
|
const auto Entry = FileMgr.getFile(*I);
|
|
|
|
auto ID = Sources.getOrCreateFileID(*Entry, SrcMgr::C_User);
|
2017-02-14 01:24:14 +08:00
|
|
|
std::string Content;
|
|
|
|
llvm::raw_string_ostream ContentStream(Content);
|
|
|
|
Rewrite.getEditBuffer(ID).write(ContentStream);
|
|
|
|
OS << " \"SourceText\": \""
|
|
|
|
<< llvm::yaml::escape(ContentStream.str()) << "\"\n";
|
|
|
|
OS << " }";
|
|
|
|
if (I != std::prev(E))
|
|
|
|
OS << ",\n";
|
|
|
|
}
|
|
|
|
OS << "\n]\n";
|
|
|
|
};
|
|
|
|
WriteToYAML(llvm::outs());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &File : ChangedFiles) {
|
2019-08-02 05:32:01 +08:00
|
|
|
const auto Entry = FileMgr.getFile(File);
|
2016-09-20 01:40:32 +08:00
|
|
|
|
2019-08-02 05:32:01 +08:00
|
|
|
auto ID = Sources.getOrCreateFileID(*Entry, SrcMgr::C_User);
|
2016-09-20 01:40:32 +08:00
|
|
|
outs() << "============== " << File << " ==============\n";
|
|
|
|
Rewrite.getEditBuffer(ID).write(llvm::outs());
|
|
|
|
outs() << "\n============================================\n";
|
|
|
|
}
|
2017-02-14 01:24:14 +08:00
|
|
|
|
2016-09-20 01:40:32 +08:00
|
|
|
return 0;
|
|
|
|
}
|