[clang] [ClangTool] Use CC1Option flag resource-dir in injectResourceDir (PR #140870)

via cfe-commits cfe-commits at lists.llvm.org
Wed May 21 02:12:35 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Wenju He (wenju-he)

<details>
<summary>Changes</summary>

This PR fixes ClangTool error in -cc1 mode:
error: unknown argument: '-resource-dir=

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


2 Files Affected:

- (modified) clang/lib/Tooling/Tooling.cpp (+5-3) 
- (modified) clang/unittests/Tooling/ToolingTest.cpp (+19) 


``````````diff
diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp
index 3c72f52040142..87a984672662b 100644
--- a/clang/lib/Tooling/Tooling.cpp
+++ b/clang/lib/Tooling/Tooling.cpp
@@ -510,9 +510,11 @@ static void injectResourceDir(CommandLineArguments &Args, const char *Argv0,
       return;
 
   // If there's no override in place add our resource dir.
-  Args = getInsertArgumentAdjuster(
-      ("-resource-dir=" + CompilerInvocation::GetResourcesPath(Argv0, MainAddr))
-          .c_str())(Args, "");
+  CommandLineArguments Extra = {
+      "-resource-dir", CompilerInvocation::GetResourcesPath(Argv0, MainAddr)};
+
+  Args =
+      getInsertArgumentAdjuster(Extra, ArgumentInsertPosition::END)(Args, "");
 }
 
 int ClangTool::run(ToolAction *Action) {
diff --git a/clang/unittests/Tooling/ToolingTest.cpp b/clang/unittests/Tooling/ToolingTest.cpp
index 8cdfffb54390e..07104ccf9835f 100644
--- a/clang/unittests/Tooling/ToolingTest.cpp
+++ b/clang/unittests/Tooling/ToolingTest.cpp
@@ -771,6 +771,25 @@ TEST(ClangToolTest, BaseVirtualFileSystemUsage) {
   EXPECT_EQ(0, Tool.run(Action.get()));
 }
 
+// Check -cc1 command doesn't crash.
+TEST(ClangToolTest, CC1Arg) {
+  FixedCompilationDatabase Compilations("/", {"-cc1"});
+  llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
+      new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
+  llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
+      new llvm::vfs::InMemoryFileSystem);
+  OverlayFileSystem->pushOverlay(InMemoryFileSystem);
+
+  InMemoryFileSystem->addFile(
+      "a.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int main() {}"));
+
+  ClangTool Tool(Compilations, std::vector<std::string>(1, "a.cpp"),
+                 std::make_shared<PCHContainerOperations>(), OverlayFileSystem);
+  std::unique_ptr<FrontendActionFactory> Action(
+      newFrontendActionFactory<SyntaxOnlyAction>());
+  EXPECT_EQ(0, Tool.run(Action.get()));
+}
+
 // Check getClangStripDependencyFileAdjuster doesn't strip args after -MD/-MMD.
 TEST(ClangToolTest, StripDependencyFileAdjuster) {
   FixedCompilationDatabase Compilations("/", {"-MD", "-c", "-MMD", "-w"});

``````````

</details>


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


More information about the cfe-commits mailing list