diff --git a/clang/lib/Tooling/ArgumentsAdjusters.cpp b/clang/lib/Tooling/ArgumentsAdjusters.cpp --- a/clang/lib/Tooling/ArgumentsAdjusters.cpp +++ b/clang/lib/Tooling/ArgumentsAdjusters.cpp @@ -13,20 +13,32 @@ #include "clang/Tooling/ArgumentsAdjusters.h" #include "clang/Basic/LLVM.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringRef.h" #include +#include namespace clang { namespace tooling { -/// Add -fsyntax-only option to the command line arguments. +/// Add -fsyntax-only option and drop options that triggers output generation. ArgumentsAdjuster getClangSyntaxOnlyAdjuster() { return [](const CommandLineArguments &Args, StringRef /*unused*/) { CommandLineArguments AdjustedArgs; bool HasSyntaxOnly = false; + const std::vector OutputCommands = { + // FIXME: Add other options that generate output. + "-save-temps", + "--save-temps", + }; for (size_t i = 0, e = Args.size(); i < e; ++i) { StringRef Arg = Args[i]; - // FIXME: Remove options that generate output. + // Skip output commands. + if (llvm::any_of(OutputCommands, [&Arg](llvm::StringRef OutputCommand) { + return Arg.startswith(OutputCommand); + })) + continue; + if (!Arg.startswith("-fcolor-diagnostics") && !Arg.startswith("-fdiagnostics-color")) AdjustedArgs.push_back(Args[i]); diff --git a/clang/unittests/Tooling/ToolingTest.cpp b/clang/unittests/Tooling/ToolingTest.cpp --- a/clang/unittests/Tooling/ToolingTest.cpp +++ b/clang/unittests/Tooling/ToolingTest.cpp @@ -13,15 +13,18 @@ #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/FrontendAction.h" #include "clang/Frontend/FrontendActions.h" +#include "clang/Tooling/ArgumentsAdjusters.h" #include "clang/Tooling/CompilationDatabase.h" #include "clang/Tooling/Tooling.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringRef.h" #include "llvm/Support/Path.h" #include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetSelect.h" #include "gtest/gtest.h" #include #include +#include namespace clang { namespace tooling { @@ -429,6 +432,37 @@ EXPECT_EQ(SyntaxOnlyCount, 1U); } +TEST(ClangToolTest, NoOutputCommands) { + FixedCompilationDatabase Compilations("/", {"-save-temps", "-save-temps=cwd", + "--save-temps", + "--save-temps=somedir"}); + + ClangTool Tool(Compilations, std::vector(1, "/a.cc")); + Tool.mapVirtualFile("/a.cc", "void a() {}"); + + std::unique_ptr Action( + newFrontendActionFactory()); + + const std::vector OutputCommands = {"-save-temps"}; + bool Ran = false; + ArgumentsAdjuster CheckSyntaxOnlyAdjuster = + [&OutputCommands, &Ran](const CommandLineArguments &Args, + StringRef /*unused*/) { + for (llvm::StringRef Arg : Args) { + for (llvm::StringRef OutputCommand : OutputCommands) + EXPECT_FALSE(Arg.contains(OutputCommand)); + } + Ran = true; + return Args; + }; + + Tool.clearArgumentsAdjusters(); + Tool.appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster()); + Tool.appendArgumentsAdjuster(CheckSyntaxOnlyAdjuster); + Tool.run(Action.get()); + EXPECT_TRUE(Ran); +} + TEST(ClangToolTest, BaseVirtualFileSystemUsage) { FixedCompilationDatabase Compilations("/", std::vector()); llvm::IntrusiveRefCntPtr OverlayFileSystem(