Author: d0k Date: Wed Apr 27 09:24:32 2016 New Revision: 267718 URL: http://llvm.org/viewvc/llvm-project?rev=267718&view=rev Log: 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. Added: clang-tools-extra/trunk/test/include-fixer/ clang-tools-extra/trunk/test/include-fixer/fixeddb.cpp Modified: clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp Modified: clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp?rev=267718&r1=267717&r2=267718&view=diff ============================================================================== --- clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp (original) +++ clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp Wed Apr 27 09:24:32 2016 @@ -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(); } Added: clang-tools-extra/trunk/test/include-fixer/fixeddb.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/include-fixer/fixeddb.cpp?rev=267718&view=auto ============================================================================== --- clang-tools-extra/trunk/test/include-fixer/fixeddb.cpp (added) +++ clang-tools-extra/trunk/test/include-fixer/fixeddb.cpp Wed Apr 27 09:24:32 2016 @@ -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; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits