Skip to content

Commit 467664b

Browse files
committed
Address review comments
1 parent fa6d1e4 commit 467664b

16 files changed

Lines changed: 45 additions & 32 deletions

File tree

‎unified/ql/lib/codeql/Definitions.qll‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ private import codeql.unified.internal.NameBinding
1010
*/
1111
cached
1212
predicate definitionOf(Identifier reference, NameBinding definition, string kind) {
13-
definition = getStaticBindingTarget(reference) and
13+
definition = getStaticBindingTargetFromIdentifier(reference) and
1414
not reference instanceof NameBinding and
1515
kind = "name"
1616
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module StaticNameResolutionStats implements EntityStatsSig {
1313
*/
1414
private predicate resolvesToValue(Identifier name) {
1515
exists(AstNode decl |
16-
decl = getStaticBindingTarget(name).getDeclaration() and
16+
decl = getStaticBindingTargetFromIdentifier(name).getDeclaration() and
1717
not decl instanceof ClassLikeDeclaration and
1818
not decl instanceof TypeAliasDeclaration and
1919
not decl instanceof TypeParameter and
@@ -49,7 +49,7 @@ module StaticNameResolutionStats implements EntityStatsSig {
4949
}
5050

5151
NameBindingNode getTarget() {
52-
result.asIdentifier() = getStaticBindingTarget(this)
52+
result.asIdentifier() = getStaticBindingTargetFromIdentifier(this)
5353
or
5454
result.isModuleScopeNode(_) and
5555
result.(NamespaceNode).ref().isIdentifier(this)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ predicate isInTypeContext(Expr expr) {
3434
or
3535
expr.getParent() instanceof TypeConstraint
3636
or
37-
exists(Identifier id | id = NameBinding::getStaticBindingTarget(expr) |
37+
exists(Identifier id | id = NameBinding::getStaticBindingTargetFromRef(expr) |
3838
id = any(ClassLikeDeclaration c).getNameNode()
3939
or
4040
id = any(TypeAliasDeclaration t).getNameNode()

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ module Unified {
150150
/** Gets a direct base class of this class. */
151151
ClassLikeDeclaration getABaseClass() {
152152
result.getNameNode() =
153-
NameBinding::getStaticBindingTarget(NameBinding::getIdentifierFromRef(this.getABaseType()
154-
.getType()))
153+
NameBinding::getStaticBindingTargetFromRef(this.getABaseType().getType())
155154
}
156155
}
157156

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -717,14 +717,25 @@ module Public {
717717
}
718718
}
719719

720-
/** Gets the declaration being accessed by `access`, as determined by static name binding. */
721-
NameBinding getStaticBindingTarget(Identifier access) {
720+
/** Gets the declaration being accessed by identifier `i`, as determined by static name binding. */
721+
NameBinding getStaticBindingTargetFromIdentifier(Identifier i) {
722722
// For unqualified accesses, use the shadowing-aware lookup
723-
result = access.(UnqualifiedMemberAccess).getTarget()
723+
result = i.(UnqualifiedMemberAccess).getTarget()
724724
or
725725
// For others, just follow the name binding graph
726-
not access instanceof UnqualifiedMemberAccess and
727-
trackNameBinding(result).asIdentifier() = access
726+
not i instanceof UnqualifiedMemberAccess and
727+
trackNameBinding(result).asIdentifier() = i
728+
}
729+
730+
/**
731+
* Gets the declaration being accessed by `access`, as determined by static name binding.
732+
*
733+
* Unlike `getStaticBindingTargetFromIdentifier`, this works with any AST node that contains
734+
* a reference to an identifier, for example `x.foo` and `foo<Int>` resolve to whatever the
735+
* identifiers `foo` resolve to.
736+
*/
737+
NameBinding getStaticBindingTargetFromRef(AstNode access) {
738+
result = getStaticBindingTargetFromIdentifier(getIdentifierFromRef(access))
728739
}
729740

730741
/**

‎unified/ql/lib/codeql/unified/internal/typeinference/Type.qll‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ predicate associatedTypeParameterInherited(
3232
) {
3333
associatedTypeParameter(base, a, _) and
3434
baseRef = c.getABaseType().getType() and
35-
base.getNameNode() = getStaticBindingTarget(baseRef) and
35+
base.getNameNode() = getStaticBindingTargetFromRef(baseRef) and
3636
name = a.getName()
3737
}
3838

‎unified/ql/lib/codeql/unified/internal/typeinference/TypeInference.qll‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ private module Input3 implements InputSig3 {
435435
)
436436
or
437437
// no mutual recursion; can be resolved directly with static name binding
438-
result.getNameNode() = getStaticBindingTarget(getIdentifierFromRef(this))
438+
result.getNameNode() = getStaticBindingTargetFromRef(this)
439439
}
440440
}
441441

@@ -457,7 +457,7 @@ private module Input3 implements InputSig3 {
457457
result = inferType(fa.getReceiver(), path)
458458
or
459459
// static field with type qualifier
460-
result = fa.getReceiver().(TypeMention).getTypeAt(path)
460+
result = fa.(MemberAccessExpr).getBase().(TypeMention).getTypeAt(path)
461461
or
462462
// field with implicit receiver
463463
result = getImplicitReceiverType(fa, path)
@@ -546,6 +546,7 @@ private module Input3 implements InputSig3 {
546546
exists(TypeMention tm | result = tm.getTypeAt(path) |
547547
tm = super.getCallee()
548548
or
549+
not super.getCallee() instanceof TypeMention and
549550
tm = super.getCallee().(MemberAccessExpr).getBase()
550551
)
551552
}
@@ -573,7 +574,7 @@ private module Input3 implements InputSig3 {
573574
}
574575

575576
private Unified::Callable getTargetViaStaticNameBinding() {
576-
exists(NameBinding b | b = getStaticBindingTarget(getIdentifierFromRef(this.getCallee())) |
577+
exists(NameBinding b | b = getStaticBindingTargetFromRef(this.getCallee()) |
577578
// object creation (including enum constructors): `String(42)`, `Optional.Some(42)`
578579
exists(ClassLikeDeclaration cls, ConstructorDeclaration init |
579580
cls.getNameNode() = b and
@@ -727,7 +728,7 @@ private module Input3 implements InputSig3 {
727728
tp = f.getType().getType().getATypeParameter() and
728729
path = TypePath::singleton(tp) and
729730
// `Optional<String>.none` does not have an unknown `Wrapper` type
730-
not exists(n.(FieldAccess).getReceiver().(TypeMention).getTypeAt(path))
731+
not exists(n.(MemberAccessExpr).getBase().(TypeMention).getTypeAt(path))
731732
) and
732733
result instanceof UnknownType
733734
}

‎unified/ql/lib/codeql/unified/internal/typeinference/TypeInferencePlugin.qll‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ private module Plugins {
1010
private import TypeInferencePluginSwift
1111
}
1212

13+
// todo: once we support multiple languages, these predicates will need to be
14+
// tagged with the language
1315
class TypeInferencePlugin extends Unit {
1416
/** Gets the boolean type which will be assigned to boolean literals. */
1517
abstract Type getBoolType();

‎unified/ql/lib/codeql/unified/internal/typeinference/TypeInferencePluginSwift.qll‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private class SwiftTypeInferencePlugin extends Plugin::TypeInferencePlugin {
147147
pragma[nomagic]
148148
override FunctionDeclaration getFunctionInvoke(Type t) {
149149
result = t.(FunctionType).getClassLikeDeclaration().getAMember() and
150-
result.getName() = "invoke"
150+
result.getName() = "callAsFunction"
151151
}
152152

153153
bindingset[t, arity, i]

‎unified/ql/lib/codeql/unified/internal/typeinference/TypeMention.qll‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ abstract class TypeMention extends AstNode {
2020
}
2121

2222
private Type resolveType(Identifier access) {
23-
exists(NameBinding b | b = getStaticBindingTarget(access) |
23+
exists(NameBinding b | b = getStaticBindingTargetFromIdentifier(access) |
2424
b = result.(ClassLikeDeclarationType).getClassLikeDeclaration().getNameNode()
2525
or
2626
b = result.(TypeParameterType).getTypeParameter().getNameNode()
@@ -40,7 +40,7 @@ abstract private class ExprTypeMention extends TypeMention, Expr {
4040
private class AliasExprTypeMention extends ExprTypeMention {
4141
private TypeAliasDeclaration alias;
4242

43-
AliasExprTypeMention() { alias.getNameNode() = getStaticBindingTarget(this) }
43+
AliasExprTypeMention() { alias.getNameNode() = getStaticBindingTargetFromRef(this) }
4444

4545
private TypeParameterType getAliasTypeParameter(int i) {
4646
result.getTypeParameter() = alias.getTypeParameter(i)

0 commit comments

Comments
 (0)