Skip to content

Commit e12f6c2

Browse files
committed
[modules] Fix error "malformed or corrupted AST file: 'SourceLocation remap refers to unknown module...'".
When a framework can be found at a new location, all references to it in the module cache become outdated. When we try to load such outdated .pcm file, we shouldn't change any already loaded and processed modules. If `Module` has `ASTFile`, it means we've read its AST block already and it is too late to undo that. If `ASTFile` is `None`, there is no value in setting it to `None` again. So we don't reset `ASTFile` in `ModuleManager::removeModules` at all. rdar://97216258 Differential Revision: https://reviews.llvm.org/D134249
1 parent 7a23920 commit e12f6c2

File tree

5 files changed

+50
-19
lines changed

5 files changed

+50
-19
lines changed

clang/include/clang/Basic/Module.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,7 @@ class Module {
608608

609609
/// Set the serialized AST file for the top-level module of this module.
610610
void setASTFile(Optional<FileEntryRef> File) {
611-
assert((!File || !getASTFile() || getASTFile() == File) &&
612-
"file path changed");
611+
assert((!getASTFile() || getASTFile() == File) && "file path changed");
613612
getTopLevelModule()->ASTFile = File;
614613
}
615614

clang/include/clang/Serialization/ModuleManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class ModuleManager {
250250
std::string &ErrorStr);
251251

252252
/// Remove the modules starting from First (to the end).
253-
void removeModules(ModuleIterator First, ModuleMap *modMap);
253+
void removeModules(ModuleIterator First);
254254

255255
/// Add an in-memory buffer the list of known buffers
256256
void addInMemoryBuffer(StringRef FileName,

clang/lib/Serialization/ASTReader.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4227,10 +4227,7 @@ ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
42274227
ReadASTCore(FileName, Type, ImportLoc,
42284228
/*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(),
42294229
ClientLoadCapabilities)) {
4230-
ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4231-
PP.getLangOpts().Modules
4232-
? &PP.getHeaderSearchInfo().getModuleMap()
4233-
: nullptr);
4230+
ModuleMgr.removeModules(ModuleMgr.begin() + NumModules);
42344231

42354232
// If we find that any modules are unusable, the global index is going
42364233
// to be out-of-date. Just remove it.

clang/lib/Serialization/ModuleManager.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ ModuleManager::addModule(StringRef FileName, ModuleKind Type,
249249
return NewlyLoaded;
250250
}
251251

252-
void ModuleManager::removeModules(ModuleIterator First, ModuleMap *modMap) {
252+
void ModuleManager::removeModules(ModuleIterator First) {
253253
auto Last = end();
254254
if (First == Last)
255255
return;
@@ -280,19 +280,10 @@ void ModuleManager::removeModules(ModuleIterator First, ModuleMap *modMap) {
280280
}
281281
}
282282

283-
// Delete the modules and erase them from the various structures.
284-
for (ModuleIterator victim = First; victim != Last; ++victim) {
283+
// Delete the modules.
284+
for (ModuleIterator victim = First; victim != Last; ++victim)
285285
Modules.erase(victim->File);
286286

287-
if (modMap) {
288-
StringRef ModuleName = victim->ModuleName;
289-
if (Module *mod = modMap->findModule(ModuleName)) {
290-
mod->setASTFile(None);
291-
}
292-
}
293-
}
294-
295-
// Delete the modules.
296287
Chain.erase(Chain.begin() + (First - begin()), Chain.end());
297288
}
298289

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
//
4+
// At first build Stable.pcm that references Movable.framework from StableFrameworks.
5+
// RUN: %clang_cc1 -fsyntax-only -F %t/JustBuilt -F %t/StableFrameworks %t/prepopulate-module-cache.m \
6+
// RUN: -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/modules.cache
7+
//
8+
// Now add Movable.framework to JustBuilt.
9+
// RUN: mkdir %t/JustBuilt
10+
// RUN: cp -r %t/StableFrameworks/Movable.framework %t/JustBuilt/Movable.framework
11+
//
12+
// Load Movable.pcm at first for JustBuilt location and then in the same TU try to load transitively for StableFrameworks location.
13+
// RUN: %clang_cc1 -fsyntax-only -F %t/JustBuilt -F %t/StableFrameworks %t/trigger-error.m \
14+
// RUN: -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/modules.cache
15+
16+
// Test the case when a dependent module is found in a different location, so
17+
// module cache has outdated information. <rdar://97216258>
18+
19+
//--- StableFrameworks/Movable.framework/Headers/Movable.h
20+
// empty
21+
22+
//--- StableFrameworks/Movable.framework/Modules/module.modulemap
23+
framework module Movable {
24+
header "Movable.h"
25+
export *
26+
}
27+
28+
29+
//--- StableFrameworks/Stable.framework/Headers/Stable.h
30+
#import <Movable/Movable.h>
31+
32+
//--- StableFrameworks/Stable.framework/Modules/module.modulemap
33+
framework module Stable {
34+
header "Stable.h"
35+
export *
36+
}
37+
38+
39+
//--- prepopulate-module-cache.m
40+
#import <Stable/Stable.h>
41+
42+
//--- trigger-error.m
43+
#import <Movable/Movable.h>
44+
#import <Stable/Stable.h>

0 commit comments

Comments
 (0)