15 #include "../ReorderFieldsAction.h" 16 #include "clang/Basic/Diagnostic.h" 17 #include "clang/Basic/DiagnosticOptions.h" 18 #include "clang/Basic/FileManager.h" 19 #include "clang/Basic/LangOptions.h" 20 #include "clang/Basic/SourceManager.h" 21 #include "clang/Frontend/TextDiagnosticPrinter.h" 22 #include "clang/Rewrite/Core/Rewriter.h" 23 #include "clang/Tooling/CommonOptionsParser.h" 24 #include "clang/Tooling/Refactoring.h" 25 #include "clang/Tooling/Tooling.h" 26 #include "llvm/ADT/IntrusiveRefCntPtr.h" 27 #include "llvm/Support/CommandLine.h" 28 #include "llvm/Support/FileSystem.h" 31 #include <system_error> 34 using namespace clang;
38 static cl::opt<std::string>
40 cl::desc(
"The name of the struct/class."),
43 static cl::list<std::string>
FieldsOrder(
"fields-order", cl::CommaSeparated,
45 cl::desc(
"The desired fields order."),
48 static cl::opt<bool>
Inplace(
"i", cl::desc(
"Overwrite edited files."),
51 const char Usage[] =
"A tool to reorder fields in C/C++ structs/classes.\n";
53 int main(
int argc,
const char **argv) {
57 auto Files = OP.getSourcePathList();
58 tooling::RefactoringTool Tool(OP.getCompilations(), Files);
61 Tool.getReplacements());
63 auto Factory = tooling::newFrontendActionFactory(&
Action);
66 return Tool.runAndSave(Factory.get());
68 int ExitCode = Tool.run(Factory.get());
69 LangOptions DefaultLangOptions;
70 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(
new DiagnosticOptions());
71 TextDiagnosticPrinter DiagnosticPrinter(errs(), &*DiagOpts);
72 DiagnosticsEngine Diagnostics(
73 IntrusiveRefCntPtr<DiagnosticIDs>(
new DiagnosticIDs()), &*DiagOpts,
74 &DiagnosticPrinter,
false);
76 auto &FileMgr = Tool.getFiles();
77 SourceManager Sources(Diagnostics, FileMgr);
78 Rewriter Rewrite(Sources, DefaultLangOptions);
79 Tool.applyAllReplacements(Rewrite);
81 for (
const auto &File : Files) {
82 const auto *
Entry = FileMgr.getFile(File);
83 const auto ID = Sources.getOrCreateFileID(
Entry, SrcMgr::C_User);
84 Rewrite.getEditBuffer(ID).write(outs());
static cl::opt< std::string > RecordName("record-name", cl::Required, cl::desc("The name of the struct/class."), cl::cat(ClangReorderFieldsCategory))
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))
llvm::unique_function< void()> Action
static cl::list< std::string > FieldsOrder("fields-order", cl::CommaSeparated, cl::OneOrMore, cl::desc("The desired fields order."), cl::cat(ClangReorderFieldsCategory))
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
int main(int argc, const char **argv)
cl::OptionCategory ClangReorderFieldsCategory("clang-reorder-fields options")