diff options
| author | Benjamin Kramer <benny.kra@googlemail.com> | 2016-04-27 14:24:32 +0000 |
|---|---|---|
| committer | Benjamin Kramer <benny.kra@googlemail.com> | 2016-04-27 14:24:32 +0000 |
| commit | f412e9029dc2edb4bc68f081a19431afcf88658e (patch) | |
| tree | 3462c80f7e61496c40f6ac2f796037456bec482a | |
| parent | df5ef8711f5b5b374144b32eecb9f2bacdecfb8a (diff) | |
| download | bcm5719-llvm-f412e9029dc2edb4bc68f081a19431afcf88658e.tar.gz bcm5719-llvm-f412e9029dc2edb4bc68f081a19431afcf88658e.zip | |
Clean up the include fixer 'driver' a bit and make the database configurable.
Also add a test for it. The library is covered by unit tests, the driver
was not.
llvm-svn: 267718
| -rw-r--r-- | clang-tools-extra/include-fixer/tool/ClangIncludeFixer.cpp | 67 | ||||
| -rw-r--r-- | clang-tools-extra/test/include-fixer/fixeddb.cpp | 9 |
2 files changed, 60 insertions, 16 deletions
diff --git a/clang-tools-extra/include-fixer/tool/ClangIncludeFixer.cpp b/clang-tools-extra/include-fixer/tool/ClangIncludeFixer.cpp index 307974d8f2f..f36c263f640 100644 --- a/clang-tools-extra/include-fixer/tool/ClangIncludeFixer.cpp +++ b/clang-tools-extra/include-fixer/tool/ClangIncludeFixer.cpp @@ -15,35 +15,70 @@ #include "clang/Tooling/Tooling.h" #include "llvm/Support/CommandLine.h" using namespace clang; +using namespace llvm; -static llvm::cl::OptionCategory tool_options("Tool options"); +namespace { +cl::OptionCategory IncludeFixerCategory("Tool options"); + +enum DatabaseFormatTy { + fixed, //< Hard-coded mapping. +}; + +cl::opt<DatabaseFormatTy> DatabaseFormat( + "db", cl::desc("Specify input format"), + cl::values(clEnumVal(fixed, "Hard-coded mapping"), clEnumValEnd), + cl::init(fixed), cl::cat(IncludeFixerCategory)); + +cl::opt<std::string> Input("input", + cl::desc("String to initialize the database"), + cl::cat(IncludeFixerCategory)); +} // namespace int main(int argc, const char **argv) { - clang::tooling::CommonOptionsParser options(argc, argv, tool_options); - clang::tooling::ClangTool tool(options.getCompilations(), - options.getSourcePathList()); + tooling::CommonOptionsParser options(argc, argv, IncludeFixerCategory); + tooling::ClangTool tool(options.getCompilations(), + options.getSourcePathList()); + // Set up the data source. - std::map<std::string, std::vector<std::string>> XrefsMap = { - {"std::string", {"<string>"}}}; - auto XrefsDB = - llvm::make_unique<include_fixer::InMemoryXrefsDB>(std::move(XrefsMap)); + std::unique_ptr<include_fixer::XrefsDB> XrefsDB; + switch (DatabaseFormat) { + case fixed: { + // Parse input and fill the database with it. + // <symbol>=<header><, header...> + // Multiple symbols can be given, separated by semicolons. + std::map<std::string, std::vector<std::string>> XrefsMap; + SmallVector<StringRef, 4> SemicolonSplits; + StringRef(Input).split(SemicolonSplits, ";"); + for (StringRef Pair : SemicolonSplits) { + auto Split = Pair.split('='); + std::vector<std::string> Headers; + SmallVector<StringRef, 4> CommaSplits; + Split.second.split(CommaSplits, ","); + for (StringRef Header : CommaSplits) + Headers.push_back(Header.trim()); + XrefsMap[Split.first.trim()] = std::move(Headers); + } + XrefsDB = + llvm::make_unique<include_fixer::InMemoryXrefsDB>(std::move(XrefsMap)); + break; + } + } // Now run our tool. - std::vector<clang::tooling::Replacement> Replacements; + std::vector<tooling::Replacement> Replacements; include_fixer::IncludeFixerActionFactory Factory(*XrefsDB, Replacements); tool.run(&Factory); // Always succeeds. // Set up a new source manager for applying the resulting replacements. - llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts( - new clang::DiagnosticOptions); - clang::DiagnosticsEngine Diagnostics(new clang::DiagnosticIDs, &*DiagOpts); - clang::TextDiagnosticPrinter DiagnosticPrinter(llvm::outs(), &*DiagOpts); - clang::SourceManager source_manager(Diagnostics, tool.getFiles()); + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); + DiagnosticsEngine Diagnostics(new DiagnosticIDs, &*DiagOpts); + TextDiagnosticPrinter DiagnosticPrinter(outs(), &*DiagOpts); + SourceManager SM(Diagnostics, tool.getFiles()); Diagnostics.setClient(&DiagnosticPrinter, false); // Write replacements to disk. - clang::Rewriter Rewrites(source_manager, clang::LangOptions()); - clang::tooling::applyAllReplacements(Replacements, Rewrites); + Rewriter Rewrites(SM, LangOptions()); + tooling::applyAllReplacements(Replacements, Rewrites); return Rewrites.overwriteChangedFiles(); } diff --git a/clang-tools-extra/test/include-fixer/fixeddb.cpp b/clang-tools-extra/test/include-fixer/fixeddb.cpp new file mode 100644 index 00000000000..5efd810a01b --- /dev/null +++ b/clang-tools-extra/test/include-fixer/fixeddb.cpp @@ -0,0 +1,9 @@ +// REQUIRES: shell +// RUN: sed -e 's#//.*$##' %s > %t.cpp +// RUN: clang-include-fixer -input='foo= "foo.h","bar.h"' %t.cpp -- +// RUN: FileCheck %s -input-file=%t.cpp + +// CHECK: #include "foo.h" +// CHECK: foo f; + +foo f; |

