10 #include "clang/Frontend/TextDiagnosticPrinter.h" 11 #include "clang/Rewrite/Core/Rewriter.h" 12 #include "clang/Tooling/ArgumentsAdjusters.h" 13 #include "clang/Tooling/CommonOptionsParser.h" 14 #include "clang/Tooling/Refactoring.h" 15 #include "clang/Tooling/Tooling.h" 16 #include "llvm/ADT/StringRef.h" 17 #include "llvm/Support/CommandLine.h" 18 #include "llvm/Support/Path.h" 19 #include "llvm/Support/Process.h" 20 #include "llvm/Support/Signals.h" 21 #include "llvm/Support/YAMLTraits.h" 25 using namespace clang;
30 std::error_code CreateNewFile(
const llvm::Twine &path) {
32 if (std::error_code ec = llvm::sys::fs::openFileForWrite(
33 path, fd, llvm::sys::fs::CD_CreateAlways, llvm::sys::fs::OF_Text))
36 return llvm::sys::Process::SafelyCloseFileDescriptor(fd);
39 cl::OptionCategory ClangMoveCategory(
"clang-move options");
41 cl::list<std::string> Names(
"names", cl::CommaSeparated,
42 cl::desc(
"The list of the names of classes being " 43 "moved, e.g. \"Foo,a::Foo,b::Foo\"."),
44 cl::cat(ClangMoveCategory));
47 OldHeader(
"old_header",
48 cl::desc(
"The relative/absolute file path of old header."),
49 cl::cat(ClangMoveCategory));
52 OldCC(
"old_cc", cl::desc(
"The relative/absolute file path of old cc."),
53 cl::cat(ClangMoveCategory));
56 NewHeader(
"new_header",
57 cl::desc(
"The relative/absolute file path of new header."),
58 cl::cat(ClangMoveCategory));
61 NewCC(
"new_cc", cl::desc(
"The relative/absolute file path of new cc."),
62 cl::cat(ClangMoveCategory));
65 OldDependOnNew(
"old_depend_on_new",
66 cl::desc(
"Whether old header will depend on new header. If " 67 "true, clang-move will " 68 "add #include of new header to old header."),
69 cl::init(
false), cl::cat(ClangMoveCategory));
72 NewDependOnOld(
"new_depend_on_old",
73 cl::desc(
"Whether new header will depend on old header. If " 74 "true, clang-move will " 75 "add #include of old header to new header."),
76 cl::init(
false), cl::cat(ClangMoveCategory));
80 cl::desc(
"The style name used for reformatting. Default is \"llvm\""),
81 cl::init(
"llvm"), cl::cat(ClangMoveCategory));
83 cl::opt<bool> Dump(
"dump_result",
84 cl::desc(
"Dump results in JSON format to stdout."),
85 cl::cat(ClangMoveCategory));
87 cl::opt<bool> DumpDecls(
89 cl::desc(
"Dump all declarations in old header (JSON format) to stdout. If " 90 "the option is specified, other command options will be ignored. " 91 "An empty JSON will be returned if old header isn't specified."),
92 cl::cat(ClangMoveCategory));
96 int main(
int argc,
const char **argv) {
97 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
98 tooling::CommonOptionsParser OptionsParser(argc, argv, ClangMoveCategory);
100 if (OldDependOnNew && NewDependOnOld) {
101 llvm::errs() <<
"Provide either --old_depend_on_new or " 102 "--new_depend_on_old. clang-move doesn't support these two " 103 "options at same time (It will introduce include cycle).\n";
107 tooling::RefactoringTool Tool(OptionsParser.getCompilations(),
108 OptionsParser.getSourcePathList());
110 Tool.appendArgumentsAdjuster(tooling::getInsertArgumentAdjuster(
111 "-fparse-all-comments", tooling::ArgumentInsertPosition::BEGIN));
113 Spec.
Names = {Names.begin(), Names.end()};
121 llvm::SmallString<128> InitialDirectory;
122 if (std::error_code EC = llvm::sys::fs::current_path(InitialDirectory))
123 llvm::report_fatal_error(
"Cannot detect current path: " +
124 Twine(EC.message()));
127 InitialDirectory.str(), Style, DumpDecls};
131 int CodeStatus = Tool.run(&Factory);
136 llvm::outs() <<
"[\n";
138 for (
auto I = Declarations.begin(),
E = Declarations.end(); I !=
E; ++I) {
139 llvm::outs() <<
" {\n";
140 llvm::outs() <<
" \"DeclarationName\": \"" << I->QualifiedName
142 llvm::outs() <<
" \"DeclarationType\": \"" << I->Kind <<
"\",\n";
143 llvm::outs() <<
" \"Templated\": " << (I->Templated ?
"true" :
"false")
145 llvm::outs() <<
" }";
147 if (I != std::prev(
E))
148 llvm::outs() <<
",\n";
150 llvm::outs() <<
"\n]\n";
154 if (!NewCC.empty()) {
155 std::error_code EC = CreateNewFile(NewCC);
157 llvm::errs() <<
"Failed to create " << NewCC <<
": " << EC.message()
162 if (!NewHeader.empty()) {
163 std::error_code EC = CreateNewFile(NewHeader);
165 llvm::errs() <<
"Failed to create " << NewHeader <<
": " << EC.message()
171 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(
new DiagnosticOptions());
172 clang::TextDiagnosticPrinter DiagnosticPrinter(errs(), &*DiagOpts);
173 DiagnosticsEngine Diagnostics(
174 IntrusiveRefCntPtr<DiagnosticIDs>(
new DiagnosticIDs()), &*DiagOpts,
175 &DiagnosticPrinter,
false);
176 auto &FileMgr = Tool.getFiles();
177 SourceManager SM(Diagnostics, FileMgr);
178 Rewriter Rewrite(SM, LangOptions());
180 if (!formatAndApplyAllReplacements(Tool.getReplacements(), Rewrite, Style)) {
181 llvm::errs() <<
"Failed applying all replacements.\n";
186 std::set<llvm::StringRef>
Files;
187 for (
const auto &it : Tool.getReplacements())
188 Files.insert(it.first);
189 auto WriteToJson = [&](llvm::raw_ostream &OS) {
191 for (
auto I = Files.begin(),
E = Files.end(); I !=
E; ++I) {
193 OS <<
" \"FilePath\": \"" << *I <<
"\",\n";
194 const auto Entry = FileMgr.getFile(*I);
195 auto ID = SM.translateFile(*
Entry);
197 llvm::raw_string_ostream ContentStream(Content);
198 Rewrite.getEditBuffer(ID).write(ContentStream);
199 OS <<
" \"SourceText\": \"" 200 << llvm::yaml::escape(ContentStream.str()) <<
"\"\n";
202 if (I != std::prev(
E))
207 WriteToJson(llvm::outs());
211 return Rewrite.overwriteChangedFiles();
Some operations such as code completion produce a set of candidates.
int main(int argc, const char **argv)
SmallVector< std::string, 4 > Names
const std::vector< Declaration > getDeclarationList() const
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::StringMap< std::string > Files