summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-reorder-fields/tool/ClangReorderFields.cpp
diff options
context:
space:
mode:
authorAlexander Shaposhnikov <shal1t712@gmail.com>2016-09-01 23:49:48 +0000
committerAlexander Shaposhnikov <shal1t712@gmail.com>2016-09-01 23:49:48 +0000
commitae4ff453a4a95140669dd0cab28ec7d7032e5449 (patch)
tree822a30d1e71e04cd647e9ecd6f61902f205a3685 /clang-tools-extra/clang-reorder-fields/tool/ClangReorderFields.cpp
parent356f79d5357a14cf57014cf05757412ad10bc236 (diff)
downloadbcm5719-llvm-ae4ff453a4a95140669dd0cab28ec7d7032e5449.tar.gz
bcm5719-llvm-ae4ff453a4a95140669dd0cab28ec7d7032e5449.zip
Add clang-reorder-fields to clang-tools-extra
This diff adds v0 of clang-reorder-fields tool to clang/tools/extra. The main idea behind this tool is to simplify and make less error-prone refactoring of large codebases when someone needs to change the order fields of a struct/class (for example to remove excess padding). Differential revision: https://reviews.llvm.org/D23279 llvm-svn: 280431
Diffstat (limited to 'clang-tools-extra/clang-reorder-fields/tool/ClangReorderFields.cpp')
-rw-r--r--clang-tools-extra/clang-reorder-fields/tool/ClangReorderFields.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-reorder-fields/tool/ClangReorderFields.cpp b/clang-tools-extra/clang-reorder-fields/tool/ClangReorderFields.cpp
new file mode 100644
index 00000000000..3061876c330
--- /dev/null
+++ b/clang-tools-extra/clang-reorder-fields/tool/ClangReorderFields.cpp
@@ -0,0 +1,93 @@
+//===-- tools/extra/clang-reorder-fields/tool/ClangReorderFields.cpp -*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains the implementation of clang-reorder-fields tool
+///
+//===----------------------------------------------------------------------===//
+
+#include "../ReorderFieldsAction.h"
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/DiagnosticOptions.h"
+#include "clang/Basic/FileManager.h"
+#include "clang/Basic/LangOptions.h"
+#include "clang/Basic/SourceManager.h"
+#include "clang/Frontend/TextDiagnosticPrinter.h"
+#include "clang/Rewrite/Core/Rewriter.h"
+#include "clang/Tooling/CommonOptionsParser.h"
+#include "clang/Tooling/Refactoring.h"
+#include "clang/Tooling/Tooling.h"
+#include "llvm/ADT/IntrusiveRefCntPtr.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileSystem.h"
+#include <cstdlib>
+#include <string>
+#include <system_error>
+
+using namespace llvm;
+using namespace clang;
+
+cl::OptionCategory ClangReorderFieldsCategory("clang-reorder-fields options");
+
+static cl::opt<std::string>
+ RecordName("record-name", cl::Required,
+ cl::desc("The name of the struct/class."),
+ cl::cat(ClangReorderFieldsCategory));
+
+static cl::list<std::string> FieldsOrder("fields-order", cl::CommaSeparated,
+ cl::OneOrMore,
+ cl::desc("The desired fields order."),
+ cl::cat(ClangReorderFieldsCategory));
+
+static cl::opt<bool> Inplace("i", cl::desc("Overwrite edited files."),
+ cl::cat(ClangReorderFieldsCategory));
+
+const char Usage[] = "A tool to reorder fields in C/C++ structs/classes.\n";
+
+int main(int argc, const char **argv) {
+ tooling::CommonOptionsParser OP(argc, argv, ClangReorderFieldsCategory,
+ Usage);
+
+ auto Files = OP.getSourcePathList();
+ tooling::RefactoringTool Tool(OP.getCompilations(), Files);
+
+ reorder_fields::ReorderFieldsAction Action(RecordName, FieldsOrder,
+ Tool.getReplacements());
+
+ auto Factory = tooling::newFrontendActionFactory(&Action);
+
+ if (Inplace)
+ return Tool.runAndSave(Factory.get());
+
+ int ExitCode = Tool.run(Factory.get());
+ LangOptions DefaultLangOptions;
+ IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions());
+ TextDiagnosticPrinter DiagnosticPrinter(errs(), &*DiagOpts);
+ DiagnosticsEngine Diagnostics(
+ IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
+ &DiagnosticPrinter, false);
+
+ auto &FileMgr = Tool.getFiles();
+ SourceManager Sources(Diagnostics, FileMgr);
+ Rewriter Rewrite(Sources, DefaultLangOptions);
+ Tool.applyAllReplacements(Rewrite);
+
+ for (const auto &File : Files) {
+ const auto *Entry = FileMgr.getFile(File);
+ const auto ID = Sources.translateFile(Entry);
+ // The method Rewriter::getRewriteBufferFor returns nullptr if
+ // the file has not been changed.
+ if (const auto *RB = Rewrite.getRewriteBufferFor(ID))
+ RB->write(outs());
+ else
+ outs() << Sources.getMemoryBufferForFile(Entry)->getBuffer();
+ }
+
+ return ExitCode;
+}
OpenPOWER on IntegriCloud