blob: 63085bca2306d90083f46789ff40a432858bb0f8 [file] [log] [blame]
Eric Liu495b2112016-09-19 17:40:32 +00001//===-- 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 Liu77b0c1a2016-09-19 20:41:39 +000033#include "clang/ASTMatchers/ASTMatchFinder.h"
Eric Liu495b2112016-09-19 17:40:32 +000034#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
42using namespace clang;
43using namespace llvm;
44
45namespace {
46
47cl::OptionCategory ChangeNamespaceCategory("Change namespace.");
48
49cl::opt<std::string> OldNamespace("old_namespace", cl::Required,
50 cl::desc("Old namespace."),
51 cl::cat(ChangeNamespaceCategory));
52
53cl::opt<std::string> NewNamespace("new_namespace", cl::Required,
54 cl::desc("New namespace."),
55 cl::cat(ChangeNamespaceCategory));
56
57cl::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
62cl::opt<bool> Inplace("i", cl::desc("Inplace edit <file>s, if specified."),
63 cl::cat(ChangeNamespaceCategory));
64
65cl::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
71int 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}