@@ -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))
@@ -1069,9 +1079,12 @@ ASTUnitRef ASTBuildOperation::buildASTUnit(std::string &Error) {
10691079 collectModuleDependencies (CompIns.getMainModule (), Visited, Filenames);
10701080 // FIXME: There exists a small window where the module file may have been
10711081 // modified after compilation finished and before we get its stamp.
1072- for (auto &Filename : Filenames) {
1073- DependencyStamps.push_back (std::make_pair (
1074- Filename, ASTManager->Impl .getBufferStamp (Filename, FileSystem)));
1082+ {
1083+ llvm::sys::ScopedLock L (DependencyStampsMtx);
1084+ for (auto &Filename : Filenames) {
1085+ DependencyStamps.push_back (std::make_pair (
1086+ Filename, ASTManager->Impl .getBufferStamp (Filename, FileSystem)));
1087+ }
10751088 }
10761089
10771090 // Since we only typecheck the primary file (plus referenced constructs
0 commit comments