Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ module Ast implements AstSig<Location> {
final private class ParameterFinal = CS::Parameter;

class Parameter extends ParameterFinal {
AstNode getPattern() { result = this }
AstNode getPattern() { none() }

Expr getDefaultValue() {
// Avoid combinatorial explosions for callables with multiple bodies
Expand Down
2 changes: 1 addition & 1 deletion go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ module CfgImpl {
class Parameter extends AstNode {
Parameter() { this = any(Go::Parameter p).getDeclaration() }

AstNode getPattern() { result = this }
AstNode getPattern() { none() }

Expr getDefaultValue() { none() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ module Ast implements AstSig<Py::Location> {
* Gets the pattern for this parameter. In Python, there is no destructuring
* pattern syntax for parameters, so the pattern is the parameter itself.
*/
AstNode getPattern() { result = this }
AstNode getPattern() { none() }
}

/**
Expand Down
7 changes: 1 addition & 6 deletions ruby/ql/lib/codeql/ruby/controlflow/ControlFlowGraph.qll
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,7 @@ private module Ast implements AstSig<Location> {
}

class Parameter extends AstNode instanceof R::Ast::Parameter {
AstNode getPattern() {
this.(R::Ast::NamedParameter).getDefiningAccess() = result
or
not exists(this.(R::Ast::NamedParameter).getDefiningAccess()) and
this = result
}
AstNode getPattern() { result = this.(R::Ast::NamedParameter).getDefiningAccess() }

Expr getDefaultValue() {
result = desugar(this.(R::Ast::KeywordParameter).getDefaultValue()) or
Expand Down
48 changes: 31 additions & 17 deletions shared/controlflow/codeql/controlflow/ControlFlowGraph.qll
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ signature module AstSig<LocationSig Location> {
/**
* Gets the pattern associated with this parameter.
*
* The pattern is included in the CFG while the parameter itself is not.
* Although, in simple cases that do not involve destructuring, it is
* allowed for the pattern to be equal to the parameter.
* The pattern, when present, is included in the CFG in addition to the
* parameter itself.
*/
AstNode getPattern();

Expand Down Expand Up @@ -777,8 +776,6 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
n instanceof Case
or
n = any(Case case).getPattern(_)
or
exists(Parameter p | exists(p.getDefaultValue()) and n = p.getPattern())
)
}

Expand All @@ -802,6 +799,8 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {

private string patternMatchTrueTag() { result = "[MatchTrue]" }

private string parameterDefaultValueTag() { result = "[ParameterDefaultValue]" }

/**
* Holds if an additional node tagged with `tag` should be created for
* `n`. Edges targeting such nodes are labeled with `t` and therefore `t`
Expand All @@ -824,6 +823,10 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
n instanceof PatternMatchExpr and
tag = patternMatchTrueTag() and
t.(BooleanSuccessor).getValue() = true
or
n = any(Parameter p | exists(p.getDefaultValue())) and
tag = parameterDefaultValueTag() and
t.(MatchingSuccessor).getValue() = false
}

/**
Expand Down Expand Up @@ -983,10 +986,7 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
)
}

private predicate hasCfg(AstNode n) {
exists(getEnclosingCallable(n)) and
(n instanceof Parameter implies n = n.(Parameter).getPattern())
}
private predicate hasCfg(AstNode n) { exists(getEnclosingCallable(n)) }

cached
private newtype TNode =
Expand Down Expand Up @@ -1529,7 +1529,7 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {

pragma[nomagic]
private AstNode getParameterPatternOrBodyEntry(Callable c, CallableContextOption ctx, int i) {
result = getRankedParameter(c, ctx, i).getPattern()
result = getRankedParameter(c, ctx, i)
or
(
not exists(getRankedParameter(c, _, _)) and
Expand Down Expand Up @@ -1562,14 +1562,28 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
n2.isBefore(getParameterPatternOrBodyEntry(c, _, 1))
or
exists(CallableContextOption ctx, Parameter p, int i | p = getRankedParameter(c, ctx, i) |
exists(MatchingSuccessor t |
n1.isAfterValue(p.getPattern(), t) and
if t.isMatch()
then n2.isBefore(getParameterPatternOrBodyEntry(c, ctx, i + 1))
else n2.isBefore(p.getDefaultValue())
n1.isBefore(p) and
n2.isAdditional(p, parameterDefaultValueTag())
or
n1.isAdditional(p, parameterDefaultValueTag()) and
n2.isBefore(p.getDefaultValue())
or
(
n1.isBefore(p)
or
n1.isAfter(p.getDefaultValue())
) and
(
n2.isBefore(p.getPattern())
or
not exists(p.getPattern()) and
n2.isAfter(p)
)
or
n1.isAfter(p.getDefaultValue()) and
n1.isAfter(p.getPattern()) and
n2.isAfter(p)
or
n1.isAfter(p) and
n2.isBefore(getParameterPatternOrBodyEntry(c, ctx, i + 1))
)
or
Expand Down Expand Up @@ -2606,7 +2620,7 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
succ2 = node.getASuccessor(t2) and
// allow for the pattern match true edge when a pattern match
// expression is not in a conditional context
not succ1.isAdditional(_, patternMatchTrueTag())
not succ1.isAdditional(_, [patternMatchTrueTag(), parameterDefaultValueTag()])
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ module Ast implements AstSig<Location> {
class Parameter extends U::Parameter {
Expr getDefaultValue() { result = super.getDefault() }

AstNode getPattern() {
result = super.getPattern()
or
not exists(super.getPattern()) and result = this
}
AstNode getPattern() { result = super.getPattern() }
}

Parameter callableGetParameter(Callable c, int index) {
Expand Down
Loading
Loading