blob: 1cfec1288cfb852459425df0adec2df18a342207 [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 Liu495b2112016-09-19 17:40:32 +000042
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<std::string> Style("style",
67 cl::desc("The style name used for reformatting."),
68 cl::init("LLVM"), cl::cat(ChangeNamespaceCategory));
69
70} // anonymous namespace
71
72int main(int argc, const char **argv) {
Eric Liu3db05212016-10-13 18:56:14 +000073 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
Eric Liu495b2112016-09-19 17:40:32 +000074 tooling::CommonOptionsParser OptionsParser(argc, argv,
75 ChangeNamespaceCategory);
76 const auto &Files = OptionsParser.getSourcePathList();
77 tooling::RefactoringTool Tool(OptionsParser.getCompilations(), Files);
78 change_namespace::ChangeNamespaceTool NamespaceTool(
Haojian Wub530f162016-10-05 17:00:40 +000079 OldNamespace, NewNamespace, FilePattern, &Tool.getReplacements(), Style);
Eric Liu495b2112016-09-19 17:40:32 +000080 ast_matchers::MatchFinder Finder;
81 NamespaceTool.registerMatchers(&Finder);
82 std::unique_ptr<tooling::FrontendActionFactory> Factory =
83 tooling::newFrontendActionFactory(&Finder);
84
85 if (int Result = Tool.run(Factory.get()))
86 return Result;
87 LangOptions DefaultLangOptions;
88 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
89 clang::TextDiagnosticPrinter DiagnosticPrinter(errs(), &*DiagOpts);
90 DiagnosticsEngine Diagnostics(
91 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
92 &DiagnosticPrinter, false);
93 auto &FileMgr = Tool.getFiles();
94 SourceManager Sources(Diagnostics, FileMgr);
95 Rewriter Rewrite(Sources, DefaultLangOptions);
96
97 if (!formatAndApplyAllReplacements(Tool.getReplacements(), Rewrite, Style)) {
98 llvm::errs() << "Failed applying all replacements.\n";
99 return 1;
100 }
101 if (Inplace)
102 return Rewrite.overwriteChangedFiles();
103
104 for (const auto &File : Files) {
105 const auto *Entry = FileMgr.getFile(File);
106
107 auto ID = Sources.getOrCreateFileID(Entry, SrcMgr::C_User);
108 // FIXME: print results in parsable format, e.g. JSON.
109 outs() << "============== " << File << " ==============\n";
110 Rewrite.getEditBuffer(ID).write(llvm::outs());
111 outs() << "\n============================================\n";
112 }
113 return 0;
114}