Skip to content

Commit 2d93e2a

Browse files
committed
[clang][cas] Fix crash on invalid with IncludeTreeBuilder
If an error occurs in FrontendAction::BeginSourceFile after IncludeTreeActionController::initializeModuleBuild is called (via BeginInvocation), the builder is left in an inconsistent state because there is no call to finalizeModuleBuild (via HandleTranslationUnit), so we should exit early without attempting to create an include tree for the broken module. Fixes a crash on invalid seen with a missing module header, but there could be other similar cases. rdar://151878898 (cherry picked from commit 5d380b9)
1 parent bd45852 commit 2d93e2a

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

clang/lib/Tooling/DependencyScanning/IncludeTreeActionController.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,13 @@ Error IncludeTreeActionController::finalizeModuleBuild(
386386
ModuleScanInstance.getInvocation().getLangOpts(),
387387
ModuleScanInstance.getInvocation().getCodeGenOpts());
388388
auto Builder = BuilderStack.pop_back_val();
389+
390+
// If there was an error, bail out early. The state of `Builder` may be
391+
// inconsistent since there is no guarantee that exitedInclude or
392+
// finalizeModuleBuild have been called for all imports.
393+
if (ModuleScanInstance.getDiagnostics().hasUnrecoverableErrorOccurred())
394+
return Error::success(); // Already reported.
395+
389396
auto Tree = Builder->finishIncludeTree(ModuleScanInstance,
390397
ModuleScanInstance.getInvocation());
391398
if (!Tree)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Tests that a missing header in a module that is itself imported by another
2+
// module does not crash/assert in the IncludeTreeBuilder.
3+
4+
// RUN: rm -rf %t
5+
// RUN: split-file %s %t
6+
7+
// RUN: not clang-scan-deps -format experimental-include-tree-full -cas-path %t/cas -- %clang -fmodules -fmodules-cache-path=%t/cache -c %t/tu0.m -I%t
8+
// RUN: not clang-scan-deps -format experimental-include-tree-full -cas-path %t/cas -- %clang -fmodules -fmodules-cache-path=%t/cache -c %t/tu1.m -I%t
9+
10+
//--- module.modulemap
11+
module MissingH {
12+
header "missing.h"
13+
}
14+
15+
module Importer {
16+
header "importer.h"
17+
}
18+
19+
//--- not-missing.h
20+
21+
//--- importer.h
22+
@import MissingH;
23+
24+
//--- tu0.m
25+
@import MissingH;
26+
27+
//--- tu1.m
28+
@import Importer;

0 commit comments

Comments
 (0)