Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
[Tooling] Fix FixedCompilationDatabase with header compile flags (#73…
Browse files Browse the repository at this point in the history
…913)

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:
  • Loading branch information
sam-mccall authored Jan 19, 2024
1 parent 689da34 commit 1ab418b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
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

0 comments on commit 1ab418b

Please sign in to comment.