Skip to content

Commit 610c788

Browse files
authored
Merge pull request #10156 from github/redsun82/swift-import-optional-module
Swift: make `ImportDecl:imported_module` optional
2 parents 436fe65 + 2ee8d1a commit 610c788

File tree

10 files changed

+36
-15
lines changed

10 files changed

+36
-15
lines changed

swift/codegen/schema.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ IfConfigClause:
312312
ImportDecl:
313313
_extends: Decl
314314
is_exported: predicate
315-
imported_module: ModuleDecl
315+
imported_module: ModuleDecl? # may be none in inactive #if clauses
316316
declarations: ValueDecl*
317317

318318
MissingMemberDecl:

swift/extractor/visitors/DeclVisitor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ codeql::ExtensionDecl DeclVisitor::translateExtensionDecl(const swift::Extension
253253
codeql::ImportDecl DeclVisitor::translateImportDecl(const swift::ImportDecl& decl) {
254254
auto entry = createEntry(decl);
255255
entry.is_exported = decl.isExported();
256-
entry.imported_module = dispatcher_.fetchLabel(decl.getModule());
256+
entry.imported_module = dispatcher_.fetchOptionalLabel(decl.getModule());
257257
entry.declarations = dispatcher_.fetchRepeatedLabels(decl.getDecls());
258258
return entry;
259259
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ module Raw {
609609

610610
predicate isExported() { import_decl_is_exported(this) }
611611

612-
ModuleDecl getImportedModule() { import_decls(this, result) }
612+
ModuleDecl getImportedModule() { import_decl_imported_modules(this, result) }
613613

614614
ValueDecl getDeclaration(int index) { import_decl_declarations(this, index, result) }
615615
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class ImportDeclBase extends Synth::TImportDecl, Decl {
1919

2020
final ModuleDecl getImportedModule() { result = getImmediateImportedModule().resolve() }
2121

22+
final predicate hasImportedModule() { exists(getImportedModule()) }
23+
2224
ValueDecl getImmediateDeclaration(int index) {
2325
result =
2426
Synth::convertValueDeclFromRaw(Synth::convertImportDeclToRaw(this)

swift/ql/lib/swift.dbscheme

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,15 +709,20 @@ if_config_clause_is_active( //dir=decl
709709
);
710710

711711
import_decls( //dir=decl
712-
unique int id: @import_decl,
713-
int imported_module: @module_decl ref
712+
unique int id: @import_decl
714713
);
715714

716715
#keyset[id]
717716
import_decl_is_exported( //dir=decl
718717
int id: @import_decl ref
719718
);
720719

720+
#keyset[id]
721+
import_decl_imported_modules( //dir=decl
722+
int id: @import_decl ref,
723+
int imported_module: @module_decl ref
724+
);
725+
721726
#keyset[id, index]
722727
import_decl_declarations( //dir=decl
723728
int id: @import_decl ref,
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
| import.swift:1:1:1:8 | import ... | getModule: | file://:0:0:0:0 | import | isExported: | no | getImportedModule: | file://:0:0:0:0 | Swift |
2-
| import.swift:2:1:2:24 | import ... | getModule: | file://:0:0:0:0 | import | isExported: | no | getImportedModule: | file://:0:0:0:0 | Swift |
3-
| import.swift:3:12:3:32 | import ... | getModule: | file://:0:0:0:0 | import | isExported: | yes | getImportedModule: | file://:0:0:0:0 | Swift |
4-
| import.swift:4:1:4:19 | import ... | getModule: | file://:0:0:0:0 | import | isExported: | no | getImportedModule: | file://:0:0:0:0 | Swift |
5-
| import.swift:5:1:5:23 | import ... | getModule: | file://:0:0:0:0 | import | isExported: | no | getImportedModule: | file://:0:0:0:0 | Swift |
1+
| import.swift:1:1:1:8 | import ... | getModule: | file://:0:0:0:0 | import | isExported: | no |
2+
| import.swift:2:1:2:24 | import ... | getModule: | file://:0:0:0:0 | import | isExported: | no |
3+
| import.swift:3:12:3:32 | import ... | getModule: | file://:0:0:0:0 | import | isExported: | yes |
4+
| import.swift:4:1:4:19 | import ... | getModule: | file://:0:0:0:0 | import | isExported: | no |
5+
| import.swift:5:1:5:23 | import ... | getModule: | file://:0:0:0:0 | import | isExported: | no |
6+
| import.swift:7:1:7:8 | import ... | getModule: | file://:0:0:0:0 | import | isExported: | no |

swift/ql/test/extractor-tests/generated/decl/ImportDecl/ImportDecl.ql

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
import codeql.swift.elements
33
import TestUtils
44

5-
from ImportDecl x, ModuleDecl getModule, string isExported, ModuleDecl getImportedModule
5+
from ImportDecl x, ModuleDecl getModule, string isExported
66
where
77
toBeTested(x) and
88
not x.isUnknown() and
99
getModule = x.getModule() and
10-
(if x.isExported() then isExported = "yes" else isExported = "no") and
11-
getImportedModule = x.getImportedModule()
12-
select x, "getModule:", getModule, "isExported:", isExported, "getImportedModule:",
13-
getImportedModule
10+
if x.isExported() then isExported = "yes" else isExported = "no"
11+
select x, "getModule:", getModule, "isExported:", isExported
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| import.swift:1:1:1:8 | import ... | file://:0:0:0:0 | Swift |
2+
| import.swift:2:1:2:24 | import ... | file://:0:0:0:0 | Swift |
3+
| import.swift:3:12:3:32 | import ... | file://:0:0:0:0 | Swift |
4+
| import.swift:4:1:4:19 | import ... | file://:0:0:0:0 | Swift |
5+
| import.swift:5:1:5:23 | import ... | file://:0:0:0:0 | Swift |
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// generated by codegen/codegen.py
2+
import codeql.swift.elements
3+
import TestUtils
4+
5+
from ImportDecl x
6+
where toBeTested(x) and not x.isUnknown()
7+
select x, x.getImportedModule()

swift/ql/test/extractor-tests/generated/decl/ImportDecl/import.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ import typealias Swift.Int
33
@_exported import struct Swift.Double
44
import func Swift.print // imports all overloads
55
import protocol Swift.Hashable
6+
#if foo
7+
import Foo
8+
#endif

0 commit comments

Comments
 (0)