2828#include " swift/Frontend/CompileJobCacheResult.h"
2929#include " swift/Frontend/DiagnosticHelper.h"
3030#include " swift/Frontend/Frontend.h"
31+ #include " swift/Frontend/MakeStyleDependencies.h"
3132#include " swift/Frontend/PrintingDiagnosticConsumer.h"
3233#include " swift/Option/Options.h"
3334#include " clang/CAS/CASOptions.h"
3435#include " clang/Frontend/CompileJobCacheResult.h"
36+ #include " clang/Frontend/FrontendOptions.h"
3537#include " llvm/ADT/ArrayRef.h"
3638#include " llvm/ADT/IntrusiveRefCntPtr.h"
3739#include " llvm/ADT/SmallVector.h"
@@ -932,17 +934,20 @@ static llvm::Error replayCompilation(SwiftScanReplayInstance &Instance,
932934 Outputs.try_emplace (ID, File);
933935 });
934936 Outputs.try_emplace (file_types::TY_CachedDiagnostics, " <cached-diagnostics>" );
937+ Outputs.try_emplace (file_types::ID::TY_SymbolGraphFile, " <symbol-graph>" );
935938
936939 // Load all the output buffer.
937940 bool Remarks = Instance.Invocation .getCASOptions ().EnableCachingRemarks ;
938941 struct OutputEntry {
939942 std::string Path;
940943 llvm::cas::ObjectProxy Proxy;
944+ file_types::ID Kind;
941945 };
942946 SmallVector<OutputEntry> OutputProxies;
943947 std::optional<llvm::cas::ObjectProxy> DiagnosticsOutput;
944948 bool UseCASBackend = Invocation.getIRGenOptions ().UseCASBackend ;
945- std::string ObjFile;
949+ const FrontendOptions &FrontendOpts =
950+ Inst.getInvocation ().getFrontendOptions ();
946951
947952 swift::cas::CachedResultLoader Loader (CAS, Comp.Output );
948953 if (auto Err = Loader.replay (
@@ -956,15 +961,38 @@ static llvm::Error replayCompilation(SwiftScanReplayInstance &Instance,
956961 if (!Proxy)
957962 return Proxy.takeError ();
958963
959- if (Kind == file_types::ID::TY_Object && UseCASBackend)
960- ObjFile = OutputPath->second ;
961-
962964 if (Kind == file_types::ID::TY_CachedDiagnostics) {
963965 assert (!DiagnosticsOutput && " more than 1 diagnostics found" );
964966 DiagnosticsOutput = std::move (*Proxy);
967+ } else if (Kind == file_types::ID::TY_SymbolGraphFile &&
968+ !FrontendOpts.SymbolGraphOutputDir .empty ()) {
969+ auto Err = Proxy->forEachReference ([&](llvm::cas::ObjectRef Ref)
970+ -> llvm::Error {
971+ auto Proxy = CAS.getProxy (Ref);
972+ if (!Proxy)
973+ return Proxy.takeError ();
974+ auto PathRef = Proxy->getReference (0 );
975+ auto ContentRef = Proxy->getReference (1 );
976+ auto Path = CAS.getProxy (PathRef);
977+ auto Content = CAS.getProxy (ContentRef);
978+ if (!Path)
979+ return Path.takeError ();
980+ if (!Content)
981+ return Content.takeError ();
982+
983+ SmallString<128 > OutputPath (FrontendOpts.SymbolGraphOutputDir );
984+ llvm::sys::path::append (OutputPath, Path->getData ());
985+
986+ OutputProxies.emplace_back (OutputEntry{
987+ std::string (OutputPath), std::move (*Content), Kind});
988+
989+ return Error::success ();
990+ });
991+ if (Err)
992+ return Err;
965993 } else
966994 OutputProxies.emplace_back (
967- OutputEntry{OutputPath->second , std::move (*Proxy)});
995+ OutputEntry{OutputPath->second , std::move (*Proxy), Kind });
968996 return Error::success ();
969997 }))
970998 return Err;
@@ -1006,11 +1034,17 @@ static llvm::Error replayCompilation(SwiftScanReplayInstance &Instance,
10061034 auto File = Backend.createFile (Output.Path );
10071035 if (!File)
10081036 return File.takeError ();
1009- if (UseCASBackend && Output.Path == ObjFile ) {
1037+ if (UseCASBackend && Output.Kind == file_types::ID::TY_Object ) {
10101038 auto Schema = std::make_unique<llvm::mccasformats::v1::MCSchema>(CAS);
10111039 if (auto E = Schema->serializeObjectFile (Output.Proxy , *File))
10121040 Inst.getDiags ().diagnose (SourceLoc (), diag::error_mccas,
10131041 toString (std::move (E)));
1042+ } else if (Output.Kind == file_types::ID::TY_Dependencies) {
1043+ if (emitMakeDependenciesFromSerializedBuffer (Output.Proxy .getData (),
1044+ *File, FrontendOpts, Input,
1045+ Inst.getDiags ()))
1046+ Inst.getDiags ().diagnose (SourceLoc (), diag::cache_replay_failed,
1047+ " failed to emit dependency file" );
10141048 } else
10151049 *File << Output.Proxy .getData ();
10161050 if (auto E = File->keep ())
0 commit comments