Skip to content

Commit 3b05edf

Browse files
authored
[clang][deps] Stop lexing if hit a failure while loading a PCH/module in a submodule. (#146976)
Otherwise we are continuing in an invalid state and can easily crash. It is a follow-up to cde90e6 but an important difference is when a failure happens in a submodule. In this case in `Preprocessor::HandleEndOfFile` `tok::eof` is replaced by `tok::annot_module_end`. And after exiting a file with bad `#include/#import` we work with a new buffer, so `BufferPtr < BufferEnd`. As there are no signs to stop lexing we just keep doing it. The fix is the same as in dc9fdaf in `Lexer::LexTokenInternal` but this time in `Lexer::LexDependencyDirectiveToken` as well. rdar://152499276
1 parent 9923565 commit 3b05edf

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

clang/lib/Lex/Lexer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4587,6 +4587,9 @@ bool Lexer::LexDependencyDirectiveToken(Token &Result) {
45874587

45884588
if (Result.is(tok::hash) && Result.isAtStartOfLine()) {
45894589
PP->HandleDirective(Result);
4590+
if (PP->hadModuleLoaderFatalFailure())
4591+
// With a fatal failure in the module loader, we abort parsing.
4592+
return true;
45904593
return false;
45914594
}
45924595
if (Result.is(tok::raw_identifier)) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// This tests that after a fatal module loader error, we do not continue parsing.
2+
3+
// RUN: rm -rf %t
4+
// RUN: split-file %s %t
5+
// RUN: mkdir %t/ModulesCache
6+
// RUN: touch %t/ModulesCache/Unusable.pcm
7+
8+
// RUN: not clang-scan-deps -format experimental-full -mode preprocess-dependency-directives -- \
9+
// RUN: %clang -fmodules -fimplicit-modules -Xclang -fdisable-module-hash -fmodules-cache-path=%t/ModulesCache \
10+
// RUN: -F %t/Frameworks -c %t/test.m -o %t/test.o
11+
// RUN: ls %t/ModulesCache | not grep AfterUnusable
12+
13+
//--- Frameworks/Unusable.framework/Headers/Unusable.h
14+
// empty
15+
//--- Frameworks/Unusable.framework/Modules/module.modulemap
16+
framework module Unusable { header "Unusable.h" }
17+
18+
//--- Frameworks/AfterUnusable.framework/Headers/AfterUnusable.h
19+
// empty
20+
//--- Frameworks/AfterUnusable.framework/Modules/module.modulemap
21+
framework module AfterUnusable { header "AfterUnusable.h" }
22+
23+
//--- Frameworks/Importer.framework/Headers/Importer.h
24+
#import <Importer/ImportUnusable.h>
25+
// Parsing should have stopped and we should not handle AfterUnusable.
26+
#import <AfterUnusable/AfterUnusable.h>
27+
28+
//--- Frameworks/Importer.framework/Headers/ImportUnusable.h
29+
// It is important that this header is a submodule.
30+
#import <Unusable/Unusable.h>
31+
// Parsing should have stopped and we should not handle AfterUnusable.
32+
#import <AfterUnusable/AfterUnusable.h>
33+
34+
//--- Frameworks/Importer.framework/Modules/module.modulemap
35+
framework module Importer {
36+
umbrella header "Importer.h"
37+
module * { export * }
38+
export *
39+
}
40+
41+
//--- test.m
42+
#import <Importer/Importer.h>

0 commit comments

Comments
 (0)