Skip to content

Commit 2fc88d5

Browse files
committed
Swift: add module to Decl and remove forward traps
Add module context to all declarations. As keeping the `std::variant`s required slightly more work for this, and we intended to remove them any way, this is done in this change. For background, we put those in as we were not extracting all modules at the time, so we were missing some DB ids that we put back in by adding the "defining" traps not only in the trap file related to the defining module but also to the one where the declaration was used. Since then we fixed module extraction, so this should not be needed any more.
1 parent dbefe73 commit 2fc88d5

File tree

27 files changed

+248
-231
lines changed

27 files changed

+248
-231
lines changed

swift/codegen/schema.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ StructDecl:
193193

194194
Decl:
195195
_extends: AstNode
196+
module: ModuleDecl
196197

197198
Expr:
198199
_extends: AstNode
@@ -310,7 +311,7 @@ IfConfigClause:
310311
ImportDecl:
311312
_extends: Decl
312313
is_exported: predicate
313-
module: ModuleDecl
314+
imported_module: ModuleDecl
314315
declarations: ValueDecl*
315316

316317
MissingMemberDecl:

swift/extractor/visitors/DeclVisitor.cpp

Lines changed: 79 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -22,64 +22,58 @@ std::string constructName(const swift::DeclName& declName) {
2222
}
2323
} // namespace
2424

