diff --git a/clang/test/Tooling/clang-check-extra-arg.cpp b/clang/test/Tooling/clang-check-extra-arg.cpp new file mode 100644 index 000000000000..f6715358453f --- /dev/null +++ b/clang/test/Tooling/clang-check-extra-arg.cpp @@ -0,0 +1,5 @@ +// RUN: clang-check "%s" -extra-arg=-Wunimplemented-warning -extra-arg-before=-Wunimplemented-warning-before -- -c 2>&1 | FileCheck %s + +// CHECK: unknown warning option '-Wunimplemented-warning-before' +// CHECK: unknown warning option '-Wunimplemented-warning' +int a(){} diff --git a/clang/tools/clang-check/ClangCheck.cpp b/clang/tools/clang-check/ClangCheck.cpp index bf4337486ad9..05783620ca3e 100644 --- a/clang/tools/clang-check/ClangCheck.cpp +++ b/clang/tools/clang-check/ClangCheck.cpp @@ -70,6 +70,11 @@ static cl::opt FixWhatYouCan( "fix-what-you-can", cl::desc(Options->getOptionHelpText(options::OPT_fix_what_you_can))); +static cl::list ArgsAfter("extra-arg", + cl::desc("Additional argument to append to the compiler command line")); +static cl::list ArgsBefore("extra-arg-before", + cl::desc("Additional argument to prepend to the compiler command line")); + namespace { // FIXME: Move FixItRewriteInPlace from lib/Rewrite/Frontend/FrontendActions.cpp @@ -123,6 +128,35 @@ public: } }; +class InsertAdjuster: public clang::tooling::ArgumentsAdjuster { +public: + enum Position { BEGIN, END }; + + InsertAdjuster(const CommandLineArguments &Extra, Position Pos) + : Extra(Extra), Pos(Pos) { + } + + virtual CommandLineArguments + Adjust(const CommandLineArguments &Args) LLVM_OVERRIDE { + CommandLineArguments Return(Args); + + CommandLineArguments::iterator I; + if (Pos == END) { + I = Return.end(); + } else { + I = Return.begin(); + ++I; // To leave the program name in place + } + + Return.insert(I, Extra.begin(), Extra.end()); + return Return; + } + +private: + const CommandLineArguments Extra; + const Position Pos; +}; + } // namespace // Anonymous namespace here causes problems with gcc <= 4.4 on MacOS 10.6. @@ -147,6 +181,12 @@ int main(int argc, const char **argv) { CommonOptionsParser OptionsParser(argc, argv); ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList()); + + if (ArgsAfter.size() > 0) + Tool.appendArgumentsAdjuster(new InsertAdjuster(ArgsAfter, InsertAdjuster::END)); + if (ArgsBefore.size() > 0) + Tool.appendArgumentsAdjuster(new InsertAdjuster(ArgsBefore, InsertAdjuster::BEGIN)); + if (Fixit) return Tool.run(newFrontendActionFactory()); clang_check::ClangCheckActionFactory Factory;