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 @@ -23,14 +23,18 @@ ArgumentsAdjuster getClangSyntaxOnlyAdjuster() { return [](const CommandLineArguments &Args, StringRef /*unused*/) { CommandLineArguments AdjustedArgs; + bool HasSyntaxOnly = false; for (size_t i = 0, e = Args.size(); i < e; ++i) { StringRef Arg = Args[i]; // FIXME: Remove options that generate output. if (!Arg.startswith("-fcolor-diagnostics") && !Arg.startswith("-fdiagnostics-color")) AdjustedArgs.push_back(Args[i]); + if (Arg == "-fsyntax-only") + HasSyntaxOnly = true; } - AdjustedArgs.push_back("-fsyntax-only"); + if (!HasSyntaxOnly) + AdjustedArgs.push_back("-fsyntax-only"); return AdjustedArgs; }; } 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 @@ -400,6 +400,33 @@ EXPECT_FALSE(Found); } +TEST(ClangToolTest, NoDoubleSyntaxOnly) { + FixedCompilationDatabase Compilations("/", {"-fsyntax-only"}); + + ClangTool Tool(Compilations, std::vector(1, "/a.cc")); + Tool.mapVirtualFile("/a.cc", "void a() {}"); + + std::unique_ptr Action( + newFrontendActionFactory()); + + size_t SyntaxOnlyCount = 0; + ArgumentsAdjuster CheckSyntaxOnlyAdjuster = + [&SyntaxOnlyCount](const CommandLineArguments &Args, + StringRef /*unused*/) { + for (llvm::StringRef Arg : Args) { + if (Arg == "-fsyntax-only") + ++SyntaxOnlyCount; + } + return Args; + }; + + Tool.clearArgumentsAdjusters(); + Tool.appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster()); + Tool.appendArgumentsAdjuster(CheckSyntaxOnlyAdjuster); + Tool.run(Action.get()); + EXPECT_EQ(SyntaxOnlyCount, 1U); +} + TEST(ClangToolTest, BaseVirtualFileSystemUsage) { FixedCompilationDatabase Compilations("/", std::vector()); llvm::IntrusiveRefCntPtr OverlayFileSystem(