Skip to content

Commit dc11ec4

Browse files
authored
QL: super and aggregates containing expressions
1 parent 05e2ec3 commit dc11ec4

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

ql/src/codeql_ql/ast/Ast.qll

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ class TypeExpr extends TType, AstNode {
243243
result.(Class).getAliasType() = this
244244
or
245245
result.(Class).getUnionMember() = this
246+
or
247+
result.(MemberCall).getSuperType() = this
246248
}
247249

248250
Type getResolvedType() { resolveTypeExpr(this, result) }
@@ -454,6 +456,15 @@ class MemberCall extends TMemberCall, Call {
454456
result = expr.getChild(_).(Generated::QualifiedRhs).getName().getValue()
455457
}
456458

459+
/**
460+
* Gets the supertype referenced in this call, that is the `Foo` in `Foo.super.bar(...)`.
461+
*
462+
* Only yields a result if this is actually a `super` call.
463+
*/
464+
TypeExpr getSuperType() {
465+
toGenerated(result) = expr.getChild(_).(Generated::SuperRef).getChild(0)
466+
}
467+
457468
override Expr getArgument(int i) {
458469
result =
459470
rank[i + 1](Expr e, int index |
@@ -813,6 +824,39 @@ class HigherOrderFormula extends THigherOrderFormula, Formula {
813824
override string getAPrimaryQlClass() { result = "HigherOrderFormula" }
814825
}
815826

827+
class ExprAggregate extends TExprAggregate, Expr {
828+
Generated::Aggregate agg;
829+
Generated::ExprAggregateBody body;
830+
string kind;
831+
832+
ExprAggregate() {
833+
this = TExprAggregate(agg) and
834+
kind = agg.getChild(0).(Generated::AggId).getValue() and
835+
body = agg.getChild(_)
836+
}
837+
838+
string getKind() { result = kind }
839+
840+
/**
841+
* Gets the ith "as" expression of this aggregate, if any.
842+
*/
843+
AsExpr getAsExpr(int i) { toGenerated(result) = body.getAsExprs().getChild(i) }
844+
845+
/**
846+
* Gets the ith "order by" expression of this aggregate, if any.
847+
*/
848+
Expr getOrderBy(int i) { toGenerated(result) = body.getOrderBys().getChild(i).getChild(0) }
849+
850+
/**
851+
* Gets the direction (ascending or descending) of the ith "order by" expression of this aggregate.
852+
*/
853+
string getOrderbyDirection(int i) {
854+
result = body.getOrderBys().getChild(i).getChild(1).(Generated::Direction).getValue()
855+
}
856+
857+
override string getAPrimaryQlClass() { result = "ExprAggregate[" + kind + "]" }
858+
}
859+
816860
/** An aggregate expression, such as `count` or `sum`. */
817861
class Aggregate extends TAggregate, Expr {
818862
Generated::Aggregate agg;
@@ -899,6 +943,8 @@ class AsExpr extends TAsExpr, AstNode {
899943
or
900944
result.(Aggregate).getAsExpr(_) = this
901945
or
946+
result.(ExprAggregate).getAsExpr(_) = this
947+
or
902948
result.(Select).getAsExpr(_) = this
903949
}
904950
}
@@ -933,7 +979,9 @@ class Expr extends TExpr, AstNode {
933979
result.(Call).getArgument(_) = this
934980
or
935981
result.(Aggregate).getOrderBy(_) = this
936-
or
982+
or
983+
result.(ExprAggregate).getOrderBy(_) = this
984+
or
937985
result.(Select).getOrderBy(_) = this
938986
}
939987
}

ql/src/codeql_ql/ast/internal/AstNodes.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ newtype TAstNode =
2323
TComparisonOp(Generated::Compop op) or
2424
TQuantifier(Generated::Quantified quant) or
2525
TAggregate(Generated::Aggregate agg) { agg.getChild(_) instanceof Generated::FullAggregateBody } or
26+
TExprAggregate(Generated::Aggregate agg) {
27+
agg.getChild(_) instanceof Generated::ExprAggregateBody
28+
} or
2629
TIdentifier(Generated::Variable var) or
2730
TAsExpr(Generated::AsExpr asExpr) or
2831
TPredicateCall(Generated::CallOrUnqualAggExpr call) or
@@ -61,7 +64,7 @@ class TFormula =
6164
class TBinOpExpr = TAddSubExpr or TMulDivModExpr;
6265

6366
class TExpr =
64-
TBinOpExpr or TLiteral or TAggregate or TIdentifier or TInlineCast or TCall or TUnaryExpr or
67+
TBinOpExpr or TLiteral or TAggregate or TExprAggregate or TIdentifier or TInlineCast or TCall or TUnaryExpr or
6568
TExprAnnotation or TDontCare or TRange or TSet;
6669

6770
class TCall = TPredicateCall or TMemberCall or TNoneCall or TAnyCall;

0 commit comments

Comments
 (0)