Skip to content

Commit 1ab418b

Browse files
authored
[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:
1 parent 689da34 commit 1ab418b

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

clang/lib/Tooling/CompilationDatabase.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ struct CompileJobAnalyzer {
156156
bool CollectChildren = Collect;
157157
switch (A->getKind()) {
158158
case driver::Action::CompileJobClass:
159+
case driver::Action::PrecompileJobClass:
159160
CollectChildren = true;
160161
break;
161162

@@ -293,7 +294,8 @@ static bool stripPositionalArgs(std::vector<const char *> Args,
293294
// -flto* flags make the BackendJobClass, which still needs analyzer.
294295
if (Cmd.getSource().getKind() == driver::Action::AssembleJobClass ||
295296
Cmd.getSource().getKind() == driver::Action::BackendJobClass ||
296-
Cmd.getSource().getKind() == driver::Action::CompileJobClass) {
297+
Cmd.getSource().getKind() == driver::Action::CompileJobClass ||
298+
Cmd.getSource().getKind() == driver::Action::PrecompileJobClass) {
297299
CompileAnalyzer.run(&Cmd.getSource());
298300
}
299301
}

clang/unittests/Tooling/CompilationDatabaseTest.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,12 +647,30 @@ TEST(ParseFixedCompilationDatabase, HandlesPositionalArgs) {
647647
FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg);
648648
ASSERT_TRUE((bool)Database);
649649
ASSERT_TRUE(ErrorMsg.empty());
650+
std::vector<CompileCommand> Result = Database->getCompileCommands("source");
651+
ASSERT_EQ(1ul, Result.size());
652+
ASSERT_EQ(".", Result[0].Directory);
653+
ASSERT_THAT(Result[0].CommandLine,
654+
ElementsAre(EndsWith("clang-tool"), "-c", "-DDEF3", "source"));
655+
EXPECT_EQ(2, Argc);
656+
}
657+
658+
TEST(ParseFixedCompilationDatabase, HandlesPositionalArgsHeader) {
659+
const char *Argv[] = {"1", "2", "--", "-xc++-header",
660+
"-c", "somefile.h", "-DDEF3"};
661+
int Argc = std::size(Argv);
662+
std::string ErrorMsg;
663+
std::unique_ptr<FixedCompilationDatabase> Database =
664+
FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg);
665+
ASSERT_TRUE((bool)Database);
666+
ASSERT_TRUE(ErrorMsg.empty());
650667
std::vector<CompileCommand> Result =
651668
Database->getCompileCommands("source");
652669
ASSERT_EQ(1ul, Result.size());
653670
ASSERT_EQ(".", Result[0].Directory);
654671
ASSERT_THAT(Result[0].CommandLine,
655-
ElementsAre(EndsWith("clang-tool"), "-c", "-DDEF3", "source"));
672+
ElementsAre(EndsWith("clang-tool"), "-xc++-header", "-c",
673+
"-DDEF3", "source"));
656674
EXPECT_EQ(2, Argc);
657675
}
658676

0 commit comments

Comments
 (0)