Skip to content

[Tooling] Fix FixedCompilationDatabase with header compile flags #73913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion clang/lib/Tooling/CompilationDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ struct CompileJobAnalyzer {
bool CollectChildren = Collect;
switch (A->getKind()) {
case driver::Action::CompileJobClass:
case driver::Action::PrecompileJobClass:
CollectChildren = true;
break;

Expand Down Expand Up @@ -293,7 +294,8 @@ static bool stripPositionalArgs(std::vector<const char *> 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());
}
}
Expand Down
20 changes: 19 additions & 1 deletion clang/unittests/Tooling/CompilationDatabaseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,12 +647,30 @@ TEST(ParseFixedCompilationDatabase, HandlesPositionalArgs) {
FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg);
ASSERT_TRUE((bool)Database);
ASSERT_TRUE(ErrorMsg.empty());
std::vector<CompileCommand> 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<FixedCompilationDatabase> Database =
FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg);
ASSERT_TRUE((bool)Database);
ASSERT_TRUE(ErrorMsg.empty());
std::vector<CompileCommand> 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);
}

Expand Down