2222#include " swift/AST/PluginLoader.h"
2323#include " swift/AST/SourceFile.h"
2424#include " swift/Frontend/Frontend.h"
25+ #include " clang/CAS/IncludeTree.h"
2526#include " llvm/CAS/CASProvidingFileSystem.h"
2627#include " llvm/CAS/CachingOnDiskFileSystem.h"
2728#include " llvm/Config/config.h"
@@ -597,14 +598,32 @@ swift::dependencies::registerBackDeployLibraries(
597598 #include " swift/Frontend/BackDeploymentLibs.def"
598599}
599600
600- void SwiftDependencyTracker::addCommonSearchPathDeps (
601- const CompilerInvocation &CI) {
601+ SwiftDependencyTracker::SwiftDependencyTracker (
602+ llvm::cas::CachingOnDiskFileSystem &FS, llvm::PrefixMapper *Mapper,
603+ const CompilerInvocation &CI)
604+ : FS(FS.createProxyFS()), Mapper(Mapper) {
602605 auto &SearchPathOpts = CI.getSearchPathOptions ();
606+
607+ auto addCommonFile = [&](StringRef path) {
608+ auto file = FS.openFileForRead (path);
609+ if (!file)
610+ return ;
611+ auto status = (*file)->status ();
612+ if (!status)
613+ return ;
614+ auto fileRef = (*file)->getObjectRefForContent ();
615+ if (!fileRef)
616+ return ;
617+
618+ std::string realPath = Mapper ? Mapper->mapToString (path) : path.str ();
619+ CommonFiles.try_emplace (realPath, **fileRef, (size_t )status->getSize ());
620+ };
621+
603622 // Add SDKSetting file.
604623 SmallString<256 > SDKSettingPath;
605624 llvm::sys::path::append (SDKSettingPath, SearchPathOpts.getSDKPath (),
606625 " SDKSettings.json" );
607- FS-> status (SDKSettingPath);
626+ addCommonFile (SDKSettingPath);
608627
609628 // Add Legacy layout file.
610629 const std::vector<std::string> AllSupportedArches = {
@@ -616,32 +635,61 @@ void SwiftDependencyTracker::addCommonSearchPathDeps(
616635 for (auto &Arch : AllSupportedArches) {
617636 SmallString<256 > LayoutFile (RuntimeLibPath);
618637 llvm::sys::path::append (LayoutFile, " layouts-" + Arch + " .yaml" );
619- FS-> status (LayoutFile);
638+ addCommonFile (LayoutFile);
620639 }
621640 }
622641
623642 // Add VFSOverlay file.
624643 for (auto &Overlay: SearchPathOpts.VFSOverlayFiles )
625- FS-> status (Overlay);
644+ addCommonFile (Overlay);
626645
627646 // Add blocklist file.
628647 for (auto &File: CI.getFrontendOptions ().BlocklistConfigFilePaths )
629- FS-> status (File);
648+ addCommonFile (File);
630649}
631650
632- void SwiftDependencyTracker::startTracking () {
633- FS->trackNewAccesses ();
651+ void SwiftDependencyTracker::startTracking (bool includeCommonDeps) {
652+ TrackedFiles.clear ();
653+ if (includeCommonDeps) {
654+ for (auto &entry : CommonFiles)
655+ TrackedFiles.emplace (entry.first (), entry.second );
656+ }
657+ }
658+
659+ void SwiftDependencyTracker::trackFile (const Twine &path) {
660+ auto file = FS->openFileForRead (path);
661+ if (!file)
662+ return ;
663+ auto status = (*file)->status ();
664+ if (!status)
665+ return ;
666+ auto fileRef = (*file)->getObjectRefForContent ();
667+ if (!fileRef)
668+ return ;
669+ std::string realPath =
670+ Mapper ? Mapper->mapToString (path.str ()) : path.str ();
671+ TrackedFiles.try_emplace (realPath, **fileRef, (size_t )status->getSize ());
634672}
635673
636674llvm::Expected<llvm::cas::ObjectProxy>
637675SwiftDependencyTracker::createTreeFromDependencies () {
638- return FS->createTreeFromNewAccesses (
639- [&](const llvm::vfs::CachedDirectoryEntry &Entry,
640- llvm::SmallVectorImpl<char > &Storage) {
641- if (Mapper)
642- return Mapper->mapDirEntry (Entry, Storage);
643- return Entry.getTreePath ();
644- });
676+ llvm::SmallVector<clang::cas::IncludeTree::FileList::FileEntry> Files;
677+ for (auto &file : TrackedFiles) {
678+ auto includeTreeFile = clang::cas::IncludeTree::File::create (
679+ FS->getCAS (), file.first , file.second .FileRef );
680+ if (!includeTreeFile)
681+ return includeTreeFile.takeError ();
682+ Files.push_back (
683+ {includeTreeFile->getRef (),
684+ (clang::cas::IncludeTree::FileList::FileSizeTy)file.second .Size });
685+ }
686+
687+ auto includeTreeList =
688+ clang::cas::IncludeTree::FileList::create (FS->getCAS (), Files, {});
689+ if (!includeTreeList)
690+ return includeTreeList.takeError ();
691+
692+ return *includeTreeList;
645693}
646694
647695bool SwiftDependencyScanningService::setupCachingDependencyScanningService (
@@ -675,17 +723,20 @@ bool SwiftDependencyScanningService::setupCachingDependencyScanningService(
675723 CacheFS = std::move (*CachingFS);
676724
677725 // Setup prefix mapping.
678- Mapper = std::make_unique<llvm::TreePathPrefixMapper>(CacheFS);
679- SmallVector<llvm::MappedPrefix, 4 > Prefixes;
680- if (auto E = llvm::MappedPrefix::transformJoined (
681- Instance.getInvocation ().getSearchPathOptions ().ScannerPrefixMapper ,
682- Prefixes)) {
683- Instance.getDiags ().diagnose (SourceLoc (), diag::error_prefix_mapping,
684- toString (std::move (E)));
685- return true ;
726+ auto &ScannerPrefixMapper =
727+ Instance.getInvocation ().getSearchPathOptions ().ScannerPrefixMapper ;
728+ if (!ScannerPrefixMapper.empty ()) {
729+ Mapper = std::make_unique<llvm::PrefixMapper>();
730+ SmallVector<llvm::MappedPrefix, 4 > Prefixes;
731+ if (auto E = llvm::MappedPrefix::transformJoined (ScannerPrefixMapper,
732+ Prefixes)) {
733+ Instance.getDiags ().diagnose (SourceLoc (), diag::error_prefix_mapping,
734+ toString (std::move (E)));
735+ return true ;
736+ }
737+ Mapper->addRange (Prefixes);
738+ Mapper->sort ();
686739 }
687- Mapper->addRange (Prefixes);
688- Mapper->sort ();
689740
690741 UseClangIncludeTree =
691742 Instance.getInvocation ().getClangImporterOptions ().UseClangIncludeTree ;
0 commit comments