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