Skip to content

Commit d69c021

Browse files
committed
[ClangImporter] check if buffer represents file before dedup
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.
1 parent 696c08e commit d69c021

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

lib/ClangImporter/ClangSourceBufferImporter.cpp

Lines changed: 10 additions & 8 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,18 +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-
};
53+
llvm::MemoryBuffer::getMemBuffer(buffer.getBuffer(), bufIdent,
54+
/*RequiresNullTerminator=*/true)};
5555
// The same underlying file can exist as multiple clang file IDs. E.g. as
5656
// 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())
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+
CONDITIONAL_ASSERT(llvm::sys::fs::exists(bufIdent));
6062
mirrorID = IDOpt.value();
61-
else
63+
} else
6264
mirrorID = swiftSourceManager.addNewSourceBuffer(std::move(mirrorBuffer));
6365
mirroredBuffers[buffer.getBufferStart()] = mirrorID;
6466
}

0 commit comments

Comments
 (0)