Skip to content

Commit 3a8d416

Browse files
committed
Avoid registering SDKSettings.json in SourceManager
1 parent 05eb6b1 commit 3a8d416

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed

clang/lib/Serialization/ASTWriter.cpp

+45-25
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,27 @@ struct InputFileEntry {
18241824
uint32_t ContentHash[2];
18251825

18261826
InputFileEntry(FileEntryRef File) : File(File) {}
1827+
1828+
void trySetContentHash(Preprocessor &PP,
1829+
std::optional<llvm::MemoryBufferRef> MemBuff) {
1830+
if (!PP.getHeaderSearchInfo()
1831+
.getHeaderSearchOpts()
1832+
.ValidateASTInputFilesContent) {
1833+
ContentHash[0] = 0;
1834+
ContentHash[1] = 0;
1835+
return;
1836+
}
1837+
1838+
if (!MemBuff) {
1839+
PP.Diag(SourceLocation(), diag::err_module_unable_to_hash_content)
1840+
<< File.getName();
1841+
return;
1842+
}
1843+
1844+
uint64_t Hash = xxh3_64bits(MemBuff->getBuffer());
1845+
ContentHash[0] = uint32_t(Hash);
1846+
ContentHash[1] = uint32_t(Hash >> 32);
1847+
}
18271848
};
18281849

18291850
} // namespace
@@ -1898,25 +1919,36 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr) {
18981919
!IsSLocFileEntryAffecting[IncludeFileID.ID];
18991920
Entry.IsModuleMap = isModuleMap(File.getFileCharacteristic());
19001921

1901-
uint64_t ContentHash = 0;
1902-
if (PP->getHeaderSearchInfo()
1903-
.getHeaderSearchOpts()
1904-
.ValidateASTInputFilesContent) {
1905-
auto MemBuff = Cache->getBufferIfLoaded();
1906-
if (MemBuff)
1907-
ContentHash = xxh3_64bits(MemBuff->getBuffer());
1908-
else
1909-
PP->Diag(SourceLocation(), diag::err_module_unable_to_hash_content)
1910-
<< Entry.File.getName();
1911-
}
1912-
Entry.ContentHash[0] = uint32_t(ContentHash);
1913-
Entry.ContentHash[1] = uint32_t(ContentHash >> 32);
1922+
Entry.trySetContentHash(*PP, Cache->getBufferIfLoaded());
1923+
19141924
if (Entry.IsSystemFile)
19151925
SystemFiles.push_back(Entry);
19161926
else
19171927
UserFiles.push_back(Entry);
19181928
}
19191929

1930+
// FIXME: This is here because the include-tree file list refers to this file
1931+
// and we need to validate it's up-to-date when reading the AST file. Make
1932+
// this more generic and include the remaining files.
1933+
StringRef Sysroot = PP->getHeaderSearchInfo().getHeaderSearchOpts().Sysroot;
1934+
if (!Sysroot.empty()) {
1935+
SmallString<128> SDKSettingsJSON = Sysroot;
1936+
llvm::sys::path::append(SDKSettingsJSON, "SDKSettings.json");
1937+
if (auto FE = PP->getFileManager().getOptionalFileRef(SDKSettingsJSON)) {
1938+
InputFileEntry Entry(*FE);
1939+
Entry.IsSystemFile = true;
1940+
Entry.IsTransient = false;
1941+
Entry.BufferOverridden = false;
1942+
Entry.IsTopLevel = true;
1943+
Entry.IsModuleMap = false;
1944+
if (auto BuffOrErr = PP->getFileManager().getBufferForFile(Entry.File))
1945+
Entry.trySetContentHash(*PP, (*BuffOrErr)->getMemBufferRef());
1946+
else
1947+
Entry.trySetContentHash(*PP, {});
1948+
SystemFiles.push_back(Entry);
1949+
}
1950+
}
1951+
19201952
// User files go at the front, system files at the back.
19211953
auto SortedFiles = llvm::concat<InputFileEntry>(std::move(UserFiles),
19221954
std::move(SystemFiles));
@@ -5950,18 +5982,6 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema *SemaPtr, StringRef isysroot,
59505982
if (Chain)
59515983
Chain->finalizeForWriting();
59525984

5953-
// FIXME: This is here because the include-tree file list refers to this file
5954-
// and we need to validate it's up-to-date when reading the AST file. Make
5955-
// this more generic and include the remaining files.
5956-
StringRef Sysroot = PP->getHeaderSearchInfo().getHeaderSearchOpts().Sysroot;
5957-
if (!Sysroot.empty()) {
5958-
SmallString<128> SDKSettingsJSON = Sysroot;
5959-
llvm::sys::path::append(SDKSettingsJSON, "SDKSettings.json");
5960-
if (auto FE = PP->getFileManager().getOptionalFileRef(SDKSettingsJSON))
5961-
PP->getSourceManager().createFileID(*FE, SourceLocation(),
5962-
SrcMgr::C_System);
5963-
}
5964-
59655985
// This needs to be done very early, since everything that writes
59665986
// SourceLocations or FileIDs depends on it.
59675987
computeNonAffectingInputFiles();

0 commit comments

Comments
 (0)