Skip to content

Commit f9840d9

Browse files
authored
Merge pull request #22705 from hvitved/unified/more-to-string
Unified: Implement `{Constructor,Function,Variable}Declaration.toString`
2 parents 8a95291 + 1090824 commit f9840d9

6 files changed

Lines changed: 235 additions & 152 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
overlay[local]
2+
module;
3+
4+
private import unified
5+
private import codeql.util.Unit
6+
7+
private module Plugins {
8+
private import AstPluginSwift
9+
}
10+
11+
class AstPlugin extends Unit {
12+
bindingset[f]
13+
string getFunctionDeclarationKeyword(FunctionDeclaration f) { none() }
14+
15+
bindingset[c]
16+
string getConstructorDeclarationKeyword(ConstructorDeclaration c) { none() }
17+
18+
bindingset[cls]
19+
string getClassLikeDeclarationKeyword(ClassLikeDeclaration cls) { none() }
20+
21+
bindingset[decl]
22+
string getVariableDeclarationKeyword(VariableDeclaration decl) { none() }
23+
}
24+
25+
bindingset[f]
26+
string getFunctionDeclarationKeyword(FunctionDeclaration f) {
27+
result = any(AstPlugin p).getFunctionDeclarationKeyword(f)
28+
}
29+
30+
bindingset[c]
31+
string getConstructorDeclarationKeyword(ConstructorDeclaration c) {
32+
result = any(AstPlugin p).getConstructorDeclarationKeyword(c)
33+
}
34+
35+
bindingset[cls]
36+
string getClassLikeDeclarationKeyword(ClassLikeDeclaration cls) {
37+
result = any(AstPlugin p).getClassLikeDeclarationKeyword(cls)
38+
}
39+
40+
bindingset[decl]
41+
string getVariableDeclarationKeyword(VariableDeclaration decl) {
42+
result = any(AstPlugin p).getVariableDeclarationKeyword(decl)
43+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
overlay[local]
2+
module;
3+
4+
private import unified
5+
private import AstPlugin
6+
7+
private class AstPluginSwift extends AstPlugin {
8+
bindingset[f]
9+
override string getFunctionDeclarationKeyword(FunctionDeclaration f) {
10+
exists(f) and result = "func"
11+
}
12+
13+
override string getClassLikeDeclarationKeyword(ClassLikeDeclaration cls) {
14+
cls.hasModifier(result) and
15+
result in ["class", "struct", "enum", "actor", "extension", "protocol"]
16+
}
17+
18+
override string getVariableDeclarationKeyword(VariableDeclaration decl) {
19+
decl.hasModifier(result) and
20+
result in ["var", "let"]
21+
}
22+
}

‎unified/ql/lib/codeql/unified/internal/FacadeAst.qll‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
overlay[local?]
55
module;
66

7+
private import AstPlugin
78
private import codeql.files.FileSystem
89
private import codeql.unified.internal.NameBinding as NameBinding
910

@@ -153,17 +154,18 @@ module Unified {
153154
NameBinding::getStaticBindingTargetFromRef(this.getABaseType().getType())
154155
}
155156

156-
private string getKind() {
157-
this.hasModifier(result) and
158-
result in ["class", "struct", "interface", "enum", "actor", "extension", "protocol"]
157+
override string toString() {
158+
result = concat(getClassLikeDeclarationKeyword(this) + " ") + concat(this.getName())
159159
}
160-
161-
override string toString() { result = concat(this.getKind() + " ") + concat(this.getName()) }
162160
}
163161

164162
class ConstructorDeclaration extends G::ConstructorDeclaration {
165163
/** Gets the name of this constructor. */
166164
string getName() { result = this.getNameNode().getValue() }
165+
166+
override string toString() {
167+
result = concat(getConstructorDeclarationKeyword(this) + " ") + concat(this.getName())
168+
}
167169
}
168170

169171
class ContinueExpr extends G::ContinueExpr {
@@ -174,6 +176,22 @@ module Unified {
174176
class FunctionDeclaration extends G::FunctionDeclaration {
175177
/** Gets the name of this function. */
176178
string getName() { result = this.getNameNode().getValue() }
179+
180+
override string toString() {
181+
result = concat(getFunctionDeclarationKeyword(this) + " ") + concat(this.getName())
182+
}
183+
}
184+
185+
class VariableDeclaration extends G::VariableDeclaration {
186+
/** Gets the name node of this variable declaration, if any. */
187+
Identifier getNameNode() { result = this.getPattern() }
188+
189+
/** Gets the name of the variable being declared, if any. */
190+
string getName() { result = this.getNameNode().getValue() }
191+
192+
override string toString() {
193+
result = concat(getVariableDeclarationKeyword(this) + " ") + concat(this.getName())
194+
}
177195
}
178196

179197
class LabeledStmt extends G::LabeledStmt {

0 commit comments

Comments
 (0)