182 lines · cpp
1//===-- ClangChangeNamespace.cpp - Standalone change namespace ------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8// This tool can be used to change the surrounding namespaces of class/function9// definitions.10//11// Example: test.cc12// namespace na {13// class X {};14// namespace nb {15// class Y { X x; };16// } // namespace nb17// } // namespace na18// To move the definition of class Y from namespace "na::nb" to "x::y", run:19// clang-change-namespace --old_namespace "na::nb" \20// --new_namespace "x::y" --file_pattern "test.cc" test.cc --21// Output:22// namespace na {23// class X {};24// } // namespace na25// namespace x {26// namespace y {27// class Y { na::X x; };28// } // namespace y29// } // namespace x30 31#include "ChangeNamespace.h"32#include "clang/ASTMatchers/ASTMatchFinder.h"33#include "clang/Frontend/FrontendActions.h"34#include "clang/Frontend/TextDiagnosticPrinter.h"35#include "clang/Rewrite/Core/Rewriter.h"36#include "clang/Tooling/CommonOptionsParser.h"37#include "clang/Tooling/Refactoring.h"38#include "clang/Tooling/Tooling.h"39#include "llvm/Support/CommandLine.h"40#include "llvm/Support/Signals.h"41#include "llvm/Support/YAMLTraits.h"42 43using namespace clang;44using namespace llvm;45 46namespace {47 48cl::OptionCategory ChangeNamespaceCategory("Change namespace.");49 50cl::opt<std::string> OldNamespace("old_namespace", cl::Required,51 cl::desc("Old namespace."),52 cl::cat(ChangeNamespaceCategory));53 54cl::opt<std::string> NewNamespace("new_namespace", cl::Required,55 cl::desc("New namespace."),56 cl::cat(ChangeNamespaceCategory));57 58cl::opt<std::string> FilePattern(59 "file_pattern", cl::Required,60 cl::desc("Only rename namespaces in files that match the given pattern."),61 cl::cat(ChangeNamespaceCategory));62 63cl::opt<bool> Inplace("i", cl::desc("Inplace edit <file>s, if specified."),64 cl::cat(ChangeNamespaceCategory));65 66cl::opt<bool>67 DumpYAML("dump_result",68 cl::desc("Dump new file contents in YAML, if specified."),69 cl::cat(ChangeNamespaceCategory));70 71cl::opt<std::string> Style("style",72 cl::desc("The style name used for reformatting."),73 cl::init("LLVM"), cl::cat(ChangeNamespaceCategory));74 75cl::opt<std::string> AllowedFile(76 "allowed_file",77 cl::desc("A file containing regexes of symbol names that are not expected "78 "to be updated when changing namespaces around them."),79 cl::init(""), cl::cat(ChangeNamespaceCategory));80 81llvm::ErrorOr<std::vector<std::string>> GetAllowedSymbolPatterns() {82 std::vector<std::string> Patterns;83 if (AllowedFile.empty())84 return Patterns;85 86 llvm::SmallVector<StringRef, 8> Lines;87 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =88 llvm::MemoryBuffer::getFile(AllowedFile);89 if (!File)90 return File.getError();91 llvm::StringRef Content = File.get()->getBuffer();92 Content.split(Lines, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);93 for (auto Line : Lines)94 Patterns.push_back(std::string(Line.trim()));95 return Patterns;96}97 98} // anonymous namespace99 100int main(int argc, const char **argv) {101 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);102 auto ExpectedParser =103 tooling::CommonOptionsParser::create(argc, argv, ChangeNamespaceCategory);104 if (!ExpectedParser) {105 llvm::errs() << ExpectedParser.takeError();106 return 1;107 }108 tooling::CommonOptionsParser &OptionsParser = ExpectedParser.get();109 const auto &Files = OptionsParser.getSourcePathList();110 tooling::RefactoringTool Tool(OptionsParser.getCompilations(), Files);111 llvm::ErrorOr<std::vector<std::string>> AllowedPatterns =112 GetAllowedSymbolPatterns();113 if (!AllowedPatterns) {114 llvm::errs() << "Failed to open allow file " << AllowedFile << ". "115 << AllowedPatterns.getError().message() << "\n";116 return 1;117 }118 change_namespace::ChangeNamespaceTool NamespaceTool(119 OldNamespace, NewNamespace, FilePattern, *AllowedPatterns,120 &Tool.getReplacements(), Style);121 ast_matchers::MatchFinder Finder;122 NamespaceTool.registerMatchers(&Finder);123 std::unique_ptr<tooling::FrontendActionFactory> Factory =124 tooling::newFrontendActionFactory(&Finder);125 126 if (int Result = Tool.run(Factory.get()))127 return Result;128 LangOptions DefaultLangOptions;129 DiagnosticOptions DiagOpts;130 clang::TextDiagnosticPrinter DiagnosticPrinter(errs(), DiagOpts);131 DiagnosticsEngine Diagnostics(DiagnosticIDs::create(), DiagOpts,132 &DiagnosticPrinter, false);133 auto &FileMgr = Tool.getFiles();134 SourceManager Sources(Diagnostics, FileMgr);135 Rewriter Rewrite(Sources, DefaultLangOptions);136 137 if (!formatAndApplyAllReplacements(Tool.getReplacements(), Rewrite, Style)) {138 llvm::errs() << "Failed applying all replacements.\n";139 return 1;140 }141 if (Inplace)142 return Rewrite.overwriteChangedFiles();143 144 std::set<llvm::StringRef> ChangedFiles;145 for (const auto &it : Tool.getReplacements())146 ChangedFiles.insert(it.first);147 148 if (DumpYAML) {149 auto WriteToYAML = [&](llvm::raw_ostream &OS) {150 OS << "[\n";151 for (auto I = ChangedFiles.begin(), E = ChangedFiles.end(); I != E; ++I) {152 OS << " {\n";153 OS << " \"FilePath\": \"" << *I << "\",\n";154 auto Entry = llvm::cantFail(FileMgr.getFileRef(*I));155 auto ID = Sources.getOrCreateFileID(Entry, SrcMgr::C_User);156 std::string Content;157 llvm::raw_string_ostream ContentStream(Content);158 Rewrite.getEditBuffer(ID).write(ContentStream);159 OS << " \"SourceText\": \""160 << llvm::yaml::escape(ContentStream.str()) << "\"\n";161 OS << " }";162 if (I != std::prev(E))163 OS << ",\n";164 }165 OS << "\n]\n";166 };167 WriteToYAML(llvm::outs());168 return 0;169 }170 171 for (const auto &File : ChangedFiles) {172 auto Entry = llvm::cantFail(FileMgr.getFileRef(File));173 174 auto ID = Sources.getOrCreateFileID(Entry, SrcMgr::C_User);175 outs() << "============== " << File << " ==============\n";176 Rewrite.getEditBuffer(ID).write(llvm::outs());177 outs() << "\n============================================\n";178 }179 180 return 0;181}182