Author: Ian Li Date: 2026-07-30T08:56:25-07:00 New Revision: 0eddedc4e718f05617c8649be78c72eda60d14a2
URL: https://github.com/llvm/llvm-project/commit/0eddedc4e718f05617c8649be78c72eda60d14a2 DIFF: https://github.com/llvm/llvm-project/commit/0eddedc4e718f05617c8649be78c72eda60d14a2.diff LOG: [clang][DependencyScanning] Return failure upon CompilerInstanceWithContext failure to create target (#211607) This was first pointed out by a static analysis scan: Success/failure results from `CompilerInstance::createTarget` in `CompilerInstanceWithContext::initialize` is currently being discarded. AFAICT, `initialize` should have failed if `CompilerInstance::createTarget` failed, as I noticed that calls in `CompilerInstanceWithContext::computeDependencies` (which is ran right after `initialize`) contains function calls (i.e. `CI.loadModule`) that make references to `CompilerInstance::getTarget`: `getTarget` then tries to obtain a reference to the very `TargetInfo` instance that the earlier `CompilerInstance::createTarget` call failed to create. AFAIK `createTarget` doesn't actually seem to fail currently, but I haven't been able to find a good reason as for why success/failure from `CompilerInstance::createTarget` should be ignored even if it failed. But please let me know if I am not seeing something here! Added: clang/test/ClangScanDeps/modules-invalid-target.c Modified: clang/lib/Tooling/DependencyScanningTool.cpp Removed: ################################################################################ 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( 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" +}] _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
