Skip to content

Commit 3940643

Browse files
committed
Swift: extract IfConfigDecl
This also adds `UnresolvedDeclRefExpr` tests, as `IfConfigDecl` consistently introduces those.
1 parent 7d5dd38 commit 3940643

34 files changed

+293
-9
lines changed

swift/codegen/schema.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,16 @@ EnumCaseDecl:
270270

271271
IfConfigDecl:
272272
_extends: Decl
273+
_children:
274+
clauses: IfConfigClause*
275+
276+
IfConfigClause:
277+
_extends: Locatable
278+
_children:
279+
condition: Expr?
280+
elements: AstNode*
281+
is_active: predicate
282+
_dir: decl
273283

274284
ImportDecl:
275285
_extends: Decl
@@ -545,7 +555,6 @@ TypeExpr:
545555
UnresolvedDeclRefExpr:
546556
_extends: Expr
547557
name: string?
548-
_pragma: qltest_skip # we should really never extract these
549558

550559
UnresolvedDotExpr:
551560
_extends: Expr

swift/extractor/infra/SwiftDispatcher.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class SwiftDispatcher {
7878
waitingForNewLabel = e;
7979
visit(e);
8080
if (auto l = store.get(e)) {
81-
if constexpr (!std::is_base_of_v<swift::TypeBase, E>) {
81+
if constexpr (std::is_base_of_v<LocatableTag, TrapTagOf<E>>) {
8282
attachLocation(e, *l);
8383
}
8484
return *l;
@@ -95,6 +95,10 @@ class SwiftDispatcher {
9595
return fetchLabelFromUnion<AstNodeTag>(node);
9696
}
9797

98+
TrapLabel<IfConfigClauseTag> fetchLabel(const swift::IfConfigClause& clause) {
99+
return fetchLabel(&clause);
100+
}
101+
98102
// Due to the lazy emission approach, we must assign a label to a corresponding AST node before
99103
// it actually gets emitted to handle recursive cases such as recursive calls, or recursive type
100104
// declarations
@@ -143,6 +147,15 @@ class SwiftDispatcher {
143147
attachLocation(locatable->getStartLoc(), locatable->getEndLoc(), locatableLabel);
144148
}
145149

150+
void attachLocation(const swift::IfConfigClause* clause, TrapLabel<LocatableTag> locatableLabel) {
151+
attachLocation(clause->Loc, clause->Loc, locatableLabel);
152+
}
153+
154+
// Emits a Location TRAP entry and attaches it to a `Locatable` trap label for a given `SourceLoc`
155+
void attachLocation(swift::SourceLoc loc, TrapLabel<LocatableTag> locatableLabel) {
156+
attachLocation(loc, loc, locatableLabel);
157+
}
158+
146159
// Emits a Location TRAP entry for a list of swift entities and attaches it to a `Locatable` trap
147160
// label
148161
template <typename Locatable>
@@ -217,7 +230,8 @@ class SwiftDispatcher {
217230
swift::Expr,
218231
swift::Pattern,
219232
swift::TypeRepr,
220-
swift::TypeBase>;
233+
swift::TypeBase,
234+
swift::IfConfigClause>;
221235

222236
void attachLocation(swift::SourceLoc start,
223237
swift::SourceLoc end,
@@ -274,6 +288,7 @@ class SwiftDispatcher {
274288
// TODO: The following methods are supposed to redirect TRAP emission to correpsonding visitors,
275289
// which are to be introduced in follow-up PRs
276290
virtual void visit(swift::Decl* decl) = 0;
291+
virtual void visit(const swift::IfConfigClause* clause) = 0;
277292
virtual void visit(swift::Stmt* stmt) = 0;
278293
virtual void visit(swift::StmtCondition* cond) = 0;
279294
virtual void visit(swift::CaseLabelItem* item) = 0;

swift/extractor/infra/SwiftTagTraits.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ MAP_TAG(Expr);
4747
#include <swift/AST/ExprNodes.def>
4848

4949
MAP_TAG(Decl);
50+
MAP_TAG(IfConfigClause);
5051
#define ABSTRACT_DECL(CLASS, PARENT) MAP_SUBTAG(CLASS##Decl, PARENT)
5152
#define DECL(CLASS, PARENT) ABSTRACT_DECL(CLASS, PARENT)
5253
#include <swift/AST/DeclNodes.def>

swift/extractor/visitors/DeclVisitor.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,18 @@ void DeclVisitor::fillAbstractStorageDecl(const swift::AbstractStorageDecl& decl
358358
fillValueDecl(decl, entry);
359359
}
360360

361+
codeql::IfConfigDecl DeclVisitor::translateIfConfigDecl(const swift::IfConfigDecl& decl) {
362+
auto entry = dispatcher_.createEntry(decl);
363+
entry.clauses = dispatcher_.fetchRepeatedLabels(decl.getClauses());
364+
return entry;
365+
}
366+
367+
codeql::IfConfigClause DeclVisitor::translateIfConfigClause(const swift::IfConfigClause& clause) {
368+
auto entry = dispatcher_.createEntry(clause);
369+
entry.condition = dispatcher_.fetchOptionalLabel(clause.Cond);
370+
entry.elements = dispatcher_.fetchRepeatedLabels(clause.Elements);
371+
entry.is_active = clause.isActive;
372+
return entry;
373+
}
374+
361375
} // namespace codeql

swift/extractor/visitors/DeclVisitor.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ namespace codeql {
1414
class DeclVisitor : public AstVisitorBase<DeclVisitor> {
1515
public:
1616
using AstVisitorBase<DeclVisitor>::AstVisitorBase;
17+
using AstVisitorBase<DeclVisitor>::visit;
18+
19+
void visit(const swift::IfConfigClause* clause) {
20+
dispatcher_.emit(translateIfConfigClause(*clause));
21+
}
1722

1823
std::variant<codeql::ConcreteFuncDecl, codeql::ConcreteFuncDeclsTrap> translateFuncDecl(
1924
const swift::FuncDecl& decl);
@@ -52,6 +57,8 @@ class DeclVisitor : public AstVisitorBase<DeclVisitor> {
5257
codeql::ExtensionDecl translateExtensionDecl(const swift::ExtensionDecl& decl);
5358
codeql::ImportDecl translateImportDecl(const swift::ImportDecl& decl);
5459
std::optional<codeql::ModuleDecl> translateModuleDecl(const swift::ModuleDecl& decl);
60+
codeql::IfConfigDecl translateIfConfigDecl(const swift::IfConfigDecl& decl);
61+
codeql::IfConfigClause translateIfConfigClause(const swift::IfConfigClause& clause);
5562

5663
private:
5764
std::string mangledName(const swift::ValueDecl& decl);

swift/extractor/visitors/SwiftVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class SwiftVisitor : private SwiftDispatcher {
2121

2222
private:
2323
void visit(swift::Decl* decl) override { declVisitor.visit(decl); }
24+
void visit(const swift::IfConfigClause* clause) override { declVisitor.visit(clause); }
2425
void visit(swift::Stmt* stmt) override { stmtVisitor.visit(stmt); }
2526
void visit(swift::StmtCondition* cond) override { stmtVisitor.visitStmtCondition(cond); }
2627
void visit(swift::CaseLabelItem* item) override { stmtVisitor.visitCaseLabelItem(item); }

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import codeql.swift.elements.decl.FuncDecl
2424
import codeql.swift.elements.decl.GenericContext
2525
import codeql.swift.elements.decl.GenericTypeDecl
2626
import codeql.swift.elements.decl.GenericTypeParamDecl
27+
import codeql.swift.elements.decl.IfConfigClause
2728
import codeql.swift.elements.decl.IfConfigDecl
2829
import codeql.swift.elements.decl.ImportDecl
2930
import codeql.swift.elements.decl.InfixOperatorDecl
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// generated by codegen/codegen.py, remove this comment if you wish to edit this file
2+
private import codeql.swift.generated.decl.IfConfigClause
3+
4+
class IfConfigClause extends IfConfigClauseBase { }
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
// generated by codegen/codegen.py, remove this comment if you wish to edit this file
21
private import codeql.swift.generated.expr.UnresolvedDeclRefExpr
32

4-
class UnresolvedDeclRefExpr extends UnresolvedDeclRefExprBase { }
3+
class UnresolvedDeclRefExpr extends UnresolvedDeclRefExprBase {
4+
override string toString() {
5+
result = getName() + " (unresolved)"
6+
or
7+
not hasName() and result = "(unresolved)"
8+
}
9+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ Element getAnImmediateChild(Element e) {
2828
or
2929
enum_element_decl_params(e, _, x)
3030
or
31+
if_config_clause_conditions(e, x)
32+
or
33+
if_config_clause_elements(e, _, x)
34+
or
35+
if_config_decl_clauses(e, _, x)
36+
or
3137
pattern_binding_decl_inits(e, _, x)
3238
or
3339
pattern_binding_decl_patterns(e, _, x)

0 commit comments

Comments
 (0)