Skip to content

Commit 11e19d9

Browse files
committed
Unified: Extend isInTypeContext
1 parent ccaf910 commit 11e19d9

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
private import unified
22
private import NameBinding as NameBinding
3+
private import ExprPositionsPlugin
34

45
/**
5-
* Holds if `expr` appears in the context of a type annotation.
6+
* Holds if `expr` appears in a context where it must refer to a type.
67
*/
78
predicate isInTypeContext(Expr expr) {
89
expr = any(TypeCastExpr n).getType()
@@ -27,8 +28,22 @@ predicate isInTypeContext(Expr expr) {
2728
or
2829
expr = any(AssociatedTypeDeclaration n).getBound()
2930
or
31+
expr = any(ClassLikeDeclaration c).getExtensionTarget()
32+
or
33+
expr = any(GenericTypeExpr gte).getATypeArgument()
34+
or
3035
expr.getParent() instanceof TypeConstraint
3136
or
37+
exists(Identifier id | id = NameBinding::getStaticBindingTarget(expr) |
38+
id = any(ClassLikeDeclaration c).getNameNode()
39+
or
40+
id = any(TypeAliasDeclaration t).getNameNode()
41+
or
42+
id = any(TypeParameter t).getNameNode()
43+
)
44+
or
45+
any(ExprPositionsPlugin p).isInTypeContext(expr)
46+
or
3247
isInTypeContext(expr.getEnclosingExpr())
3348
}
3449

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
private import unified
2+
private import codeql.util.Unit
3+
4+
private module Plugins {
5+
private import ExprPositionsPluginSwift
6+
}
7+
8+
class ExprPositionsPlugin extends Unit {
9+
predicate isInTypeContext(Expr e) { none() }
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
private import unified
2+
private import ExprPositionsPlugin
3+
4+
private class ExprPositionsPluginSwift extends ExprPositionsPlugin {
5+
override predicate isInTypeContext(Expr e) { e = any(GenericTypeExpr g).getBase() }
6+
}

0 commit comments

Comments
 (0)