[clang] [Tooling] Fix FixedCompilationDatabase with header compile flags (PR #73913)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 30 01:23:33 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Sam McCall (sam-mccall)

<details>
<summary>Changes</summary>

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:


---
Full diff: https://github.com/llvm/llvm-project/pull/73913.diff


2 Files Affected:

- (modified) clang/lib/Tooling/CompilationDatabase.cpp (+3-1) 
- (modified) clang/unittests/Tooling/CompilationDatabaseTest.cpp (+19) 


``````````diff
diff --git a/clang/lib/Tooling/CompilationDatabase.cpp b/clang/lib/Tooling/CompilationDatabase.cpp
index fdf6015508d94ba..2b6e6a09f1335c2 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<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());
     }
   }
diff --git a/clang/unittests/Tooling/CompilationDatabaseTest.cpp b/clang/unittests/Tooling/CompilationDatabaseTest.cpp
index 5173d472486bfc2..fffef88a91f7c4e 100644
--- a/clang/unittests/Tooling/CompilationDatabaseTest.cpp
+++ b/clang/unittests/Tooling/CompilationDatabaseTest.cpp
@@ -656,6 +656,25 @@ TEST(ParseFixedCompilationDatabase, HandlesPositionalArgs) {
   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"), "-xc++-header", "-c",
+                          "-DDEF3", "source"));
+  EXPECT_EQ(2, Argc);
+}
+
 TEST(ParseFixedCompilationDatabase, HandlesPositionalArgsSyntaxOnly) {
   // Adjust the given command line arguments to ensure that any positional
   // arguments in them are stripped.

``````````

</details>


https://github.com/llvm/llvm-project/pull/73913


More information about the cfe-commits mailing list