blob: b49a78d6e871208c2e060b8a7574ad94ad2b0d66 [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"
Eric Liu3db05212016-10-13 18:56:14 +000041#include "llvm/Support/Signals.h"
Eric Liua13c4192017-02-13 17:24:14 +000042#include "llvm/Support/YAMLTraits.h"
Eric Liu495b2112016-09-19 17:40:32 +000043
44using namespace clang;
45using namespace llvm;
46
47namespace {
48
49cl::OptionCategory ChangeNamespaceCategory("Change namespace.");
50
51cl::opt<std::string> OldNamespace("old_namespace", cl::Required,
52 cl::desc("Old namespace."),
53 cl::cat(ChangeNamespaceCategory));
54
55cl::opt<std::string> NewNamespace("new_namespace", cl::Required,
56 cl::desc("New namespace."),
57 cl::cat(ChangeNamespaceCategory));
58
59cl::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
64cl::opt<bool> Inplace("i", cl::desc("Inplace edit <file>s, if specified."),
65 cl::cat(ChangeNamespaceCategory));
66
Eric Liua13c4192017-02-13 17:24:14 +000067cl::opt<bool>
68 DumpYAML("dump_result",
69 cl::desc("Dump new file contents in YAML, if specified."),
70 cl::cat(ChangeNamespaceCategory));
71
Eric Liu495b2112016-09-19 17:40:32 +000072cl::opt<std::string> Style("style",
73 cl::desc("The style name used for reformatting."),
74 cl::init("LLVM"), cl::cat(ChangeNamespaceCategory));
75
76} // anonymous namespace
77
78int main(int argc, const char **argv) {
Eric Liu3db05212016-10-13 18:56:14 +000079 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
Eric Liu495b2112016-09-19 17:40:32 +000080 tooling::CommonOptionsParser OptionsParser(argc, argv,
81 ChangeNamespaceCategory);
82 const auto &Files = OptionsParser.getSourcePathList();
83 tooling::RefactoringTool Tool(OptionsParser.getCompilations(), Files);
84 change_namespace::ChangeNamespaceTool NamespaceTool(
Haojian Wub530f162016-10-05 17:00:40 +000085 OldNamespace, NewNamespace, FilePattern, &Tool.getReplacements(), Style);
Eric Liu495b2112016-09-19 17:40:32 +000086 ast_matchers::MatchFinder Finder;
87 NamespaceTool.registerMatchers(&Finder);
88 std::unique_ptr<tooling::FrontendActionFactory> Factory =
89 tooling::newFrontendActionFactory(&Finder);
90
91 if (int Result = Tool.run(Factory.get()))
92 return Result;
93 LangOptions DefaultLangOptions;
94 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
95 clang::TextDiagnosticPrinter DiagnosticPrinter(errs(), &*DiagOpts);
96 DiagnosticsEngine Diagnostics(
97 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
98 &DiagnosticPrinter, false);
99 auto &FileMgr = Tool.getFiles();
100 SourceManager Sources(Diagnostics, FileMgr);
101 Rewriter Rewrite(Sources, DefaultLangOptions);
102
103 if (!formatAndApplyAllReplacements(Tool.getReplacements(), Rewrite, Style)) {
104 llvm::errs() << "Failed applying all replacements.\n";
105 return 1;
106 }
107 if (Inplace)
108 return Rewrite.overwriteChangedFiles();
109
Eric Liua13c4192017-02-13 17:24:14 +0000110 std::set<llvm::StringRef> ChangedFiles;
111 for (const auto &it : Tool.getReplacements())
112 ChangedFiles.insert(it.first);
113
114 if (DumpYAML) {
115 auto WriteToYAML = [&](llvm::raw_ostream &OS) {
116 OS << "[\n";
117 for (auto I = ChangedFiles.begin(), E = ChangedFiles.end(); I != E; ++I) {
118 OS << " {\n";
119 OS << " \"FilePath\": \"" << *I << "\",\n";
120 const auto *Entry = FileMgr.getFile(*I);
121 auto ID = Sources.getOrCreateFileID(Entry, SrcMgr::C_User);
122 std::string Content;
123 llvm::raw_string_ostream ContentStream(Content);
124 Rewrite.getEditBuffer(ID).write(ContentStream);
125 OS << " \"SourceText\": \""
126 << llvm::yaml::escape(ContentStream.str()) << "\"\n";
127 OS << " }";
128 if (I != std::prev(E))
129 OS << ",\n";
130 }
131 OS << "\n]\n";
132 };
133 WriteToYAML(llvm::outs());
134 return 0;
135 }
136
137 for (const auto &File : ChangedFiles) {
Eric Liu495b2112016-09-19 17:40:32 +0000138 const auto *Entry = FileMgr.getFile(File);
139
140 auto ID = Sources.getOrCreateFileID(Entry, SrcMgr::C_User);
Eric Liu495b2112016-09-19 17:40:32 +0000141 outs() << "============== " << File << " ==============\n";
142 Rewrite.getEditBuffer(ID).write(llvm::outs());
143 outs() << "\n============================================\n";
144 }
Eric Liua13c4192017-02-13 17:24:14 +0000145
Eric Liu495b2112016-09-19 17:40:32 +0000146 return 0;
147}