Skip to content

Commit 73ad130

Browse files
committed
Swift: extract PoundDiagnosticDecl
1 parent c95a6ea commit 73ad130

File tree

12 files changed

+92
-8
lines changed

12 files changed

+92
-8
lines changed

swift/extractor/translators/DeclTranslator.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,23 @@ std::optional<codeql::OpaqueTypeDecl> DeclTranslator::translateOpaqueTypeDecl(
401401
return std::nullopt;
402402
}
403403

404+
static int translateDiagnosticsKind(swift::DiagnosticKind kind) {
405+
switch (kind) {
406+
case swift::DiagnosticKind::Error:
407+
return 1;
408+
case swift::DiagnosticKind::Warning:
409+
return 2;
410+
default:
411+
return 0;
412+
}
413+
}
414+
415+
codeql::PoundDiagnosticDecl DeclTranslator::translatePoundDiagnosticDecl(
416+
const swift::PoundDiagnosticDecl& decl) {
417+
auto entry = createEntry(decl);
418+
entry.kind = translateDiagnosticsKind(decl.getKind());
419+
entry.message = dispatcher.fetchLabel(decl.getMessage());
420+
return entry;
421+
}
422+
404423
} // namespace codeql

swift/extractor/translators/DeclTranslator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class DeclTranslator : public AstTranslatorBase<DeclTranslator> {
4646
std::optional<codeql::ModuleDecl> translateModuleDecl(const swift::ModuleDecl& decl);
4747
codeql::IfConfigDecl translateIfConfigDecl(const swift::IfConfigDecl& decl);
4848
std::optional<codeql::OpaqueTypeDecl> translateOpaqueTypeDecl(const swift::OpaqueTypeDecl& decl);
49+
codeql::PoundDiagnosticDecl translatePoundDiagnosticDecl(const swift::PoundDiagnosticDecl& decl);
4950

5051
private:
5152
std::string mangledName(const swift::ValueDecl& decl);

swift/ql/lib/codeql/swift/elements/decl/PoundDiagnosticDecl.qll

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ private import codeql.swift.generated.decl.PoundDiagnosticDecl
22

33
class PoundDiagnosticDecl extends Generated::PoundDiagnosticDecl {
44
override string toString() {
5-
result = "#..." // TODO: Once we extract whether this is an error or a warning we can improve this.
5+
this.isError() and result = "#error(...)"
6+
or
7+
this.isWarning() and result = "#warning(...)"
68
}
9+
10+
predicate isError() { this.getKind() = 1 }
11+
12+
predicate isWarning() { this.getKind() = 2 }
713
}

swift/ql/lib/codeql/swift/generated/ParentChild.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,14 +358,17 @@ private module Impl {
358358
private Element getImmediateChildOfPoundDiagnosticDecl(
359359
PoundDiagnosticDecl e, int index, string partialPredicateCall
360360
) {
361-
exists(int b, int bDecl, int n |
361+
exists(int b, int bDecl, int n, int nMessage |
362362
b = 0 and
363363
bDecl = b + 1 + max(int i | i = -1 or exists(getImmediateChildOfDecl(e, i, _)) | i) and
364364
n = bDecl and
365+
nMessage = n + 1 and
365366
(
366367
none()
367368
or
368369
result = getImmediateChildOfDecl(e, index - b, partialPredicateCall)
370+
or
371+
index = n and result = e.getImmediateMessage() and partialPredicateCall = "Message()"
369372
)
370373
)
371374
}

swift/ql/lib/codeql/swift/generated/Raw.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ module Raw {
123123

124124
class PoundDiagnosticDecl extends @pound_diagnostic_decl, Decl {
125125
override string toString() { result = "PoundDiagnosticDecl" }
126+
127+
int getKind() { pound_diagnostic_decls(this, result, _) }
128+
129+
StringLiteralExpr getMessage() { pound_diagnostic_decls(this, _, result) }
126130
}
127131

128132
class PrecedenceGroupDecl extends @precedence_group_decl, Decl {

swift/ql/lib/codeql/swift/generated/decl/PoundDiagnosticDecl.qll

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,38 @@
22
private import codeql.swift.generated.Synth
33
private import codeql.swift.generated.Raw
44
import codeql.swift.elements.decl.Decl
5+
import codeql.swift.elements.expr.StringLiteralExpr
56

67
module Generated {
8+
/**
9+
* A diagnostic directive, which is either `#error` or `#warning`.
10+
*/
711
class PoundDiagnosticDecl extends Synth::TPoundDiagnosticDecl, Decl {
812
override string getAPrimaryQlClass() { result = "PoundDiagnosticDecl" }
13+
14+
/**
15+
* Gets the This is 1 for `#error` and 2 for `#warning`.
16+
*/
17+
int getKind() {
18+
result = Synth::convertPoundDiagnosticDeclToRaw(this).(Raw::PoundDiagnosticDecl).getKind()
19+
}
20+
21+
/**
22+
* Gets the message of this pound diagnostic declaration.
23+
*
24+
* This includes nodes from the "hidden" AST. It can be overridden in subclasses to change the
25+
* behavior of both the `Immediate` and non-`Immediate` versions.
26+
*/
27+
StringLiteralExpr getImmediateMessage() {
28+
result =
29+
Synth::convertStringLiteralExprFromRaw(Synth::convertPoundDiagnosticDeclToRaw(this)
30+
.(Raw::PoundDiagnosticDecl)
31+
.getMessage())
32+
}
33+
34+
/**
35+
* Gets the message of this pound diagnostic declaration.
36+
*/
37+
final StringLiteralExpr getMessage() { result = getImmediateMessage().resolve() }
938
}
1039
}

swift/ql/lib/swift.dbscheme

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ pattern_binding_decl_patterns( //dir=decl
281281
);
282282

283283
pound_diagnostic_decls( //dir=decl
284-
unique int id: @pound_diagnostic_decl
284+
unique int id: @pound_diagnostic_decl,
285+
int kind: int ref,
286+
int message: @string_literal_expr_or_none ref
285287
);
286288

287289
precedence_group_decls( //dir=decl
@@ -2443,6 +2445,11 @@ variadic_sequence_types( //dir=type
24432445
| @unspecified_element
24442446
;
24452447

2448+
@string_literal_expr_or_none =
2449+
@string_literal_expr
2450+
| @unspecified_element
2451+
;
2452+
24462453
@tap_expr_or_none =
24472454
@tap_expr
24482455
| @unspecified_element

swift/ql/test/extractor-tests/generated/decl/PoundDiagnosticDecl/MISSING_SOURCE.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| diagnostics.swift:2:1:2:25 | #warning(...) | getModule: | file://:0:0:0:0 | diagnostics | getKind: | 2 | getMessage: | diagnostics.swift:2:10:2:10 | I'm a warning |
2+
| diagnostics.swift:3:1:3:26 | #error(...) | getModule: | file://:0:0:0:0 | diagnostics | getKind: | 1 | getMessage: | diagnostics.swift:3:8:3:8 | And I'm an error |
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// generated by codegen/codegen.py
2+
import codeql.swift.elements
3+
import TestUtils
4+
5+
from PoundDiagnosticDecl x, ModuleDecl getModule, int getKind, StringLiteralExpr getMessage
6+
where
7+
toBeTested(x) and
8+
not x.isUnknown() and
9+
getModule = x.getModule() and
10+
getKind = x.getKind() and
11+
getMessage = x.getMessage()
12+
select x, "getModule:", getModule, "getKind:", getKind, "getMessage:", getMessage

0 commit comments

Comments
 (0)