Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 7af168f

Browse files
authored
Merge pull request #179 from smowton/smowton/feature/printast
Add barebones PrintAST for Go
2 parents 2282def + 1a823b2 commit 7af168f

File tree

12 files changed

+1273
-9
lines changed

12 files changed

+1273
-9
lines changed

ql/src/semmle/go/AST.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ class AstNode extends @node, Locatable {
4545
/** Gets the innermost function definition to which this AST node belongs, if any. */
4646
FuncDef getEnclosingFunction() { result = getParent().parentInSameFunction*() }
4747

48+
/**
49+
* Describes important CodeQL classes for this node. May
50+
* return multiple values.
51+
*/
52+
string describeQlClass() { result = "???" }
53+
4854
override string toString() { result = "AST node" }
4955
}
5056

ql/src/semmle/go/Comments.qll

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class Comment extends @comment, AstNode {
2727
CommentGroup getGroup() { this = result.getAComment() }
2828

2929
override string toString() { result = "comment" }
30+
31+
override string describeQlClass() { result = "Comment" }
3032
}
3133

3234
/**
@@ -59,6 +61,8 @@ class CommentGroup extends @comment_group, AstNode {
5961
int getNumComment() { result = count(getAComment()) }
6062

6163
override string toString() { result = "comment group" }
64+
65+
override string describeQlClass() { result = "CommentGroup" }
6266
}
6367

6468
/**
@@ -109,6 +113,8 @@ class DocComment extends CommentGroup {
109113

110114
/** Gets the program element documented by this comment group. */
111115
Documentable getDocumentedElement() { result = node }
116+
117+
override string describeQlClass() { result = "DocComment" }
112118
}
113119

114120
/**
@@ -120,7 +126,9 @@ class DocComment extends CommentGroup {
120126
* // Single line comment
121127
* ```
122128
*/
123-
class SlashSlashComment extends @slashslashcomment, Comment { }
129+
class SlashSlashComment extends @slashslashcomment, Comment {
130+
override string describeQlClass() { result = "SlashSlashComment" }
131+
}
124132

125133
/**
126134
* A block comment starting with `/*` and ending with <code>*&#47;</code>.
@@ -132,7 +140,9 @@ class SlashSlashComment extends @slashslashcomment, Comment { }
132140
* comment *&#47
133141
* </pre>
134142
*/
135-
class SlashStarComment extends @slashstarcomment, Comment { }
143+
class SlashStarComment extends @slashstarcomment, Comment {
144+
override string describeQlClass() { result = "SlashStarComment" }
145+
}
136146

137147
/**
138148
* A single-line comment starting with `//`.
@@ -194,4 +204,6 @@ class BuildConstraintComment extends LineComment {
194204
// comment text starts with `+build`
195205
getText().regexpMatch("\\s*\\+build.*")
196206
}
207+
208+
override string describeQlClass() { result = "BuildConstraintComment" }
197209
}

ql/src/semmle/go/Decls.qll

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class Decl extends @decl, ExprParent, StmtParent {
3030
*/
3131
class BadDecl extends @baddecl, Decl {
3232
override string toString() { result = "bad declaration" }
33+
34+
override string describeQlClass() { result = "BadDecl" }
3335
}
3436

3537
/**
@@ -46,34 +48,44 @@ class GenDecl extends @gendecl, Decl, Documentable {
4648
int getNumSpec() { result = count(getASpec()) }
4749

4850
override predicate mayHaveSideEffects() { getASpec().mayHaveSideEffects() }
51+
52+
override string describeQlClass() { result = "GenDecl" }
4953
}
5054

5155
/**
5256
* An import declaration.
5357
*/
5458
class ImportDecl extends @importdecl, GenDecl {
5559
override string toString() { result = "import declaration" }
60+
61+
override string describeQlClass() { result = "ImportDecl" }
5662
}
5763

5864
/**
5965
* A constant declaration.
6066
*/
6167
class ConstDecl extends @constdecl, GenDecl {
6268
override string toString() { result = "constant declaration" }
69+
70+
override string describeQlClass() { result = "ConstDecl" }
6371
}
6472

6573
/**
6674
* A type declaration.
6775
*/
6876
class TypeDecl extends @typedecl, GenDecl {
6977
override string toString() { result = "type declaration" }
78+
79+
override string describeQlClass() { result = "TypeDecl" }
7080
}
7181

7282
/**
7383
* A variable declaration.
7484
*/
7585
class VarDecl extends @vardecl, GenDecl {
7686
override string toString() { result = "variable declaration" }
87+
88+
override string describeQlClass() { result = "VarDecl" }
7789
}
7890

