Skip to content

Commit 84a2f36

Browse files
committed
Cfg: Distinguish parameters from their patterns.
1 parent 6161922 commit 84a2f36

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

shared/controlflow/codeql/controlflow/ControlFlowGraph.qll

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ signature module AstSig<LocationSig Location> {
5252

5353
/** A parameter of a callable. */
5454
class Parameter extends AstNode {
55+
/**
56+
* Gets the pattern associated with this parameter.
57+
*
58+
* The pattern is included in the CFG while the parameter itself is not.
59+
* Although, in simple cases that do not involve destructuring, it is
60+
* allowed for the pattern to be equal to the parameter.
61+
*/
62+
AstNode getPattern();
63+
5564
/** Gets the default value of this parameter, if any. */
5665
Expr getDefaultValue();
5766
}
@@ -631,7 +640,7 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
631640
or
632641
n = any(Case case).getPattern(_)
633642
or
634-
exists(n.(Parameter).getDefaultValue())
643+
exists(Parameter p | exists(p.getDefaultValue()) and n = p.getPattern())
635644
)
636645
}
637646

@@ -803,24 +812,27 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
803812
)
804813
}
805814

815+
private predicate hasCfg(AstNode n) {
816+
exists(getEnclosingCallable(n)) and
817+
(n instanceof Parameter implies n = n.(Parameter).getPattern())
818+
}
819+
806820
cached
807821
private newtype TNode =
808-
TBeforeNode(AstNode n) { Input1::cfgCachedStageRef() and exists(getEnclosingCallable(n)) } or
809-
TAstNode(AstNode n) { postOrInOrder(n) and exists(getEnclosingCallable(n)) } or
822+
TBeforeNode(AstNode n) { Input1::cfgCachedStageRef() and hasCfg(n) } or
823+
TAstNode(AstNode n) { postOrInOrder(n) and hasCfg(n) } or
810824
TAfterValueNode(AstNode n, ConditionalSuccessor t) {
811825
inConditionalContext(n, t.getKind()) and
812-
exists(getEnclosingCallable(n)) and
826+
hasCfg(n) and
813827
not constantCondition(n, t.getDual())
814828
} or
815829
TAfterNode(AstNode n) {
816-
exists(getEnclosingCallable(n)) and
830+
hasCfg(n) and
817831
not inConditionalContext(n, _) and
818832
not cannotTerminateNormally(n) and
819833
not simpleLeafNode(n)
820834
} or
821-
TAdditionalNode(AstNode n, string tag) {
822-
additionalNode(n, tag, _) and exists(getEnclosingCallable(n))
823-
} or
835+
TAdditionalNode(AstNode n, string tag) { additionalNode(n, tag, _) and hasCfg(n) } or
824836
TEntryNode(Callable c) { callableHasBodyPart(c, _) } or
825837
TAnnotatedExitNode(Callable c, Boolean normal) { callableHasBodyPart(c, _) } or
826838
TExitNode(Callable c) { callableHasBodyPart(c, _) }
@@ -1390,8 +1402,8 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
13901402
}
13911403

13921404
pragma[nomagic]
1393-
private AstNode getParameterOrBodyEntry(Callable c, CallableContextOption ctx, int i) {
1394-
result = getRankedParameter(c, ctx, i)
1405+
private AstNode getParameterPatternOrBodyEntry(Callable c, CallableContextOption ctx, int i) {
1406+
result = getRankedParameter(c, ctx, i).getPattern()
13951407
or
13961408
(
13971409
not exists(getRankedParameter(c, _, _)) and
@@ -1409,18 +1421,18 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
14091421
or
14101422
exists(Callable c |
14111423
n1.(EntryNodeImpl).getEnclosingCallable() = c and
1412-
n2.isBefore(getParameterOrBodyEntry(c, _, 1))
1424+
n2.isBefore(getParameterPatternOrBodyEntry(c, _, 1))
14131425
or
14141426
exists(CallableContextOption ctx, Parameter p, int i | p = getRankedParameter(c, ctx, i) |
14151427
exists(MatchingSuccessor t |
1416-
n1.isAfterValue(p, t) and
1428+
n1.isAfterValue(p.getPattern(), t) and
14171429
if t.isMatch()
1418-
then n2.isBefore(getParameterOrBodyEntry(c, ctx, i + 1))
1430+
then n2.isBefore(getParameterPatternOrBodyEntry(c, ctx, i + 1))
14191431
else n2.isBefore(p.getDefaultValue())
14201432
)
14211433
or
14221434
n1.isAfter(p.getDefaultValue()) and
1423-
n2.isBefore(getParameterOrBodyEntry(c, ctx, i + 1))
1435+
n2.isBefore(getParameterPatternOrBodyEntry(c, ctx, i + 1))
14241436
)
14251437
or
14261438
exists(Input1::CallableContext ctx, int i |
@@ -1796,6 +1808,7 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
17961808
* and therefore should use default left-to-right evaluation.
17971809
*/
17981810
private predicate defaultCfg(AstNode ast) {
1811+
hasCfg(ast) and
17991812
not explicitStep(any(PreControlFlowNode n | n.isBefore(ast)), _)
18001813
}
18011814

0 commit comments

Comments
 (0)