Skip to content

Commit 221c703

Browse files
committed
Add explicit OpenSourcesAsVolatile option
1 parent a22419f commit 221c703

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,12 @@ namespace swift {
496496
/// Enable verification when every SubstitutionMap is constructed.
497497
bool VerifyAllSubstitutionMaps = false;
498498

499+
/// If set to true, the source manager will avoid memory mapping source files
500+
/// with the expectation they may change on disk. This is most useful when
501+
/// opening files under sourcekitd on Windows, as memory mapping on Windows
502+
/// prevents files from being written.
503+
bool OpenSourcesAsVolatile = false;
504+
499505
/// Load swiftmodule files in memory as volatile and avoid mmap.
500506
bool EnableVolatileModules = false;
501507

include/swift/Basic/SourceManager.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class SourceManager {
9494
private:
9595
llvm::SourceMgr LLVMSourceMgr;
9696
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem;
97-
bool EditorMode = false;
97+
bool OpenSourcesAsVolatile = false;
9898
unsigned IDEInspectionTargetBufferID = 0U;
9999
unsigned IDEInspectionTargetOffset;
100100

@@ -167,8 +167,10 @@ class SourceManager {
167167
return FileSystem;
168168
}
169169

170-
void setEditorMode() {
171-
EditorMode = true;
170+
// Setting this prevents SourceManager from memory mapping sources (via opening files
171+
// with isVolatile=true);
172+
void setOpenSourcesAsVolatile() {
173+
OpenSourcesAsVolatile = true;
172174
}
173175

174176
void setIDEInspectionTarget(unsigned BufferID, unsigned Offset) {

lib/Basic/SourceLoc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ unsigned SourceManager::getExternalSourceBufferID(StringRef Path) {
699699
swift::vfs::getFileOrSTDIN(*getFileSystem(), Path,
700700
/* FileSize */ -1,
701701
/* RequiresNullTerminator */ true,
702-
/* isVolatile */ this->EditorMode);
702+
/* isVolatile */ this->OpenSourcesAsVolatile);
703703
if (InputFileOrErr) {
704704
// This assertion ensures we can look up from the map in the future when
705705
// using the same Path.

lib/Frontend/Frontend.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -552,11 +552,9 @@ bool CompilerInstance::setup(const CompilerInvocation &Invoke,
552552
if (LangOpts.EnableModuleLoadingRemarks) {
553553
Invocation.getSearchPathOptions().dump(LangOpts.Target.isOSDarwin());
554554
}
555-
556-
// If the compiler instance is managed by an IDE, inform the source manager to avoid
557-
// memory-mapping source files that are likely to be mutated in an editor.
558-
if (LangOpts.DiagnosticsEditorMode) {
559-
this->getSourceMgr().setEditorMode();
555+
556+
if (LangOpts.OpenSourcesAsVolatile) {
557+
this->getSourceMgr().setOpenSourcesAsVolatile();
560558
}
561559

562560
// If we expect an implicit stdlib import, load in the standard library. If we

lib/IDETool/CompilerInvocation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ bool ide::initCompilerInvocation(
225225
LangOpts.AttachCommentsToDecls = true;
226226
LangOpts.DiagnosticsEditorMode = true;
227227
LangOpts.CollectParsedToken = true;
228+
#if defined(_WIN32)
229+
// Source files that might be open in an editor should not be memory mapped on Windows,
230+
// as they will become read-only.
231+
LangOpts.OpenSourcesAsVolatile = true;
232+
#endif
228233
if (LangOpts.PlaygroundTransform) {
229234
// The playground instrumenter changes the AST in ways that disrupt the
230235
// SourceKit functionality. Since we don't need the instrumenter, and all we

0 commit comments

Comments
 (0)