From f836b471e384917d1a749cfd108eeb6f9c78db6c Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Thu, 25 Nov 2021 13:43:48 +0100 Subject: [PATCH] Remove PatternGuard class --- ruby/ql/lib/codeql/ruby/ast/Control.qll | 90 +++++--------------- ruby/ql/lib/codeql/ruby/ast/internal/AST.qll | 29 +++---- ruby/ql/test/library-tests/ast/Ast.expected | 14 ++- 3 files changed, 40 insertions(+), 93 deletions(-) diff --git a/ruby/ql/lib/codeql/ruby/ast/Control.qll b/ruby/ql/lib/codeql/ruby/ast/Control.qll index 9006760df2c84..26f4ed02a0c5d 100644 --- a/ruby/ql/lib/codeql/ruby/ast/Control.qll +++ b/ruby/ql/lib/codeql/ruby/ast/Control.qll @@ -539,94 +539,50 @@ class InClause extends Expr, TInClause { final CasePattern getPattern() { toGenerated(result) = g.getPattern() } /** - * Gets the pattern guard in this case-in expression. In the + * Gets the pattern guard's condition in this case-in expression, if any. In the * following example, the pattern guard is `x > 10`. * ```rb * case foo * in [ x ] if x > 10 then ... + * in [ x ] unless x < 10 then ... * end * ``` */ - final PatternGuard getPatternGuard() { toGenerated(result) = g.getGuard() } + final Expr getCondition() { toGenerated(result) = g.getGuard().getAFieldOrChild() } - final override string toString() { result = "in ... then ..." } - - override AstNode getAChild(string pred) { - result = super.getAChild(pred) - or - pred = "getBody" and result = this.getBody() - or - pred = "getPattern" and result = this.getPattern() - or - pred = "getGuard" and result = this.getPatternGuard() - } -} + /** + * Holds if the pattern guard in this case-in expression is an `if` condition. For example: + * ```rb + * case foo + * in [ x ] if x > 10 then ... + * end + * ``` + */ + predicate hasIfCondition() { g.getGuard() instanceof Ruby::IfGuard } -/** - * A guard used in pattern matching. For example: - * ```rb - * in pattern if guard - * in pattern unless guard - * ``` - */ -class PatternGuard extends AstNode, TPatternGuard { /** - * Gets the condition expression. For example, the result is `foo` in the - * following: + * Holds if the pattern guard in this case-in expression is an `unless` condition. For example: * ```rb - * if foo - * unless foo + * case foo + * in [ x ] unless x < 10 then ... + * end * ``` */ - Expr getCondition() { none() } + predicate hasUnlessCondition() { g.getGuard() instanceof Ruby::UnlessGuard } + + final override string toString() { result = "in ... then ..." } override AstNode getAChild(string pred) { result = super.getAChild(pred) or + pred = "getBody" and result = this.getBody() + or + pred = "getPattern" and result = this.getPattern() + or pred = "getCondition" and result = this.getCondition() } } -/** - * An `if` pattern guard. For example: - * ```rb - * case foo - * in [ bar] if bar > 10 - * end - * ``` - */ -class IfGuard extends PatternGuard, TIfGuard { - private Ruby::IfGuard g; - - IfGuard() { this = TIfGuard(g) } - - final override Expr getCondition() { toGenerated(result) = g.getCondition() } - - final override string getAPrimaryQlClass() { result = "IfGuard" } - - final override string toString() { result = "if ..." } -} - -/** - * An `unless` pattern guard. For example: - * ```rb - * case foo - * in [ bar] unless bar > 10 - * end - * ``` - */ -class UnlessGuard extends PatternGuard, TUnlessGuard { - private Ruby::UnlessGuard g; - - UnlessGuard() { this = TUnlessGuard(g) } - - final override Expr getCondition() { toGenerated(result) = g.getCondition() } - - final override string getAPrimaryQlClass() { result = "UnlessGuard" } - - final override string toString() { result = "unless ..." } -} - /** * A loop. That is, a `for` loop, a `while` or `until` loop, or their * expression-modifier variants. diff --git a/ruby/ql/lib/codeql/ruby/ast/internal/AST.qll b/ruby/ql/lib/codeql/ruby/ast/internal/AST.qll index f817b91ecc07f..6a4e2f64a3bd6 100644 --- a/ruby/ql/lib/codeql/ruby/ast/internal/AST.qll +++ b/ruby/ql/lib/codeql/ruby/ast/internal/AST.qll @@ -161,7 +161,6 @@ private module Cached { THereDoc(Ruby::HeredocBeginning g) or TIdentifierMethodCall(Ruby::Identifier g) { isIdentifierMethodCall(g) } or TIf(Ruby::If g) or - TIfGuard(Ruby::IfGuard g) or TIfModifierExpr(Ruby::IfModifier g) or TInClause(Ruby::InClause g) or TInstanceVariableAccessReal(Ruby::InstanceVariable g, AST::InstanceVariable v) { @@ -305,7 +304,6 @@ private module Cached { TUnaryPlusExpr(Ruby::Unary g) { g instanceof @ruby_unary_plus } or TUndefStmt(Ruby::Undef g) or TUnlessExpr(Ruby::Unless g) or - TUnlessGuard(Ruby::UnlessGuard g) or TUnlessModifierExpr(Ruby::UnlessModifier g) or TUntilExpr(Ruby::Until g) or TUntilModifierExpr(Ruby::UntilModifier g) or @@ -332,15 +330,15 @@ private module Cached { TForwardArgument or TGEExpr or TGTExpr or TGlobalVariableAccessReal or THashKeySymbolLiteral or THashLiteral or THashPattern or THashSplatExpr or THashSplatNilParameter or THashSplatParameter or THereDoc or TIdentifierMethodCall or TIf or - TIfGuard or TIfModifierExpr or TInClause or TInstanceVariableAccessReal or - TIntegerLiteralReal or TKeywordParameter or TLEExpr or TLShiftExprReal or TLTExpr or - TLambda or TLeftAssignmentList or TLine or TLocalVariableAccessReal or - TLogicalAndExprReal or TLogicalOrExprReal or TMethod or TModuleDeclaration or - TModuloExprReal or TMulExprReal or TNEExpr or TNextStmt or TNilLiteral or - TNoRegExpMatchExpr or TNotExpr or TOptionalParameter or TPair or TParenthesizedExpr or - TRShiftExprReal or TRangeLiteralReal or TRationalLiteral or TRedoStmt or TRegExpLiteral or - TRegExpMatchExpr or TRegularArrayLiteral or TRegularMethodCall or TRegularStringLiteral or - TRegularSuperCall or TRescueClause or TRescueModifierExpr or TRetryStmt or TReturnStmt or + TIfModifierExpr or TInClause or TInstanceVariableAccessReal or TIntegerLiteralReal or + TKeywordParameter or TLEExpr or TLShiftExprReal or TLTExpr or TLambda or + TLeftAssignmentList or TLine or TLocalVariableAccessReal or TLogicalAndExprReal or + TLogicalOrExprReal or TMethod or TModuleDeclaration or TModuloExprReal or TMulExprReal or + TNEExpr or TNextStmt or TNilLiteral or TNoRegExpMatchExpr or TNotExpr or + TOptionalParameter or TPair or TParenthesizedExpr or TRShiftExprReal or TRangeLiteralReal or + TRationalLiteral or TRedoStmt or TRegExpLiteral or TRegExpMatchExpr or + TRegularArrayLiteral or TRegularMethodCall or TRegularStringLiteral or TRegularSuperCall or + TRescueClause or TRescueModifierExpr or TRetryStmt or TReturnStmt or TScopeResolutionConstantAccess or TScopeResolutionMethodCall or TSelfReal or TSimpleParameterReal or TSimpleSymbolLiteral or TSingletonClass or TSingletonMethod or TSpaceshipExpr or TSplatExprReal or TSplatParameter or TStringArrayLiteral or @@ -348,9 +346,8 @@ private module Cached { TStringTextComponent or TSubExprReal or TSubshellLiteral or TSymbolArrayLiteral or TTernaryIfExpr or TThen or TTokenConstantAccess or TTokenMethodName or TTokenSuperCall or TToplevel or TTrueLiteral or TTuplePatternParameter or TUnaryMinusExpr or TUnaryPlusExpr or - TUndefStmt or TUnlessExpr or TUnlessGuard or TUnlessModifierExpr or TUntilExpr or - TUntilModifierExpr or TVariableReferencePattern or TWhenExpr or TWhileExpr or - TWhileModifierExpr or TYieldCall; + TUndefStmt or TUnlessExpr or TUnlessModifierExpr or TUntilExpr or TUntilModifierExpr or + TVariableReferencePattern or TWhenExpr or TWhileExpr or TWhileModifierExpr or TYieldCall; class TAstNodeSynth = TAddExprSynth or TAssignExprSynth or TBitwiseAndExprSynth or TBitwiseOrExprSynth or @@ -441,7 +438,6 @@ private module Cached { n = THashSplatParameter(result) or n = THereDoc(result) or n = TIdentifierMethodCall(result) or - n = TIfGuard(result) or n = TIfModifierExpr(result) or n = TIf(result) or n = TInClause(result) or @@ -513,7 +509,6 @@ private module Cached { n = TUnaryPlusExpr(result) or n = TUndefStmt(result) or n = TUnlessExpr(result) or - n = TUnlessGuard(result) or n = TUnlessModifierExpr(result) or n = TUntilExpr(result) or n = TUntilModifierExpr(result) or @@ -642,8 +637,6 @@ class TControlExpr = TConditionalExpr or TCaseExpr or TCaseMatch or TLoop; class TConditionalExpr = TIfExpr or TUnlessExpr or TIfModifierExpr or TUnlessModifierExpr or TTernaryIfExpr; -class TPatternGuard = TIfGuard or TUnlessGuard; - class TIfExpr = TIf or TElsif; class TConditionalLoop = TWhileExpr or TUntilExpr or TWhileModifierExpr or TUntilModifierExpr; diff --git a/ruby/ql/test/library-tests/ast/Ast.expected b/ruby/ql/test/library-tests/ast/Ast.expected index ceff692c8c189..37728a4a45ff1 100644 --- a/ruby/ql/test/library-tests/ast/Ast.expected +++ b/ruby/ql/test/library-tests/ast/Ast.expected @@ -733,18 +733,16 @@ control/cases.rb: # 31| getReceiver: [Self, SelfVariableAccess] self # 32| getBranch/getInClause: [InClause] in ... then ... # 32| getPattern: [LocalVariableAccess] x -# 32| getGuard: [UnlessGuard] unless ... -# 32| getCondition: [LTExpr] ... < ... -# 32| getAnOperand/getLeftOperand/getLesserOperand/getReceiver: [LocalVariableAccess] x -# 32| getAnOperand/getArgument/getGreaterOperand/getRightOperand: [IntegerLiteral] 0 +# 32| getCondition: [LTExpr] ... < ... +# 32| getAnOperand/getLeftOperand/getLesserOperand/getReceiver: [LocalVariableAccess] x +# 32| getAnOperand/getArgument/getGreaterOperand/getRightOperand: [IntegerLiteral] 0 # 32| getBody: [StmtSequence] then ... # 33| getStmt: [BooleanLiteral] true # 34| getBranch/getInClause: [InClause] in ... then ... # 34| getPattern: [LocalVariableAccess] x -# 34| getGuard: [IfGuard] if ... -# 34| getCondition: [LTExpr] ... < ... -# 34| getAnOperand/getLeftOperand/getLesserOperand/getReceiver: [LocalVariableAccess] x -# 34| getAnOperand/getArgument/getGreaterOperand/getRightOperand: [IntegerLiteral] 0 +# 34| getCondition: [LTExpr] ... < ... +# 34| getAnOperand/getLeftOperand/getLesserOperand/getReceiver: [LocalVariableAccess] x +# 34| getAnOperand/getArgument/getGreaterOperand/getRightOperand: [IntegerLiteral] 0 # 34| getBody: [StmtSequence] then ... # 35| getStmt: [BooleanLiteral] true # 36| getBranch/getElseBranch: [StmtSequence] else ...