From 1ab418beb3cc9c31ebb2d5779069426d761ceb8f Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Fri, 19 Jan 2024 14:02:04 +0100 Subject: [PATCH] [Tooling] Fix FixedCompilationDatabase with header compile flags (#73913) Summary: The logic to strip positional args feels very fragile, but it's terribly useful when you want to use a tool on a file and have the exact argv. Today doesn't work with header-parsing actions because these are "precompile" rather than "compile", from tooling's perspective it's all the same. Reviewers: kadircet Subscribers: --- clang/lib/Tooling/CompilationDatabase.cpp | 4 +++- .../Tooling/CompilationDatabaseTest.cpp | 20 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/clang/lib/Tooling/CompilationDatabase.cpp b/clang/lib/Tooling/CompilationDatabase.cpp index 87ad8f25a1ab..9d8f0d03a7d5 100644 --- a/clang/lib/Tooling/CompilationDatabase.cpp +++ b/clang/lib/Tooling/CompilationDatabase.cpp @@ -156,6 +156,7 @@ struct CompileJobAnalyzer { bool CollectChildren = Collect; switch (A->getKind()) { case driver::Action::CompileJobClass: + case driver::Action::PrecompileJobClass: CollectChildren = true; break; @@ -293,7 +294,8 @@ static bool stripPositionalArgs(std::vector Args, // -flto* flags make the BackendJobClass, which still needs analyzer. if (Cmd.getSource().getKind() == driver::Action::AssembleJobClass || Cmd.getSource().getKind() == driver::Action::BackendJobClass || - Cmd.getSource().getKind() == driver::Action::CompileJobClass) { + Cmd.getSource().getKind() == driver::Action::CompileJobClass || + Cmd.getSource().getKind() == driver::Action::PrecompileJobClass) { CompileAnalyzer.run(&Cmd.getSource()); } } diff --git a/clang/unittests/Tooling/CompilationDatabaseTest.cpp b/clang/unittests/Tooling/CompilationDatabaseTest.cpp index 5173d472486b..45062cf7c16f 100644 --- a/clang/unittests/Tooling/CompilationDatabaseTest.cpp +++ b/clang/unittests/Tooling/CompilationDatabaseTest.cpp @@ -647,12 +647,30 @@ TEST(ParseFixedCompilationDatabase, HandlesPositionalArgs) { FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg); ASSERT_TRUE((bool)Database); ASSERT_TRUE(ErrorMsg.empty()); + std::vector Result = Database->getCompileCommands("source"); + ASSERT_EQ(1ul, Result.size()); + ASSERT_EQ(".", Result[0].Directory); + ASSERT_THAT(Result[0].CommandLine, + ElementsAre(EndsWith("clang-tool"), "-c", "-DDEF3", "source")); + EXPECT_EQ(2, Argc); +} + +TEST(ParseFixedCompilationDatabase, HandlesPositionalArgsHeader) { + const char *Argv[] = {"1", "2", "--", "-xc++-header", + "-c", "somefile.h", "-DDEF3"}; + int Argc = std::size(Argv); + std::string ErrorMsg; + std::unique_ptr Database = + FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg); + ASSERT_TRUE((bool)Database); + ASSERT_TRUE(ErrorMsg.empty()); std::vector Result = Database->getCompileCommands("source"); ASSERT_EQ(1ul, Result.size()); ASSERT_EQ(".", Result[0].Directory); ASSERT_THAT(Result[0].CommandLine, - ElementsAre(EndsWith("clang-tool"), "-c", "-DDEF3", "source")); + ElementsAre(EndsWith("clang-tool"), "-xc++-header", "-c", + "-DDEF3", "source")); EXPECT_EQ(2, Argc); }