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" |
| 41 | |
| 42 | using namespace clang; |
| 43 | using namespace llvm; |
| 44 | |
| 45 | namespace { |
| 46 | |
| 47 | cl::OptionCategory ChangeNamespaceCategory("Change namespace."); |
| 48 | |
| 49 | cl::opt<std::string> OldNamespace("old_namespace", cl::Required, |
| 50 | cl::desc("Old namespace."), |
| 51 | cl::cat(ChangeNamespaceCategory)); |
| 52 | |
| 53 | cl::opt<std::string> NewNamespace("new_namespace", cl::Required, |
| 54 | cl::desc("New namespace."), |
| 55 | cl::cat(ChangeNamespaceCategory)); |
| 56 | |
| 57 | cl::opt<std::string> FilePattern( |
| 58 | "file_pattern", cl::Required, |
| 59 | cl::desc("Only rename namespaces in files that match the given pattern."), |
| 60 | cl::cat(ChangeNamespaceCategory)); |
| 61 | |
| 62 | cl::opt<bool> Inplace("i", cl::desc("Inplace edit <file>s, if specified."), |
| 63 | cl::cat(ChangeNamespaceCategory)); |
| 64 | |
| 65 | cl::opt<std::string> Style("style", |
| 66 | cl::desc("The style name used for reformatting."), |
| 67 | cl::init("LLVM"), cl::cat(ChangeNamespaceCategory)); |
| 68 | |
| 69 | } // anonymous namespace |
| 70 | |
| 71 | int main(int argc, const char **argv) { |
| 72 | tooling::CommonOptionsParser OptionsParser(argc, argv, |
| 73 | ChangeNamespaceCategory); |
| 74 | const auto &Files = OptionsParser.getSourcePathList(); |
| 75 | tooling::RefactoringTool Tool(OptionsParser.getCompilations(), Files); |
| 76 | change_namespace::ChangeNamespaceTool NamespaceTool( |
| 77 | OldNamespace, NewNamespace, FilePattern, &Tool.getReplacements()); |
| 78 | ast_matchers::MatchFinder Finder; |
| 79 | NamespaceTool.registerMatchers(&Finder); |
| 80 | std::unique_ptr<tooling::FrontendActionFactory> Factory = |
| 81 | tooling::newFrontendActionFactory(&Finder); |
| 82 | |
| 83 | if (int Result = Tool.run(Factory.get())) |
| 84 | return Result; |
| 85 | LangOptions DefaultLangOptions; |
| 86 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
| 87 | clang::TextDiagnosticPrinter DiagnosticPrinter(errs(), &*DiagOpts); |
| 88 | DiagnosticsEngine Diagnostics( |
| 89 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, |
| 90 | &DiagnosticPrinter, false); |
| 91 | auto &FileMgr = Tool.getFiles(); |
| 92 | SourceManager Sources(Diagnostics, FileMgr); |
| 93 | Rewriter Rewrite(Sources, DefaultLangOptions); |
| 94 | |
| 95 | if (!formatAndApplyAllReplacements(Tool.getReplacements(), Rewrite, Style)) { |
| 96 | llvm::errs() << "Failed applying all replacements.\n"; |
| 97 | return 1; |
| 98 | } |
| 99 | if (Inplace) |
| 100 | return Rewrite.overwriteChangedFiles(); |
| 101 | |
| 102 | for (const auto &File : Files) { |
| 103 | const auto *Entry = FileMgr.getFile(File); |
| 104 | |
| 105 | auto ID = Sources.getOrCreateFileID(Entry, SrcMgr::C_User); |
| 106 | // FIXME: print results in parsable format, e.g. JSON. |
| 107 | outs() << "============== " << File << " ==============\n"; |
| 108 | Rewrite.getEditBuffer(ID).write(llvm::outs()); |
| 109 | outs() << "\n============================================\n"; |
| 110 | } |
| 111 | return 0; |
| 112 | } |