[clang] [ClangTool] Use CC1Option flag resource-dir in injectResourceDir (PR #140870)
Wenju He via cfe-commits
cfe-commits at lists.llvm.org
Mon May 26 16:21:19 PDT 2025
https://github.com/wenju-he updated https://github.com/llvm/llvm-project/pull/140870
>From f5e675f17ea737b0668e626f34d013153368425e Mon Sep 17 00:00:00 2001
From: Wenju He <wenju.he at intel.com>
Date: Wed, 21 May 2025 02:08:34 -0700
Subject: [PATCH 1/3] [ClangTool] Use CC1Option flag resource-dir in
injectResourceDir
This PR fixes ClangTool error in -cc1 mode:
error: unknown argument: '-resource-dir=
---
clang/lib/Tooling/Tooling.cpp | 8 +++++---
clang/unittests/Tooling/ToolingTest.cpp | 19 +++++++++++++++++++
2 files changed, 24 insertions(+), 3 deletions(-)
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"});
>From 9d8ce5cf034a9ff7bf427fb76126c1f7b1af300c Mon Sep 17 00:00:00 2001
From: Wenju He <wenju.he at intel.com>
Date: Wed, 21 May 2025 02:12:23 -0700
Subject: [PATCH 2/3] update comment
---
clang/unittests/Tooling/ToolingTest.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/unittests/Tooling/ToolingTest.cpp b/clang/unittests/Tooling/ToolingTest.cpp
index 07104ccf9835f..9969656b00956 100644
--- a/clang/unittests/Tooling/ToolingTest.cpp
+++ b/clang/unittests/Tooling/ToolingTest.cpp
@@ -771,7 +771,7 @@ TEST(ClangToolTest, BaseVirtualFileSystemUsage) {
EXPECT_EQ(0, Tool.run(Action.get()));
}
-// Check -cc1 command doesn't crash.
+// Check -cc1 command doesn't fail.
TEST(ClangToolTest, CC1Arg) {
FixedCompilationDatabase Compilations("/", {"-cc1"});
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
>From 73377dd0d87c6b03397d461377f88068378c23bd Mon Sep 17 00:00:00 2001
From: Wenju He <wenju.he at intel.com>
Date: Mon, 26 May 2025 16:11:35 -0700
Subject: [PATCH 3/3] add CC1Option to resource_dir_EQ
---
clang/include/clang/Driver/Options.td | 2 +-
clang/lib/Tooling/Tooling.cpp | 8 +++-----
2 files changed, 4 insertions(+), 6 deletions(-)
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 9a4253113488d..e4ac2ddc93073 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5897,7 +5897,7 @@ def resource_dir : Separate<["-"], "resource-dir">,
HelpText<"The directory which holds the compiler resource files">,
MarshallingInfoString<HeaderSearchOpts<"ResourceDir">>;
def resource_dir_EQ : Joined<["-"], "resource-dir=">, Flags<[NoXarchOption]>,
- Visibility<[ClangOption, CLOption, DXCOption, FlangOption]>,
+ Visibility<[ClangOption, CC1Option, CLOption, DXCOption, FlangOption]>,
Alias<resource_dir>;
def rpath : Separate<["-"], "rpath">, Flags<[LinkerInput]>, Group<Link_Group>,
Visibility<[ClangOption, FlangOption]>;
diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp
index 87a984672662b..3c72f52040142 100644
--- a/clang/lib/Tooling/Tooling.cpp
+++ b/clang/lib/Tooling/Tooling.cpp
@@ -510,11 +510,9 @@ static void injectResourceDir(CommandLineArguments &Args, const char *Argv0,
return;
// If there's no override in place add our resource dir.
- CommandLineArguments Extra = {
- "-resource-dir", CompilerInvocation::GetResourcesPath(Argv0, MainAddr)};
-
- Args =
- getInsertArgumentAdjuster(Extra, ArgumentInsertPosition::END)(Args, "");
+ Args = getInsertArgumentAdjuster(
+ ("-resource-dir=" + CompilerInvocation::GetResourcesPath(Argv0, MainAddr))
+ .c_str())(Args, "");
}
int ClangTool::run(ToolAction *Action) {
More information about the cfe-commits
mailing list