14#include "../ReorderFieldsAction.h"
15#include "clang/Basic/Diagnostic.h"
16#include "clang/Basic/DiagnosticOptions.h"
17#include "clang/Basic/FileManager.h"
18#include "clang/Basic/LangOptions.h"
19#include "clang/Basic/SourceManager.h"
20#include "clang/Frontend/TextDiagnosticPrinter.h"
21#include "clang/Rewrite/Core/Rewriter.h"
22#include "clang/Tooling/CommonOptionsParser.h"
23#include "clang/Tooling/Refactoring.h"
24#include "clang/Tooling/Tooling.h"
25#include "llvm/ADT/IntrusiveRefCntPtr.h"
26#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/FileSystem.h"
30#include <system_error>
37static cl::opt<std::string>
38 RecordName(
"record-name", cl::Required,
39 cl::desc(
"The name of the struct/class."),
42static cl::list<std::string>
FieldsOrder(
"fields-order", cl::CommaSeparated,
44 cl::desc(
"The desired fields order."),
47static cl::opt<bool>
Inplace(
"i", cl::desc(
"Overwrite edited files."),
50const char Usage[] =
"A tool to reorder fields in C/C++ structs/classes.\n";
52int main(
int argc,
const char **argv) {
53 auto ExpectedParser = tooling::CommonOptionsParser::create(
55 if (!ExpectedParser) {
56 llvm::errs() << ExpectedParser.takeError();
60 tooling::CommonOptionsParser &OP = ExpectedParser.get();
62 auto Files = OP.getSourcePathList();
63 tooling::RefactoringTool Tool(OP.getCompilations(), Files);
66 Tool.getReplacements());
68 auto Factory = tooling::newFrontendActionFactory(&
Action);
71 return Tool.runAndSave(Factory.get());
73 int ExitCode = Tool.run(Factory.get());
74 LangOptions DefaultLangOptions;
75 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(
new DiagnosticOptions());
76 TextDiagnosticPrinter DiagnosticPrinter(errs(), &*DiagOpts);
78 IntrusiveRefCntPtr<DiagnosticIDs>(
new DiagnosticIDs()), &*DiagOpts,
79 &DiagnosticPrinter,
false);
81 auto &FileMgr = Tool.getFiles();
83 Rewriter Rewrite(Sources, DefaultLangOptions);
84 Tool.applyAllReplacements(Rewrite);
86 for (
const auto &File : Files) {
87 auto Entry = llvm::cantFail(FileMgr.getFileRef(File));
88 const auto ID = Sources.getOrCreateFileID(
Entry, SrcMgr::C_User);
89 Rewrite.getEditBuffer(
ID).write(outs());
int main(int argc, const char **argv)
static cl::list< std::string > FieldsOrder("fields-order", cl::CommaSeparated, cl::OneOrMore, cl::desc("The desired fields order."), cl::cat(ClangReorderFieldsCategory))
cl::OptionCategory ClangReorderFieldsCategory("clang-reorder-fields options")
static cl::opt< bool > Inplace("i", cl::desc("Overwrite edited files."), cl::cat(ClangReorderFieldsCategory))
WantDiagnostics Diagnostics
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Some operations such as code completion produce a set of candidates.