This is an archive of the discontinued LLVM Phabricator instance.

WIP: Frontend: Use an InMemoryFileSystem for the RemappedFiles parameter in ASTUnit
Needs ReviewPublic

Authored by dexonsmith on Nov 11 2020, 1:50 PM.

Details

Summary

Re-implement the RemappedFiles parameter to various ASTUnit entry points by storing buffers in an InMemoryFileSystem. This parameter is commonly used by indexing tools to pass in source files that haven't been saved to disk (yet). This is the same strategy ClangTool switched to in 2015.

The previous implementation added them to PreprocessorOptions::RemappedFileBuffers, which eventually triggers calls to FileManager::getVirtualFile and SourceManager::overrideFileContents. That's one of the blockers for sinking the content cache from SourceManager down to FileManager. The new strategy is also a bit cleaner (the old one predates llvm::vfs::FileSystem).

Follow-ups will handle the other two uses of RemappedFileBuffers in ASTUnit. One is for the preamble's serialized AST. The other is for overriding the main file buffer, which is a bit tricky since we need the ASTWriter to save the preamble-only version.

WIP: I'm not totally happy with this patch yet, but tests are finally passing so I'm posting this for early review. In the meantime I'll continue peeling off what I can for prep patches (or there may be changes I can just delete). Among other things, I want to add a test (in a prep patch) for the handling of a file showing up twice in the same RemappedFiles vector, since it was pretty tough to discover I needed special logic for that.

Diff Detail

Event Timeline

dexonsmith created this revision.Nov 11 2020, 1:50 PM
dexonsmith requested review of this revision.Nov 11 2020, 1:50 PM
dexonsmith added inline comments.
clang/lib/Frontend/PrecompiledPreamble.cpp
571–579

@kadircet, I'm curious if clang-tools-extra/clangd/Preamble.cpp's call to ASTUnit::CanReuse would benefit from this logic as well. I would guess clangd doesn't even try to reuse preambles when unsaved files have changed (since you might see the same crashes or wrong results I saw before adding this), but after this patch lands you might be able to do that safely.

Note, I just discovered this patch changes behaviour in the ASTReader that was only indirectly tested previously via the file-to-file remapping command-line options. I've blocked this on https://reviews.llvm.org/D91342 for now, since that patch reimplements the file-to-file remapping, and whatever we figure out for updating that test should apply here too.