From 84dc519667a4f06ed078f08eff2372a223a50b3e Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Fri, 18 Aug 2017 16:21:08 +0000 Subject: [PATCH] [CommonOptionsParser] Expose ArgumentsAdjustingCompilationDatabase This is useful for tools such as clang-diff which do not use CommonOptionsParser due to the need for multiple compilation databases. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311170 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Tooling/CommonOptionsParser.h | 24 +++++++++ lib/Tooling/CommonOptionsParser.cpp | 57 +++++++++------------ 2 files changed, 47 insertions(+), 34 deletions(-) diff --git a/include/clang/Tooling/CommonOptionsParser.h b/include/clang/Tooling/CommonOptionsParser.h index 3d630c5f76..c535603d36 100644 --- a/include/clang/Tooling/CommonOptionsParser.h +++ b/include/clang/Tooling/CommonOptionsParser.h @@ -27,6 +27,7 @@ #ifndef LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H #define LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H +#include "clang/Tooling/ArgumentsAdjusters.h" #include "clang/Tooling/CompilationDatabase.h" #include "llvm/Support/CommandLine.h" @@ -111,6 +112,29 @@ private: std::vector ExtraArgsAfter; }; +class ArgumentsAdjustingCompilations : public CompilationDatabase { +public: + ArgumentsAdjustingCompilations( + std::unique_ptr Compilations) + : Compilations(std::move(Compilations)) {} + + void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster); + + std::vector + getCompileCommands(StringRef FilePath) const override; + + std::vector getAllFiles() const override; + + std::vector getAllCompileCommands() const override; + +private: + std::unique_ptr Compilations; + std::vector Adjusters; + + std::vector + adjustCommands(std::vector Commands) const; +}; + } // namespace tooling } // namespace clang diff --git a/lib/Tooling/CommonOptionsParser.cpp b/lib/Tooling/CommonOptionsParser.cpp index 9e9689e6b2..3ef223fa55 100644 --- a/lib/Tooling/CommonOptionsParser.cpp +++ b/lib/Tooling/CommonOptionsParser.cpp @@ -25,7 +25,6 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/CommandLine.h" -#include "clang/Tooling/ArgumentsAdjusters.h" #include "clang/Tooling/CommonOptionsParser.h" #include "clang/Tooling/Tooling.h" @@ -54,43 +53,33 @@ const char *const CommonOptionsParser::HelpMessage = "\tsuffix of a path in the compile command database.\n" "\n"; -namespace { -class ArgumentsAdjustingCompilations : public CompilationDatabase { -public: - ArgumentsAdjustingCompilations( - std::unique_ptr Compilations) - : Compilations(std::move(Compilations)) {} - - void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster) { - Adjusters.push_back(std::move(Adjuster)); - } - - std::vector - getCompileCommands(StringRef FilePath) const override { - return adjustCommands(Compilations->getCompileCommands(FilePath)); - } +void ArgumentsAdjustingCompilations::appendArgumentsAdjuster( + ArgumentsAdjuster Adjuster) { + Adjusters.push_back(std::move(Adjuster)); +} - std::vector getAllFiles() const override { - return Compilations->getAllFiles(); - } +std::vector ArgumentsAdjustingCompilations::getCompileCommands( + StringRef FilePath) const { + return adjustCommands(Compilations->getCompileCommands(FilePath)); +} - std::vector getAllCompileCommands() const override { - return adjustCommands(Compilations->getAllCompileCommands()); - } +std::vector +ArgumentsAdjustingCompilations::getAllFiles() const { + return Compilations->getAllFiles(); +} -private: - std::unique_ptr Compilations; - std::vector Adjusters; +std::vector +ArgumentsAdjustingCompilations::getAllCompileCommands() const { + return adjustCommands(Compilations->getAllCompileCommands()); +} - std::vector - adjustCommands(std::vector Commands) const { - for (CompileCommand &Command : Commands) - for (const auto &Adjuster : Adjusters) - Command.CommandLine = Adjuster(Command.CommandLine, Command.Filename); - return Commands; - } -}; -} // namespace +std::vector ArgumentsAdjustingCompilations::adjustCommands( + std::vector Commands) const { + for (CompileCommand &Command : Commands) + for (const auto &Adjuster : Adjusters) + Command.CommandLine = Adjuster(Command.CommandLine, Command.Filename); + return Commands; +} CommonOptionsParser::CommonOptionsParser( int &argc, const char **argv, cl::OptionCategory &Category, -- 2.40.0