25-
std::variant<codeql::ConcreteFuncDecl, codeql::ConcreteFuncDeclsTrap>
26-
DeclVisitor::translateFuncDecl(const swift::FuncDecl& decl) {
27-
auto id = dispatcher_.assignNewLabel(decl, mangledName(decl));
28-
if (!dispatcher_.shouldEmitDeclBody(decl)) {
29-
return ConcreteFuncDeclsTrap{id};
25+
std::optional<codeql::ConcreteFuncDecl> DeclVisitor::translateFuncDecl(
26+
const swift::FuncDecl& decl) {
27+
if (auto entry = createNamedEntry(decl)) {
28+
fillAbstractFunctionDecl(decl, *entry);
29+
return entry;
3030
}
31-
ConcreteFuncDecl entry{id};
32-
fillAbstractFunctionDecl(decl, entry);
33-
return entry;
31+
return std::nullopt;
3432
}
3533

36-
std::variant<codeql::ConstructorDecl, codeql::ConstructorDeclsTrap>
37-
DeclVisitor::translateConstructorDecl(const swift::ConstructorDecl& decl) {
38-
auto id = dispatcher_.assignNewLabel(decl, mangledName(decl));
39-
if (!dispatcher_.shouldEmitDeclBody(decl)) {
40-
return ConstructorDeclsTrap{id};
34+
std::optional<codeql::ConstructorDecl> DeclVisitor::translateConstructorDecl(
35+
const swift::ConstructorDecl& decl) {
36+
if (auto entry = createNamedEntry(decl)) {
37+
fillAbstractFunctionDecl(decl, *entry);
38+
return entry;
4139
}
42-
ConstructorDecl entry{id};
43-
fillAbstractFunctionDecl(decl, entry);
44-
return entry;
40+
return std::nullopt;
4541
}
4642

47-
std::variant<codeql::DestructorDecl, codeql::DestructorDeclsTrap>
48-
DeclVisitor::translateDestructorDecl(const swift::DestructorDecl& decl) {
49-
auto id = dispatcher_.assignNewLabel(decl, mangledName(decl));
50-
if (!dispatcher_.shouldEmitDeclBody(decl)) {
51-
return DestructorDeclsTrap{id};
43+
std::optional<codeql::DestructorDecl> DeclVisitor::translateDestructorDecl(
44+
const swift::DestructorDecl& decl) {
45+
if (auto entry = createNamedEntry(decl)) {
46+
fillAbstractFunctionDecl(decl, *entry);
47+
return entry;
5248
}
53-
DestructorDecl entry{id};
54-
fillAbstractFunctionDecl(decl, entry);
55-
return entry;
49+
return std::nullopt;
5650
}
5751

5852
codeql::PrefixOperatorDecl DeclVisitor::translatePrefixOperatorDecl(
5953
const swift::PrefixOperatorDecl& decl) {
60-
PrefixOperatorDecl entry{dispatcher_.assignNewLabel(decl)};
54+
auto entry = createEntry(decl);
6155
fillOperatorDecl(decl, entry);
6256
return entry;
6357
}
6458

6559
codeql::PostfixOperatorDecl DeclVisitor::translatePostfixOperatorDecl(
6660
const swift::PostfixOperatorDecl& decl) {
67-
PostfixOperatorDecl entry{dispatcher_.assignNewLabel(decl)};
61+
auto entry = createEntry(decl);
6862
fillOperatorDecl(decl, entry);
6963
return entry;
7064
}
7165

7266
codeql::InfixOperatorDecl DeclVisitor::translateInfixOperatorDecl(
7367
const swift::InfixOperatorDecl& decl) {
74-
InfixOperatorDecl entry{dispatcher_.assignNewLabel(decl)};
68+
auto entry = createEntry(decl);
7569
entry.precedence_group = dispatcher_.fetchOptionalLabel(decl.getPrecedenceGroup());
7670
fillOperatorDecl(decl, entry);
7771
return entry;
7872
}
7973

8074
codeql::PrecedenceGroupDecl DeclVisitor::translatePrecedenceGroupDecl(
8175
const swift::PrecedenceGroupDecl& decl) {
82-
PrecedenceGroupDecl entry{dispatcher_.assignNewLabel(decl)};
76+
auto entry = createEntry(decl);
8377
return entry;
8478
}
8579

@@ -95,15 +89,15 @@ std::optional<codeql::ParamDecl> DeclVisitor::translateParamDecl(const swift::Pa
9589

9690
codeql::TopLevelCodeDecl DeclVisitor::translateTopLevelCodeDecl(
9791
const swift::TopLevelCodeDecl& decl) {
98-
TopLevelCodeDecl entry{dispatcher_.assignNewLabel(decl)};
92+
auto entry = createEntry(decl);
9993
assert(decl.getBody() && "Expect top level code to have body");
10094
entry.body = dispatcher_.fetchLabel(decl.getBody());
10195
return entry;
10296
}
10397

10498
codeql::PatternBindingDecl DeclVisitor::translatePatternBindingDecl(
10599
const swift::PatternBindingDecl& decl) {
106-
PatternBindingDecl entry{dispatcher_.assignNewLabel(decl)};
100+
auto entry = createEntry(decl);
107101
for (unsigned i = 0; i < decl.getNumPatternEntries(); ++i) {
108102
auto pattern = decl.getPattern(i);
109103
assert(pattern && "Expect pattern binding decl to have all patterns");
@@ -118,7 +112,7 @@ std::optional<codeql::ConcreteVarDecl> DeclVisitor::translateVarDecl(const swift
118112
// We do not deduplicate variables from non-swift (PCM, clang modules) modules as the mangler
119113
// crashes sometimes
120114
if (decl.getDeclContext()->isLocalContext() || decl.getModuleContext()->isNonSwiftModule()) {
121-
entry.emplace(dispatcher_.assignNewLabel(decl));
115+
entry = createEntry(decl);
122116
} else {
123117
entry = createNamedEntry(decl);
124118
if (!entry) {
@@ -130,123 +124,106 @@ std::optional<codeql::ConcreteVarDecl> DeclVisitor::translateVarDecl(const swift
130124
return entry;
131125
}
132126

133-
std::variant<codeql::StructDecl, codeql::StructDeclsTrap> DeclVisitor::translateStructDecl(
134-
const swift::StructDecl& decl) {
135-
auto id = dispatcher_.assignNewLabel(decl, mangledName(decl));
136-
if (!dispatcher_.shouldEmitDeclBody(decl)) {
137-
return StructDeclsTrap{id};
127+
std::optional<codeql::StructDecl> DeclVisitor::translateStructDecl(const swift::StructDecl& decl) {
128+
if (auto entry = createNamedEntry(decl)) {
129+
fillNominalTypeDecl(decl, *entry);
130+
return entry;
138131
}
139-
StructDecl entry{id};
140-
fillNominalTypeDecl(decl, entry);
141-
return entry;
132+
return std::nullopt;
142133
}
143134

144-
std::variant<codeql::ClassDecl, codeql::ClassDeclsTrap> DeclVisitor::translateClassDecl(
145-
const swift::ClassDecl& decl) {
146-
auto id = dispatcher_.assignNewLabel(decl, mangledName(decl));
147-
if (!dispatcher_.shouldEmitDeclBody(decl)) {
148-
return ClassDeclsTrap{id};
135+
std::optional<codeql::ClassDecl> DeclVisitor::translateClassDecl(const swift::ClassDecl& decl) {
136+
if (auto entry = createNamedEntry(decl)) {
137+
fillNominalTypeDecl(decl, *entry);
138+
return entry;
149139
}
150-
ClassDecl entry{id};
151-
fillNominalTypeDecl(decl, entry);
152-
return entry;
140+
return std::nullopt;
153141
}
154142

155-
std::variant<codeql::EnumDecl, codeql::EnumDeclsTrap> DeclVisitor::translateEnumDecl(
156-
const swift::EnumDecl& decl) {
157-
auto id = dispatcher_.assignNewLabel(decl, mangledName(decl));
158-
if (!dispatcher_.shouldEmitDeclBody(decl)) {
159-
return EnumDeclsTrap{id};
143+
std::optional<codeql::EnumDecl> DeclVisitor::translateEnumDecl(const swift::EnumDecl& decl) {
144+
if (auto entry = createNamedEntry(decl)) {
145+
fillNominalTypeDecl(decl, *entry);
146+
return entry;
160147
}
161-
EnumDecl entry{id};
162-
fillNominalTypeDecl(decl, entry);
163-
return entry;
148+
return std::nullopt;
164149
}
165150

166-
std::variant<codeql::ProtocolDecl, codeql::ProtocolDeclsTrap> DeclVisitor::translateProtocolDecl(
151+
std::optional<codeql::ProtocolDecl> DeclVisitor::translateProtocolDecl(
167152
const swift::ProtocolDecl& decl) {
168-
auto id = dispatcher_.assignNewLabel(decl, mangledName(decl));
169-
if (!dispatcher_.shouldEmitDeclBody(decl)) {
170-
return ProtocolDeclsTrap{id};
153+
if (auto entry = createNamedEntry(decl)) {
154+
fillNominalTypeDecl(decl, *entry);
155+
return entry;
171156
}
172-
ProtocolDecl entry{id};
173-
fillNominalTypeDecl(decl, entry);
174-
return entry;
157+
return std::nullopt;
175158
}
176159

177160
codeql::EnumCaseDecl DeclVisitor::translateEnumCaseDecl(const swift::EnumCaseDecl& decl) {
178-
EnumCaseDecl entry{dispatcher_.assignNewLabel(decl)};
161+
auto entry = createEntry(decl);
179162
entry.elements = dispatcher_.fetchRepeatedLabels(decl.getElements());
180163
return entry;
181164
}
182165

183-
std::variant<codeql::EnumElementDecl, codeql::EnumElementDeclsTrap>
184-
DeclVisitor::translateEnumElementDecl(const swift::EnumElementDecl& decl) {
185-
auto id = dispatcher_.assignNewLabel(decl, mangledName(decl));
186-
if (!dispatcher_.shouldEmitDeclBody(decl)) {
187-
return EnumElementDeclsTrap{id, decl.getNameStr().str()};
166+
std::optional<codeql::EnumElementDecl> DeclVisitor::translateEnumElementDecl(
167+
const swift::EnumElementDecl& decl) {
168+
auto entry = createNamedEntry(decl);
169+
if (!entry) {
170+
return std::nullopt;
188171
}
189-
EnumElementDecl entry{id};
190-
entry.name = decl.getNameStr().str();
172+
entry->name = decl.getNameStr().str();
191173
if (decl.hasParameterList()) {
192-
entry.params = dispatcher_.fetchRepeatedLabels(*decl.getParameterList());
174+
entry->params = dispatcher_.fetchRepeatedLabels(*decl.getParameterList());
193175
}
194-
fillValueDecl(decl, entry);
176+
fillValueDecl(decl, *entry);
195177
return entry;
196178
}
197179

198180
codeql::GenericTypeParamDecl DeclVisitor::translateGenericTypeParamDecl(
199181
const swift::GenericTypeParamDecl& decl) {
200182
// TODO: deduplicate
201-
GenericTypeParamDecl entry{dispatcher_.assignNewLabel(decl)};
183+
auto entry = createEntry(decl);
202184
fillTypeDecl(decl, entry);
203185
return entry;
204186
}
205187

206-
std::variant<codeql::AssociatedTypeDecl, codeql::AssociatedTypeDeclsTrap>
207-
DeclVisitor::translateAssociatedTypeDecl(const swift::AssociatedTypeDecl& decl) {
208-
auto id = dispatcher_.assignNewLabel(decl, mangledName(decl));
209-
if (!dispatcher_.shouldEmitDeclBody(decl)) {
210-
return AssociatedTypeDeclsTrap{id};
188+
std::optional<codeql::AssociatedTypeDecl> DeclVisitor::translateAssociatedTypeDecl(
189+
const swift::AssociatedTypeDecl& decl) {
190+
if (auto entry = createNamedEntry(decl)) {
191+
fillTypeDecl(decl, *entry);
192+
return entry;
211193
}
212-
AssociatedTypeDecl entry{id};
213-
fillTypeDecl(decl, entry);
214-
return entry;
194+
return std::nullopt;
215195
}
216196

217-
std::variant<codeql::TypeAliasDecl, codeql::TypeAliasDeclsTrap> DeclVisitor::translateTypeAliasDecl(
197+
std::optional<codeql::TypeAliasDecl> DeclVisitor::translateTypeAliasDecl(
218198
const swift::TypeAliasDecl& decl) {
219-
auto id = dispatcher_.assignNewLabel(decl, mangledName(decl));
220-
if (!dispatcher_.shouldEmitDeclBody(decl)) {
221-
return TypeAliasDeclsTrap{id};
199+
if (auto entry = createNamedEntry(decl)) {
200+
fillTypeDecl(decl, *entry);
201+
return entry;
222202
}
223-
TypeAliasDecl entry{id};
224-
fillTypeDecl(decl, entry);
225-
return entry;
203+
return std::nullopt;
226204
}
227205

228-
std::variant<codeql::AccessorDecl, codeql::AccessorDeclsTrap> DeclVisitor::translateAccessorDecl(
206+
std::optional<codeql::AccessorDecl> DeclVisitor::translateAccessorDecl(
229207
const swift::AccessorDecl& decl) {
230-
auto id = dispatcher_.assignNewLabel(decl, mangledName(decl));
231-
if (!dispatcher_.shouldEmitDeclBody(decl)) {
232-
return AccessorDeclsTrap{id};
208+
auto entry = createNamedEntry(decl);
209+
if (!entry) {
210+
return std::nullopt;
233211
}
234-
AccessorDecl entry{id};
235212
switch (decl.getAccessorKind()) {
236213
case swift::AccessorKind::Get:
237-
entry.is_getter = true;
214+
entry->is_getter = true;
238215
break;
239216
case swift::AccessorKind::Set:
240-
entry.is_setter = true;
217+
entry->is_setter = true;
241218
break;
242219
case swift::AccessorKind::WillSet:
243-
entry.is_will_set = true;
220+
entry->is_will_set = true;
244221
break;
245222
case swift::AccessorKind::DidSet:
246-
entry.is_did_set = true;
223+
entry->is_did_set = true;
247224
break;
248225
}
249-
fillAbstractFunctionDecl(decl, entry);
226+
fillAbstractFunctionDecl(decl, *entry);
250227
return entry;
251228
}
252229

@@ -265,17 +242,17 @@ std::optional<codeql::SubscriptDecl> DeclVisitor::translateSubscriptDecl(
265242
}
266243

267244
codeql::ExtensionDecl DeclVisitor::translateExtensionDecl(const swift::ExtensionDecl& decl) {
268-
ExtensionDecl entry{dispatcher_.assignNewLabel(decl)};
245+
auto entry = createEntry(decl);
269246
entry.extended_type_decl = dispatcher_.fetchLabel(decl.getExtendedNominal());
270247
fillGenericContext(decl, entry);
271248
fillIterableDeclContext(decl, entry);
272249
return entry;
273250
}
274251

275252
codeql::ImportDecl DeclVisitor::translateImportDecl(const swift::ImportDecl& decl) {
276-
auto entry = dispatcher_.createEntry(decl);
253+
auto entry = createEntry(decl);
277254
entry.is_exported = decl.isExported();
278-
entry.module = dispatcher_.fetchLabel(decl.getModule());
255+
entry.imported_module = dispatcher_.fetchLabel(decl.getModule());
279256
entry.declarations = dispatcher_.fetchRepeatedLabels(decl.getDecls());
280257
return entry;
281258
}
@@ -389,7 +366,7 @@ void DeclVisitor::fillAbstractStorageDecl(const swift::AbstractStorageDecl& decl
389366
}
390367

391368
codeql::IfConfigDecl DeclVisitor::translateIfConfigDecl(const swift::IfConfigDecl& decl) {
392-
auto entry = dispatcher_.createEntry(decl);
369+
auto entry = createEntry(decl);
393370
entry.clauses = dispatcher_.fetchRepeatedLabels(decl.getClauses());
394371
return entry;
395372
}

0 commit comments

Comments
 (0)