Skip to content

Commit 0c33688

Browse files
authored
Merge pull request #58681 from ahoppen/pr/guard-dependency-stamps
[SourceKit] Guard DependencyStamps with a lock
2 parents c9a1978 + 351770c commit 0c33688

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,20 @@ class ASTBuildOperation
308308
/// can be determined at construction time of the \c ASTBuildOperation.
309309
const std::vector<FileContent> FileContents;
310310

311+
/// Guards \c DependencyStamps. This prevents reading from \c DependencyStamps
312+
/// while it is being modified. It does not provide any ordering gurantees
313+
/// that \c DependencyStamps have been computed in \c buildASTUnit before they
314+
/// are accessed in \c matchesSourceState but that's fine (see comment on
315+
/// \c DependencyStamps).
316+
llvm::sys::Mutex DependencyStampsMtx;
317+
311318
/// \c DependencyStamps contains the stamps of all module depenecies needed
312319
/// for the AST build. These stamps are only known after the AST is built.
313320
/// Before the AST has been built, we thus assume that all dependency stamps
314321
/// match. This seems to be a reasonable assumption since the dependencies
315322
/// shouldn't change (much) in the time between an \c ASTBuildOperation is
316323
/// created and until it produced an AST.
324+
/// Must only be accessed if \c DependencyStampsMtx has been claimed.
317325
SmallVector<std::pair<std::string, BufferStamp>, 8> DependencyStamps = {};
318326

319327
/// The ASTManager from which this operation got scheduled. Used to update
@@ -898,6 +906,8 @@ bool ASTBuildOperation::matchesSourceState(
898906
}
899907
}
900908

909+
llvm::sys::ScopedLock L(DependencyStampsMtx);
910+
901911
for (auto &Dependency : DependencyStamps) {
902912
if (Dependency.second !=
903913
ASTManager->Impl.getBufferStamp(Dependency.first, OtherFileSystem))
@@ -1070,9 +1080,12 @@ ASTUnitRef ASTBuildOperation::buildASTUnit(std::string &Error) {
10701080
collectModuleDependencies(CompIns.getMainModule(), Visited, Filenames);
10711081
// FIXME: There exists a small window where the module file may have been
10721082
// modified after compilation finished and before we get its stamp.
1073-
for (auto &Filename : Filenames) {
1074-
DependencyStamps.push_back(std::make_pair(
1075-
Filename, ASTManager->Impl.getBufferStamp(Filename, FileSystem)));
1083+
{
1084+
llvm::sys::ScopedLock L(DependencyStampsMtx);
1085+
for (auto &Filename : Filenames) {
1086+
DependencyStamps.push_back(std::make_pair(
1087+
Filename, ASTManager->Impl.getBufferStamp(Filename, FileSystem)));
1088+
}
10761089
}
10771090

10781091
// Since we only typecheck the primary file (plus referenced constructs

0 commit comments

Comments
 (0)