Skip to content

Commit d9bc165

Browse files
committed
Add helper predicates for FieldDecl
1 parent 82e8d3a commit d9bc165

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

go/ql/lib/semmle/go/Decls.qll

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ class FieldBase extends @field, ExprParent {
461461
* Examples:
462462
*
463463
* ```go
464+
* io.Reader
464465
* Name string `json:"name"`
465466
* x, y int
466467
* ```
@@ -469,8 +470,9 @@ class FieldBase extends @field, ExprParent {
469470
*
470471
* ```go
471472
* struct {
472-
* Name string `json:"name"`
473-
* x, y int
473+
* io.Reader // embedded field
474+
* Name string `json:"name"` // field with tag
475+
* x, y int // declares two fields with the same type
474476
* }
475477
* ```
476478
*/
@@ -482,12 +484,24 @@ class FieldDecl extends FieldBase, Documentable, ExprParent {
482484
/**
483485
* Gets the expression representing the name of the `i`th field declared in this declaration
484486
* (0-based).
487+
*
488+
* This is not defined for embedded fields.
485489
*/
486490
Expr getNameExpr(int i) {
487491
i >= 0 and
488492
result = this.getChildExpr(i + 1)
489493
}
490494

495+
/**
496+
* Gets the `i`th field declared in this declaration (0-based).
497+
*
498+
* This is not defined for embedded fields.
499+
*/
500+
Field getField(int i) { this.getNameExpr(i).(Ident).declares(result) }
501+
502+
/** Holds if this field declaration declares an embedded type. */
503+
predicate isEmbedded() { not exists(this.getNameExpr(_)) }
504+
491505
/** Gets the tag expression of this field declaration, if any. */
492506
Expr getTag() { result = this.getChildExpr(-1) }
493507

0 commit comments

Comments
 (0)