1
1
#include " swift/extractor/invocation/SwiftDiagnosticsConsumer.h"
2
- #include " swift/extractor/trap/generated/TrapEntries .h"
2
+ #include " swift/extractor/trap/generated/TrapClasses .h"
3
3
#include " swift/extractor/trap/TrapDomain.h"
4
4
#include " swift/extractor/infra/SwiftDiagnosticKind.h"
5
5
11
11
12
12
using namespace codeql ;
13
13
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
+
14
32
void SwiftDiagnosticsConsumer::handleDiagnostic (swift::SourceManager& sourceManager,
15
33
const swift::DiagnosticInfo& diagInfo) {
16
- auto message = getDiagMessage (sourceManager, diagInfo);
17
- DiagnosticsTrap diag{};
18
- diag.id = trap.createTypedLabel <DiagnosticsTag>();
34
+ if (diagInfo.IsChildNote ) return ;
35
+ Diagnostics diag{trap.createTypedLabel <DiagnosticsTag>()};
19
36
diag.kind = translateDiagnosticsKind (diagInfo.Kind );
20
- diag.text = message ;
37
+ diag.text = getDiagMessage (sourceManager, diagInfo) ;
21
38
trap.emit (diag);
22
39
locationExtractor.attachLocation (sourceManager, diagInfo, diag.id );
40
+
41
+ forwardToLog (sourceManager, diagInfo, diag.text );
42
+ for (const auto & child : diagInfo.ChildDiagnosticInfo ) {
43
+ forwardToLog (sourceManager, *child);
44
+ }
23
45
}
24
46
25
47
std::string SwiftDiagnosticsConsumer::getDiagMessage (swift::SourceManager& sourceManager,
@@ -29,3 +51,28 @@ std::string SwiftDiagnosticsConsumer::getDiagMessage(swift::SourceManager& sourc
29
51
swift::DiagnosticEngine::formatDiagnosticText (out, diagInfo.FormatString , diagInfo.FormatArgs );
30
52
return text.str ().str ();
31
53
}
54
+
55
+ void SwiftDiagnosticsConsumer::forwardToLog (swift::SourceManager& sourceManager,
56
+ const swift::DiagnosticInfo& diagInfo,
57
+ const std::string& message) {
58
+ auto [file, line, column] = DisplayLoc::from (sourceManager, diagInfo.Loc );
59
+ using Kind = swift::DiagnosticKind;
60
+ switch (diagInfo.Kind ) {
61
+ case Kind::Error:
62
+ LOG_ERROR (" {}:{}:{} {}" , file, line, column, message);
63
+ break ;
64
+ case Kind::Warning:
65
+ LOG_WARNING (" {}:{}:{} {}" , file, line, column, message);
66
+ break ;
67
+ case Kind::Remark:
68
+ LOG_INFO (" {}:{}:{} {}" , file, line, column, message);
69
+ break ;
70
+ case Kind::Note:
71
+ LOG_DEBUG (" {}:{}:{} {}" , file, line, column, message);
72
+ break ;
73
+ default :
74
+ LOG_ERROR (" unknown diagnostic kind {}, {}:{}:{} {}" , diagInfo.Kind , file, line, column,
75
+ message);
76
+ break ;
77
+ }
78
+ }
0 commit comments