Skip to content

Commit 3c2b376

Browse files
committed
[NFC] Refactor Reference Dependency Emission
Invert the responsibility of the entrypoint so that FrontendTool is directing the actual serialization work. The entrypoint now solely exists to construct a dependency graph. While I'm here, prepare the way for serializing dependency graphs for modules by optimistically modeling a ModuleOrSourceFile input.
1 parent dfdebfa commit 3c2b376

File tree

3 files changed

+56
-35
lines changed

3 files changed

+56
-35
lines changed

include/swift/AST/FineGrainedDependencies.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,14 @@ namespace swift {
5959
class DependencyTracker;
6060
class DiagnosticEngine;
6161
class FrontendOptions;
62+
class ModuleDecl;
6263
class SourceFile;
6364

6465
/// Use a new namespace to help keep the experimental code from clashing.
6566
namespace fine_grained_dependencies {
6667

68+
class SourceFileDepGraph;
69+
6770
using StringVec = std::vector<std::string>;
6871

6972
template <typename Element> using ConstPtrVec = std::vector<const Element *>;
@@ -343,11 +346,11 @@ class BiIndexedTwoStageMap {
343346
// MARK: Start of fine-grained-dependency-specific code
344347
//==============================================================================
345348

346-
/// The entry point into this system from the frontend:
347-
/// Write out the .swiftdeps file for a frontend compilation of a primary file.
348-
bool emitReferenceDependencies(DiagnosticEngine &diags, SourceFile *SF,
349-
const DependencyTracker &depTracker,
350-
StringRef outputPath, bool alsoEmitDotFile);
349+
bool withReferenceDependencies(
350+
llvm::PointerUnion<ModuleDecl *, SourceFile *> MSF,
351+
const DependencyTracker &depTracker, StringRef outputPath,
352+
bool alsoEmitDotFile, llvm::function_ref<bool(SourceFileDepGraph &&)>);
353+
351354
//==============================================================================
352355
// MARK: Enums
353356
//==============================================================================

lib/AST/FrontendSourceFileDepGraphFactory.cpp

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -236,30 +236,20 @@ std::string DependencyKey::computeNameForProvidedEntity<
236236
// MARK: Entry point into frontend graph construction
237237
//==============================================================================
238238

239-
bool fine_grained_dependencies::emitReferenceDependencies(
240-
DiagnosticEngine &diags, SourceFile *const SF,
241-
const DependencyTracker &depTracker,
242-
StringRef outputPath,
243-
const bool alsoEmitDotFile) {
244-
245-
// Before writing to the dependencies file path, preserve any previous file
246-
// that may have been there. No error handling -- this is just a nicety, it
247-
// doesn't matter if it fails.
248-
llvm::sys::fs::rename(outputPath, outputPath + "~");
249-
250-
SourceFileDepGraph g = FrontendSourceFileDepGraphFactory(
251-
SF, outputPath, depTracker, alsoEmitDotFile)
252-
.construct();
253-
254-
bool hadError = writeFineGrainedDependencyGraphToPath(diags, outputPath, g);
255-
256-
// If path is stdout, cannot read it back, so check for "-"
257-
assert(outputPath == "-" || g.verifyReadsWhatIsWritten(outputPath));
258-
259-
if (alsoEmitDotFile)
260-
g.emitDotFile(outputPath, diags);
261-
262-
return hadError;
239+
bool fine_grained_dependencies::withReferenceDependencies(
240+
llvm::PointerUnion<ModuleDecl *, SourceFile *> MSF,
241+
const DependencyTracker &depTracker, StringRef outputPath,
242+
bool alsoEmitDotFile,
243+
llvm::function_ref<bool(SourceFileDepGraph &&)> cont) {
244+
if (MSF.dyn_cast<ModuleDecl *>()) {
245+
llvm_unreachable("Cannot construct dependency graph for modules!");
246+
} else {
247+
auto *SF = MSF.get<SourceFile *>();
248+
SourceFileDepGraph g = FrontendSourceFileDepGraphFactory(
249+
SF, outputPath, depTracker, alsoEmitDotFile)
250+
.construct();
251+
return cont(std::move(g));
252+
}
263253
}
264254

265255
//==============================================================================

lib/FrontendTool/FrontendTool.cpp

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
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+
13731403
static 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
}
13961424
static void

0 commit comments

Comments
 (0)