30
30
#include " swift/AST/DiagnosticsSema.h"
31
31
#include " swift/AST/FileSystem.h"
32
32
#include " swift/AST/FineGrainedDependencies.h"
33
+ #include " swift/AST/FineGrainedDependencyFormat.h"
33
34
#include " swift/AST/GenericSignatureBuilder.h"
34
35
#include " swift/AST/IRGenOptions.h"
35
36
#include " swift/AST/IRGenRequests.h"
@@ -1370,6 +1371,35 @@ static bool dumpAST(CompilerInstance &Instance) {
1370
1371
return Instance.getASTContext ().hadError ();
1371
1372
}
1372
1373
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
+
1373
1403
static void emitReferenceDependenciesForAllPrimaryInputsIfNeeded (
1374
1404
CompilerInstance &Instance) {
1375
1405
const auto &Invocation = Instance.getInvocation ();
@@ -1384,13 +1414,11 @@ static void emitReferenceDependenciesForAllPrimaryInputsIfNeeded(
1384
1414
const std::string &referenceDependenciesFilePath =
1385
1415
Invocation.getReferenceDependenciesFilePathForPrimary (
1386
1416
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 ;
1393
1419
}
1420
+
1421
+ emitReferenceDependencies (Instance, SF, referenceDependenciesFilePath);
1394
1422
}
1395
1423
}
1396
1424
static void
0 commit comments