@@ -1771,6 +1771,29 @@ struct InputFileEntry {
17711771 uint32_t ContentHash[2 ];
17721772
17731773 InputFileEntry (FileEntryRef File) : File(File) {}
1774+
1775+ void trySetContentHash (
1776+ Preprocessor &PP,
1777+ llvm::function_ref<std::optional<llvm::MemoryBufferRef>()> GetMemBuff) {
1778+ ContentHash[0 ] = 0 ;
1779+ ContentHash[1 ] = 0 ;
1780+
1781+ if (!PP.getHeaderSearchInfo ()
1782+ .getHeaderSearchOpts ()
1783+ .ValidateASTInputFilesContent )
1784+ return ;
1785+
1786+ auto MemBuff = GetMemBuff ();
1787+ if (!MemBuff) {
1788+ PP.Diag (SourceLocation (), diag::err_module_unable_to_hash_content)
1789+ << File.getName ();
1790+ return ;
1791+ }
1792+
1793+ uint64_t Hash = xxh3_64bits (MemBuff->getBuffer ());
1794+ ContentHash[0 ] = uint32_t (Hash);
1795+ ContentHash[1 ] = uint32_t (Hash >> 32 );
1796+ }
17741797};
17751798
17761799} // namespace
@@ -1843,25 +1866,41 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
18431866 Entry.IsTopLevel = getAffectingIncludeLoc (SourceMgr, File).isInvalid ();
18441867 Entry.IsModuleMap = isModuleMap (File.getFileCharacteristic ());
18451868
1846- uint64_t ContentHash = 0 ;
1847- if (PP->getHeaderSearchInfo ()
1848- .getHeaderSearchOpts ()
1849- .ValidateASTInputFilesContent ) {
1850- auto MemBuff = Cache->getBufferIfLoaded ();
1851- if (MemBuff)
1852- ContentHash = xxh3_64bits (MemBuff->getBuffer ());
1853- else
1854- PP->Diag (SourceLocation (), diag::err_module_unable_to_hash_content)
1855- << Entry.File .getName ();
1856- }
1857- Entry.ContentHash [0 ] = uint32_t (ContentHash);
1858- Entry.ContentHash [1 ] = uint32_t (ContentHash >> 32 );
1869+ Entry.trySetContentHash (*PP, [&] { return Cache->getBufferIfLoaded (); });
1870+
18591871 if (Entry.IsSystemFile )
18601872 SystemFiles.push_back (Entry);
18611873 else
18621874 UserFiles.push_back (Entry);
18631875 }
18641876
1877+ // FIXME: Make providing input files not in the SourceManager more flexible.
1878+ // The SDKSettings.json file is necessary for correct evaluation of
1879+ // availability annotations.
1880+ StringRef Sysroot = PP->getHeaderSearchInfo ().getHeaderSearchOpts ().Sysroot ;
1881+ if (!Sysroot.empty ()) {
1882+ SmallString<128 > SDKSettingsJSON = Sysroot;
1883+ llvm::sys::path::append (SDKSettingsJSON, " SDKSettings.json" );
1884+ FileManager &FM = PP->getFileManager ();
1885+ if (auto FE = FM.getOptionalFileRef (SDKSettingsJSON)) {
1886+ InputFileEntry Entry (*FE);
1887+ Entry.IsSystemFile = true ;
1888+ Entry.IsTransient = false ;
1889+ Entry.BufferOverridden = false ;
1890+ Entry.IsTopLevel = true ;
1891+ Entry.IsModuleMap = false ;
1892+ std::unique_ptr<MemoryBuffer> MB;
1893+ Entry.trySetContentHash (*PP, [&]() -> std::optional<MemoryBufferRef> {
1894+ if (auto MBOrErr = FM.getBufferForFile (Entry.File )) {
1895+ MB = std::move (*MBOrErr);
1896+ return MB->getMemBufferRef ();
1897+ }
1898+ return std::nullopt ;
1899+ });
1900+ SystemFiles.push_back (Entry);
1901+ }
1902+ }
1903+
18651904 // User files go at the front, system files at the back.
18661905 auto SortedFiles = llvm::concat<InputFileEntry>(std::move (UserFiles),
18671906 std::move (SystemFiles));
0 commit comments