33 #include "clang/ASTMatchers/ASTMatchFinder.h" 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 #include "llvm/Support/Signals.h" 42 #include "llvm/Support/YAMLTraits.h" 44 using namespace clang;
49 cl::OptionCategory ChangeNamespaceCategory(
"Change namespace.");
51 cl::opt<std::string> OldNamespace(
"old_namespace", cl::Required,
52 cl::desc(
"Old namespace."),
53 cl::cat(ChangeNamespaceCategory));
55 cl::opt<std::string> NewNamespace(
"new_namespace", cl::Required,
56 cl::desc(
"New namespace."),
57 cl::cat(ChangeNamespaceCategory));
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));
64 cl::opt<bool>
Inplace(
"i", cl::desc(
"Inplace edit <file>s, if specified."),
65 cl::cat(ChangeNamespaceCategory));
68 DumpYAML(
"dump_result",
69 cl::desc(
"Dump new file contents in YAML, if specified."),
70 cl::cat(ChangeNamespaceCategory));
72 cl::opt<std::string> Style(
"style",
73 cl::desc(
"The style name used for reformatting."),
74 cl::init(
"LLVM"), cl::cat(ChangeNamespaceCategory));
76 cl::opt<std::string> WhiteListFile(
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));
82 llvm::ErrorOr<std::vector<std::string>> GetWhiteListedSymbolPatterns() {
83 std::vector<std::string> Patterns;
84 if (WhiteListFile.empty())
87 llvm::SmallVector<StringRef, 8>
Lines;
88 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
File =
89 llvm::MemoryBuffer::getFile(WhiteListFile);
91 return File.getError();
92 llvm::StringRef Content = File.get()->getBuffer();
93 Content.split(Lines,
'\n', -1,
false);
94 for (
auto Line : Lines)
95 Patterns.push_back(
Line.trim());
101 int main(
int argc,
const char **argv) {
102 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
103 tooling::CommonOptionsParser OptionsParser(argc, argv,
104 ChangeNamespaceCategory);
105 const auto &Files = OptionsParser.getSourcePathList();
106 tooling::RefactoringTool Tool(OptionsParser.getCompilations(), Files);
107 llvm::ErrorOr<std::vector<std::string>> WhiteListPatterns =
108 GetWhiteListedSymbolPatterns();
109 if (!WhiteListPatterns) {
110 llvm::errs() <<
"Failed to open whitelist file " << WhiteListFile <<
". " 111 << WhiteListPatterns.getError().message() <<
"\n";
115 OldNamespace, NewNamespace, FilePattern, *WhiteListPatterns,
116 &Tool.getReplacements(), Style);
117 ast_matchers::MatchFinder Finder;
118 NamespaceTool.registerMatchers(&Finder);
119 std::unique_ptr<tooling::FrontendActionFactory> Factory =
120 tooling::newFrontendActionFactory(&Finder);
122 if (
int Result = Tool.run(Factory.get()))
124 LangOptions DefaultLangOptions;
125 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts =
new DiagnosticOptions();
126 clang::TextDiagnosticPrinter DiagnosticPrinter(errs(), &*DiagOpts);
127 DiagnosticsEngine Diagnostics(
128 IntrusiveRefCntPtr<DiagnosticIDs>(
new DiagnosticIDs()), &*DiagOpts,
129 &DiagnosticPrinter,
false);
130 auto &FileMgr = Tool.getFiles();
131 SourceManager Sources(Diagnostics, FileMgr);
132 Rewriter Rewrite(Sources, DefaultLangOptions);
134 if (!formatAndApplyAllReplacements(Tool.getReplacements(), Rewrite, Style)) {
135 llvm::errs() <<
"Failed applying all replacements.\n";
139 return Rewrite.overwriteChangedFiles();
141 std::set<llvm::StringRef> ChangedFiles;
142 for (
const auto &it : Tool.getReplacements())
143 ChangedFiles.insert(it.first);
146 auto WriteToYAML = [&](llvm::raw_ostream &OS) {
148 for (
auto I = ChangedFiles.begin(), E = ChangedFiles.end(); I != E; ++I) {
150 OS <<
" \"FilePath\": \"" << *I <<
"\",\n";
151 const auto *
Entry = FileMgr.getFile(*I);
152 auto ID = Sources.getOrCreateFileID(
Entry, SrcMgr::C_User);
154 llvm::raw_string_ostream ContentStream(Content);
155 Rewrite.getEditBuffer(ID).write(ContentStream);
156 OS <<
" \"SourceText\": \"" 157 << llvm::yaml::escape(ContentStream.str()) <<
"\"\n";
159 if (I != std::prev(E))
164 WriteToYAML(llvm::outs());
168 for (
const auto &File : ChangedFiles) {
169 const auto *
Entry = FileMgr.getFile(File);
171 auto ID = Sources.getOrCreateFileID(
Entry, SrcMgr::C_User);
172 outs() <<
"============== " << File <<
" ==============\n";
173 Rewrite.getEditBuffer(ID).write(llvm::outs());
174 outs() <<
"\n============================================\n";
Some operations such as code completion produce a set of candidates.
static cl::opt< bool > Inplace("i", cl::desc("Overwrite edited files."), cl::cat(ClangReorderFieldsCategory))
int main(int argc, const char **argv)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//