From dee20c105b392d207d02871994f9fafd72fdf91b Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Thu, 6 Jun 2013 11:52:19 +0000 Subject: [PATCH] ClangTool: strip -o from the command line Summary: This patch creates a new ArgumentsAdjuster, which removes all -o parameters from the command line. This adjuster is inserted by default into the ClangTool pipeline. Reviewers: klimek CC: cfe-commits, revane Differential Revision: http://llvm-reviews.chandlerc.com/D925 llvm-svn: 183398 --- .../include/clang/Tooling/ArgumentsAdjusters.h | 6 ++++++ clang/include/clang/Tooling/Tooling.h | 2 +- clang/lib/Tooling/ArgumentsAdjusters.cpp | 17 +++++++++++++++++ clang/lib/Tooling/Tooling.cpp | 5 +++-- clang/test/Tooling/clang-check-strip-o.cpp | 11 +++++++++++ 5 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 clang/test/Tooling/clang-check-strip-o.cpp diff --git a/clang/include/clang/Tooling/ArgumentsAdjusters.h b/clang/include/clang/Tooling/ArgumentsAdjusters.h index 492ddd2b0078..71acef8650fe 100644 --- a/clang/include/clang/Tooling/ArgumentsAdjusters.h +++ b/clang/include/clang/Tooling/ArgumentsAdjusters.h @@ -52,6 +52,12 @@ class ClangSyntaxOnlyAdjuster : public ArgumentsAdjuster { virtual CommandLineArguments Adjust(const CommandLineArguments &Args); }; +/// \brief An argument adjuster which removes output-related command line +/// arguments. +class ClangStripOutputAdjuster : public ArgumentsAdjuster { + virtual CommandLineArguments Adjust(const CommandLineArguments &Args); +}; + } // end namespace tooling } // end namespace clang diff --git a/clang/include/clang/Tooling/Tooling.h b/clang/include/clang/Tooling/Tooling.h index dc8163e553fa..97bc2ab14645 100644 --- a/clang/include/clang/Tooling/Tooling.h +++ b/clang/include/clang/Tooling/Tooling.h @@ -233,7 +233,7 @@ class ClangTool { // Contains a list of pairs (, ). std::vector< std::pair > MappedFileContents; - SmallVector ArgsAdjusters; + SmallVector ArgsAdjusters; }; template diff --git a/clang/lib/Tooling/ArgumentsAdjusters.cpp b/clang/lib/Tooling/ArgumentsAdjusters.cpp index c44b20dd5a82..a69971e006eb 100644 --- a/clang/lib/Tooling/ArgumentsAdjusters.cpp +++ b/clang/lib/Tooling/ArgumentsAdjusters.cpp @@ -37,6 +37,23 @@ ClangSyntaxOnlyAdjuster::Adjust(const CommandLineArguments &Args) { return AdjustedArgs; } +CommandLineArguments +ClangStripOutputAdjuster::Adjust(const CommandLineArguments &Args) { + CommandLineArguments AdjustedArgs; + for (size_t i = 0, e = Args.size(); i < e; ++i) { + StringRef Arg = Args[i]; + if(!Arg.startswith("-o")) + AdjustedArgs.push_back(Args[i]); + + if(Arg == "-o") { + // Output is specified as -o foo. Skip the next argument also. + ++i; + } + // Else, the output is specified as -ofoo. Just do nothing. + } + return AdjustedArgs; +} + } // end namespace tooling } // end namespace clang diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp index cdb9bc71ee2b..a121cd0fd643 100644 --- a/clang/lib/Tooling/Tooling.cpp +++ b/clang/lib/Tooling/Tooling.cpp @@ -236,8 +236,9 @@ void ToolInvocation::addFileMappingsTo(SourceManager &Sources) { ClangTool::ClangTool(const CompilationDatabase &Compilations, ArrayRef SourcePaths) - : Files((FileSystemOptions())), - ArgsAdjusters(1, new ClangSyntaxOnlyAdjuster()) { + : Files((FileSystemOptions())) { + ArgsAdjusters.push_back(new ClangStripOutputAdjuster()); + ArgsAdjusters.push_back(new ClangSyntaxOnlyAdjuster()); for (unsigned I = 0, E = SourcePaths.size(); I != E; ++I) { SmallString<1024> File(getAbsolutePath(SourcePaths[I])); diff --git a/clang/test/Tooling/clang-check-strip-o.cpp b/clang/test/Tooling/clang-check-strip-o.cpp new file mode 100644 index 000000000000..38f09a0f92d1 --- /dev/null +++ b/clang/test/Tooling/clang-check-strip-o.cpp @@ -0,0 +1,11 @@ +// RUN: rm -rf %t +// RUN: mkdir %t +// RUN: echo '[{"directory":".","command":"clang++ -c %t/test.cpp -o foo -ofoo","file":"%t/test.cpp"}]' | sed -e 's/\\/\//g' > %t/compile_commands.json +// RUN: cp "%s" "%t/test.cpp" +// RUN: clang-check -p "%t" "%t/test.cpp" -extra-arg=-v 2>&1|FileCheck %s +// FIXME: Make the above easier. + +// CHECK: Invocation +// CHECK-NOT: {{ -v}} +// CHECK: C++ requires +invalid;