Skip to content

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

Merged
merged 6 commits into from
Sep 18, 2024

Conversation

DougGregor
Copy link
Member

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.

…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.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

Note: only the last two commits are new, building on top of #76512

Copy link
Member

@ahoppen ahoppen left a 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;
Copy link
Member

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?

Copy link
Member Author

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.

@DougGregor DougGregor merged commit 3a570e0 into swiftlang:main Sep 18, 2024
3 checks passed
@DougGregor DougGregor deleted the one-loc-to-file-lookup branch September 18, 2024 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants