4545#include " swift/Frontend/FrontendOptions.h"
4646#include " swift/Frontend/ModuleInterfaceLoader.h"
4747#include " swift/Strings.h"
48- #include " clang/Basic/Module .h"
48+ #include " clang/CAS/IncludeTree .h"
4949#include " llvm/ADT/STLExtras.h"
5050#include " llvm/ADT/SetOperations.h"
5151#include " llvm/ADT/SetVector.h"
6969#include < sstream>
7070#include < stack>
7171#include < string>
72- #include < algorithm>
7372
7473using namespace swift ;
7574using namespace swift ::dependencies;
@@ -101,9 +100,6 @@ class ExplicitModuleDependencyResolver {
101100 if (resolvingDepInfo.isFinalized ())
102101 return false ;
103102
104- if (auto ID = resolvingDepInfo.getClangIncludeTree ())
105- includeTrees.push_back (*ID);
106-
107103 for (const auto &depModuleID : dependencies) {
108104 const auto &depInfo = cache.findKnownDependency (depModuleID);
109105 switch (depModuleID.Kind ) {
@@ -322,8 +318,10 @@ class ExplicitModuleDependencyResolver {
322318 // Collect CAS deppendencies from clang modules.
323319 if (!clangDepDetails.CASFileSystemRootID .empty ())
324320 rootIDs.push_back (clangDepDetails.CASFileSystemRootID );
325- if (!clangDepDetails.CASClangIncludeTreeRootID .empty ())
326- includeTrees.push_back (clangDepDetails.CASClangIncludeTreeRootID );
321+ if (!clangDepDetails.CASClangIncludeTreeRootID .empty ()) {
322+ if (addIncludeTree (clangDepDetails.CASClangIncludeTreeRootID ))
323+ return true ;
324+ }
327325
328326 collectUsedVFSOverlay (clangDepDetails);
329327
@@ -358,12 +356,14 @@ class ExplicitModuleDependencyResolver {
358356 auto bridgeRoot = tracker->createTreeFromDependencies ();
359357 if (!bridgeRoot)
360358 return diagnoseCASFSCreationError (bridgeRoot.takeError ());
361- fileListIDs.push_back (bridgeRoot->getID ().toString ());
359+
360+ fileListRefs.push_back (bridgeRoot->getRef ());
362361 }
363362 }
364- } else
365- includeTrees.push_back (sourceDepDetails.textualModuleDetails
366- .CASBridgingHeaderIncludeTreeRootID );
363+ } else if (addIncludeTree (sourceDepDetails.textualModuleDetails
364+ .CASBridgingHeaderIncludeTreeRootID ))
365+ return true ;
366+
367367 return false ;
368368 };
369369
@@ -499,9 +499,7 @@ class ExplicitModuleDependencyResolver {
499499 auto root = tracker->createTreeFromDependencies ();
500500 if (!root)
501501 return diagnoseCASFSCreationError (root.takeError ());
502- auto rootID = root->getID ().toString ();
503- dependencyInfoCopy.updateCASFileSystemRootID (rootID);
504- fileListIDs.push_back (rootID);
502+ fileListRefs.push_back (root->getRef ());
505503 } else if (auto *textualDep =
506504 resolvingDepInfo.getAsSwiftInterfaceModule ()) {
507505 tracker->startTracking ();
@@ -516,9 +514,7 @@ class ExplicitModuleDependencyResolver {
516514 auto root = tracker->createTreeFromDependencies ();
517515 if (!root)
518516 return diagnoseCASFSCreationError (root.takeError ());
519- auto rootID = root->getID ().toString ();
520- dependencyInfoCopy.updateCASFileSystemRootID (rootID);
521- fileListIDs.push_back (rootID);
517+ fileListRefs.push_back (root->getRef ());
522518 }
523519
524520 // Update build command line.
@@ -530,15 +526,8 @@ class ExplicitModuleDependencyResolver {
530526 commandline.push_back (rootID);
531527 }
532528
533- for (auto tree : includeTrees) {
534- commandline.push_back (" -clang-include-tree-root" );
535- commandline.push_back (tree);
536- }
537-
538- for (auto list : fileListIDs) {
539- commandline.push_back (" -clang-include-tree-filelist" );
540- commandline.push_back (list);
541- }
529+ if (computeCASFileSystem (dependencyInfoCopy))
530+ return true ;
542531 }
543532
544533 // Compute and update module cache key.
@@ -636,6 +625,53 @@ class ExplicitModuleDependencyResolver {
636625 cmd.push_back (" -cache-disable-replay" );
637626 }
638627
628+ bool addIncludeTree (StringRef includeTree) {
629+ auto &db = cache.getScanService ().getCAS ();
630+ auto casID = db.parseID (includeTree);
631+ if (!casID) {
632+ instance.getDiags ().diagnose (SourceLoc (), diag::error_invalid_cas_id,
633+ includeTree, toString (casID.takeError ()));
634+ return true ;
635+ }
636+ auto ref = db.getReference (*casID);
637+ if (!ref) {
638+ instance.getDiags ().diagnose (SourceLoc (), diag::error_load_input_from_cas,
639+ includeTree);
640+ return true ;
641+ }
642+
643+ auto root = clang::cas::IncludeTreeRoot::get (db, *ref);
644+ if (!root) {
645+ instance.getDiags ().diagnose (SourceLoc (), diag::error_cas_malformed_input,
646+ includeTree, toString (root.takeError ()));
647+ return true ;
648+ }
649+
650+ fileListRefs.push_back (root->getFileListRef ());
651+ return false ;
652+ }
653+
654+ bool computeCASFileSystem (ModuleDependencyInfo &dependencyInfoCopy) {
655+ if (fileListRefs.empty ())
656+ return false ;
657+
658+ auto &db = cache.getScanService ().getCAS ();
659+ auto casFS =
660+ clang::cas::IncludeTree::FileList::create (db, {}, fileListRefs);
661+ if (!casFS) {
662+ instance.getDiags ().diagnose (SourceLoc (), diag::error_cas,
663+ " CAS IncludeTree FileList creation" ,
664+ toString (casFS.takeError ()));
665+ return true ;
666+ }
667+
668+ auto casID = casFS->getID ().toString ();
669+ dependencyInfoCopy.updateCASFileSystemRootID (casID);
670+ commandline.push_back (" -clang-include-tree-filelist" );
671+ commandline.push_back (casID);
672+ return false ;
673+ }
674+
639675private:
640676 const ModuleDependencyID &moduleID;
641677 ModuleDependenciesCache &cache;
@@ -644,8 +680,7 @@ class ExplicitModuleDependencyResolver {
644680
645681 std::optional<SwiftDependencyTracker> tracker;
646682 std::vector<std::string> rootIDs;
647- std::vector<std::string> includeTrees;
648- std::vector<std::string> fileListIDs;
683+ std::vector<llvm::cas::ObjectRef> fileListRefs;
649684 std::vector<std::string> commandline;
650685 std::vector<std::string> bridgingHeaderBuildCmd;
651686 llvm::StringMap<MacroPluginDependency> macros;
0 commit comments