Skip to content

Conversation

@hnrklssn
Copy link
Member

Clang can end up with multiple file IDs representing the same file, but in different contexts. For example, files in the current module always have file IDs >0, while files in imported modules always have file IDs <-1. As we end up compiling multiple modules, the same file can be seen both as the "current" module, and a transitive import. We don't want multiple Swift buffer IDs for the same file, as it breaks things like -verify that compare buffer IDs.

rdar://162661286

@hnrklssn
Copy link
Member Author

@swift-ci please smoke test

Copy link
Contributor

@egorzhdan egorzhdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very familiar with this logic, but the change seems reasonable!

SourceManager::addNewSourceBuffer(std::unique_ptr<llvm::MemoryBuffer> Buffer) {
assert(Buffer);
StringRef BufIdentifier = Buffer->getBufferIdentifier();
assert(BufIdentIDMap.find(BufIdentifier) == BufIdentIDMap.end());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
assert(BufIdentIDMap.find(BufIdentifier) == BufIdentIDMap.end());
CONDITIONAL_ASSERT(BufIdentIDMap.find(BufIdentifier) == BufIdentIDMap.end());

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this assert in the end. It was a bit optimistic, and the complete fix is a bit out of scope for this PR.

Copy link
Contributor

@beccadax beccadax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, didn't know that. LGTM.

Clang can end up with multiple file IDs representing the same file, but
in different contexts. For example, file in the current module always
have file IDs >0, while files in imported modules always have file IDs
<-1. As we end up compiling multiple modules, the same file can be seen
both as the "current" module, and a transitive import. We don't want
multiple Swift buffer IDs for the same file, as it breaks things like
-verify that compare buffer IDs.

rdar://162661286
@hnrklssn hnrklssn force-pushed the dedup-clang-buffers branch from c3fc710 to 696c08e Compare October 16, 2025 06:11
@hnrklssn
Copy link
Member Author

Removed the assert I added in addNewSourceBuffer. We're not quite there yet; turns out there are a bunch of other places opening duplicated source buffers as well. But this is a start.

@hnrklssn
Copy link
Member Author

@swift-ci please smoke test

@hnrklssn hnrklssn enabled auto-merge October 16, 2025 06:13
Virtual files from different modules can differ despite having identical
names (e.g. `<module-imports>`. This fixes some diagnostics related test
failures when these virtual files were (incorrectly) deduplicated and
the module imports from a previous module were shown, by checking if the
buffer has a corresponding clang::FileEntry.
@hnrklssn
Copy link
Member Author

@swift-ci please smoke test

This would trigger when path prefixes are remapped, as the buffer
identifier is the remapped path, not the path on the file system. There
is no easy way to recover the original path, so drop the assert.
@hnrklssn
Copy link
Member Author

@swift-ci please smoke test

@hnrklssn
Copy link
Member Author

@swift-ci please test macos platform

@hnrklssn hnrklssn merged commit 6035f3c into swiftlang:main Oct 18, 2025
4 checks passed
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.

5 participants