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" +}]