Swift: Check whether a SourceLoc is valid before using it.

This commit is contained in:
Alexandre Boulgakov 2023-08-24 18:14:34 +01:00
Родитель e3d6b3e537
Коммит 7e05551f16
1 изменённых файлов: 19 добавлений и 2 удалений

Просмотреть файл

@ -11,6 +11,24 @@
using namespace codeql;
namespace {
struct DisplayLoc {
llvm::StringRef file;
unsigned line;
unsigned column;
static DisplayLoc from(swift::SourceManager& sourceManager, swift::SourceLoc loc) {
if (loc.isInvalid()) {
return {"<invalid loc>", 0, 0};
}
auto file = sourceManager.getDisplayNameForLoc(loc);
auto [line, column] = sourceManager.getLineAndColumnInBuffer(loc);
return {file, line, column};
}
};
} // namespace
void SwiftDiagnosticsConsumer::handleDiagnostic(swift::SourceManager& sourceManager,
const swift::DiagnosticInfo& diagInfo) {
if (diagInfo.IsChildNote) return;
@ -37,8 +55,7 @@ std::string SwiftDiagnosticsConsumer::getDiagMessage(swift::SourceManager& sourc
void SwiftDiagnosticsConsumer::forwardToLog(swift::SourceManager& sourceManager,
const swift::DiagnosticInfo& diagInfo,
const std::string& message) {
auto file = sourceManager.getDisplayNameForLoc(diagInfo.Loc);
auto [line, column] = sourceManager.getLineAndColumnInBuffer(diagInfo.Loc);
auto [file, line, column] = DisplayLoc::from(sourceManager, diagInfo.Loc);
using Kind = swift::DiagnosticKind;
switch (diagInfo.Kind) {
case Kind::Error: