-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Reimplement ModuleDecl::getSourceFileContainingLocation() using SourceManager #76551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…butes When rendering a swift_attr attribute for parsing, we were creating both the backing buffer and a source file, but not providing the buffer's ID to the source file, so we couldn't find the source code again. Fix that. While here, also register the source file with the module, so we can find the source file again based on location.
…ager The "buffer ID" in a SourceFile, which is used to find the source file's contents in the SourceManager, has always been optional. However, the effectively every SourceFile actually does have a buffer ID, and the vast majority of accesses to this information dereference the optional without checking. Update the handful of call sites that provided `nullopt` as the buffer ID to provide a proper buffer instead. These were mostly unit tests and testing programs, with a few places that passed a never-empty optional through to the SourceFile constructor. Then, remove optionality from the representation and accessors. It is now the case that every SourceFile has a buffer ID, simplying a bunch of code.
-1 was once used as a sentinel value for "no buffer"; make sure it doesn't persist anywhere.
Now that every source file has a buffer ID, introduce the reverse mapping so clients can find the source file(s) in their module that reference that buffer ID.
…eManager ModuleDecl kept track of all of the source files in the module so that it could find the source file containing a given location, which relied on a sorted array all of these source files. SourceManager has its own similar data structure for a similar query mapping the locations to buffer IDs. Replace ModuleDecl's dats structure with a use of the SourceManager's version with the mapping from buffer IDs to source files.
@swift-ci please smoke test |
Note: only the last two commits are new, building on top of #76512 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice cleanup to make the buffer ID non-optional.
llvm::DenseMap< | ||
unsigned, | ||
llvm::TinyPtrVector<SourceFile *> | ||
> bufferIDToSourceFiles; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you remind me of scenarios where multiple SourceFile
can reference the same buffer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only place I've seen this happen thus far is in the Clang importer, where it's effectively uniquing the text of C __attribute__((swift_attr("<this is a Swift attribute>")))
to not create lots of buffers, but it still needs to provide a different SourceFile *
for each imported module.
We might consider doing similar uniquing for macro expansion buffers, because there could be a lot of identical buffers.
ModuleDecl kept track of all of the source files in the module so that it could find the source file containing a given location, which relied on a sorted array all of these source files. SourceManager has its own similar data structure for a similar query mapping the locations to buffer IDs.
Replace ModuleDecl's dats structure with a use of the SourceManager's location -> buffer ID mapping. Augment SourceManager with a bufferID -> SourceFile mapping so we can do this.