3030#include " swift/AST/DiagnosticsSema.h"
3131#include " swift/AST/FileSystem.h"
3232#include " swift/AST/FineGrainedDependencies.h"
33+ #include " swift/AST/FineGrainedDependencyFormat.h"
3334#include " swift/AST/GenericSignatureBuilder.h"
3435#include " swift/AST/IRGenOptions.h"
3536#include " swift/AST/IRGenRequests.h"
@@ -1370,6 +1371,35 @@ static bool dumpAST(CompilerInstance &Instance) {
13701371 return Instance.getASTContext ().hadError ();
13711372}
13721373
1374+ static bool emitReferenceDependencies (CompilerInstance &Instance,
1375+ SourceFile *const SF,
1376+ StringRef outputPath) {
1377+ const auto alsoEmitDotFile = Instance.getInvocation ()
1378+ .getLangOptions ()
1379+ .EmitFineGrainedDependencySourcefileDotFiles ;
1380+
1381+ // Before writing to the dependencies file path, preserve any previous file
1382+ // that may have been there. No error handling -- this is just a nicety, it
1383+ // doesn't matter if it fails.
1384+ llvm::sys::fs::rename (outputPath, outputPath + " ~" );
1385+
1386+ using SourceFileDepGraph = fine_grained_dependencies::SourceFileDepGraph;
1387+ return fine_grained_dependencies::withReferenceDependencies (
1388+ SF, *Instance.getDependencyTracker (), outputPath, alsoEmitDotFile,
1389+ [&](SourceFileDepGraph &&g) -> bool {
1390+ const bool hadError =
1391+ fine_grained_dependencies::writeFineGrainedDependencyGraphToPath (
1392+ Instance.getDiags (), outputPath, g);
1393+
1394+ // If path is stdout, cannot read it back, so check for "-"
1395+ assert (outputPath == " -" || g.verifyReadsWhatIsWritten (outputPath));
1396+
1397+ if (alsoEmitDotFile)
1398+ g.emitDotFile (outputPath, Instance.getDiags ());
1399+ return hadError;
1400+ });
1401+ }
1402+
13731403static void emitReferenceDependenciesForAllPrimaryInputsIfNeeded (
13741404 CompilerInstance &Instance) {
13751405 const auto &Invocation = Instance.getInvocation ();
@@ -1384,13 +1414,11 @@ static void emitReferenceDependenciesForAllPrimaryInputsIfNeeded(
13841414 const std::string &referenceDependenciesFilePath =
13851415 Invocation.getReferenceDependenciesFilePathForPrimary (
13861416 SF->getFilename ());
1387- if (!referenceDependenciesFilePath.empty ()) {
1388- const auto LangOpts = Invocation.getLangOptions ();
1389- (void )fine_grained_dependencies::emitReferenceDependencies (
1390- Instance.getDiags (), SF, *Instance.getDependencyTracker (),
1391- referenceDependenciesFilePath,
1392- LangOpts.EmitFineGrainedDependencySourcefileDotFiles );
1417+ if (referenceDependenciesFilePath.empty ()) {
1418+ continue ;
13931419 }
1420+
1421+ emitReferenceDependencies (Instance, SF, referenceDependenciesFilePath);
13941422 }
13951423}
13961424static void
0 commit comments