Skip to content

Commit 6035f3c

Browse files
authored
Merge pull request #84904 from hnrklssn/dedup-clang-buffers
[ClangImporter] Deduplicate clang buffers representing the same file
2 parents 1b2e6ad + acc967b commit 6035f3c

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

lib/ClangImporter/ClangSourceBufferImporter.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "ClangSourceBufferImporter.h"
14+
#include "swift/Basic/Assertions.h"
1415
#include "swift/Basic/SourceManager.h"
1516
#include "clang/Basic/SourceManager.h"
1617
#include "llvm/Support/MemoryBuffer.h"
@@ -47,12 +48,19 @@ SourceLoc ClangSourceBufferImporter::resolveSourceLocation(
4748
if (mirrorIter != mirroredBuffers.end()) {
4849
mirrorID = mirrorIter->second;
4950
} else {
51+
StringRef bufIdent = buffer.getBufferIdentifier();
5052
std::unique_ptr<llvm::MemoryBuffer> mirrorBuffer{
51-
llvm::MemoryBuffer::getMemBuffer(buffer.getBuffer(),
52-
buffer.getBufferIdentifier(),
53-
/*RequiresNullTerminator=*/true)
54-
};
55-
mirrorID = swiftSourceManager.addNewSourceBuffer(std::move(mirrorBuffer));
53+
llvm::MemoryBuffer::getMemBuffer(buffer.getBuffer(), bufIdent,
54+
/*RequiresNullTerminator=*/true)};
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. Don't deduplicate files like "<module-imports>" that have
58+
// different contents in different modules, despite the same name.
59+
auto IDOpt = swiftSourceManager.getIDForBufferIdentifier(bufIdent);
60+
if (IDOpt.has_value() && clangSrcMgr.getFileEntryForID(clangFileID)) {
61+
mirrorID = IDOpt.value();
62+
} else
63+
mirrorID = swiftSourceManager.addNewSourceBuffer(std::move(mirrorBuffer));
5664
mirroredBuffers[buffer.getBufferStart()] = mirrorID;
5765
}
5866
loc = swiftSourceManager.getLocForOffset(mirrorID, decomposedLoc.second);

0 commit comments

Comments
 (0)