7991
/**
@@ -133,6 +145,8 @@ class FuncDef extends @funcdef, StmtParent, ExprParent {
133145
* Gets a call to this function.
134146
*/
135147
DataFlow::CallNode getACall() { result.getACallee() = this }
148+
149+
override string describeQlClass() { result = "FuncDef" }
136150
}
137151

138152
/**
@@ -155,6 +169,8 @@ class FuncDecl extends @funcdecl, Decl, Documentable, FuncDef {
155169
DeclaredFunction getFunction() { this = result.getFuncDecl() }
156170

157171
override string toString() { result = "function declaration" }
172+
173+
override string describeQlClass() { result = "FuncDecl" }
158174
}
159175

160176
/**
@@ -219,6 +235,8 @@ class MethodDecl extends FuncDecl {
219235
* is the variable `p`.
220236
*/
221237
ReceiverVariable getReceiver() { result.getFunction() = this }
238+
239+
override string describeQlClass() { result = "MethodDecl" }
222240
}
223241

224242
/**
@@ -243,6 +261,8 @@ class Spec extends @spec, ExprParent, Documentable {
243261
* Memory allocation is not considered an observable side effect.
244262
*/
245263
predicate mayHaveSideEffects() { none() }
264+
265+
override string describeQlClass() { result = "Spec" }
246266
}
247267

248268
/**
@@ -262,6 +282,8 @@ class ImportSpec extends @importspec, Spec {
262282
string getPath() { result = getPathExpr().getValue() }
263283

264284
override string toString() { result = "import specifier" }
285+
286+
override string describeQlClass() { result = "ImportSpec" }
265287
}
266288

267289
/**
@@ -344,6 +366,8 @@ class ValueSpec extends @valuespec, Spec {
344366
override predicate mayHaveSideEffects() { getAnInit().mayHaveSideEffects() }
345367

346368
override string toString() { result = "value declaration specifier" }
369+
370+
override string describeQlClass() { result = "ValueSpec" }
347371
}
348372

349373
/**
@@ -360,6 +384,8 @@ class TypeSpec extends @typespec, Spec {
360384
Expr getTypeExpr() { result = getChildExpr(1) }
361385

362386
override string toString() { result = "type declaration specifier" }
387+
388+
override string describeQlClass() { result = "TypeSpec" }
363389
}
364390

365391
/**
@@ -397,13 +423,17 @@ class FieldDecl extends @field, Documentable, ExprParent {
397423
StructType getDeclaringType() { result = getDeclaringStructTypeExpr().getType() }
398424

399425
override string toString() { result = "field declaration" }
426+
427+
override string describeQlClass() { result = "FieldDecl" }
400428
}
401429

402430
/**
403431
* An embedded field declaration in a struct.
404432
*/
405433
class EmbeddedFieldDecl extends FieldDecl {
406434
EmbeddedFieldDecl() { not exists(this.getNameExpr(_)) }
435+
436+
override string describeQlClass() { result = "EmbeddedFieldDecl" }
407437
}
408438

409439
/**
@@ -453,6 +483,8 @@ class ParameterDecl extends @field, Documentable, ExprParent {
453483
}
454484

455485
override string toString() { result = "parameter declaration" }
486+
487+
override string describeQlClass() { result = "ParameterDecl" }
456488
}
457489

458490
/**
@@ -482,6 +514,8 @@ class ReceiverDecl extends @field, Documentable, ExprParent {
482514
Expr getNameExpr() { result = getChildExpr(1) }
483515

484516
override string toString() { result = "receiver declaration" }
517+
518+
override string describeQlClass() { result = "ReceiverDecl" }
485519
}
486520

487521
/**
@@ -531,6 +565,8 @@ class ResultVariableDecl extends @field, Documentable, ExprParent {
531565
int getIndex() { fields(this, _, -(result + 1)) }
532566

533567
override string toString() { result = "result variable declaration" }
568+
569+
override string describeQlClass() { result = "ResultVariableDecl" }
534570
}
535571

536572
/**
@@ -579,6 +615,8 @@ class MethodSpec extends InterfaceMemberSpec {
579615
Expr getNameExpr() { result = name }
580616

581617
override string toString() { result = "method declaration" }
618+
619+
override string describeQlClass() { result = "MethodSpec" }
582620
}
583621

584622
/**
@@ -588,4 +626,6 @@ class EmbeddingSpec extends InterfaceMemberSpec {
588626
EmbeddingSpec() { not exists(getChildExpr(1)) }
589627

590628
override string toString() { result = "interface embedding" }
629+
630+
override string describeQlClass() { result = "EmbeddingSpec" }
591631
}

0 commit comments

Comments
 (0)