Skip to content

Commit 7a7ff4a

Browse files
committed
Apply review comments
1 parent 5d14070 commit 7a7ff4a

File tree

11 files changed

+34
-26
lines changed

11 files changed

+34
-26
lines changed

go/downgrades/e47462df302b3e58a60d2e21f99aba63f973326f/old.dbscheme renamed to go/downgrades/d2066c23fe3de023d76d7e5a52dec6771d308534/old.dbscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ underlying_type(unique int named: @namedtype ref, int tp: @type ref);
208208
component_types(int parent: @compositetype ref, int index: int ref, string name: string ref, int tp: @type ref);
209209

210210
#keyset[parent, index]
211-
component_tags(int parent: @compositetype ref, int index: int ref, string tag: string ref);
211+
component_tags(int parent: @structtype ref, int index: int ref, string tag: string ref);
212212

213213
#keyset[interface, index]
214214
interface_private_method_ids(int interface: @interfacetype ref, int index: int ref, string id: string ref);

go/extractor/dbscheme/tables.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,14 +1150,14 @@ var ComponentTypesTable = NewTable("component_types",
11501150
EntityColumn(TypeType, "tp"),
11511151
).KeySet("parent", "index")
11521152

1153-
// ComponentTagsTable is the table associating composite types with their component types' tags
1153+
// ComponentTagsTable is the table associating struct types with their component types' tags
11541154
var ComponentTagsTable = NewTable("component_tags",
1155-
EntityColumn(CompositeType, "parent"),
1155+
EntityColumn(StructType, "parent"),
11561156
IntColumn("index"),
11571157
StringColumn("tag"),
11581158
).KeySet("parent", "index")
11591159

