Skip to content

Commit 696c08e

Browse files
committed
[ClangImporter] Deduplicate clang buffers representing the same file
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
1 parent d653b0c commit 696c08e

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

lib/ClangImporter/ClangSourceBufferImporter.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ SourceLoc ClangSourceBufferImporter::resolveSourceLocation(
5252
buffer.getBufferIdentifier(),
5353
/*RequiresNullTerminator=*/true)
5454
};
55-
mirrorID = swiftSourceManager.addNewSourceBuffer(std::move(mirrorBuffer));
55+
// The same underlying file can exist as multiple clang file IDs. E.g. as
56+
// part of the its own module, and then later loaded as an import in another
57+
// module.
58+
auto IDOpt = swiftSourceManager.getIDForBufferIdentifier(buffer.getBufferIdentifier());
59+
if (IDOpt.has_value())
60+
mirrorID = IDOpt.value();
61+
else
62+
mirrorID = swiftSourceManager.addNewSourceBuffer(std::move(mirrorBuffer));
5663
mirroredBuffers[buffer.getBufferStart()] = mirrorID;
5764
}
5865
loc = swiftSourceManager.getLocForOffset(mirrorID, decomposedLoc.second);

0 commit comments

Comments
 (0)