Skip to content

Commit 12f3bbc

Browse files
committed
Avoid opening file with content validation disabled
1 parent ca3f33c commit 12f3bbc

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,8 +1775,9 @@ struct InputFileEntry {
17751775

17761776
InputFileEntry(FileEntryRef File) : File(File) {}
17771777

1778-
void trySetContentHash(Preprocessor &PP,
1779-
std::optional<llvm::MemoryBufferRef> MemBuff) {
1778+
void trySetContentHash(
1779+
Preprocessor &PP,
1780+
llvm::function_ref<std::optional<llvm::MemoryBufferRef>()> GetMemBuff) {
17801781
ContentHash[0] = 0;
17811782
ContentHash[1] = 0;
17821783

@@ -1785,6 +1786,7 @@ struct InputFileEntry {
17851786
.ValidateASTInputFilesContent)
17861787
return;
17871788

1789+
auto MemBuff = GetMemBuff();
17881790
if (!MemBuff) {
17891791
PP.Diag(SourceLocation(), diag::err_module_unable_to_hash_content)
17901792
<< File.getName();
@@ -1869,7 +1871,7 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr) {
18691871
!IsSLocFileEntryAffecting[IncludeFileID.ID];
18701872
Entry.IsModuleMap = isModuleMap(File.getFileCharacteristic());
18711873

1872-
Entry.trySetContentHash(*PP, Cache->getBufferIfLoaded());
1874+
Entry.trySetContentHash(*PP, [&] { return Cache->getBufferIfLoaded(); });
18731875

18741876
if (Entry.IsSystemFile)
18751877
SystemFiles.push_back(Entry);
@@ -1892,10 +1894,14 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr) {
18921894
Entry.BufferOverridden = false;
18931895
Entry.IsTopLevel = true;
18941896
Entry.IsModuleMap = false;
1895-
auto Convert = [](const ErrorOr<std::unique_ptr<MemoryBuffer>> &MB) {
1896-
return MB ? std::optional((*MB)->getMemBufferRef()) : std::nullopt;
1897-
};
1898-
Entry.trySetContentHash(*PP, Convert(FM.getBufferForFile(Entry.File)));
1897+
std::unique_ptr<MemoryBuffer> MB;
1898+
Entry.trySetContentHash(*PP, [&] -> std::optional<MemoryBufferRef> {
1899+
if (auto MBOrErr = FM.getBufferForFile(Entry.File)) {
1900+
MB = std::move(*MBOrErr);
1901+
return MB->getMemBufferRef();
1902+
}
1903+
return std::nullopt;
1904+
});
18991905
SystemFiles.push_back(Entry);
19001906
}
19011907
}

0 commit comments

Comments
 (0)