Skip to content

Commit 7e05551

Browse files
committed
Swift: Check whether a SourceLoc is valid before using it.
1 parent e3d6b3e commit 7e05551

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

swift/extractor/invocation/SwiftDiagnosticsConsumer.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@
1111

1212
using namespace codeql;
1313

14+
namespace {
15+
struct DisplayLoc {
16+
llvm::StringRef file;
17+
unsigned line;
18+
unsigned column;
19+
20+
static DisplayLoc from(swift::SourceManager& sourceManager, swift::SourceLoc loc) {
21+
if (loc.isInvalid()) {
22+
return {"<invalid loc>", 0, 0};
23+
}
24+
auto file = sourceManager.getDisplayNameForLoc(loc);
25+
auto [line, column] = sourceManager.getLineAndColumnInBuffer(loc);
26+
return {file, line, column};
27+
}
28+
};
29+
30+
} // namespace
31+
1432
void SwiftDiagnosticsConsumer::handleDiagnostic(swift::SourceManager& sourceManager,
1533
const swift::DiagnosticInfo& diagInfo) {
1634
if (diagInfo.IsChildNote) return;
@@ -37,8 +55,7 @@ std::string SwiftDiagnosticsConsumer::getDiagMessage(swift::SourceManager& sourc
3755
void SwiftDiagnosticsConsumer::forwardToLog(swift::SourceManager& sourceManager,
3856
const swift::DiagnosticInfo& diagInfo,
3957
const std::string& message) {
40-
auto file = sourceManager.getDisplayNameForLoc(diagInfo.Loc);
41-
auto [line, column] = sourceManager.getLineAndColumnInBuffer(diagInfo.Loc);
58+
auto [file, line, column] = DisplayLoc::from(sourceManager, diagInfo.Loc);
4259
using Kind = swift::DiagnosticKind;
4360
switch (diagInfo.Kind) {
4461
case Kind::Error:

0 commit comments

Comments
 (0)