mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
Swift: route compiler diagnostics through our log
(cherry picked from commit b8c55612e5)
This commit is contained in:
committed by
Alexandre Boulgakov
parent
7af1e96943
commit
e3d6b3e537
@@ -1,5 +1,5 @@
|
||||
#include "swift/extractor/invocation/SwiftDiagnosticsConsumer.h"
|
||||
#include "swift/extractor/trap/generated/TrapEntries.h"
|
||||
#include "swift/extractor/trap/generated/TrapClasses.h"
|
||||
#include "swift/extractor/trap/TrapDomain.h"
|
||||
#include "swift/extractor/infra/SwiftDiagnosticKind.h"
|
||||
|
||||
@@ -13,13 +13,17 @@ using namespace codeql;
|
||||
|
||||
void SwiftDiagnosticsConsumer::handleDiagnostic(swift::SourceManager& sourceManager,
|
||||
const swift::DiagnosticInfo& diagInfo) {
|
||||
auto message = getDiagMessage(sourceManager, diagInfo);
|
||||
DiagnosticsTrap diag{};
|
||||
diag.id = trap.createTypedLabel<DiagnosticsTag>();
|
||||
if (diagInfo.IsChildNote) return;
|
||||
Diagnostics diag{trap.createTypedLabel<DiagnosticsTag>()};
|
||||
diag.kind = translateDiagnosticsKind(diagInfo.Kind);
|
||||
diag.text = message;
|
||||
diag.text = getDiagMessage(sourceManager, diagInfo);
|
||||
trap.emit(diag);
|
||||
locationExtractor.attachLocation(sourceManager, diagInfo, diag.id);
|
||||
|
||||
forwardToLog(sourceManager, diagInfo, diag.text);
|
||||
for (const auto& child : diagInfo.ChildDiagnosticInfo) {
|
||||
forwardToLog(sourceManager, *child);
|
||||
}
|
||||
}
|
||||
|
||||
std::string SwiftDiagnosticsConsumer::getDiagMessage(swift::SourceManager& sourceManager,
|
||||
@@ -29,3 +33,29 @@ std::string SwiftDiagnosticsConsumer::getDiagMessage(swift::SourceManager& sourc
|
||||
swift::DiagnosticEngine::formatDiagnosticText(out, diagInfo.FormatString, diagInfo.FormatArgs);
|
||||
return text.str().str();
|
||||
}
|
||||
|
||||
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);
|
||||
using Kind = swift::DiagnosticKind;
|
||||
switch (diagInfo.Kind) {
|
||||
case Kind::Error:
|
||||
LOG_ERROR("{}:{}:{} {}", file, line, column, message);
|
||||
break;
|
||||
case Kind::Warning:
|
||||
LOG_WARNING("{}:{}:{} {}", file, line, column, message);
|
||||
break;
|
||||
case Kind::Remark:
|
||||
LOG_INFO("{}:{}:{} {}", file, line, column, message);
|
||||
break;
|
||||
case Kind::Note:
|
||||
LOG_DEBUG("{}:{}:{} {}", file, line, column, message);
|
||||
break;
|
||||
default:
|
||||
LOG_ERROR("unknown diagnostic kind {}, {}:{}:{} {}", diagInfo.Kind, file, line, column,
|
||||
message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <swift/AST/DiagnosticConsumer.h>
|
||||
#include "swift/extractor/infra/SwiftLocationExtractor.h"
|
||||
#include "swift/logging/SwiftLogging.h"
|
||||
|
||||
namespace codeql {
|
||||
|
||||
@@ -17,8 +18,17 @@ class SwiftDiagnosticsConsumer : public swift::DiagnosticConsumer {
|
||||
private:
|
||||
static std::string getDiagMessage(swift::SourceManager& sourceManager,
|
||||
const swift::DiagnosticInfo& diagInfo);
|
||||
void forwardToLog(swift::SourceManager& sourceManager,
|
||||
const swift::DiagnosticInfo& diagInfo,
|
||||
const std::string& message);
|
||||
|
||||
void forwardToLog(swift::SourceManager& sourceManager, const swift::DiagnosticInfo& diagInfo) {
|
||||
forwardToLog(sourceManager, diagInfo, getDiagMessage(sourceManager, diagInfo));
|
||||
}
|
||||
|
||||
TrapDomain& trap;
|
||||
SwiftLocationExtractor locationExtractor;
|
||||
Logger logger{"compiler"};
|
||||
};
|
||||
|
||||
} // namespace codeql
|
||||
|
||||
@@ -91,6 +91,8 @@ class Observer : public swift::FrontendObserver {
|
||||
}
|
||||
|
||||
void configuredCompiler(swift::CompilerInstance& instance) override {
|
||||
// remove default consumers to avoid double messaging
|
||||
instance.getDiags().takeConsumers();
|
||||
instance.addDiagnosticConsumer(&diagConsumer);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user