|
11 | 11 | //===----------------------------------------------------------------------===// |
12 | 12 |
|
13 | 13 | #include "ClangSourceBufferImporter.h" |
| 14 | +#include "swift/Basic/Assertions.h" |
14 | 15 | #include "swift/Basic/SourceManager.h" |
15 | 16 | #include "clang/Basic/SourceManager.h" |
16 | 17 | #include "llvm/Support/MemoryBuffer.h" |
@@ -47,12 +48,19 @@ SourceLoc ClangSourceBufferImporter::resolveSourceLocation( |
47 | 48 | if (mirrorIter != mirroredBuffers.end()) { |
48 | 49 | mirrorID = mirrorIter->second; |
49 | 50 | } else { |
| 51 | + StringRef bufIdent = buffer.getBufferIdentifier(); |
50 | 52 | 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)); |
56 | 64 | mirroredBuffers[buffer.getBufferStart()] = mirrorID; |
57 | 65 | } |
58 | 66 | loc = swiftSourceManager.getLocForOffset(mirrorID, decomposedLoc.second); |
|
0 commit comments