@@ -46,12 +46,18 @@ struct SwiftModuleTraceInfo {
4646 bool SupportsLibraryEvolution;
4747};
4848
49+ struct SwiftMacroTraceInfo {
50+ Identifier Name;
51+ std::string Path;
52+ };
53+
4954struct LoadedModuleTraceFormat {
5055 static const unsigned CurrentVersion = 2 ;
5156 unsigned Version;
5257 Identifier Name;
5358 std::string Arch;
5459 std::vector<SwiftModuleTraceInfo> SwiftModules;
60+ std::vector<SwiftMacroTraceInfo> SwiftMacros;
5561};
5662} // namespace
5763
@@ -68,6 +74,15 @@ template <> struct ObjectTraits<SwiftModuleTraceInfo> {
6874 }
6975};
7076
77+ template <>
78+ struct ObjectTraits <SwiftMacroTraceInfo> {
79+ static void mapping (Output &out, SwiftMacroTraceInfo &contents) {
80+ StringRef name = contents.Name .str ();
81+ out.mapRequired (" name" , name);
82+ out.mapRequired (" path" , contents.Path );
83+ }
84+ };
85+
7186// Version notes:
7287// 1. Keys: name, arch, swiftmodules
7388// 2. New keys: version, swiftmodulesDetailedInfo
@@ -87,6 +102,8 @@ template <> struct ObjectTraits<LoadedModuleTraceFormat> {
87102 out.mapRequired (" swiftmodules" , moduleNames);
88103
89104 out.mapRequired (" swiftmodulesDetailedInfo" , contents.SwiftModules );
105+
106+ out.mapRequired (" swiftmacros" , contents.SwiftMacros );
90107 }
91108};
92109} // namespace json
@@ -549,8 +566,7 @@ void ABIDependencyEvaluator::printABIExportMap(llvm::raw_ostream &os) const {
549566// FIXME: Use the VFS instead of handling paths directly. We are particularly
550567// sloppy about handling relative paths in the dependency tracker.
551568static void computeSwiftModuleTraceInfo (
552- ASTContext &ctx,
553- const SmallPtrSetImpl<ModuleDecl *> &abiDependencies,
569+ ASTContext &ctx, const SmallPtrSetImpl<ModuleDecl *> &abiDependencies,
554570 const llvm::DenseMap<StringRef, ModuleDecl *> &pathToModuleDecl,
555571 const DependencyTracker &depTracker, StringRef prebuiltCachePath,
556572 std::vector<SwiftModuleTraceInfo> &traceInfo) {
@@ -567,8 +583,8 @@ static void computeSwiftModuleTraceInfo(
567583 SmallVector<std::string, 16 > dependencies{deps.begin (), deps.end ()};
568584 auto incrDeps = depTracker.getIncrementalDependencyPaths ();
569585 dependencies.append (incrDeps.begin (), incrDeps.end ());
570- auto sharedLibraryExtRegex =
571- llvm::Regex ( " dylib|so|dll " , llvm::Regex::IgnoreCase);
586+ // NOTE: macro dependencies are handled differently.
587+ // See 'computeSwiftMacroTraceInfo()'.
572588 for (const auto &depPath : dependencies) {
573589
574590 // Decide if this is a swiftmodule based on the extension of the raw
@@ -580,10 +596,8 @@ static void computeSwiftModuleTraceInfo(
580596 auto isSwiftmodule = moduleFileType == file_types::TY_SwiftModuleFile;
581597 auto isSwiftinterface =
582598 moduleFileType == file_types::TY_SwiftModuleInterfaceFile;
583- auto isSharedLibrary =
584- sharedLibraryExtRegex.match (llvm::sys::path::extension (depPath));
585599
586- if (!(isSwiftmodule || isSwiftinterface || isSharedLibrary ))
600+ if (!(isSwiftmodule || isSwiftinterface))
587601 continue ;
588602
589603 auto dep = pathToModuleDecl.find (depPath);
@@ -639,34 +653,6 @@ static void computeSwiftModuleTraceInfo(
639653 continue ;
640654 }
641655
642- // If we found a shared library, it must be a compiler plugin dependency.
643- if (isSharedLibrary) {
644- // Infer the module name by dropping the library prefix and extension.
645- // e.g "/path/to/lib/libPlugin.dylib" -> "Plugin"
646- auto moduleName = llvm::sys::path::stem (depPath);
647- #if !defined(_WIN32)
648- moduleName.consume_front (" lib" );
649- #endif
650-
651- StringRef realDepPath =
652- fs::real_path (depPath, buffer, /* expand_tile*/ true )
653- ? StringRef (depPath)
654- : buffer.str ();
655-
656- traceInfo.push_back (
657- {/* Name=*/
658- ctx.getIdentifier (moduleName),
659- /* Path=*/
660- realDepPath.str (),
661- /* IsImportedDirectly=*/
662- false ,
663- /* SupportsLibraryEvolution=*/
664- false });
665- buffer.clear ();
666-
667- continue ;
668- }
669-
670656 // Skip cached modules in the prebuilt cache. We will add the corresponding
671657 // swiftinterface from the SDK directly, but this isn't checked. :-/
672658 //
@@ -706,6 +692,22 @@ static void computeSwiftModuleTraceInfo(
706692 });
707693}
708694
695+ static void
696+ computeSwiftMacroTraceInfo (ASTContext &ctx, const DependencyTracker &depTracker,
697+ std::vector<SwiftMacroTraceInfo> &traceInfo) {
698+ for (const auto ¯oDep : depTracker.getMacroPluginDependencies ()) {
699+ traceInfo.push_back ({macroDep.moduleName , macroDep.path });
700+ }
701+
702+ // Again, almost a re-implementation of reversePathSortedFilenames :(.
703+ std::sort (
704+ traceInfo.begin (), traceInfo.end (),
705+ [](const SwiftMacroTraceInfo &m1, const SwiftMacroTraceInfo &m2) -> bool {
706+ return std::lexicographical_compare (m1.Path .rbegin (), m1.Path .rend (),
707+ m2.Path .rbegin (), m2.Path .rend ());
708+ });
709+ }
710+
709711// [NOTE: Bailing-vs-crashing-in-trace-emission] There are certain edge cases
710712// in trace emission where an invariant that you think should hold does not hold
711713// in practice. For example, sometimes we have seen modules without any
@@ -765,10 +767,14 @@ bool swift::emitLoadedModuleTraceIfNeeded(ModuleDecl *mainModule,
765767 *depTracker, opts.PrebuiltModuleCachePath ,
766768 swiftModules);
767769
770+ std::vector<SwiftMacroTraceInfo> swiftMacros;
771+ computeSwiftMacroTraceInfo (ctxt, *depTracker, swiftMacros);
772+
768773 LoadedModuleTraceFormat trace = {
769774 /* version=*/ LoadedModuleTraceFormat::CurrentVersion,
770775 /* name=*/ mainModule->getName (),
771- /* arch=*/ ctxt.LangOpts .Target .getArchName ().str (), swiftModules};
776+ /* arch=*/ ctxt.LangOpts .Target .getArchName ().str (), swiftModules,
777+ swiftMacros};
772778
773779 // raw_fd_ostream is unbuffered, and we may have multiple processes writing,
774780 // so first write to memory and then dump the buffer to the trace file.
0 commit comments