diff options
author | Alexander Kornienko <alexfh@google.com> | 2014-11-04 08:51:24 +0000 |
---|---|---|
committer | Alexander Kornienko <alexfh@google.com> | 2014-11-04 08:51:24 +0000 |
commit | 616860994d63dfae6e92b31db953eed855b7c42d (patch) | |
tree | a56a4927e6e3391e07020630add1ff8629062056 /clang/lib/Tooling/CommonOptionsParser.cpp | |
parent | ea9b8ee43d2855797319962225ef1ed438362de7 (diff) | |
download | bcm5719-llvm-616860994d63dfae6e92b31db953eed855b7c42d.tar.gz bcm5719-llvm-616860994d63dfae6e92b31db953eed855b7c42d.zip |
[clang-tidy] Move -extra-arg handling to CommonOptionsProvider
Summary:
Handle -extra-arg and -extra-arg-before options in the
CommonOptionsProvider so they can be used in all clang tools. Adjust arguments
in a CompilationDatabase wrapper instead of adding ArgumentsAdjuster to the
tool.
Reviewers: djasper, klimek
Reviewed By: klimek
Subscribers: klimek, cfe-commits
Differential Revision: http://reviews.llvm.org/D6073
llvm-svn: 221248
Diffstat (limited to 'clang/lib/Tooling/CommonOptionsParser.cpp')
-rw-r--r-- | clang/lib/Tooling/CommonOptionsParser.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/clang/lib/Tooling/CommonOptionsParser.cpp b/clang/lib/Tooling/CommonOptionsParser.cpp index c15f40d7f79..f16a6be8d0c 100644 --- a/clang/lib/Tooling/CommonOptionsParser.cpp +++ b/clang/lib/Tooling/CommonOptionsParser.cpp @@ -25,6 +25,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/CommandLine.h" +#include "clang/Tooling/ArgumentsAdjusters.h" #include "clang/Tooling/CommonOptionsParser.h" #include "clang/Tooling/Tooling.h" @@ -53,6 +54,42 @@ const char *const CommonOptionsParser::HelpMessage = "\tsuffix of a path in the compile command database.\n" "\n"; +class ArgumentsAdjustingCompilations : public CompilationDatabase { +public: + ArgumentsAdjustingCompilations( + std::unique_ptr<CompilationDatabase> Compilations) + : Compilations(std::move(Compilations)) {} + + void appendArgumentsAdjuster(std::unique_ptr<ArgumentsAdjuster> Adjuster) { + Adjusters.push_back(std::move(Adjuster)); + } + + std::vector<CompileCommand> + getCompileCommands(StringRef FilePath) const override { + return adjustCommands(Compilations->getCompileCommands(FilePath)); + } + + std::vector<std::string> getAllFiles() const override { + return Compilations->getAllFiles(); + } + + std::vector<CompileCommand> getAllCompileCommands() const override { + return adjustCommands(Compilations->getAllCompileCommands()); + } + +private: + std::unique_ptr<CompilationDatabase> Compilations; + std::vector<std::unique_ptr<ArgumentsAdjuster>> Adjusters; + + std::vector<CompileCommand> + adjustCommands(std::vector<CompileCommand> Commands) const { + for (CompileCommand &Command : Commands) + for (const auto &Adjuster : Adjusters) + Command.CommandLine = Adjuster->Adjust(Command.CommandLine); + return Commands; + } +}; + CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv, cl::OptionCategory &Category, const char *Overview) { @@ -65,6 +102,16 @@ CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv, cl::Positional, cl::desc("<source0> [... <sourceN>]"), cl::OneOrMore, cl::cat(Category)); + static cl::list<std::string> ArgsAfter( + "extra-arg", + cl::desc("Additional argument to append to the compiler command line"), + cl::cat(Category)); + + static cl::list<std::string> ArgsBefore( + "extra-arg-before", + cl::desc("Additional argument to prepend to the compiler command line"), + cl::cat(Category)); + // Hide unrelated options. StringMap<cl::Option*> Options; cl::getRegisteredOptions(Options); @@ -91,4 +138,14 @@ CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv, if (!Compilations) llvm::report_fatal_error(ErrorMessage); } + auto AdjustingCompilations = + llvm::make_unique<ArgumentsAdjustingCompilations>( + std::move(Compilations)); + AdjustingCompilations->appendArgumentsAdjuster( + llvm::make_unique<InsertArgumentAdjuster>(ArgsBefore, + InsertArgumentAdjuster::BEGIN)); + AdjustingCompilations->appendArgumentsAdjuster( + llvm::make_unique<InsertArgumentAdjuster>(ArgsAfter, + InsertArgumentAdjuster::END)); + Compilations = std::move(AdjustingCompilations); } |