1160-
// InterfacePrivateMethodIdsTable is the table associating interface types with their private method ids
1160+
// InterfacePrivateMethodIdsTable is the table associating interface types with the indices and ids of their private methods.
11611161
var InterfacePrivateMethodIdsTable = NewTable("interface_private_method_ids",
11621162
EntityColumn(InterfaceType, "interface"),
11631163
IntColumn("index"),

go/extractor/extractor.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,9 @@ func extractType(tw *trap.Writer, tp types.Type) trap.Label {
16241624
name = ""
16251625
}
16261626
extractComponentType(tw, lbl, i, name, field.Type())
1627-
dbscheme.ComponentTagsTable.Emit(tw, lbl, i, tp.Tag(i))
1627+
if tp.Tag(i) != "" {
1628+
dbscheme.ComponentTagsTable.Emit(tw, lbl, i, tp.Tag(i))
1629+
}
16281630
}
16291631
case *types.Pointer:
16301632
kind = dbscheme.PointerType.Index()
@@ -1643,11 +1645,8 @@ func extractType(tw *trap.Writer, tp types.Type) trap.Label {
16431645

16441646
extractComponentType(tw, lbl, i, meth.Name(), meth.Type())
16451647

1646-
// meth.Id() will be equal to meth.Name() for an exported method, or
1647-
// packge-qualified otherwise.
1648-
privateMethodId := meth.Id()
1649-
if privateMethodId != meth.Name() {
1650-
dbscheme.InterfacePrivateMethodIdsTable.Emit(tw, lbl, i, privateMethodId)
1648+
if !meth.Exported() {
1649+
dbscheme.InterfacePrivateMethodIdsTable.Emit(tw, lbl, i, meth.Id())
16511650
}
16521651
}
16531652
for i := 0; i < tp.NumEmbeddeds(); i++ {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
22
category: minorAnalysis
33
---
4-
* Added methods `StructTag.hasOwnFieldWithTag` and `Field.getTag`, which enable CodeQL queries to examine struct field tags.
5-
* Added method `InterfaceType.getMethodTypeById`, which enables CodeQL queries to distinguish interfaces with matching non-exported method names that are declared in different packages, and are therefore incompatible.
4+
* Added member predicates `StructTag.hasOwnFieldWithTag` and `Field.getTag`, which enable CodeQL queries to examine struct field tags.
5+
* Added member predicate `InterfaceType.getMethodTypeByQualifiedName`, which enables CodeQL queries to distinguish interfaces with matching non-exported method names that are declared in different packages, and are therefore incompatible.

go/ql/lib/go.dbscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ underlying_type(unique int named: @namedtype ref, int tp: @type ref);
208208
component_types(int parent: @compositetype ref, int index: int ref, string name: string ref, int tp: @type ref);
209209

210210
#keyset[parent, index]
211-
component_tags(int parent: @compositetype ref, int index: int ref, string tag: string ref);
211+
component_tags(int parent: @structtype ref, int index: int ref, string tag: string ref);
212212

213213
#keyset[interface, index]
214214
interface_private_method_ids(int interface: @interfacetype ref, int index: int ref, string id: string ref);

go/ql/lib/semmle/go/Types.qll

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,11 @@ class StructType extends @structtype, CompositeType {
465465
*/
466466
predicate hasOwnFieldWithTag(int i, string name, Type tp, boolean isEmbedded, string tag) {
467467
this.hasOwnField(i, name, tp, isEmbedded) and
468-
component_tags(this, i, tag)
468+
(
469+
component_tags(this, i, tag)
470+
or
471+
not component_tags(this, i, _) and tag = ""
472+
)
469473
}
470474

471475
/**
@@ -586,10 +590,13 @@ class StructType extends @structtype, CompositeType {
586590
override string pp() {
587591
result =
588592
"struct { " +
589-
concat(int i, string name, Type tp, string tag, string tagToPrint |
593+
concat(int i, string name, Type tp, string tagToPrint |
590594
component_types(this, i, name, tp) and
591-
component_tags(this, i, tag) and
592-
(if tag = "" then tagToPrint = "" else tagToPrint = " `" + tag + "`")
595+
(
596+
tagToPrint = " `" + any(string tag | component_tags(this, i, tag)) + "`"
597+
or
598+
tagToPrint = "" and not component_tags(this, i, _)
599+
)
593600
|
594601
name + " " + tp.pp() + tagToPrint, "; " order by i
595602
) + " }"
@@ -761,26 +768,28 @@ class InterfaceType extends @interfacetype, CompositeType {
761768
}
762769

763770
/**
764-
* Gets the type of method `id` of this interface type.
771+
* Gets the type of method `qname` of this interface type.
765772
*
766-
* This differs from `getMethodType` in that if the method is not exported, the `id`
767-
* will be package-qualified. This means that the set of `id`s` together with any
773+
* This differs from `getMethodType` in that if the method is not exported, the `qname`
774+
* will be package-qualified. This means that the set of `qname`s` together with any
768775
* embedded types fully distinguishes the interface from any other, whereas the set
769776
* of names matched by `getMethodName` may be ambiguous between interfaces with matching
770777
* exported methods and unexported methods that have matching names but belong to
771778
* different packages.
772779
*
773780
* For example, `interface { Exported() int; notExported() int }` declared in two
774781
* different packages defines two distinct types, but they appear identical according to
775-
* `getMethodType`.
782+
* `getMethodType`. If the packages were named `a` and `b`, `getMethodType` would yield
783+
* `notExported -> int` for both, whereas this method would yield `a.notExported -> int`
784+
* and `b.notExported -> int` respectively.
776785
*/
777-
Type getMethodTypeById(string id) {
786+
Type getMethodTypeByQualifiedName(string qname) {
778787
exists(int i, string name | i >= 0 |
779788
component_types(this, i, name, result) and
780789
(
781-
interface_private_method_ids(this, i, id)
790+
interface_private_method_ids(this, i, qname)
782791
or
783-
name = id and not interface_private_method_ids(this, i, _)
792+
name = qname and not interface_private_method_ids(this, i, _)
784793
)
785794
)
786795
}

go/ql/lib/upgrades/a58b81b1b4c4cccc8ca11731c1db86622f33af57/go.dbscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ underlying_type(unique int named: @namedtype ref, int tp: @type ref);
208208
component_types(int parent: @compositetype ref, int index: int ref, string name: string ref, int tp: @type ref);
209209

210210
#keyset[parent, index]
211-
component_tags(int parent: @compositetype ref, int index: int ref, string tag: string ref);
211+
component_tags(int parent: @structtype ref, int index: int ref, string tag: string ref);
212212

213213
#keyset[interface, index]
214214
interface_private_method_ids(int interface: @interfacetype ref, int index: int ref, string id: string ref);

0 commit comments

Comments
 (0)