mirror of
https://github.com/apple/swift.git
synced 2026-10-02 10:58:03 +02:00
Reaching a header through two spellings of its path gives two `FileID`s that share one `MemoryBuffer` but report different presumed filenames, so both mirror onto the same Swift buffer at the same offset and the second `openVirtualFile` collides with the first. This aborts the Android `swift-inspect` build in the Windows toolchain job: ``` FAILED: lib/libSwiftInspectLinux.a (aarch64-unknown-linux-android23) error: compile command failed due to exception 3 (use -v to see invocation) Assertion failed: existingFile.Name == name, file swift\lib\Basic\SourceLoc.cpp, line 178 ... 9. While evaluating request UnqualifiedLookupRequest(looking up 'Elf64_Ehdr' ...) 11. ...\android-ndk-r30...\sysroot\usr\include\linux\elf.h:198:3: importing 'Elf64_Ehdr' ``` Two `FileID`s only share a buffer when they name the same file, so when the presumed path is merely another spelling of the mirrored buffer's own identifier, use that identifier instead. A `#line` directive naming a genuinely different file keeps its own name, and the assertion in `SourceManager::openVirtualFile` is left intact for the parser's `#sourceLocation` handling. This happens on `stable/23.x` because of https://github.com/llvm/llvm-project/pull/189475, which is not on `stable/21.x`.
131 lines
5.4 KiB
C++
131 lines
5.4 KiB
C++
//===--- ClangSourceBufferImporter.cpp - Map Clang buffers to Swift -------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "ClangSourceBufferImporter.h"
|
|
#include "swift/Basic/SourceManager.h"
|
|
#include "clang/Basic/SourceManager.h"
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
#include "llvm/Support/Path.h"
|
|
|
|
using namespace swift;
|
|
using namespace swift::importer;
|
|
|
|
/// Pick a virtual file name for a mirrored buffer that does not depend on how
|
|
/// the file's path happened to be spelled at this point of use.
|
|
static StringRef getStableVirtualFileName(StringRef presumedFile,
|
|
StringRef bufferIdentifier) {
|
|
if (presumedFile == bufferIdentifier)
|
|
return bufferIdentifier;
|
|
|
|
namespace path = llvm::sys::path;
|
|
SmallString<128> presumed{presumedFile};
|
|
SmallString<128> buffered{bufferIdentifier};
|
|
path::remove_dots(presumed, /*remove_dot_dot=*/true);
|
|
path::remove_dots(buffered, /*remove_dot_dot=*/true);
|
|
|
|
std::string presumedSlashes = path::convert_to_slash(presumed);
|
|
std::string bufferedSlashes = path::convert_to_slash(buffered);
|
|
bool sameFile =
|
|
path::is_style_windows(path::Style::native)
|
|
? StringRef(presumedSlashes).equals_insensitive(bufferedSlashes)
|
|
: presumedSlashes == bufferedSlashes;
|
|
return sameFile ? bufferIdentifier : presumedFile;
|
|
}
|
|
|
|
static SourceLoc findEndOfLine(SourceManager &SM, SourceLoc loc,
|
|
unsigned bufferID) {
|
|
CharSourceRange entireBuffer = SM.getRangeForBuffer(bufferID);
|
|
CharSourceRange rangeFromLoc{SM, loc, entireBuffer.getEnd()};
|
|
StringRef textFromLoc = SM.extractText(rangeFromLoc);
|
|
size_t newlineOffset = textFromLoc.find_first_of({"\r\n\0", 3});
|
|
if (newlineOffset == StringRef::npos)
|
|
return entireBuffer.getEnd();
|
|
return loc.getAdvancedLoc(newlineOffset);
|
|
}
|
|
|
|
SourceLoc ClangSourceBufferImporter::resolveSourceLocation(
|
|
const clang::SourceManager &clangSrcMgr,
|
|
clang::SourceLocation clangLoc) {
|
|
SourceLoc loc;
|
|
|
|
clangLoc = clangSrcMgr.getFileLoc(clangLoc);
|
|
auto decomposedLoc = clangSrcMgr.getDecomposedLoc(clangLoc);
|
|
if (decomposedLoc.first.isInvalid())
|
|
return loc;
|
|
|
|
auto clangFileID = decomposedLoc.first;
|
|
auto buffer = clangSrcMgr.getBufferOrFake(clangFileID);
|
|
unsigned mirrorID;
|
|
|
|
auto mirrorIter = mirroredBuffers.find(buffer.getBufferStart());
|
|
if (mirrorIter != mirroredBuffers.end()) {
|
|
mirrorID = mirrorIter->second;
|
|
} else {
|
|
StringRef bufIdent = buffer.getBufferIdentifier();
|
|
std::unique_ptr<llvm::MemoryBuffer> mirrorBuffer{
|
|
llvm::MemoryBuffer::getMemBuffer(buffer.getBuffer(), bufIdent,
|
|
/*RequiresNullTerminator=*/true)};
|
|
// The same underlying file can exist as multiple clang file IDs. E.g. as
|
|
// part of the its own module, and then later loaded as an import in another
|
|
// module. Don't deduplicate files like "<module-imports>" that have
|
|
// different contents in different modules, despite the same name.
|
|
auto IDOpt = swiftSourceManager.getIDForBufferIdentifier(bufIdent);
|
|
if (IDOpt.has_value() && clangSrcMgr.getFileEntryForID(clangFileID)) {
|
|
mirrorID = IDOpt.value();
|
|
} else
|
|
mirrorID = swiftSourceManager.addNewSourceBuffer(std::move(mirrorBuffer));
|
|
mirroredBuffers[buffer.getBufferStart()] = mirrorID;
|
|
}
|
|
loc = swiftSourceManager.getLocForOffset(mirrorID, decomposedLoc.second);
|
|
|
|
auto presumedLoc = clangSrcMgr.getPresumedLoc(clangLoc);
|
|
if (!presumedLoc.getFilename())
|
|
return loc;
|
|
if (presumedLoc.getLine() == 0)
|
|
return SourceLoc();
|
|
|
|
unsigned bufferLineNumber =
|
|
clangSrcMgr.getLineNumber(decomposedLoc.first, decomposedLoc.second);
|
|
|
|
StringRef presumedFile = getStableVirtualFileName(
|
|
presumedLoc.getFilename(),
|
|
swiftSourceManager.getIdentifierForBuffer(mirrorID));
|
|
SourceLoc startOfLine = loc.getAdvancedLoc(-presumedLoc.getColumn() + 1);
|
|
|
|
// FIXME: Virtual files can't actually model the EOF position correctly, so
|
|
// if this virtual file would start at EOF, just hope the physical location
|
|
// will do.
|
|
if (startOfLine != swiftSourceManager.getRangeForBuffer(mirrorID).getEnd()) {
|
|
bool isNewVirtualFile = swiftSourceManager.openVirtualFile(
|
|
startOfLine, presumedFile, presumedLoc.getLine() - bufferLineNumber);
|
|
if (isNewVirtualFile) {
|
|
SourceLoc endOfLine = findEndOfLine(swiftSourceManager, loc, mirrorID);
|
|
swiftSourceManager.closeVirtualFile(endOfLine);
|
|
}
|
|
}
|
|
|
|
using SourceManagerRef = llvm::IntrusiveRefCntPtr<const clang::SourceManager>;
|
|
auto iter = std::lower_bound(sourceManagersWithDiagnostics.begin(),
|
|
sourceManagersWithDiagnostics.end(),
|
|
&clangSrcMgr,
|
|
[](const SourceManagerRef &inArray,
|
|
const clang::SourceManager *toInsert) {
|
|
return std::less<const clang::SourceManager *>()(inArray.get(), toInsert);
|
|
});
|
|
if (iter == sourceManagersWithDiagnostics.end() ||
|
|
iter->get() != &clangSrcMgr) {
|
|
sourceManagersWithDiagnostics.insert(iter, &clangSrcMgr);
|
|
}
|
|
|
|
return loc;
|
|
}
|