-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathClangSourceBufferImporter.cpp
98 lines (85 loc) · 3.85 KB
/
ClangSourceBufferImporter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//===--- 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"
using namespace swift;
using namespace swift::importer;
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 {
std::unique_ptr<llvm::MemoryBuffer> mirrorBuffer{
llvm::MemoryBuffer::getMemBuffer(buffer.getBuffer(),
buffer.getBufferIdentifier(),
/*RequiresNullTerminator=*/true)
};
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 = presumedLoc.getFilename();
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;
}