[clang] [clang][DependencyScanning] Return failure upon CompilerInstanceWithContext failure to create target (PR #211607)
Ian Li via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 29 14:47:00 PDT 2026
https://github.com/ianayl updated https://github.com/llvm/llvm-project/pull/211607
>From a2fad177580e66e74714edc8d4f033cff77b103d Mon Sep 17 00:00:00 2001
From: "Li, Ian" <ian.li at intel.com>
Date: Wed, 22 Jul 2026 12:36:43 -0700
Subject: [PATCH 1/2] Return error if CompilerInstance target creation failed
---
clang/lib/Tooling/DependencyScanningTool.cpp | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/clang/lib/Tooling/DependencyScanningTool.cpp b/clang/lib/Tooling/DependencyScanningTool.cpp
index b435e42af28b4..4072244f33fc3 100644
--- a/clang/lib/Tooling/DependencyScanningTool.cpp
+++ b/clang/lib/Tooling/DependencyScanningTool.cpp
@@ -500,9 +500,7 @@ bool CompilerInstanceWithContext::initialize(
// once here, and the information is reused for all computeDependencies calls.
// We do not need to call createTarget explicitly if we go through
// CompilerInstance::ExecuteAction to perform scanning.
- CI.createTarget();
-
- return true;
+ return CI.createTarget();
}
bool CompilerInstanceWithContext::computeDependencies(
>From dae21eecca9a02cf019806fabac08d024d43cfe8 Mon Sep 17 00:00:00 2001
From: "Li, Ian" <ian.li at intel.com>
Date: Wed, 29 Jul 2026 14:44:07 -0700
Subject: [PATCH 2/2] Test that CI.createTarget failures are properly caught
---
.../ClangScanDeps/modules-invalid-target.c | 29 +++++++++++++++++++
1 file changed, 29 insertions(+)
create mode 100644 clang/test/ClangScanDeps/modules-invalid-target.c
diff --git a/clang/test/ClangScanDeps/modules-invalid-target.c b/clang/test/ClangScanDeps/modules-invalid-target.c
new file mode 100644
index 0000000000000..333bfdfe48c9e
--- /dev/null
+++ b/clang/test/ClangScanDeps/modules-invalid-target.c
@@ -0,0 +1,29 @@
+// Test that failure to create a target when initializing
+// CompilerInstanceWithContext (i.e. the user provides a bad target triple)
+// is properly diagnosed, instead of continuing to run with Target = nullptr.
+
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+// RUN: sed "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
+
+// RUN: not clang-scan-deps -compilation-database %t/cdb.json -format \
+// RUN: experimental-full -module-names=M 2>&1 | FileCheck %s
+
+// Check that CompilerInstanceWithContext::initializeOrError properly errors
+// during target creation, instead of an assert or a segfault later down the
+// line:
+// CHECK: Error while scanning dependencies for M:
+// CHECK-NEXT: error: unknown target triple 'unknown-unknown-unknown'
+
+//--- module.modulemap
+module M { header "M.h" }
+
+//--- M.h
+void m(void);
+
+//--- cdb.json.template
+[{
+ "file": "",
+ "directory": "DIR",
+ "command": "clang -fmodules -fmodules-cache-path=DIR/cache -I DIR -x c --target=unknown-unknown-unknown"
+}]
More information about the cfe-commits
mailing list