Skip to content

Commit 3986dff

Browse files
committed
Keep methods and fields separate in SourceOrSinkElement
This improves performance.
1 parent b6a31b1 commit 3986dff

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

go/ql/lib/semmle/go/dataflow/ExternalFlow.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,8 @@ SourceSinkInterpretationInput::SourceOrSinkElement interpretElement(
477477
exists(string p | p = interpretPackage(pkg) |
478478
result.hasTypeInfo(p, type, subtypes) and
479479
(
480-
result.asEntity().(Field).hasQualifiedName(p, type, name) or
481-
result.asEntity().(Method).hasQualifiedName(p, type, name)
480+
result.asFieldEntity().hasQualifiedName(p, type, name) or
481+
result.asMethodEntity().hasQualifiedName(p, type, name)
482482
)
483483
or
484484
subtypes = true and
@@ -488,12 +488,12 @@ SourceSinkInterpretationInput::SourceOrSinkElement interpretElement(
488488
m2.getName() = name and
489489
m2.getReceiverBaseType().hasQualifiedName(pkg2, type2)
490490
|
491-
result.asEntity() = m2 and
491+
result.asMethodEntity() = m2 and
492492
result.hasTypeInfo(pkg2, type2, subtypes)
493493
)
494494
or
495495
type = "" and
496-
exists(Entity e | e.hasQualifiedName(p, name) | result.asEntity() = e)
496+
exists(Entity e | e.hasQualifiedName(p, name) | result.asOtherEntity() = e)
497497
)
498498
}
499499

go/ql/lib/semmle/go/dataflow/internal/FlowSummaryImpl.qll

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,12 @@ module SourceSinkInterpretationInput implements
153153
// Methods having multiple qualified names, a given Method is liable to have
154154
// more than one SourceOrSinkElement, one for each of the names it claims.
155155
private newtype TSourceOrSinkElement =
156-
TMethodOrFieldEntityElement(Entity e, string pkg, string type, boolean subtypes) {
157-
(
158-
e.(Method).hasQualifiedName(pkg, type, _) or
159-
e.(Field).hasQualifiedName(pkg, type, _)
160-
) and
156+
TMethodEntityElement(Method m, string pkg, string type, boolean subtypes) {
157+
m.hasQualifiedName(pkg, type, _) and
158+
subtypes = [true, false]
159+
} or
160+
TFieldEntityElement(Field f, string pkg, string type, boolean subtypes) {
161+
f.hasQualifiedName(pkg, type, _) and
161162
subtypes = [true, false]
162163
} or
163164
TOtherEntityElement(Entity e) {
@@ -170,10 +171,15 @@ module SourceSinkInterpretationInput implements
170171
class SourceOrSinkElement extends TSourceOrSinkElement {
171172
/** Gets this source or sink element as an entity, if it is one. */
172173
Entity asEntity() {
173-
this = TMethodOrFieldEntityElement(result, _, _, _) or
174-
this = TOtherEntityElement(result)
174+
result = [this.asMethodEntity(), this.asFieldEntity(), this.asOtherEntity()]
175175
}
176176

177+
Method asMethodEntity() { this = TMethodEntityElement(result, _, _, _) }
178+
179+
Field asFieldEntity() { this = TFieldEntityElement(result, _, _, _) }
180+
181+
Entity asOtherEntity() { this = TOtherEntityElement(result) }
182+
177183
/** Gets this source or sink element as an AST node, if it is one. */
178184
AstNode asAstNode() { this = TAstElement(result) }
179185

@@ -182,7 +188,8 @@ module SourceSinkInterpretationInput implements
182188
* with the given values for `pkg`, `type` and `subtypes`.
183189
*/
184190
predicate hasTypeInfo(string pkg, string type, boolean subtypes) {
185-
this = TMethodOrFieldEntityElement(_, pkg, type, subtypes)
191+
this = TMethodEntityElement(_, pkg, type, subtypes) or
192+
this = TFieldEntityElement(_, pkg, type, subtypes)
186193
}
187194

188195
/** Gets a textual representation of this source or sink element. */
@@ -239,11 +246,10 @@ module SourceSinkInterpretationInput implements
239246
cn = this.asCall().getNode() and
240247
callTarget = cn.getTarget()
241248
|
242-
result.asEntity() = callTarget and
243249
(
244-
not callTarget instanceof Method
250+
result.asOtherEntity() = callTarget
245251
or
246-
callTarget instanceof Method and
252+
result.asMethodEntity() = callTarget and
247253
elementAppliesToQualifier(result, cn.getReceiver())
248254
)
249255
)
@@ -302,7 +308,7 @@ module SourceSinkInterpretationInput implements
302308
// `syntacticQualBaseType`'s underlying type might be a struct type and `sse`
303309
// might be a promoted method or field in it.
304310
targetType =
305-
getIntermediateEmbeddedType(sse.asEntity(), syntacticQualBaseType.getUnderlyingType())
311+
getIntermediateEmbeddedType(sse.asMethodEntity(), syntacticQualBaseType.getUnderlyingType())
306312
)
307313
)
308314
}
@@ -382,7 +388,7 @@ module SourceSinkInterpretationInput implements
382388
or
383389
exists(DataFlow::FieldReadNode frn | frn = n |
384390
c = "" and
385-
frn.getField() = pragma[only_bind_into](e).asEntity() and
391+
frn.getField() = pragma[only_bind_into](e).asFieldEntity() and
386392
elementAppliesToQualifier(pragma[only_bind_into](e), frn.getBase())
387393
)
388394
)
@@ -400,7 +406,7 @@ module SourceSinkInterpretationInput implements
400406
or
401407
exists(SourceOrSinkElement e, DataFlow::Write fw, DataFlow::Node base, Field f |
402408
e = mid.asElement() and
403-
f = e.asEntity()
409+
f = e.asFieldEntity()
404410
|
405411
c = "" and
406412
fw.writesField(base, f, node.asNode()) and

0 commit comments

Comments
 (0)