- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathDependencyChecking.cpp
152 lines (129 loc) · 5.22 KB
/
DependencyChecking.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//===--- DependencyChecking.cpp -------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include"DependencyChecking.h"
#include"swift/AST/SourceFile.h"
#include"swift/Frontend/Frontend.h"
usingnamespaceswift;
namespace {
/// For each dependency file in \p CI, run \p callback until the callback
/// returns \c true. Returns \c true if any callback call returns \c true, \c
/// false otherwise.
staticbool
forEachDependencyUntilTrue(CompilerInstance &CI,
std::optional<unsigned> excludeBufferID,
llvm::function_ref<bool(StringRef)> callback) {
// Check files in the current module. If 'excludeBufferID' is None, exclude
// all source files.
if (excludeBufferID.has_value()) {
for (FileUnit *file : CI.getMainModule()->getFiles()) {
StringRef filename;
if (auto SF = dyn_cast<SourceFile>(file)) {
if (SF->getBufferID() == excludeBufferID)
continue;
filename = SF->getFilename();
} elseif (auto LF = dyn_cast<LoadedFile>(file))
filename = LF->getFilename();
else
continue;
// Ignore synthesized files.
if (filename.empty() || filename.front() == '<')
continue;
if (callback(filename))
returntrue;
}
}
// Check other non-system depenencies (e.g. modules, headers).
for (auto &dep : CI.getDependencyTracker()->getDependencies()) {
if (callback(dep))
returntrue;
}
for (auto dep : CI.getDependencyTracker()->getIncrementalDependencyPaths()) {
if (callback(dep))
returntrue;
}
for (auto dep : CI.getDependencyTracker()->getMacroPluginDependencyPaths()) {
if (callback(dep))
returntrue;
}
returnfalse;
}
/// Collect hash codes of the dependencies into \c Map.
staticvoidcacheDependencyHashIfNeeded(CompilerInstance &CI,
std::optional<unsigned> excludeBufferID,
llvm::StringMap<llvm::hash_code> &Map) {
auto &FS = CI.getFileSystem();
forEachDependencyUntilTrue(CI, excludeBufferID, [&](StringRef filename) {
if (Map.count(filename))
returnfalse;
auto stat = FS.status(filename);
if (!stat)
returnfalse;
// We will check the hash only if the modification time of the dependecy
// is zero. See 'areAnyDependentFilesInvalidated() below'.
if (stat->getLastModificationTime() != llvm::sys::TimePoint<>())
returnfalse;
auto buf = FS.getBufferForFile(filename);
Map[filename] = llvm::hash_value(buf.get()->getBuffer());
returnfalse;
});
}
/// Check if any dependent files are modified since \p timestamp.
staticbool
areAnyDependentFilesInvalidated(CompilerInstance &CI, llvm::vfs::FileSystem &FS,
std::optional<unsigned> excludeBufferID,
llvm::sys::TimePoint<> timestamp,
const llvm::StringMap<llvm::hash_code> &Map) {
returnforEachDependencyUntilTrue(
CI, excludeBufferID, [&](StringRef filePath) {
auto stat = FS.status(filePath);
if (!stat)
// Missing.
returntrue;
auto lastModTime = stat->getLastModificationTime();
if (lastModTime > timestamp)
// Modified.
returntrue;
// If the last modification time is zero, this file is probably from a
// virtual file system. We need to check the content.
if (lastModTime == llvm::sys::TimePoint<>()) {
// Get the hash code of the last content.
auto oldHashEntry = Map.find(filePath);
if (oldHashEntry == Map.end())
// Unreachable? Not virtual in old filesystem, but virtual in new
// one.
returntrue;
auto oldHash = oldHashEntry->second;
// Calculate the hash code of the current content.
auto newContent = FS.getBufferForFile(filePath);
if (!newContent)
// Unreachable? stat succeeded, but couldn't get the content.
returntrue;
auto newHash = llvm::hash_value(newContent.get()->getBuffer());
if (oldHash != newHash)
returntrue;
}
returnfalse;
});
}
} // namespace
voidide::cacheDependencyHashIfNeeded(CompilerInstance &CI,
std::optional<unsigned> excludeBufferID,
llvm::StringMap<llvm::hash_code> &Map) {
return ::cacheDependencyHashIfNeeded(CI, excludeBufferID, Map);
}
boolide::areAnyDependentFilesInvalidated(
CompilerInstance &CI, llvm::vfs::FileSystem &FS,
std::optional<unsigned> excludeBufferID, llvm::sys::TimePoint<> timestamp,
const llvm::StringMap<llvm::hash_code> &Map) {
return ::areAnyDependentFilesInvalidated(CI, FS, excludeBufferID, timestamp,
Map);
}