From d361ef37afacb2acbd96da2df017c932bae16d4e Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Fri, 9 Apr 2021 18:34:09 +0200 Subject: [PATCH 1/8] Rename Method -> MethodDeclaration --- ql/src/codeql_ruby/ast/Expr.qll | 2 +- ql/src/codeql_ruby/ast/Method.qll | 6 +- ql/src/codeql_ruby/ast/internal/AST.qll | 9 +-- .../internal/ControlFlowGraphImpl.qll | 2 +- .../dataflow/internal/DataFlowPrivate.qll | 6 +- ql/test/library-tests/ast/Ast.expected | 56 +++++++++---------- .../ast/modules/classes.expected | 4 +- ql/test/library-tests/ast/modules/classes.ql | 2 +- .../library-tests/ast/modules/module_base.ql | 2 +- .../ast/modules/modules.expected | 12 ++-- ql/test/library-tests/ast/modules/modules.ql | 2 +- .../ast/modules/singleton_classes.expected | 4 +- .../ast/modules/singleton_classes.ql | 4 +- ql/test/library-tests/ast/params/params.ql | 2 +- 14 files changed, 60 insertions(+), 53 deletions(-) diff --git a/ql/src/codeql_ruby/ast/Expr.qll b/ql/src/codeql_ruby/ast/Expr.qll index 4e44bc98a25..04fc3023d93 100644 --- a/ql/src/codeql_ruby/ast/Expr.qll +++ b/ql/src/codeql_ruby/ast/Expr.qll @@ -123,7 +123,7 @@ private class Ensure extends StmtSequence, TEnsure { class BodyStmt extends StmtSequence, TBodyStmt { // Not defined by dispatch, as it should not be exposed private Generated::AstNode getChild(int i) { - result = any(Generated::Method g | this = TMethod(g)).getChild(i) + result = any(Generated::Method g | this = TMethodDeclaration(g)).getChild(i) or result = any(Generated::SingletonMethod g | this = TSingletonMethod(g)).getChild(i) or diff --git a/ql/src/codeql_ruby/ast/Method.qll b/ql/src/codeql_ruby/ast/Method.qll index 648392b28f0..f01eba6c323 100644 --- a/ql/src/codeql_ruby/ast/Method.qll +++ b/ql/src/codeql_ruby/ast/Method.qll @@ -32,12 +32,12 @@ class MethodBase extends Callable, BodyStmt, Scope, TMethodBase { } /** A normal method. */ -class Method extends MethodBase, TMethod { +class MethodDeclaration extends MethodBase, TMethodDeclaration { private Generated::Method g; - Method() { this = TMethod(g) } + MethodDeclaration() { this = TMethodDeclaration(g) } - final override string getAPrimaryQlClass() { result = "Method" } + final override string getAPrimaryQlClass() { result = "MethodDeclaration" } final override string getName() { result = g.getName().(Generated::Token).getValue() or diff --git a/ql/src/codeql_ruby/ast/internal/AST.qll b/ql/src/codeql_ruby/ast/internal/AST.qll index 26e2e6149c3..b78c6c19bf0 100644 --- a/ql/src/codeql_ruby/ast/internal/AST.qll +++ b/ql/src/codeql_ruby/ast/internal/AST.qll @@ -138,7 +138,7 @@ private module Cached { g instanceof @binary_and or g instanceof @binary_ampersandampersand } or TLogicalOrExpr(Generated::Binary g) { g instanceof @binary_or or g instanceof @binary_pipepipe } or - TMethod(Generated::Method g) or + TMethodDeclaration(Generated::Method g) or TModuleDeclaration(Generated::Module g) or TModuloExpr(Generated::Binary g) { g instanceof @binary_percent } or TMulExpr(Generated::Binary g) { g instanceof @binary_star } or @@ -301,7 +301,7 @@ private module Cached { n = TLocalVariableAccess(result, _) or n = TLogicalAndExpr(result) or n = TLogicalOrExpr(result) or - n = TMethod(result) or + n = TMethodDeclaration(result) or n = TModuleDeclaration(result) or n = TModuloExpr(result) or n = TMulExpr(result) or @@ -402,7 +402,8 @@ class TStmtSequence = TBeginBlock or TEndBlock or TThen or TElse or TDo or TEnsure or TStringInterpolationComponent or TBlock or TBodyStmt or TParenthesizedExpr; -class TBodyStmt = TBeginExpr or TModuleBase or TMethod or TLambda or TDoBlock or TSingletonMethod; +class TBodyStmt = + TBeginExpr or TModuleBase or TMethodDeclaration or TLambda or TDoBlock or TSingletonMethod; class TLiteral = TNumericLiteral or TNilLiteral or TBooleanLiteral or TStringlikeLiteral or TCharacterLiteral or @@ -428,7 +429,7 @@ class TArrayLiteral = TRegularArrayLiteral or TStringArrayLiteral or TSymbolArra class TCallable = TMethodBase or TLambda or TBlock; -class TMethodBase = TMethod or TSingletonMethod; +class TMethodBase = TMethodDeclaration or TSingletonMethod; class TBlock = TDoBlock or TBraceBlock; diff --git a/ql/src/codeql_ruby/controlflow/internal/ControlFlowGraphImpl.qll b/ql/src/codeql_ruby/controlflow/internal/ControlFlowGraphImpl.qll index a648a2c1b71..45ca798d99d 100644 --- a/ql/src/codeql_ruby/controlflow/internal/ControlFlowGraphImpl.qll +++ b/ql/src/codeql_ruby/controlflow/internal/ControlFlowGraphImpl.qll @@ -925,7 +925,7 @@ module Trees { private class MethodNameTree extends LeafTree, MethodName, ASTInternal::TTokenMethodName { } - private class MethodTree extends BodyStmtPostOrderTree, Method { + private class MethodDeclarationTree extends BodyStmtPostOrderTree, MethodDeclaration { /** Gets the `i`th child in the body of this block. */ final override AstNode getBodyChild(int i, boolean rescuable) { result = this.getParameter(i) and rescuable = false diff --git a/ql/src/codeql_ruby/dataflow/internal/DataFlowPrivate.qll b/ql/src/codeql_ruby/dataflow/internal/DataFlowPrivate.qll index 09c84fda825..f21a08a9cf3 100644 --- a/ql/src/codeql_ruby/dataflow/internal/DataFlowPrivate.qll +++ b/ql/src/codeql_ruby/dataflow/internal/DataFlowPrivate.qll @@ -269,7 +269,11 @@ private module ReturnNodes { scope = node.getScope() | stmt instanceof ReturnStmt and - (scope instanceof Method or scope instanceof SingletonMethod or scope instanceof Lambda) + ( + scope instanceof MethodDeclaration or + scope instanceof SingletonMethod or + scope instanceof Lambda + ) or stmt instanceof NextStmt and (scope instanceof Block or scope instanceof Lambda) diff --git a/ql/test/library-tests/ast/Ast.expected b/ql/test/library-tests/ast/Ast.expected index 7078ea3c675..26e31fac3f5 100644 --- a/ql/test/library-tests/ast/Ast.expected +++ b/ql/test/library-tests/ast/Ast.expected @@ -34,9 +34,9 @@ calls/calls.rb: # 26| getStmt: [AddExpr] ... + ... # 26| getAnOperand/getLeftOperand: [LocalVariableAccess] x # 26| getAnOperand/getRightOperand: [IntegerLiteral] 1 -# 30| getStmt: [Method] method_that_yields +# 30| getStmt: [MethodDeclaration] method_that_yields # 31| getStmt: [YieldCall] yield ... -# 35| getStmt: [Method] another_method_that_yields +# 35| getStmt: [MethodDeclaration] another_method_that_yields # 36| getStmt: [YieldCall] yield ... # 36| getArgument: [IntegerLiteral] 100 # 36| getArgument: [IntegerLiteral] 200 @@ -145,7 +145,7 @@ calls/calls.rb: # 131| getReceiver: [ConstantReadAccess] X # 132| getStmt: [MethodCall] call to bar # 132| getReceiver: [ConstantReadAccess] X -# 136| getStmt: [Method] some_method +# 136| getStmt: [MethodDeclaration] some_method # 137| getStmt: [MethodCall] call to foo # 138| getStmt: [MethodCall] call to bar # 138| getReceiver: [ConstantReadAccess] X @@ -154,20 +154,20 @@ calls/calls.rb: # 143| getStmt: [MethodCall] call to bar # 144| getStmt: [MethodCall] call to baz # 144| getReceiver: [ConstantReadAccess] X -# 148| getStmt: [Method] method_with_keyword_param +# 148| getStmt: [MethodDeclaration] method_with_keyword_param # 148| getParameter: [KeywordParameter] keyword # 148| getDefiningAccess: [LocalVariableAccess] keyword # 148| getDefaultValue: [MethodCall] call to foo -# 150| getStmt: [Method] method_with_keyword_param2 +# 150| getStmt: [MethodDeclaration] method_with_keyword_param2 # 150| getParameter: [KeywordParameter] keyword # 150| getDefiningAccess: [LocalVariableAccess] keyword # 150| getDefaultValue: [MethodCall] call to foo # 150| getReceiver: [ConstantReadAccess] X -# 154| getStmt: [Method] method_with_optional_param +# 154| getStmt: [MethodDeclaration] method_with_optional_param # 154| getParameter: [OptionalParameter] param # 154| getDefiningAccess: [LocalVariableAccess] param # 154| getDefaultValue: [MethodCall] call to foo -# 156| getStmt: [Method] method_with_optional_param2 +# 156| getStmt: [MethodDeclaration] method_with_optional_param2 # 156| getParameter: [OptionalParameter] param # 156| getDefiningAccess: [LocalVariableAccess] param # 156| getDefaultValue: [MethodCall] call to foo @@ -377,7 +377,7 @@ calls/calls.rb: # 279| getValue: [MethodCall] call to bar # 279| getReceiver: [ConstantReadAccess] X # 284| getStmt: [ClassDeclaration] MyClass -# 285| getStmt: [Method] my_method +# 285| getStmt: [MethodDeclaration] my_method # 286| getStmt: [SuperCall] call to super # 287| getStmt: [SuperCall] call to super # 288| getStmt: [SuperCall] call to super @@ -420,7 +420,7 @@ calls/calls.rb: # 293| getAnOperand/getLeftOperand: [LocalVariableAccess] x # 293| getAnOperand/getRightOperand: [IntegerLiteral] 200 # 301| getStmt: [ClassDeclaration] AnotherClass -# 302| getStmt: [Method] another_method +# 302| getStmt: [MethodDeclaration] another_method # 303| getStmt: [MethodCall] call to super # 303| getReceiver: [MethodCall] call to foo # 304| getStmt: [MethodCall] call to super @@ -532,11 +532,11 @@ modules/classes.rb: # 16| getStmt: [ClassDeclaration] MyClass # 16| getScopeExpr: [ConstantReadAccess] MyModule # 20| getStmt: [ClassDeclaration] Wibble -# 21| getStmt: [Method] method_a +# 21| getStmt: [MethodDeclaration] method_a # 22| getStmt: [MethodCall] call to puts # 22| getArgument: [StringLiteral] "a" # 22| getComponent: [StringTextComponent] a -# 25| getStmt: [Method] method_b +# 25| getStmt: [MethodDeclaration] method_b # 26| getStmt: [MethodCall] call to puts # 26| getArgument: [StringLiteral] "b" # 26| getComponent: [StringTextComponent] b @@ -552,11 +552,11 @@ modules/classes.rb: # 40| getComponent: [StringTextComponent] hello # 41| getStmt: [ClassDeclaration] class << ... # 41| getValue: [LocalVariableAccess] x -# 42| getStmt: [Method] length +# 42| getStmt: [MethodDeclaration] length # 43| getStmt: [MulExpr] ... * ... # 43| getAnOperand/getLeftOperand: [IntegerLiteral] 100 # 43| getAnOperand/getRightOperand: [SuperCall] call to super -# 46| getStmt: [Method] wibble +# 46| getStmt: [MethodDeclaration] wibble # 47| getStmt: [MethodCall] call to puts # 47| getArgument: [StringLiteral] "wibble" # 47| getComponent: [StringTextComponent] wibble @@ -700,7 +700,7 @@ constants/constants.rb: # 14| getAnOperand/getLeftOperand: [ConstantAssignment] GREETING # 14| getAnOperand/getRightOperand: [StringLiteral] "Hello" # 14| getComponent: [StringTextComponent] Hello -# 16| getStmt: [Method] foo +# 16| getStmt: [MethodDeclaration] foo # 17| getStmt: [AssignExpr] ... = ... # 17| getAnOperand/getLeftOperand: [ConstantAssignment] Names # 17| getAnOperand/getRightOperand: [ArrayLiteral] [...] @@ -1096,7 +1096,7 @@ literals/literals.rb: # 154| getStmt: [MethodCall] call to name # 154| getComponent: [StringTextComponent] # 154| -# 157| getStmt: [Method] m +# 157| getStmt: [MethodDeclaration] m # 158| getStmt: [AssignExpr] ... = ... # 158| getAnOperand/getLeftOperand: [LocalVariableAccess] query # 158| getAnOperand/getRightOperand: [HereDoc] <<-BLA @@ -1326,14 +1326,14 @@ modules/modules.rb: # 4| getStmt: [ModuleDeclaration] Foo # 5| getStmt: [ModuleDeclaration] Bar # 6| getStmt: [ClassDeclaration] ClassInFooBar -# 9| getStmt: [Method] method_in_foo_bar +# 9| getStmt: [MethodDeclaration] method_in_foo_bar # 12| getStmt: [MethodCall] call to puts # 12| getArgument: [StringLiteral] "module Foo::Bar" # 12| getComponent: [StringTextComponent] module Foo::Bar # 13| getStmt: [AssignExpr] ... = ... # 13| getAnOperand/getLeftOperand: [GlobalVariableAccess] $global_var # 13| getAnOperand/getRightOperand: [IntegerLiteral] 0 -# 16| getStmt: [Method] method_in_foo +# 16| getStmt: [MethodDeclaration] method_in_foo # 19| getStmt: [ClassDeclaration] ClassInFoo # 22| getStmt: [MethodCall] call to puts # 22| getArgument: [StringLiteral] "module Foo" @@ -1342,7 +1342,7 @@ modules/modules.rb: # 23| getAnOperand/getLeftOperand: [GlobalVariableAccess] $global_var # 23| getAnOperand/getRightOperand: [IntegerLiteral] 1 # 26| getStmt: [ModuleDeclaration] Foo -# 27| getStmt: [Method] method_in_another_definition_of_foo +# 27| getStmt: [MethodDeclaration] method_in_another_definition_of_foo # 30| getStmt: [ClassDeclaration] ClassInAnotherDefinitionOfFoo # 33| getStmt: [MethodCall] call to puts # 33| getArgument: [StringLiteral] "module Foo again" @@ -1351,8 +1351,8 @@ modules/modules.rb: # 34| getAnOperand/getLeftOperand: [GlobalVariableAccess] $global_var # 34| getAnOperand/getRightOperand: [IntegerLiteral] 2 # 37| getStmt: [ModuleDeclaration] Bar -# 38| getStmt: [Method] method_a -# 41| getStmt: [Method] method_b +# 38| getStmt: [MethodDeclaration] method_a +# 41| getStmt: [MethodDeclaration] method_b # 44| getStmt: [MethodCall] call to puts # 44| getArgument: [StringLiteral] "module Bar" # 44| getComponent: [StringTextComponent] module Bar @@ -1362,7 +1362,7 @@ modules/modules.rb: # 48| getStmt: [ModuleDeclaration] Bar # 48| getScopeExpr: [ConstantReadAccess] Foo # 49| getStmt: [ClassDeclaration] ClassInAnotherDefinitionOfFooBar -# 52| getStmt: [Method] method_in_another_definition_of_foo_bar +# 52| getStmt: [MethodDeclaration] method_in_another_definition_of_foo_bar # 55| getStmt: [MethodCall] call to puts # 55| getArgument: [StringLiteral] "module Foo::Bar again" # 55| getComponent: [StringTextComponent] module Foo::Bar again @@ -1603,7 +1603,7 @@ operations/operations.rb: # 84| getAnOperand/getRightOperand: [LocalVariableAccess] qux params/params.rb: # 1| [Toplevel] params.rb -# 4| getStmt: [Method] identifier_method_params +# 4| getStmt: [MethodDeclaration] identifier_method_params # 4| getParameter: [SimpleParameter] foo # 4| getDefiningAccess: [LocalVariableAccess] foo # 4| getParameter: [SimpleParameter] bar @@ -1637,7 +1637,7 @@ params/params.rb: # 14| getStmt: [AddExpr] ... + ... # 14| getAnOperand/getLeftOperand: [LocalVariableAccess] foo # 14| getAnOperand/getRightOperand: [LocalVariableAccess] bar -# 17| getStmt: [Method] destructured_method_param +# 17| getStmt: [MethodDeclaration] destructured_method_param # 17| getParameter: [TuplePatternParameter] (..., ...) # 17| getElement: [LocalVariableAccess] a # 17| getElement: [LocalVariableAccess] b @@ -1671,7 +1671,7 @@ params/params.rb: # 26| getAnOperand/getRightOperand: [LocalVariableAccess] second # 26| getAnOperand/getRightOperand: [LocalVariableAccess] third # 26| getAnOperand/getRightOperand: [LocalVariableAccess] fourth -# 30| getStmt: [Method] method_with_splat +# 30| getStmt: [MethodDeclaration] method_with_splat # 30| getParameter: [SimpleParameter] wibble # 30| getDefiningAccess: [LocalVariableAccess] wibble # 30| getParameter: [SplatParameter] *splat @@ -1696,7 +1696,7 @@ params/params.rb: # 38| getDefiningAccess: [LocalVariableAccess] blah # 38| getParameter: [HashSplatParameter] **wibble # 38| getDefiningAccess: [LocalVariableAccess] wibble -# 41| getStmt: [Method] method_with_keyword_params +# 41| getStmt: [MethodDeclaration] method_with_keyword_params # 41| getParameter: [SimpleParameter] x # 41| getDefiningAccess: [LocalVariableAccess] x # 41| getParameter: [KeywordParameter] foo @@ -1709,7 +1709,7 @@ params/params.rb: # 42| getAnOperand/getLeftOperand: [LocalVariableAccess] x # 42| getAnOperand/getRightOperand: [LocalVariableAccess] foo # 42| getAnOperand/getRightOperand: [LocalVariableAccess] bar -# 46| getStmt: [Method] use_block_with_keyword +# 46| getStmt: [MethodDeclaration] use_block_with_keyword # 46| getParameter: [BlockParameter] &block # 46| getDefiningAccess: [LocalVariableAccess] block # 47| getStmt: [MethodCall] call to puts @@ -1746,7 +1746,7 @@ params/params.rb: # 54| getAnOperand/getLeftOperand: [LocalVariableAccess] x # 54| getAnOperand/getRightOperand: [LocalVariableAccess] y # 54| getAnOperand/getRightOperand: [LocalVariableAccess] z -# 58| getStmt: [Method] method_with_optional_params +# 58| getStmt: [MethodDeclaration] method_with_optional_params # 58| getParameter: [SimpleParameter] val1 # 58| getDefiningAccess: [LocalVariableAccess] val1 # 58| getParameter: [OptionalParameter] val2 @@ -1755,7 +1755,7 @@ params/params.rb: # 58| getParameter: [OptionalParameter] val3 # 58| getDefiningAccess: [LocalVariableAccess] val3 # 58| getDefaultValue: [IntegerLiteral] 100 -# 62| getStmt: [Method] use_block_with_optional +# 62| getStmt: [MethodDeclaration] use_block_with_optional # 62| getParameter: [BlockParameter] &block # 62| getDefiningAccess: [LocalVariableAccess] block # 63| getStmt: [MethodCall] call to call diff --git a/ql/test/library-tests/ast/modules/classes.expected b/ql/test/library-tests/ast/modules/classes.expected index e81722ecfa2..08543bb9fb0 100644 --- a/ql/test/library-tests/ast/modules/classes.expected +++ b/ql/test/library-tests/ast/modules/classes.expected @@ -23,8 +23,8 @@ classesWithNameScopeExprs classesWithGlobalNameScopeExprs | classes.rb:55:1:56:3 | MyClassInGlobalScope | exprsInClasses -| classes.rb:20:1:37:3 | Wibble | 0 | classes.rb:21:3:23:5 | method_a | Method | -| classes.rb:20:1:37:3 | Wibble | 1 | classes.rb:25:3:27:5 | method_b | Method | +| classes.rb:20:1:37:3 | Wibble | 0 | classes.rb:21:3:23:5 | method_a | MethodDeclaration | +| classes.rb:20:1:37:3 | Wibble | 1 | classes.rb:25:3:27:5 | method_b | MethodDeclaration | | classes.rb:20:1:37:3 | Wibble | 2 | classes.rb:29:3:29:20 | call to some_method_call | MethodCall | | classes.rb:20:1:37:3 | Wibble | 3 | classes.rb:30:3:30:19 | ... = ... | AssignExpr | | classes.rb:20:1:37:3 | Wibble | 4 | classes.rb:32:3:33:5 | ClassInWibble | ClassDeclaration | diff --git a/ql/test/library-tests/ast/modules/classes.ql b/ql/test/library-tests/ast/modules/classes.ql index a4b392a32cb..ed6b8ad45b1 100644 --- a/ql/test/library-tests/ast/modules/classes.ql +++ b/ql/test/library-tests/ast/modules/classes.ql @@ -12,7 +12,7 @@ query predicate exprsInClasses(ClassDeclaration c, int i, Expr e, string eClass) e = c.getStmt(i) and eClass = e.getAPrimaryQlClass() } -query predicate methodsInClasses(ClassDeclaration c, Method m, string name) { +query predicate methodsInClasses(ClassDeclaration c, MethodDeclaration m, string name) { m = c.getMethod(name) } diff --git a/ql/test/library-tests/ast/modules/module_base.ql b/ql/test/library-tests/ast/modules/module_base.ql index e95faa62969..859fe2d8bc7 100644 --- a/ql/test/library-tests/ast/modules/module_base.ql +++ b/ql/test/library-tests/ast/modules/module_base.ql @@ -4,6 +4,6 @@ query predicate moduleBases(ModuleBase mb, string pClass) { pClass = mb.getAPrim query predicate moduleBaseClasses(ModuleBase mb, ClassDeclaration c) { c = mb.getAClass() } -query predicate moduleBaseMethods(ModuleBase mb, Method m) { m = mb.getAMethod() } +query predicate moduleBaseMethods(ModuleBase mb, MethodDeclaration m) { m = mb.getAMethod() } query predicate moduleBaseModules(ModuleBase mb, ModuleDeclaration m) { m = mb.getAModule() } diff --git a/ql/test/library-tests/ast/modules/modules.expected b/ql/test/library-tests/ast/modules/modules.expected index f7f52051674..4226cddf806 100644 --- a/ql/test/library-tests/ast/modules/modules.expected +++ b/ql/test/library-tests/ast/modules/modules.expected @@ -36,24 +36,24 @@ modulesWithGlobalNameScopeExprs | modules.rb:60:1:61:3 | MyModuleInGlobalScope | exprsInModules | modules.rb:4:1:24:3 | Foo | 0 | modules.rb:5:3:14:5 | Bar | ModuleDeclaration | -| modules.rb:4:1:24:3 | Foo | 1 | modules.rb:16:3:17:5 | method_in_foo | Method | +| modules.rb:4:1:24:3 | Foo | 1 | modules.rb:16:3:17:5 | method_in_foo | MethodDeclaration | | modules.rb:4:1:24:3 | Foo | 2 | modules.rb:19:3:20:5 | ClassInFoo | ClassDeclaration | | modules.rb:4:1:24:3 | Foo | 3 | modules.rb:22:3:22:19 | call to puts | MethodCall | | modules.rb:4:1:24:3 | Foo | 4 | modules.rb:23:3:23:17 | ... = ... | AssignExpr | | modules.rb:5:3:14:5 | Bar | 0 | modules.rb:6:5:7:7 | ClassInFooBar | ClassDeclaration | -| modules.rb:5:3:14:5 | Bar | 1 | modules.rb:9:5:10:7 | method_in_foo_bar | Method | +| modules.rb:5:3:14:5 | Bar | 1 | modules.rb:9:5:10:7 | method_in_foo_bar | MethodDeclaration | | modules.rb:5:3:14:5 | Bar | 2 | modules.rb:12:5:12:26 | call to puts | MethodCall | | modules.rb:5:3:14:5 | Bar | 3 | modules.rb:13:5:13:19 | ... = ... | AssignExpr | -| modules.rb:26:1:35:3 | Foo | 0 | modules.rb:27:3:28:5 | method_in_another_definition_of_foo | Method | +| modules.rb:26:1:35:3 | Foo | 0 | modules.rb:27:3:28:5 | method_in_another_definition_of_foo | MethodDeclaration | | modules.rb:26:1:35:3 | Foo | 1 | modules.rb:30:3:31:5 | ClassInAnotherDefinitionOfFoo | ClassDeclaration | | modules.rb:26:1:35:3 | Foo | 2 | modules.rb:33:3:33:25 | call to puts | MethodCall | | modules.rb:26:1:35:3 | Foo | 3 | modules.rb:34:3:34:17 | ... = ... | AssignExpr | -| modules.rb:37:1:46:3 | Bar | 0 | modules.rb:38:3:39:5 | method_a | Method | -| modules.rb:37:1:46:3 | Bar | 1 | modules.rb:41:3:42:5 | method_b | Method | +| modules.rb:37:1:46:3 | Bar | 0 | modules.rb:38:3:39:5 | method_a | MethodDeclaration | +| modules.rb:37:1:46:3 | Bar | 1 | modules.rb:41:3:42:5 | method_b | MethodDeclaration | | modules.rb:37:1:46:3 | Bar | 2 | modules.rb:44:3:44:19 | call to puts | MethodCall | | modules.rb:37:1:46:3 | Bar | 3 | modules.rb:45:3:45:17 | ... = ... | AssignExpr | | modules.rb:48:1:57:3 | Bar | 0 | modules.rb:49:3:50:5 | ClassInAnotherDefinitionOfFooBar | ClassDeclaration | -| modules.rb:48:1:57:3 | Bar | 1 | modules.rb:52:3:53:5 | method_in_another_definition_of_foo_bar | Method | +| modules.rb:48:1:57:3 | Bar | 1 | modules.rb:52:3:53:5 | method_in_another_definition_of_foo_bar | MethodDeclaration | | modules.rb:48:1:57:3 | Bar | 2 | modules.rb:55:3:55:30 | call to puts | MethodCall | | modules.rb:48:1:57:3 | Bar | 3 | modules.rb:56:3:56:17 | ... = ... | AssignExpr | | modules.rb:63:1:81:3 | Test | 0 | modules.rb:65:3:68:5 | Foo1 | ModuleDeclaration | diff --git a/ql/test/library-tests/ast/modules/modules.ql b/ql/test/library-tests/ast/modules/modules.ql index 21adb23e633..b10b99c55a5 100644 --- a/ql/test/library-tests/ast/modules/modules.ql +++ b/ql/test/library-tests/ast/modules/modules.ql @@ -12,7 +12,7 @@ query predicate exprsInModules(ModuleDeclaration m, int i, Expr e, string eClass e = m.getStmt(i) and eClass = e.getAPrimaryQlClass() } -query predicate methodsInModules(ModuleDeclaration mod, Method method, string name) { +query predicate methodsInModules(ModuleDeclaration mod, MethodDeclaration method, string name) { method = mod.getMethod(name) } diff --git a/ql/test/library-tests/ast/modules/singleton_classes.expected b/ql/test/library-tests/ast/modules/singleton_classes.expected index f1c13b476d6..2b11f88cfe1 100644 --- a/ql/test/library-tests/ast/modules/singleton_classes.expected +++ b/ql/test/library-tests/ast/modules/singleton_classes.expected @@ -1,8 +1,8 @@ singletonClasses | classes.rb:41:1:52:3 | class << ... | ClassDeclaration | classes.rb:41:10:41:10 | x | exprsInSingletonClasses -| classes.rb:41:1:52:3 | class << ... | 0 | classes.rb:42:3:44:5 | length | Method | -| classes.rb:41:1:52:3 | class << ... | 1 | classes.rb:46:3:48:5 | wibble | Method | +| classes.rb:41:1:52:3 | class << ... | 0 | classes.rb:42:3:44:5 | length | MethodDeclaration | +| classes.rb:41:1:52:3 | class << ... | 1 | classes.rb:46:3:48:5 | wibble | MethodDeclaration | | classes.rb:41:1:52:3 | class << ... | 2 | classes.rb:50:3:50:21 | call to another_method_call | MethodCall | | classes.rb:41:1:52:3 | class << ... | 3 | classes.rb:51:3:51:20 | ... = ... | AssignExpr | methodsInSingletonClasses diff --git a/ql/test/library-tests/ast/modules/singleton_classes.ql b/ql/test/library-tests/ast/modules/singleton_classes.ql index d02dd8cb68c..850007c0071 100644 --- a/ql/test/library-tests/ast/modules/singleton_classes.ql +++ b/ql/test/library-tests/ast/modules/singleton_classes.ql @@ -8,4 +8,6 @@ query predicate exprsInSingletonClasses(SingletonClass sc, int i, Expr e, string e = sc.getStmt(i) and eClass = e.getAPrimaryQlClass() } -query predicate methodsInSingletonClasses(SingletonClass sc, Method m) { m = sc.getAMethod() } +query predicate methodsInSingletonClasses(SingletonClass sc, MethodDeclaration m) { + m = sc.getAMethod() +} diff --git a/ql/test/library-tests/ast/params/params.ql b/ql/test/library-tests/ast/params/params.ql index eb77ac0e418..28913458b9b 100644 --- a/ql/test/library-tests/ast/params/params.ql +++ b/ql/test/library-tests/ast/params/params.ql @@ -28,7 +28,7 @@ query predicate optionalParams(OptionalParameter op, string name, AstNode defaul //////////////////////////////////////////////////////////////////////////////// // Query predicates for various contexts of parameters -query predicate paramsInMethods(Method m, int i, Parameter p, string pClass) { +query predicate paramsInMethods(MethodDeclaration m, int i, Parameter p, string pClass) { p = m.getParameter(i) and pClass = p.getAPrimaryQlClass() } From 5837af09362aab1a776367cb74c9fd6ead4ae4dd Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Tue, 13 Apr 2021 10:22:03 +0200 Subject: [PATCH 2/8] Add MethodBase::getMethod --- ql/src/codeql_ruby/ast/Method.qll | 41 ++++++++++++++++++++++ ql/src/codeql_ruby/ast/internal/Method.qll | 10 ++++++ 2 files changed, 51 insertions(+) create mode 100644 ql/src/codeql_ruby/ast/internal/Method.qll diff --git a/ql/src/codeql_ruby/ast/Method.qll b/ql/src/codeql_ruby/ast/Method.qll index f01eba6c323..988bc8fde06 100644 --- a/ql/src/codeql_ruby/ast/Method.qll +++ b/ql/src/codeql_ruby/ast/Method.qll @@ -1,8 +1,40 @@ private import codeql_ruby.AST private import codeql_ruby.controlflow.ControlFlowGraph private import internal.AST +private import internal.Method private import internal.TreeSitter +/** + * A representation of a method. + */ +class Method extends TMethod { + /** Get a declaration of this module, if any. */ + MethodBase getADeclaration() { result.getMethod() = this } + + /** Gets a textual representation of this method. */ + string toString() { + exists(Module m, string name | + this = TInstanceMethod(m, name) and result = m.toString() + "." + name + ) + } + + /** Gets the location of this method. */ + Location getLocation() { + result = + min(MethodBase decl, Module m, string name, Location loc, int weight | + this = TInstanceMethod(m, name) and + decl = methodDeclaration(m, name) and + loc = decl.getLocation() and + if exists(loc.getFile().getRelativePath()) then weight = 0 else weight = 1 + | + loc + order by + weight, count(decl.getAStmt()) desc, loc.getFile().getAbsolutePath(), loc.getStartLine(), + loc.getStartColumn() + ) + } +} + /** A callable. */ class Callable extends Expr, Scope, TCallable { /** Gets the number of parameters of this callable. */ @@ -24,6 +56,9 @@ class MethodBase extends Callable, BodyStmt, Scope, TMethodBase { /** Gets the name of this method. */ string getName() { none() } + /** Gets the method defined by this declaration. */ + Method getMethod() { none() } + override AstNode getAChild(string pred) { result = Callable.super.getAChild(pred) or @@ -44,6 +79,12 @@ class MethodDeclaration extends MethodBase, TMethodDeclaration { result = g.getName().(Generated::Setter).getName().getValue() + "=" } + final override Method getMethod() { + exists(Module owner, string name | + result = TInstanceMethod(owner, name) and this = methodDeclaration(owner, name) + ) + } + /** * Holds if this is a setter method, as in the following example: * ```rb diff --git a/ql/src/codeql_ruby/ast/internal/Method.qll b/ql/src/codeql_ruby/ast/internal/Method.qll new file mode 100644 index 00000000000..82e0f53d47f --- /dev/null +++ b/ql/src/codeql_ruby/ast/internal/Method.qll @@ -0,0 +1,10 @@ +private import codeql_ruby.ast.Method +private import codeql_ruby.ast.Module +private import codeql_ruby.ast.internal.Module + +newtype TMethod = + TInstanceMethod(TModule owner, string name) { exists(methodDeclaration(owner, name)) } + +MethodDeclaration methodDeclaration(TModule owner, string name) { + exists(ModuleBase m | m.getModule() = owner and result = m.getMethod(name)) +} From bf556a2b536f3ef7bc562f69a4adafe8a9cf8606 Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Tue, 13 Apr 2021 16:48:06 +0200 Subject: [PATCH 3/8] Implement method lookup --- ql/src/codeql_ruby/ast/Method.qll | 6 ++ ql/src/codeql_ruby/ast/Module.qll | 15 ++++ ql/src/codeql_ruby/ast/internal/Module.qll | 84 ++++++++++++++++++++++ 3 files changed, 105 insertions(+) diff --git a/ql/src/codeql_ruby/ast/Method.qll b/ql/src/codeql_ruby/ast/Method.qll index 988bc8fde06..04eeaff9a1f 100644 --- a/ql/src/codeql_ruby/ast/Method.qll +++ b/ql/src/codeql_ruby/ast/Method.qll @@ -11,6 +11,12 @@ class Method extends TMethod { /** Get a declaration of this module, if any. */ MethodBase getADeclaration() { result.getMethod() = this } + /** Get the name of this method */ + string getName() { this = TInstanceMethod(_, result) } + + /** Get the module in which this method is defined */ + Module getModule() { this = TInstanceMethod(result, _) } + /** Gets a textual representation of this method. */ string toString() { exists(Module m, string name | diff --git a/ql/src/codeql_ruby/ast/Module.qll b/ql/src/codeql_ruby/ast/Module.qll index d449a576e4a..6e90a9f87db 100644 --- a/ql/src/codeql_ruby/ast/Module.qll +++ b/ql/src/codeql_ruby/ast/Module.qll @@ -11,6 +11,21 @@ class Module extends TModule { /** Get a declaration of this module, if any. */ ModuleBase getADeclaration() { result.getModule() = this } + /** Get the super class of this module, if any. */ + Module getSuperClass() { result = getSuperClass(this) } + + /** Get a method defined in this module by name. */ + Method getMethod(string name) { result.getName() = name and result.getModule() = this } + + /** Look up a method in this module's ancestor chain. */ + Method lookupMethod(string name) { result = lookupMethod(this, name) } + + /** Get a `prepend`ed module. */ + Module getAPrependedModule() { result = getAPrependedModule(this) } + + /** Get an `include`d module. */ + Module getAnIncludedModule() { result = getAnIncludedModule(this) } + /** Gets a textual representation of this module. */ string toString() { this = TResolved(result) diff --git a/ql/src/codeql_ruby/ast/internal/Module.qll b/ql/src/codeql_ruby/ast/internal/Module.qll index a1f128d0a08..01a77f583d0 100644 --- a/ql/src/codeql_ruby/ast/internal/Module.qll +++ b/ql/src/codeql_ruby/ast/internal/Module.qll @@ -34,6 +34,66 @@ private module Cached { result = scopeAppend(container, n.getName()) ) } + + cached + Module getSuperClass(Module cls) { + cls = TResolved("Object") and result = TResolved("BasicObject") + or + cls = TResolved("Module") and result = TResolved("Object") + or + cls = TResolved("Class") and result = TResolved("Module") + or + not cls = TResolved(builtin()) and + ( + exists(ClassDeclaration d | + d = cls.getADeclaration() and + result = resolveScopeExpr(d.getSuperclassExpr()) + ) + or + result = TResolved("Object") and + forex(ClassDeclaration d | d = cls.getADeclaration() | + not exists(resolveScopeExpr(d.getSuperclassExpr())) + ) + ) + } + + cached + Module getAnIncludedModule(Module m) { + m = TResolved("Object") and result = TResolved("Kernel") + or + exists(IncludeOrPrependCall c | + c.getMethodName() = "include" and + ( + m = resolveScopeExpr(c.getReceiver()) + or + m = enclosingModule(c).getModule() and + ( + c.getReceiver() instanceof Self + or + not exists(c.getReceiver()) + ) + ) and + result = resolveScopeExpr(c.getAnArgument()) + ) + } + + cached + Module getAPrependedModule(Module m) { + exists(IncludeOrPrependCall c | + c.getMethodName() = "prepend" and + ( + m = resolveScopeExpr(c.getReceiver()) + or + m = enclosingModule(c).getModule() and + ( + c.getReceiver() instanceof Self + or + not exists(c.getReceiver()) + ) + ) and + result = resolveScopeExpr(c.getAnArgument()) + ) + } } import Cached @@ -242,3 +302,27 @@ private string qualifiedModuleName(string container, string name) { container = "Object" and name = result ) } + +private Module getAncestors(Module m) { + result = m or + result = getAncestors(m.getAnIncludedModule()) or + result = getAncestors(m.getAPrependedModule()) +} + +private Method lookupMethod0(Module m, string name) { + result = lookupMethod0(m.getAPrependedModule(), name) + or + not exists(getAncestors(m.getAPrependedModule()).getMethod(name)) and + ( + result = m.getMethod(name) + or + not exists(m.getMethod(name)) and result = lookupMethod0(m.getAnIncludedModule(), name) + ) +} + +Method lookupMethod(Module m, string name) { + result = lookupMethod0(m, name) + or + not exists(lookupMethod0(m, name)) and + result = lookupMethod(m.getSuperClass(), name) +} From 07726fd979eaf06a269de3ca5b89e37bad885438 Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Fri, 16 Apr 2021 10:55:08 +0200 Subject: [PATCH 4/8] Add some module and method tests --- .../ast/modules/modules.expected | 46 ------- ql/test/library-tests/ast/modules/modules.ql | 2 - .../library-tests/modules/ancestors.expected | 96 ++++++++++++++ ql/test/library-tests/modules/ancestors.ql | 20 +++ ql/test/library-tests/modules/hello.rb | 22 ++++ .../library-tests/modules/methods.expected | 70 ++++++++++ ql/test/library-tests/modules/methods.ql | 13 ++ .../library-tests/modules/modules.expected | 100 ++++++++++++++ ql/test/library-tests/modules/modules.ql | 11 ++ ql/test/library-tests/modules/modules.rb | 122 ++++++++++++++++++ .../modules/superclasses.expected | 91 +++++++++++++ ql/test/library-tests/modules/superclasses.ql | 11 ++ 12 files changed, 556 insertions(+), 48 deletions(-) create mode 100644 ql/test/library-tests/modules/ancestors.expected create mode 100644 ql/test/library-tests/modules/ancestors.ql create mode 100644 ql/test/library-tests/modules/hello.rb create mode 100644 ql/test/library-tests/modules/methods.expected create mode 100644 ql/test/library-tests/modules/methods.ql create mode 100644 ql/test/library-tests/modules/modules.expected create mode 100644 ql/test/library-tests/modules/modules.ql create mode 100644 ql/test/library-tests/modules/modules.rb create mode 100644 ql/test/library-tests/modules/superclasses.expected create mode 100644 ql/test/library-tests/modules/superclasses.ql diff --git a/ql/test/library-tests/ast/modules/modules.expected b/ql/test/library-tests/ast/modules/modules.expected index 4226cddf806..89b33b76ef7 100644 --- a/ql/test/library-tests/ast/modules/modules.expected +++ b/ql/test/library-tests/ast/modules/modules.expected @@ -101,49 +101,3 @@ modulesInModules | modules.rb:95:1:99:3 | IncludeTest2 | modules.rb:97:3:98:5 | Z | Z | | modules.rb:101:1:105:3 | PrependTest | modules.rb:103:3:104:5 | Y | Y | | modules.rb:107:1:110:3 | MM | modules.rb:108:3:109:5 | MM | MM | -moduleTypes -| classes.rb:2:1:56:3 | classes.rb | file://:0:0:0:0 | Object | -| classes.rb:3:1:4:3 | Foo | modules.rb:4:1:24:3 | Foo | -| classes.rb:7:1:8:3 | Bar | modules.rb:37:1:46:3 | Bar | -| classes.rb:11:1:12:3 | Baz | classes.rb:11:1:12:3 | Baz | -| classes.rb:15:1:15:20 | MyModule | classes.rb:15:1:15:20 | MyModule | -| classes.rb:16:1:17:3 | MyClass | classes.rb:16:1:17:3 | MyModule::MyClass | -| classes.rb:20:1:37:3 | Wibble | classes.rb:20:1:37:3 | Wibble | -| classes.rb:32:3:33:5 | ClassInWibble | classes.rb:32:3:33:5 | Wibble::ClassInWibble | -| classes.rb:35:3:36:5 | ModuleInWibble | classes.rb:35:3:36:5 | Wibble::ModuleInWibble | -| classes.rb:55:1:56:3 | MyClassInGlobalScope | classes.rb:55:1:56:3 | MyClassInGlobalScope | -| modules.rb:1:1:2:3 | Empty | modules.rb:1:1:2:3 | Empty | -| modules.rb:1:1:122:1 | modules.rb | file://:0:0:0:0 | Object | -| modules.rb:4:1:24:3 | Foo | modules.rb:4:1:24:3 | Foo | -| modules.rb:5:3:14:5 | Bar | modules.rb:5:3:14:5 | Foo::Bar | -| modules.rb:6:5:7:7 | ClassInFooBar | modules.rb:6:5:7:7 | Foo::Bar::ClassInFooBar | -| modules.rb:19:3:20:5 | ClassInFoo | modules.rb:19:3:20:5 | Foo::ClassInFoo | -| modules.rb:26:1:35:3 | Foo | modules.rb:4:1:24:3 | Foo | -| modules.rb:30:3:31:5 | ClassInAnotherDefinitionOfFoo | modules.rb:30:3:31:5 | Foo::ClassInAnotherDefinitionOfFoo | -| modules.rb:37:1:46:3 | Bar | modules.rb:37:1:46:3 | Bar | -| modules.rb:48:1:57:3 | Bar | modules.rb:5:3:14:5 | Foo::Bar | -| modules.rb:49:3:50:5 | ClassInAnotherDefinitionOfFooBar | modules.rb:49:3:50:5 | Foo::Bar::ClassInAnotherDefinitionOfFooBar | -| modules.rb:60:1:61:3 | MyModuleInGlobalScope | modules.rb:60:1:61:3 | MyModuleInGlobalScope | -| modules.rb:63:1:81:3 | Test | modules.rb:63:1:81:3 | Test | -| modules.rb:65:3:68:5 | Foo1 | modules.rb:65:3:68:5 | Test::Foo1 | -| modules.rb:66:5:67:7 | Bar | modules.rb:66:5:67:7 | Test::Foo1::Bar | -| modules.rb:70:3:74:5 | Foo2 | modules.rb:70:3:74:5 | Test::Foo2 | -| modules.rb:71:5:71:19 | Foo2 | modules.rb:71:5:71:19 | Test::Foo2::Foo2 | -| modules.rb:72:5:73:7 | Bar | modules.rb:72:5:73:7 | Test::Foo2::Foo2::Bar | -| modules.rb:76:3:80:5 | Foo3 | modules.rb:76:3:80:5 | Test::Foo3 | -| modules.rb:78:5:79:7 | Bar | modules.rb:37:1:46:3 | Bar | -| modules.rb:83:1:86:3 | Other | modules.rb:83:1:86:3 | Other | -| modules.rb:84:3:85:5 | Foo1 | modules.rb:84:3:85:5 | Other::Foo1 | -| modules.rb:88:1:93:3 | IncludeTest | modules.rb:88:1:93:3 | IncludeTest | -| modules.rb:91:3:92:5 | Y | modules.rb:91:3:92:5 | Test::Foo1::Y | -| modules.rb:95:1:99:3 | IncludeTest2 | modules.rb:95:1:99:3 | IncludeTest2 | -| modules.rb:97:3:98:5 | Z | modules.rb:97:3:98:5 | Test::Foo1::Z | -| modules.rb:101:1:105:3 | PrependTest | modules.rb:101:1:105:3 | PrependTest | -| modules.rb:103:3:104:5 | Y | modules.rb:103:3:104:5 | Test::Foo2::Y | -| modules.rb:107:1:110:3 | MM | modules.rb:107:1:110:3 | MM | -| modules.rb:108:3:109:5 | MM | modules.rb:108:3:109:5 | MM::MM | -| modules.rb:112:1:113:3 | YY | modules.rb:112:1:113:3 | YY | -| modules.rb:115:1:118:3 | XX | modules.rb:115:1:118:3 | XX | -| modules.rb:116:7:117:9 | YY | modules.rb:116:7:117:9 | XX::YY | -| modules.rb:120:1:121:3 | Baz | modules.rb:120:1:121:3 | Test::Foo1::Bar::Baz | -| toplevel.rb:1:1:5:23 | toplevel.rb | file://:0:0:0:0 | Object | diff --git a/ql/test/library-tests/ast/modules/modules.ql b/ql/test/library-tests/ast/modules/modules.ql index b10b99c55a5..e5d1e15b2ca 100644 --- a/ql/test/library-tests/ast/modules/modules.ql +++ b/ql/test/library-tests/ast/modules/modules.ql @@ -23,5 +23,3 @@ query predicate classesInModules(ModuleDeclaration mod, ClassDeclaration klass, query predicate modulesInModules(ModuleDeclaration mod, ModuleDeclaration child, string name) { child = mod.getModule(name) } - -query predicate moduleTypes(ModuleBase def, Module type) { type = def.getModule() } diff --git a/ql/test/library-tests/modules/ancestors.expected b/ql/test/library-tests/modules/ancestors.expected new file mode 100644 index 00000000000..4d00bf4dd04 --- /dev/null +++ b/ql/test/library-tests/modules/ancestors.expected @@ -0,0 +1,96 @@ +#-----| Object +#-----| include -> Kernel +#-----| super -> BasicObject + +#-----| Class +#-----| super -> Module + +#-----| Kernel + +#-----| Module +#-----| super -> Object + +#-----| BasicObject + +hello.rb: +# 1| EnglishWords + +# 11| Greeting +#-----| super -> Object +#-----| include -> EnglishWords + +# 18| HelloWorld +#-----| super -> Greeting + +modules.rb: +# 1| Empty + +# 4| Foo + +# 37| Bar +#-----| super -> Object + +# 60| MyModuleInGlobalScope + +# 63| Test + +# 83| Other + +# 88| IncludeTest +#-----| include -> Test + +# 95| IncludeTest2 +#-----| include -> Test + +# 101| PrependTest +#-----| prepend -> Test + +# 107| MM + +# 112| YY +#-----| super -> Object + +# 115| XX + +# 5| Foo::Bar + +# 19| Foo::ClassInFoo +#-----| super -> Object + +# 30| Foo::ClassInAnotherDefinitionOfFoo +#-----| super -> Object + +# 65| Test::Foo1 + +# 70| Test::Foo2 + +# 76| Test::Foo3 + +# 84| Other::Foo1 + +# 116| XX::YY +#-----| super -> YY + +# 6| Foo::Bar::ClassInFooBar +#-----| super -> Object + +# 71| Test::Foo2::Foo2 + +# 108| MM::MM + +# 49| Foo::Bar::ClassInAnotherDefinitionOfFooBar +#-----| super -> Object + +# 66| Test::Foo1::Bar +#-----| super -> Object + +# 91| Test::Foo1::Y + +# 97| Test::Foo1::Z + +# 103| Test::Foo2::Y + +# 72| Test::Foo2::Foo2::Bar +#-----| super -> Object + +# 120| Test::Foo1::Bar::Baz diff --git a/ql/test/library-tests/modules/ancestors.ql b/ql/test/library-tests/modules/ancestors.ql new file mode 100644 index 00000000000..bbc4973fea8 --- /dev/null +++ b/ql/test/library-tests/modules/ancestors.ql @@ -0,0 +1,20 @@ +/** + * @kind graph + */ + +import ruby + +query predicate nodes(Module node, string key, string value) { + key = "semmle.label" and value = node.toString() +} + +query predicate edges(Module source, Module target, string key, string value) { + key = "semmle.label" and + ( + target = source.getSuperClass() and value = "super" + or + target = source.getAPrependedModule() and value = "prepend" + or + target = source.getAnIncludedModule() and value = "include" + ) +} diff --git a/ql/test/library-tests/modules/hello.rb b/ql/test/library-tests/modules/hello.rb new file mode 100644 index 00000000000..1cfa0f7e4f3 --- /dev/null +++ b/ql/test/library-tests/modules/hello.rb @@ -0,0 +1,22 @@ +module EnglishWords + def hello + return "hello" + end + def world + return "world" + end +end + + +class Greeting + include EnglishWords + def message + return hello + end +end + +class HelloWorld < Greeting + def message + return super + " " + world + "!" + end +end \ No newline at end of file diff --git a/ql/test/library-tests/modules/methods.expected b/ql/test/library-tests/modules/methods.expected new file mode 100644 index 00000000000..5f7d1a5a76a --- /dev/null +++ b/ql/test/library-tests/modules/methods.expected @@ -0,0 +1,70 @@ +method +| hello.rb:2:5:4:7 | EnglishWords.hello | +| hello.rb:5:5:7:7 | EnglishWords.world | +| hello.rb:13:5:15:7 | Greeting.message | +| hello.rb:19:5:21:7 | HelloWorld.message | +| modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | +| modules.rb:16:3:17:5 | Foo.method_in_foo | +| modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | +| modules.rb:38:3:39:5 | Bar.method_a | +| modules.rb:41:3:42:5 | Bar.method_b | +| modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | +getADeclaration +| hello.rb:2:5:4:7 | EnglishWords.hello | hello.rb:2:5:4:7 | hello | +| hello.rb:5:5:7:7 | EnglishWords.world | hello.rb:5:5:7:7 | world | +| hello.rb:13:5:15:7 | Greeting.message | hello.rb:13:5:15:7 | message | +| hello.rb:19:5:21:7 | HelloWorld.message | hello.rb:19:5:21:7 | message | +| modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | modules.rb:9:5:10:7 | method_in_foo_bar | +| modules.rb:16:3:17:5 | Foo.method_in_foo | modules.rb:16:3:17:5 | method_in_foo | +| modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | modules.rb:27:3:28:5 | method_in_another_definition_of_foo | +| modules.rb:38:3:39:5 | Bar.method_a | modules.rb:38:3:39:5 | method_a | +| modules.rb:41:3:42:5 | Bar.method_b | modules.rb:41:3:42:5 | method_b | +| modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | modules.rb:52:3:53:5 | method_in_another_definition_of_foo_bar | +getName +| hello.rb:2:5:4:7 | EnglishWords.hello | hello | +| hello.rb:5:5:7:7 | EnglishWords.world | world | +| hello.rb:13:5:15:7 | Greeting.message | message | +| hello.rb:19:5:21:7 | HelloWorld.message | message | +| modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | method_in_foo_bar | +| modules.rb:16:3:17:5 | Foo.method_in_foo | method_in_foo | +| modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | method_in_another_definition_of_foo | +| modules.rb:38:3:39:5 | Bar.method_a | method_a | +| modules.rb:41:3:42:5 | Bar.method_b | method_b | +| modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | method_in_another_definition_of_foo_bar | +getModule +| hello.rb:2:5:4:7 | EnglishWords.hello | hello.rb:1:1:8:3 | EnglishWords | +| hello.rb:5:5:7:7 | EnglishWords.world | hello.rb:1:1:8:3 | EnglishWords | +| hello.rb:13:5:15:7 | Greeting.message | hello.rb:11:1:16:3 | Greeting | +| hello.rb:19:5:21:7 | HelloWorld.message | hello.rb:18:1:22:3 | HelloWorld | +| modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | modules.rb:5:3:14:5 | Foo::Bar | +| modules.rb:16:3:17:5 | Foo.method_in_foo | modules.rb:4:1:24:3 | Foo | +| modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | modules.rb:4:1:24:3 | Foo | +| modules.rb:38:3:39:5 | Bar.method_a | modules.rb:37:1:46:3 | Bar | +| modules.rb:41:3:42:5 | Bar.method_b | modules.rb:37:1:46:3 | Bar | +| modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | modules.rb:5:3:14:5 | Foo::Bar | +getMethod +| hello.rb:1:1:8:3 | EnglishWords | hello | hello.rb:2:5:4:7 | EnglishWords.hello | +| hello.rb:1:1:8:3 | EnglishWords | world | hello.rb:5:5:7:7 | EnglishWords.world | +| hello.rb:11:1:16:3 | Greeting | message | hello.rb:13:5:15:7 | Greeting.message | +| hello.rb:18:1:22:3 | HelloWorld | message | hello.rb:19:5:21:7 | HelloWorld.message | +| modules.rb:4:1:24:3 | Foo | method_in_another_definition_of_foo | modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | +| modules.rb:4:1:24:3 | Foo | method_in_foo | modules.rb:16:3:17:5 | Foo.method_in_foo | +| modules.rb:5:3:14:5 | Foo::Bar | method_in_another_definition_of_foo_bar | modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | +| modules.rb:5:3:14:5 | Foo::Bar | method_in_foo_bar | modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | +| modules.rb:37:1:46:3 | Bar | method_a | modules.rb:38:3:39:5 | Bar.method_a | +| modules.rb:37:1:46:3 | Bar | method_b | modules.rb:41:3:42:5 | Bar.method_b | +lookupMethod +| hello.rb:1:1:8:3 | EnglishWords | hello | hello.rb:2:5:4:7 | EnglishWords.hello | +| hello.rb:1:1:8:3 | EnglishWords | world | hello.rb:5:5:7:7 | EnglishWords.world | +| hello.rb:11:1:16:3 | Greeting | hello | hello.rb:2:5:4:7 | EnglishWords.hello | +| hello.rb:11:1:16:3 | Greeting | message | hello.rb:13:5:15:7 | Greeting.message | +| hello.rb:11:1:16:3 | Greeting | world | hello.rb:5:5:7:7 | EnglishWords.world | +| hello.rb:18:1:22:3 | HelloWorld | hello | hello.rb:2:5:4:7 | EnglishWords.hello | +| hello.rb:18:1:22:3 | HelloWorld | message | hello.rb:19:5:21:7 | HelloWorld.message | +| hello.rb:18:1:22:3 | HelloWorld | world | hello.rb:5:5:7:7 | EnglishWords.world | +| modules.rb:4:1:24:3 | Foo | method_in_another_definition_of_foo | modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | +| modules.rb:4:1:24:3 | Foo | method_in_foo | modules.rb:16:3:17:5 | Foo.method_in_foo | +| modules.rb:5:3:14:5 | Foo::Bar | method_in_another_definition_of_foo_bar | modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | +| modules.rb:5:3:14:5 | Foo::Bar | method_in_foo_bar | modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | +| modules.rb:37:1:46:3 | Bar | method_a | modules.rb:38:3:39:5 | Bar.method_a | +| modules.rb:37:1:46:3 | Bar | method_b | modules.rb:41:3:42:5 | Bar.method_b | diff --git a/ql/test/library-tests/modules/methods.ql b/ql/test/library-tests/modules/methods.ql new file mode 100644 index 00000000000..1bb7296026d --- /dev/null +++ b/ql/test/library-tests/modules/methods.ql @@ -0,0 +1,13 @@ +import ruby + +query Method method() { any() } + +query MethodBase getADeclaration(Method m) { result = m.getADeclaration() } + +query string getName(Method m) { result = m.getName() } + +query Module getModule(Method m) { result = m.getModule() } + +query Method getMethod(Module m, string name) { result = m.getMethod(name) } + +query Method lookupMethod(Module m, string name) { result = m.lookupMethod(name) } diff --git a/ql/test/library-tests/modules/modules.expected b/ql/test/library-tests/modules/modules.expected new file mode 100644 index 00000000000..fc802f224d7 --- /dev/null +++ b/ql/test/library-tests/modules/modules.expected @@ -0,0 +1,100 @@ +getModule +| file://:0:0:0:0 | BasicObject | +| file://:0:0:0:0 | Class | +| file://:0:0:0:0 | Kernel | +| file://:0:0:0:0 | Module | +| file://:0:0:0:0 | Object | +| hello.rb:1:1:8:3 | EnglishWords | +| hello.rb:11:1:16:3 | Greeting | +| hello.rb:18:1:22:3 | HelloWorld | +| modules.rb:1:1:2:3 | Empty | +| modules.rb:4:1:24:3 | Foo | +| modules.rb:5:3:14:5 | Foo::Bar | +| modules.rb:6:5:7:7 | Foo::Bar::ClassInFooBar | +| modules.rb:19:3:20:5 | Foo::ClassInFoo | +| modules.rb:30:3:31:5 | Foo::ClassInAnotherDefinitionOfFoo | +| modules.rb:37:1:46:3 | Bar | +| modules.rb:49:3:50:5 | Foo::Bar::ClassInAnotherDefinitionOfFooBar | +| modules.rb:60:1:61:3 | MyModuleInGlobalScope | +| modules.rb:63:1:81:3 | Test | +| modules.rb:65:3:68:5 | Test::Foo1 | +| modules.rb:66:5:67:7 | Test::Foo1::Bar | +| modules.rb:70:3:74:5 | Test::Foo2 | +| modules.rb:71:5:71:19 | Test::Foo2::Foo2 | +| modules.rb:72:5:73:7 | Test::Foo2::Foo2::Bar | +| modules.rb:76:3:80:5 | Test::Foo3 | +| modules.rb:83:1:86:3 | Other | +| modules.rb:84:3:85:5 | Other::Foo1 | +| modules.rb:88:1:93:3 | IncludeTest | +| modules.rb:91:3:92:5 | Test::Foo1::Y | +| modules.rb:95:1:99:3 | IncludeTest2 | +| modules.rb:97:3:98:5 | Test::Foo1::Z | +| modules.rb:101:1:105:3 | PrependTest | +| modules.rb:103:3:104:5 | Test::Foo2::Y | +| modules.rb:107:1:110:3 | MM | +| modules.rb:108:3:109:5 | MM::MM | +| modules.rb:112:1:113:3 | YY | +| modules.rb:115:1:118:3 | XX | +| modules.rb:116:7:117:9 | XX::YY | +| modules.rb:120:1:121:3 | Test::Foo1::Bar::Baz | +getADeclaration +| file://:0:0:0:0 | Object | hello.rb:1:1:22:3 | hello.rb | +| file://:0:0:0:0 | Object | modules.rb:1:1:122:1 | modules.rb | +| hello.rb:1:1:8:3 | EnglishWords | hello.rb:1:1:8:3 | EnglishWords | +| hello.rb:11:1:16:3 | Greeting | hello.rb:11:1:16:3 | Greeting | +| hello.rb:18:1:22:3 | HelloWorld | hello.rb:18:1:22:3 | HelloWorld | +| modules.rb:1:1:2:3 | Empty | modules.rb:1:1:2:3 | Empty | +| modules.rb:4:1:24:3 | Foo | modules.rb:4:1:24:3 | Foo | +| modules.rb:4:1:24:3 | Foo | modules.rb:26:1:35:3 | Foo | +| modules.rb:5:3:14:5 | Foo::Bar | modules.rb:5:3:14:5 | Bar | +| modules.rb:5:3:14:5 | Foo::Bar | modules.rb:48:1:57:3 | Bar | +| modules.rb:6:5:7:7 | Foo::Bar::ClassInFooBar | modules.rb:6:5:7:7 | ClassInFooBar | +| modules.rb:19:3:20:5 | Foo::ClassInFoo | modules.rb:19:3:20:5 | ClassInFoo | +| modules.rb:30:3:31:5 | Foo::ClassInAnotherDefinitionOfFoo | modules.rb:30:3:31:5 | ClassInAnotherDefinitionOfFoo | +| modules.rb:37:1:46:3 | Bar | modules.rb:37:1:46:3 | Bar | +| modules.rb:37:1:46:3 | Bar | modules.rb:78:5:79:7 | Bar | +| modules.rb:49:3:50:5 | Foo::Bar::ClassInAnotherDefinitionOfFooBar | modules.rb:49:3:50:5 | ClassInAnotherDefinitionOfFooBar | +| modules.rb:60:1:61:3 | MyModuleInGlobalScope | modules.rb:60:1:61:3 | MyModuleInGlobalScope | +| modules.rb:63:1:81:3 | Test | modules.rb:63:1:81:3 | Test | +| modules.rb:65:3:68:5 | Test::Foo1 | modules.rb:65:3:68:5 | Foo1 | +| modules.rb:66:5:67:7 | Test::Foo1::Bar | modules.rb:66:5:67:7 | Bar | +| modules.rb:70:3:74:5 | Test::Foo2 | modules.rb:70:3:74:5 | Foo2 | +| modules.rb:71:5:71:19 | Test::Foo2::Foo2 | modules.rb:71:5:71:19 | Foo2 | +| modules.rb:72:5:73:7 | Test::Foo2::Foo2::Bar | modules.rb:72:5:73:7 | Bar | +| modules.rb:76:3:80:5 | Test::Foo3 | modules.rb:76:3:80:5 | Foo3 | +| modules.rb:83:1:86:3 | Other | modules.rb:83:1:86:3 | Other | +| modules.rb:84:3:85:5 | Other::Foo1 | modules.rb:84:3:85:5 | Foo1 | +| modules.rb:88:1:93:3 | IncludeTest | modules.rb:88:1:93:3 | IncludeTest | +| modules.rb:91:3:92:5 | Test::Foo1::Y | modules.rb:91:3:92:5 | Y | +| modules.rb:95:1:99:3 | IncludeTest2 | modules.rb:95:1:99:3 | IncludeTest2 | +| modules.rb:97:3:98:5 | Test::Foo1::Z | modules.rb:97:3:98:5 | Z | +| modules.rb:101:1:105:3 | PrependTest | modules.rb:101:1:105:3 | PrependTest | +| modules.rb:103:3:104:5 | Test::Foo2::Y | modules.rb:103:3:104:5 | Y | +| modules.rb:107:1:110:3 | MM | modules.rb:107:1:110:3 | MM | +| modules.rb:108:3:109:5 | MM::MM | modules.rb:108:3:109:5 | MM | +| modules.rb:112:1:113:3 | YY | modules.rb:112:1:113:3 | YY | +| modules.rb:115:1:118:3 | XX | modules.rb:115:1:118:3 | XX | +| modules.rb:116:7:117:9 | XX::YY | modules.rb:116:7:117:9 | YY | +| modules.rb:120:1:121:3 | Test::Foo1::Bar::Baz | modules.rb:120:1:121:3 | Baz | +getSuperClass +| file://:0:0:0:0 | Class | file://:0:0:0:0 | Module | +| file://:0:0:0:0 | Module | file://:0:0:0:0 | Object | +| file://:0:0:0:0 | Object | file://:0:0:0:0 | BasicObject | +| hello.rb:11:1:16:3 | Greeting | file://:0:0:0:0 | Object | +| hello.rb:18:1:22:3 | HelloWorld | hello.rb:11:1:16:3 | Greeting | +| modules.rb:6:5:7:7 | Foo::Bar::ClassInFooBar | file://:0:0:0:0 | Object | +| modules.rb:19:3:20:5 | Foo::ClassInFoo | file://:0:0:0:0 | Object | +| modules.rb:30:3:31:5 | Foo::ClassInAnotherDefinitionOfFoo | file://:0:0:0:0 | Object | +| modules.rb:37:1:46:3 | Bar | file://:0:0:0:0 | Object | +| modules.rb:49:3:50:5 | Foo::Bar::ClassInAnotherDefinitionOfFooBar | file://:0:0:0:0 | Object | +| modules.rb:66:5:67:7 | Test::Foo1::Bar | file://:0:0:0:0 | Object | +| modules.rb:72:5:73:7 | Test::Foo2::Foo2::Bar | file://:0:0:0:0 | Object | +| modules.rb:112:1:113:3 | YY | file://:0:0:0:0 | Object | +| modules.rb:116:7:117:9 | XX::YY | modules.rb:112:1:113:3 | YY | +getAPrependedModule +| modules.rb:101:1:105:3 | PrependTest | modules.rb:63:1:81:3 | Test | +getAnIncludedModule +| file://:0:0:0:0 | Object | file://:0:0:0:0 | Kernel | +| hello.rb:11:1:16:3 | Greeting | hello.rb:1:1:8:3 | EnglishWords | +| modules.rb:88:1:93:3 | IncludeTest | modules.rb:63:1:81:3 | Test | +| modules.rb:95:1:99:3 | IncludeTest2 | modules.rb:63:1:81:3 | Test | diff --git a/ql/test/library-tests/modules/modules.ql b/ql/test/library-tests/modules/modules.ql new file mode 100644 index 00000000000..a29d2e93756 --- /dev/null +++ b/ql/test/library-tests/modules/modules.ql @@ -0,0 +1,11 @@ +import ruby + +query Module getModule() { any() } + +query ModuleBase getADeclaration(Module m) { result = m.getADeclaration() } + +query Module getSuperClass(Module m) { result = m.getSuperClass() } + +query Module getAPrependedModule(Module m) { result = m.getAPrependedModule() } + +query Module getAnIncludedModule(Module m) { result = m.getAnIncludedModule() } diff --git a/ql/test/library-tests/modules/modules.rb b/ql/test/library-tests/modules/modules.rb new file mode 100644 index 00000000000..8287b0a1bc4 --- /dev/null +++ b/ql/test/library-tests/modules/modules.rb @@ -0,0 +1,122 @@ +module Empty +end + +module Foo + module Bar + class ClassInFooBar + end + + def method_in_foo_bar + end + + puts 'module Foo::Bar' + $global_var = 0 + end + + def method_in_foo + end + + class ClassInFoo + end + + puts 'module Foo' + $global_var = 1 +end + +module Foo + def method_in_another_definition_of_foo + end + + class ClassInAnotherDefinitionOfFoo + end + + puts 'module Foo again' + $global_var = 2 +end + +module Bar + def method_a + end + + def method_b + end + + puts 'module Bar' + $global_var = 3 +end + +module Foo::Bar + class ClassInAnotherDefinitionOfFooBar + end + + def method_in_another_definition_of_foo_bar + end + + puts 'module Foo::Bar again' + $global_var = 4 +end + +# a module where the name is a scope resolution using the global scope +module ::MyModuleInGlobalScope +end + +module Test + + module Foo1 + class Foo1::Bar + end + end + + module Foo2 + module Foo2 end + class Foo2::Bar + end + end + + module Foo3 + Foo3 = Object + class Foo3::Bar + end + end +end + +module Other + module Foo1 + end +end + +module IncludeTest + include ::Test + Object.module_eval { prepend Other } + module Foo1::Y + end +end + +module IncludeTest2 + include Test + module Foo1::Z + end +end + +module PrependTest + prepend ::Test + module Foo2::Y + end +end + +module MM + module MM::MM + end +end + +class YY +end + +module XX + class YY < YY + end +end + +module Test::Foo1::Bar::Baz +end + diff --git a/ql/test/library-tests/modules/superclasses.expected b/ql/test/library-tests/modules/superclasses.expected new file mode 100644 index 00000000000..38c127ff92b --- /dev/null +++ b/ql/test/library-tests/modules/superclasses.expected @@ -0,0 +1,91 @@ +#-----| Object +#-----| -> BasicObject + +#-----| Class +#-----| -> Module + +#-----| Kernel + +#-----| Module +#-----| -> Object + +#-----| BasicObject + +hello.rb: +# 1| EnglishWords + +# 11| Greeting +#-----| -> Object + +# 18| HelloWorld +#-----| -> Greeting + +modules.rb: +# 1| Empty + +# 4| Foo + +# 37| Bar +#-----| -> Object + +# 60| MyModuleInGlobalScope + +# 63| Test + +# 83| Other + +# 88| IncludeTest + +# 95| IncludeTest2 + +# 101| PrependTest + +# 107| MM + +# 112| YY +#-----| -> Object + +# 115| XX + +# 5| Foo::Bar + +# 19| Foo::ClassInFoo +#-----| -> Object + +# 30| Foo::ClassInAnotherDefinitionOfFoo +#-----| -> Object + +# 65| Test::Foo1 + +# 70| Test::Foo2 + +# 76| Test::Foo3 + +# 84| Other::Foo1 + +# 116| XX::YY +#-----| -> YY + +# 6| Foo::Bar::ClassInFooBar +#-----| -> Object + +# 71| Test::Foo2::Foo2 + +# 108| MM::MM + +# 49| Foo::Bar::ClassInAnotherDefinitionOfFooBar +#-----| -> Object + +# 66| Test::Foo1::Bar +#-----| -> Object + +# 91| Test::Foo1::Y + +# 97| Test::Foo1::Z + +# 103| Test::Foo2::Y + +# 72| Test::Foo2::Foo2::Bar +#-----| -> Object + +# 120| Test::Foo1::Bar::Baz diff --git a/ql/test/library-tests/modules/superclasses.ql b/ql/test/library-tests/modules/superclasses.ql new file mode 100644 index 00000000000..4e8227d5c45 --- /dev/null +++ b/ql/test/library-tests/modules/superclasses.ql @@ -0,0 +1,11 @@ +/** + * @kind graph + */ + +import ruby + +query predicate nodes(Module node, string key, string value) { + key = "semmle.label" and value = node.toString() +} + +query predicate edges(Module source, Module target) { target = source.getSuperClass() } From bf4f91e03829b38314f64ca0f08c29c4b6261cf4 Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Fri, 16 Apr 2021 16:37:42 +0200 Subject: [PATCH 5/8] Address comments --- ql/src/codeql_ruby/ast/Call.qll | 15 +++++++++++++++ ql/src/codeql_ruby/ast/Method.qll | 6 +++--- ql/src/codeql_ruby/ast/Module.qll | 10 +++++----- ql/src/codeql_ruby/ast/internal/Module.qll | 12 ++---------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/ql/src/codeql_ruby/ast/Call.qll b/ql/src/codeql_ruby/ast/Call.qll index b38b2b37263..366aab182a1 100644 --- a/ql/src/codeql_ruby/ast/Call.qll +++ b/ql/src/codeql_ruby/ast/Call.qll @@ -85,6 +85,21 @@ class MethodCall extends Call, TMethodCall { */ Expr getReceiver() { none() } + /** + * Holds if the receiver is `self` or there is no receiver, which has the same + * meaning as an explict `self`. For example: + * + * ```rb + * self.foo + * foo + * ``` + */ + predicate receiverIsSelf() { + this.getReceiver() instanceof Self + or + not exists(this.getReceiver()) + } + /** * Gets the name of the method being called. For example, in: * diff --git a/ql/src/codeql_ruby/ast/Method.qll b/ql/src/codeql_ruby/ast/Method.qll index 04eeaff9a1f..dffabffa649 100644 --- a/ql/src/codeql_ruby/ast/Method.qll +++ b/ql/src/codeql_ruby/ast/Method.qll @@ -8,13 +8,13 @@ private import internal.TreeSitter * A representation of a method. */ class Method extends TMethod { - /** Get a declaration of this module, if any. */ + /** Gets a declaration of this module, if any. */ MethodBase getADeclaration() { result.getMethod() = this } - /** Get the name of this method */ + /** Gets the name of this method */ string getName() { this = TInstanceMethod(_, result) } - /** Get the module in which this method is defined */ + /** Gets the module in which this method is defined */ Module getModule() { this = TInstanceMethod(result, _) } /** Gets a textual representation of this method. */ diff --git a/ql/src/codeql_ruby/ast/Module.qll b/ql/src/codeql_ruby/ast/Module.qll index 6e90a9f87db..ae46b46d0c2 100644 --- a/ql/src/codeql_ruby/ast/Module.qll +++ b/ql/src/codeql_ruby/ast/Module.qll @@ -8,22 +8,22 @@ private import internal.TreeSitter * A representation of a run-time `module` or `class` value. */ class Module extends TModule { - /** Get a declaration of this module, if any. */ + /** Gets a declaration of this module, if any. */ ModuleBase getADeclaration() { result.getModule() = this } - /** Get the super class of this module, if any. */ + /** Gets the super class of this module, if any. */ Module getSuperClass() { result = getSuperClass(this) } - /** Get a method defined in this module by name. */ + /** Gets a method defined in this module by name. */ Method getMethod(string name) { result.getName() = name and result.getModule() = this } /** Look up a method in this module's ancestor chain. */ Method lookupMethod(string name) { result = lookupMethod(this, name) } - /** Get a `prepend`ed module. */ + /** Gets a `prepend`ed module. */ Module getAPrependedModule() { result = getAPrependedModule(this) } - /** Get an `include`d module. */ + /** Gets an `include`d module. */ Module getAnIncludedModule() { result = getAnIncludedModule(this) } /** Gets a textual representation of this module. */ diff --git a/ql/src/codeql_ruby/ast/internal/Module.qll b/ql/src/codeql_ruby/ast/internal/Module.qll index 01a77f583d0..554516f3c2f 100644 --- a/ql/src/codeql_ruby/ast/internal/Module.qll +++ b/ql/src/codeql_ruby/ast/internal/Module.qll @@ -67,11 +67,7 @@ private module Cached { m = resolveScopeExpr(c.getReceiver()) or m = enclosingModule(c).getModule() and - ( - c.getReceiver() instanceof Self - or - not exists(c.getReceiver()) - ) + c.receiverIsSelf() ) and result = resolveScopeExpr(c.getAnArgument()) ) @@ -85,11 +81,7 @@ private module Cached { m = resolveScopeExpr(c.getReceiver()) or m = enclosingModule(c).getModule() and - ( - c.getReceiver() instanceof Self - or - not exists(c.getReceiver()) - ) + c.receiverIsSelf() ) and result = resolveScopeExpr(c.getAnArgument()) ) From 122315db3fc347c06c979055ed89d76e8f9b2efe Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Tue, 20 Apr 2021 13:41:11 +0200 Subject: [PATCH 6/8] Remove 'Method' class --- ql/src/codeql_ruby/ast/Method.qll | 47 ---------- ql/src/codeql_ruby/ast/Module.qll | 6 -- ql/src/codeql_ruby/ast/internal/Method.qll | 10 -- ql/src/codeql_ruby/ast/internal/Module.qll | 14 ++- .../library-tests/modules/methods.expected | 92 +++++-------------- ql/test/library-tests/modules/methods.ql | 13 +-- 6 files changed, 36 insertions(+), 146 deletions(-) delete mode 100644 ql/src/codeql_ruby/ast/internal/Method.qll diff --git a/ql/src/codeql_ruby/ast/Method.qll b/ql/src/codeql_ruby/ast/Method.qll index dffabffa649..f01eba6c323 100644 --- a/ql/src/codeql_ruby/ast/Method.qll +++ b/ql/src/codeql_ruby/ast/Method.qll @@ -1,46 +1,8 @@ private import codeql_ruby.AST private import codeql_ruby.controlflow.ControlFlowGraph private import internal.AST -private import internal.Method private import internal.TreeSitter -/** - * A representation of a method. - */ -class Method extends TMethod { - /** Gets a declaration of this module, if any. */ - MethodBase getADeclaration() { result.getMethod() = this } - - /** Gets the name of this method */ - string getName() { this = TInstanceMethod(_, result) } - - /** Gets the module in which this method is defined */ - Module getModule() { this = TInstanceMethod(result, _) } - - /** Gets a textual representation of this method. */ - string toString() { - exists(Module m, string name | - this = TInstanceMethod(m, name) and result = m.toString() + "." + name - ) - } - - /** Gets the location of this method. */ - Location getLocation() { - result = - min(MethodBase decl, Module m, string name, Location loc, int weight | - this = TInstanceMethod(m, name) and - decl = methodDeclaration(m, name) and - loc = decl.getLocation() and - if exists(loc.getFile().getRelativePath()) then weight = 0 else weight = 1 - | - loc - order by - weight, count(decl.getAStmt()) desc, loc.getFile().getAbsolutePath(), loc.getStartLine(), - loc.getStartColumn() - ) - } -} - /** A callable. */ class Callable extends Expr, Scope, TCallable { /** Gets the number of parameters of this callable. */ @@ -62,9 +24,6 @@ class MethodBase extends Callable, BodyStmt, Scope, TMethodBase { /** Gets the name of this method. */ string getName() { none() } - /** Gets the method defined by this declaration. */ - Method getMethod() { none() } - override AstNode getAChild(string pred) { result = Callable.super.getAChild(pred) or @@ -85,12 +44,6 @@ class MethodDeclaration extends MethodBase, TMethodDeclaration { result = g.getName().(Generated::Setter).getName().getValue() + "=" } - final override Method getMethod() { - exists(Module owner, string name | - result = TInstanceMethod(owner, name) and this = methodDeclaration(owner, name) - ) - } - /** * Holds if this is a setter method, as in the following example: * ```rb diff --git a/ql/src/codeql_ruby/ast/Module.qll b/ql/src/codeql_ruby/ast/Module.qll index ae46b46d0c2..17661c281ba 100644 --- a/ql/src/codeql_ruby/ast/Module.qll +++ b/ql/src/codeql_ruby/ast/Module.qll @@ -14,12 +14,6 @@ class Module extends TModule { /** Gets the super class of this module, if any. */ Module getSuperClass() { result = getSuperClass(this) } - /** Gets a method defined in this module by name. */ - Method getMethod(string name) { result.getName() = name and result.getModule() = this } - - /** Look up a method in this module's ancestor chain. */ - Method lookupMethod(string name) { result = lookupMethod(this, name) } - /** Gets a `prepend`ed module. */ Module getAPrependedModule() { result = getAPrependedModule(this) } diff --git a/ql/src/codeql_ruby/ast/internal/Method.qll b/ql/src/codeql_ruby/ast/internal/Method.qll deleted file mode 100644 index 82e0f53d47f..00000000000 --- a/ql/src/codeql_ruby/ast/internal/Method.qll +++ /dev/null @@ -1,10 +0,0 @@ -private import codeql_ruby.ast.Method -private import codeql_ruby.ast.Module -private import codeql_ruby.ast.internal.Module - -newtype TMethod = - TInstanceMethod(TModule owner, string name) { exists(methodDeclaration(owner, name)) } - -MethodDeclaration methodDeclaration(TModule owner, string name) { - exists(ModuleBase m | m.getModule() = owner and result = m.getMethod(name)) -} diff --git a/ql/src/codeql_ruby/ast/internal/Module.qll b/ql/src/codeql_ruby/ast/internal/Module.qll index 554516f3c2f..90dd5500775 100644 --- a/ql/src/codeql_ruby/ast/internal/Module.qll +++ b/ql/src/codeql_ruby/ast/internal/Module.qll @@ -301,18 +301,22 @@ private Module getAncestors(Module m) { result = getAncestors(m.getAPrependedModule()) } -private Method lookupMethod0(Module m, string name) { +MethodDeclaration getMethod(TModule owner, string name) { + exists(ModuleBase m | m.getModule() = owner and result = m.getMethod(name)) +} + +private MethodDeclaration lookupMethod0(Module m, string name) { result = lookupMethod0(m.getAPrependedModule(), name) or - not exists(getAncestors(m.getAPrependedModule()).getMethod(name)) and + not exists(getMethod(getAncestors(m.getAPrependedModule()), name)) and ( - result = m.getMethod(name) + result = getMethod(m, name) or - not exists(m.getMethod(name)) and result = lookupMethod0(m.getAnIncludedModule(), name) + not exists(getMethod(m, name)) and result = lookupMethod0(m.getAnIncludedModule(), name) ) } -Method lookupMethod(Module m, string name) { +MethodDeclaration lookupMethod(Module m, string name) { result = lookupMethod0(m, name) or not exists(lookupMethod0(m, name)) and diff --git a/ql/test/library-tests/modules/methods.expected b/ql/test/library-tests/modules/methods.expected index 5f7d1a5a76a..1a745517cfd 100644 --- a/ql/test/library-tests/modules/methods.expected +++ b/ql/test/library-tests/modules/methods.expected @@ -1,70 +1,26 @@ -method -| hello.rb:2:5:4:7 | EnglishWords.hello | -| hello.rb:5:5:7:7 | EnglishWords.world | -| hello.rb:13:5:15:7 | Greeting.message | -| hello.rb:19:5:21:7 | HelloWorld.message | -| modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | -| modules.rb:16:3:17:5 | Foo.method_in_foo | -| modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | -| modules.rb:38:3:39:5 | Bar.method_a | -| modules.rb:41:3:42:5 | Bar.method_b | -| modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | -getADeclaration -| hello.rb:2:5:4:7 | EnglishWords.hello | hello.rb:2:5:4:7 | hello | -| hello.rb:5:5:7:7 | EnglishWords.world | hello.rb:5:5:7:7 | world | -| hello.rb:13:5:15:7 | Greeting.message | hello.rb:13:5:15:7 | message | -| hello.rb:19:5:21:7 | HelloWorld.message | hello.rb:19:5:21:7 | message | -| modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | modules.rb:9:5:10:7 | method_in_foo_bar | -| modules.rb:16:3:17:5 | Foo.method_in_foo | modules.rb:16:3:17:5 | method_in_foo | -| modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | modules.rb:27:3:28:5 | method_in_another_definition_of_foo | -| modules.rb:38:3:39:5 | Bar.method_a | modules.rb:38:3:39:5 | method_a | -| modules.rb:41:3:42:5 | Bar.method_b | modules.rb:41:3:42:5 | method_b | -| modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | modules.rb:52:3:53:5 | method_in_another_definition_of_foo_bar | -getName -| hello.rb:2:5:4:7 | EnglishWords.hello | hello | -| hello.rb:5:5:7:7 | EnglishWords.world | world | -| hello.rb:13:5:15:7 | Greeting.message | message | -| hello.rb:19:5:21:7 | HelloWorld.message | message | -| modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | method_in_foo_bar | -| modules.rb:16:3:17:5 | Foo.method_in_foo | method_in_foo | -| modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | method_in_another_definition_of_foo | -| modules.rb:38:3:39:5 | Bar.method_a | method_a | -| modules.rb:41:3:42:5 | Bar.method_b | method_b | -| modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | method_in_another_definition_of_foo_bar | -getModule -| hello.rb:2:5:4:7 | EnglishWords.hello | hello.rb:1:1:8:3 | EnglishWords | -| hello.rb:5:5:7:7 | EnglishWords.world | hello.rb:1:1:8:3 | EnglishWords | -| hello.rb:13:5:15:7 | Greeting.message | hello.rb:11:1:16:3 | Greeting | -| hello.rb:19:5:21:7 | HelloWorld.message | hello.rb:18:1:22:3 | HelloWorld | -| modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | modules.rb:5:3:14:5 | Foo::Bar | -| modules.rb:16:3:17:5 | Foo.method_in_foo | modules.rb:4:1:24:3 | Foo | -| modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | modules.rb:4:1:24:3 | Foo | -| modules.rb:38:3:39:5 | Bar.method_a | modules.rb:37:1:46:3 | Bar | -| modules.rb:41:3:42:5 | Bar.method_b | modules.rb:37:1:46:3 | Bar | -| modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | modules.rb:5:3:14:5 | Foo::Bar | getMethod -| hello.rb:1:1:8:3 | EnglishWords | hello | hello.rb:2:5:4:7 | EnglishWords.hello | -| hello.rb:1:1:8:3 | EnglishWords | world | hello.rb:5:5:7:7 | EnglishWords.world | -| hello.rb:11:1:16:3 | Greeting | message | hello.rb:13:5:15:7 | Greeting.message | -| hello.rb:18:1:22:3 | HelloWorld | message | hello.rb:19:5:21:7 | HelloWorld.message | -| modules.rb:4:1:24:3 | Foo | method_in_another_definition_of_foo | modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | -| modules.rb:4:1:24:3 | Foo | method_in_foo | modules.rb:16:3:17:5 | Foo.method_in_foo | -| modules.rb:5:3:14:5 | Foo::Bar | method_in_another_definition_of_foo_bar | modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | -| modules.rb:5:3:14:5 | Foo::Bar | method_in_foo_bar | modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | -| modules.rb:37:1:46:3 | Bar | method_a | modules.rb:38:3:39:5 | Bar.method_a | -| modules.rb:37:1:46:3 | Bar | method_b | modules.rb:41:3:42:5 | Bar.method_b | +| hello.rb:1:1:8:3 | EnglishWords | hello | hello.rb:2:5:4:7 | hello | +| hello.rb:1:1:8:3 | EnglishWords | world | hello.rb:5:5:7:7 | world | +| hello.rb:11:1:16:3 | Greeting | message | hello.rb:13:5:15:7 | message | +| hello.rb:18:1:22:3 | HelloWorld | message | hello.rb:19:5:21:7 | message | +| modules.rb:4:1:24:3 | Foo | method_in_another_definition_of_foo | modules.rb:27:3:28:5 | method_in_another_definition_of_foo | +| modules.rb:4:1:24:3 | Foo | method_in_foo | modules.rb:16:3:17:5 | method_in_foo | +| modules.rb:5:3:14:5 | Foo::Bar | method_in_another_definition_of_foo_bar | modules.rb:52:3:53:5 | method_in_another_definition_of_foo_bar | +| modules.rb:5:3:14:5 | Foo::Bar | method_in_foo_bar | modules.rb:9:5:10:7 | method_in_foo_bar | +| modules.rb:37:1:46:3 | Bar | method_a | modules.rb:38:3:39:5 | method_a | +| modules.rb:37:1:46:3 | Bar | method_b | modules.rb:41:3:42:5 | method_b | lookupMethod -| hello.rb:1:1:8:3 | EnglishWords | hello | hello.rb:2:5:4:7 | EnglishWords.hello | -| hello.rb:1:1:8:3 | EnglishWords | world | hello.rb:5:5:7:7 | EnglishWords.world | -| hello.rb:11:1:16:3 | Greeting | hello | hello.rb:2:5:4:7 | EnglishWords.hello | -| hello.rb:11:1:16:3 | Greeting | message | hello.rb:13:5:15:7 | Greeting.message | -| hello.rb:11:1:16:3 | Greeting | world | hello.rb:5:5:7:7 | EnglishWords.world | -| hello.rb:18:1:22:3 | HelloWorld | hello | hello.rb:2:5:4:7 | EnglishWords.hello | -| hello.rb:18:1:22:3 | HelloWorld | message | hello.rb:19:5:21:7 | HelloWorld.message | -| hello.rb:18:1:22:3 | HelloWorld | world | hello.rb:5:5:7:7 | EnglishWords.world | -| modules.rb:4:1:24:3 | Foo | method_in_another_definition_of_foo | modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | -| modules.rb:4:1:24:3 | Foo | method_in_foo | modules.rb:16:3:17:5 | Foo.method_in_foo | -| modules.rb:5:3:14:5 | Foo::Bar | method_in_another_definition_of_foo_bar | modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | -| modules.rb:5:3:14:5 | Foo::Bar | method_in_foo_bar | modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | -| modules.rb:37:1:46:3 | Bar | method_a | modules.rb:38:3:39:5 | Bar.method_a | -| modules.rb:37:1:46:3 | Bar | method_b | modules.rb:41:3:42:5 | Bar.method_b | +| hello.rb:1:1:8:3 | EnglishWords | hello | hello.rb:2:5:4:7 | hello | +| hello.rb:1:1:8:3 | EnglishWords | world | hello.rb:5:5:7:7 | world | +| hello.rb:11:1:16:3 | Greeting | hello | hello.rb:2:5:4:7 | hello | +| hello.rb:11:1:16:3 | Greeting | message | hello.rb:13:5:15:7 | message | +| hello.rb:11:1:16:3 | Greeting | world | hello.rb:5:5:7:7 | world | +| hello.rb:18:1:22:3 | HelloWorld | hello | hello.rb:2:5:4:7 | hello | +| hello.rb:18:1:22:3 | HelloWorld | message | hello.rb:19:5:21:7 | message | +| hello.rb:18:1:22:3 | HelloWorld | world | hello.rb:5:5:7:7 | world | +| modules.rb:4:1:24:3 | Foo | method_in_another_definition_of_foo | modules.rb:27:3:28:5 | method_in_another_definition_of_foo | +| modules.rb:4:1:24:3 | Foo | method_in_foo | modules.rb:16:3:17:5 | method_in_foo | +| modules.rb:5:3:14:5 | Foo::Bar | method_in_another_definition_of_foo_bar | modules.rb:52:3:53:5 | method_in_another_definition_of_foo_bar | +| modules.rb:5:3:14:5 | Foo::Bar | method_in_foo_bar | modules.rb:9:5:10:7 | method_in_foo_bar | +| modules.rb:37:1:46:3 | Bar | method_a | modules.rb:38:3:39:5 | method_a | +| modules.rb:37:1:46:3 | Bar | method_b | modules.rb:41:3:42:5 | method_b | diff --git a/ql/test/library-tests/modules/methods.ql b/ql/test/library-tests/modules/methods.ql index 1bb7296026d..323a4c61476 100644 --- a/ql/test/library-tests/modules/methods.ql +++ b/ql/test/library-tests/modules/methods.ql @@ -1,13 +1,6 @@ import ruby +import codeql_ruby.ast.internal.Module as M -query Method method() { any() } +query MethodBase getMethod(Module m, string name) { result = M::getMethod(m, name) } -query MethodBase getADeclaration(Method m) { result = m.getADeclaration() } - -query string getName(Method m) { result = m.getName() } - -query Module getModule(Method m) { result = m.getModule() } - -query Method getMethod(Module m, string name) { result = m.getMethod(name) } - -query Method lookupMethod(Module m, string name) { result = m.lookupMethod(name) } +query MethodBase lookupMethod(Module m, string name) { result = M::lookupMethod(m, name) } From 1245674df8e4b2c69fb651ad9568b0ebbe9d4343 Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Tue, 20 Apr 2021 16:17:28 +0200 Subject: [PATCH 7/8] Add missing @id properties --- ql/src/codeql_ruby/controlflow/internal/Cfg.ql | 1 + ql/test/library-tests/ast/Ast.ql | 1 + ql/test/library-tests/modules/ancestors.ql | 1 + ql/test/library-tests/modules/superclasses.ql | 1 + 4 files changed, 4 insertions(+) diff --git a/ql/src/codeql_ruby/controlflow/internal/Cfg.ql b/ql/src/codeql_ruby/controlflow/internal/Cfg.ql index ded6b103222..a0b0200b346 100644 --- a/ql/src/codeql_ruby/controlflow/internal/Cfg.ql +++ b/ql/src/codeql_ruby/controlflow/internal/Cfg.ql @@ -1,5 +1,6 @@ /** * @kind graph + * @id rb/test/cfg */ import codeql_ruby.CFG diff --git a/ql/test/library-tests/ast/Ast.ql b/ql/test/library-tests/ast/Ast.ql index babafaac548..4cd412f83a0 100644 --- a/ql/test/library-tests/ast/Ast.ql +++ b/ql/test/library-tests/ast/Ast.ql @@ -1,5 +1,6 @@ /** * @kind graph + * @id rb/test/print-ast */ import codeql_ruby.printAst diff --git a/ql/test/library-tests/modules/ancestors.ql b/ql/test/library-tests/modules/ancestors.ql index bbc4973fea8..897ddf94f35 100644 --- a/ql/test/library-tests/modules/ancestors.ql +++ b/ql/test/library-tests/modules/ancestors.ql @@ -1,5 +1,6 @@ /** * @kind graph + * @id rb/test/ancestors */ import ruby diff --git a/ql/test/library-tests/modules/superclasses.ql b/ql/test/library-tests/modules/superclasses.ql index 4e8227d5c45..d2141aa38ff 100644 --- a/ql/test/library-tests/modules/superclasses.ql +++ b/ql/test/library-tests/modules/superclasses.ql @@ -1,5 +1,6 @@ /** * @kind graph + * @id rb/test/supertypes */ import ruby From 549e5ab9d6e6aa377fe95807f94af4cb72494616 Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Wed, 21 Apr 2021 10:42:24 +0200 Subject: [PATCH 8/8] Revert "Rename Method -> MethodDeclaration" This reverts commit d361ef37afacb2acbd96da2df017c932bae16d4e. --- ql/src/codeql_ruby/ast/Expr.qll | 2 +- ql/src/codeql_ruby/ast/Method.qll | 6 +- ql/src/codeql_ruby/ast/internal/AST.qll | 9 ++- ql/src/codeql_ruby/ast/internal/Module.qll | 6 +- .../internal/ControlFlowGraphImpl.qll | 2 +- .../dataflow/internal/DataFlowPrivate.qll | 6 +- ql/test/library-tests/ast/Ast.expected | 56 +++++++++---------- .../ast/modules/classes.expected | 4 +- ql/test/library-tests/ast/modules/classes.ql | 2 +- .../library-tests/ast/modules/module_base.ql | 2 +- .../ast/modules/modules.expected | 12 ++-- ql/test/library-tests/ast/modules/modules.ql | 2 +- .../ast/modules/singleton_classes.expected | 4 +- .../ast/modules/singleton_classes.ql | 4 +- ql/test/library-tests/ast/params/params.ql | 2 +- 15 files changed, 56 insertions(+), 63 deletions(-) diff --git a/ql/src/codeql_ruby/ast/Expr.qll b/ql/src/codeql_ruby/ast/Expr.qll index 04fc3023d93..4e44bc98a25 100644 --- a/ql/src/codeql_ruby/ast/Expr.qll +++ b/ql/src/codeql_ruby/ast/Expr.qll @@ -123,7 +123,7 @@ private class Ensure extends StmtSequence, TEnsure { class BodyStmt extends StmtSequence, TBodyStmt { // Not defined by dispatch, as it should not be exposed private Generated::AstNode getChild(int i) { - result = any(Generated::Method g | this = TMethodDeclaration(g)).getChild(i) + result = any(Generated::Method g | this = TMethod(g)).getChild(i) or result = any(Generated::SingletonMethod g | this = TSingletonMethod(g)).getChild(i) or diff --git a/ql/src/codeql_ruby/ast/Method.qll b/ql/src/codeql_ruby/ast/Method.qll index f01eba6c323..648392b28f0 100644 --- a/ql/src/codeql_ruby/ast/Method.qll +++ b/ql/src/codeql_ruby/ast/Method.qll @@ -32,12 +32,12 @@ class MethodBase extends Callable, BodyStmt, Scope, TMethodBase { } /** A normal method. */ -class MethodDeclaration extends MethodBase, TMethodDeclaration { +class Method extends MethodBase, TMethod { private Generated::Method g; - MethodDeclaration() { this = TMethodDeclaration(g) } + Method() { this = TMethod(g) } - final override string getAPrimaryQlClass() { result = "MethodDeclaration" } + final override string getAPrimaryQlClass() { result = "Method" } final override string getName() { result = g.getName().(Generated::Token).getValue() or diff --git a/ql/src/codeql_ruby/ast/internal/AST.qll b/ql/src/codeql_ruby/ast/internal/AST.qll index b78c6c19bf0..26e2e6149c3 100644 --- a/ql/src/codeql_ruby/ast/internal/AST.qll +++ b/ql/src/codeql_ruby/ast/internal/AST.qll @@ -138,7 +138,7 @@ private module Cached { g instanceof @binary_and or g instanceof @binary_ampersandampersand } or TLogicalOrExpr(Generated::Binary g) { g instanceof @binary_or or g instanceof @binary_pipepipe } or - TMethodDeclaration(Generated::Method g) or + TMethod(Generated::Method g) or TModuleDeclaration(Generated::Module g) or TModuloExpr(Generated::Binary g) { g instanceof @binary_percent } or TMulExpr(Generated::Binary g) { g instanceof @binary_star } or @@ -301,7 +301,7 @@ private module Cached { n = TLocalVariableAccess(result, _) or n = TLogicalAndExpr(result) or n = TLogicalOrExpr(result) or - n = TMethodDeclaration(result) or + n = TMethod(result) or n = TModuleDeclaration(result) or n = TModuloExpr(result) or n = TMulExpr(result) or @@ -402,8 +402,7 @@ class TStmtSequence = TBeginBlock or TEndBlock or TThen or TElse or TDo or TEnsure or TStringInterpolationComponent or TBlock or TBodyStmt or TParenthesizedExpr; -class TBodyStmt = - TBeginExpr or TModuleBase or TMethodDeclaration or TLambda or TDoBlock or TSingletonMethod; +class TBodyStmt = TBeginExpr or TModuleBase or TMethod or TLambda or TDoBlock or TSingletonMethod; class TLiteral = TNumericLiteral or TNilLiteral or TBooleanLiteral or TStringlikeLiteral or TCharacterLiteral or @@ -429,7 +428,7 @@ class TArrayLiteral = TRegularArrayLiteral or TStringArrayLiteral or TSymbolArra class TCallable = TMethodBase or TLambda or TBlock; -class TMethodBase = TMethodDeclaration or TSingletonMethod; +class TMethodBase = TMethod or TSingletonMethod; class TBlock = TDoBlock or TBraceBlock; diff --git a/ql/src/codeql_ruby/ast/internal/Module.qll b/ql/src/codeql_ruby/ast/internal/Module.qll index 90dd5500775..d7708271e11 100644 --- a/ql/src/codeql_ruby/ast/internal/Module.qll +++ b/ql/src/codeql_ruby/ast/internal/Module.qll @@ -301,11 +301,11 @@ private Module getAncestors(Module m) { result = getAncestors(m.getAPrependedModule()) } -MethodDeclaration getMethod(TModule owner, string name) { +Method getMethod(TModule owner, string name) { exists(ModuleBase m | m.getModule() = owner and result = m.getMethod(name)) } -private MethodDeclaration lookupMethod0(Module m, string name) { +private Method lookupMethod0(Module m, string name) { result = lookupMethod0(m.getAPrependedModule(), name) or not exists(getMethod(getAncestors(m.getAPrependedModule()), name)) and @@ -316,7 +316,7 @@ private MethodDeclaration lookupMethod0(Module m, string name) { ) } -MethodDeclaration lookupMethod(Module m, string name) { +Method lookupMethod(Module m, string name) { result = lookupMethod0(m, name) or not exists(lookupMethod0(m, name)) and diff --git a/ql/src/codeql_ruby/controlflow/internal/ControlFlowGraphImpl.qll b/ql/src/codeql_ruby/controlflow/internal/ControlFlowGraphImpl.qll index 45ca798d99d..a648a2c1b71 100644 --- a/ql/src/codeql_ruby/controlflow/internal/ControlFlowGraphImpl.qll +++ b/ql/src/codeql_ruby/controlflow/internal/ControlFlowGraphImpl.qll @@ -925,7 +925,7 @@ module Trees { private class MethodNameTree extends LeafTree, MethodName, ASTInternal::TTokenMethodName { } - private class MethodDeclarationTree extends BodyStmtPostOrderTree, MethodDeclaration { + private class MethodTree extends BodyStmtPostOrderTree, Method { /** Gets the `i`th child in the body of this block. */ final override AstNode getBodyChild(int i, boolean rescuable) { result = this.getParameter(i) and rescuable = false diff --git a/ql/src/codeql_ruby/dataflow/internal/DataFlowPrivate.qll b/ql/src/codeql_ruby/dataflow/internal/DataFlowPrivate.qll index f21a08a9cf3..09c84fda825 100644 --- a/ql/src/codeql_ruby/dataflow/internal/DataFlowPrivate.qll +++ b/ql/src/codeql_ruby/dataflow/internal/DataFlowPrivate.qll @@ -269,11 +269,7 @@ private module ReturnNodes { scope = node.getScope() | stmt instanceof ReturnStmt and - ( - scope instanceof MethodDeclaration or - scope instanceof SingletonMethod or - scope instanceof Lambda - ) + (scope instanceof Method or scope instanceof SingletonMethod or scope instanceof Lambda) or stmt instanceof NextStmt and (scope instanceof Block or scope instanceof Lambda) diff --git a/ql/test/library-tests/ast/Ast.expected b/ql/test/library-tests/ast/Ast.expected index 26e31fac3f5..7078ea3c675 100644 --- a/ql/test/library-tests/ast/Ast.expected +++ b/ql/test/library-tests/ast/Ast.expected @@ -34,9 +34,9 @@ calls/calls.rb: # 26| getStmt: [AddExpr] ... + ... # 26| getAnOperand/getLeftOperand: [LocalVariableAccess] x # 26| getAnOperand/getRightOperand: [IntegerLiteral] 1 -# 30| getStmt: [MethodDeclaration] method_that_yields +# 30| getStmt: [Method] method_that_yields # 31| getStmt: [YieldCall] yield ... -# 35| getStmt: [MethodDeclaration] another_method_that_yields +# 35| getStmt: [Method] another_method_that_yields # 36| getStmt: [YieldCall] yield ... # 36| getArgument: [IntegerLiteral] 100 # 36| getArgument: [IntegerLiteral] 200 @@ -145,7 +145,7 @@ calls/calls.rb: # 131| getReceiver: [ConstantReadAccess] X # 132| getStmt: [MethodCall] call to bar # 132| getReceiver: [ConstantReadAccess] X -# 136| getStmt: [MethodDeclaration] some_method +# 136| getStmt: [Method] some_method # 137| getStmt: [MethodCall] call to foo # 138| getStmt: [MethodCall] call to bar # 138| getReceiver: [ConstantReadAccess] X @@ -154,20 +154,20 @@ calls/calls.rb: # 143| getStmt: [MethodCall] call to bar # 144| getStmt: [MethodCall] call to baz # 144| getReceiver: [ConstantReadAccess] X -# 148| getStmt: [MethodDeclaration] method_with_keyword_param +# 148| getStmt: [Method] method_with_keyword_param # 148| getParameter: [KeywordParameter] keyword # 148| getDefiningAccess: [LocalVariableAccess] keyword # 148| getDefaultValue: [MethodCall] call to foo -# 150| getStmt: [MethodDeclaration] method_with_keyword_param2 +# 150| getStmt: [Method] method_with_keyword_param2 # 150| getParameter: [KeywordParameter] keyword # 150| getDefiningAccess: [LocalVariableAccess] keyword # 150| getDefaultValue: [MethodCall] call to foo # 150| getReceiver: [ConstantReadAccess] X -# 154| getStmt: [MethodDeclaration] method_with_optional_param +# 154| getStmt: [Method] method_with_optional_param # 154| getParameter: [OptionalParameter] param # 154| getDefiningAccess: [LocalVariableAccess] param # 154| getDefaultValue: [MethodCall] call to foo -# 156| getStmt: [MethodDeclaration] method_with_optional_param2 +# 156| getStmt: [Method] method_with_optional_param2 # 156| getParameter: [OptionalParameter] param # 156| getDefiningAccess: [LocalVariableAccess] param # 156| getDefaultValue: [MethodCall] call to foo @@ -377,7 +377,7 @@ calls/calls.rb: # 279| getValue: [MethodCall] call to bar # 279| getReceiver: [ConstantReadAccess] X # 284| getStmt: [ClassDeclaration] MyClass -# 285| getStmt: [MethodDeclaration] my_method +# 285| getStmt: [Method] my_method # 286| getStmt: [SuperCall] call to super # 287| getStmt: [SuperCall] call to super # 288| getStmt: [SuperCall] call to super @@ -420,7 +420,7 @@ calls/calls.rb: # 293| getAnOperand/getLeftOperand: [LocalVariableAccess] x # 293| getAnOperand/getRightOperand: [IntegerLiteral] 200 # 301| getStmt: [ClassDeclaration] AnotherClass -# 302| getStmt: [MethodDeclaration] another_method +# 302| getStmt: [Method] another_method # 303| getStmt: [MethodCall] call to super # 303| getReceiver: [MethodCall] call to foo # 304| getStmt: [MethodCall] call to super @@ -532,11 +532,11 @@ modules/classes.rb: # 16| getStmt: [ClassDeclaration] MyClass # 16| getScopeExpr: [ConstantReadAccess] MyModule # 20| getStmt: [ClassDeclaration] Wibble -# 21| getStmt: [MethodDeclaration] method_a +# 21| getStmt: [Method] method_a # 22| getStmt: [MethodCall] call to puts # 22| getArgument: [StringLiteral] "a" # 22| getComponent: [StringTextComponent] a -# 25| getStmt: [MethodDeclaration] method_b +# 25| getStmt: [Method] method_b # 26| getStmt: [MethodCall] call to puts # 26| getArgument: [StringLiteral] "b" # 26| getComponent: [StringTextComponent] b @@ -552,11 +552,11 @@ modules/classes.rb: # 40| getComponent: [StringTextComponent] hello # 41| getStmt: [ClassDeclaration] class << ... # 41| getValue: [LocalVariableAccess] x -# 42| getStmt: [MethodDeclaration] length +# 42| getStmt: [Method] length # 43| getStmt: [MulExpr] ... * ... # 43| getAnOperand/getLeftOperand: [IntegerLiteral] 100 # 43| getAnOperand/getRightOperand: [SuperCall] call to super -# 46| getStmt: [MethodDeclaration] wibble +# 46| getStmt: [Method] wibble # 47| getStmt: [MethodCall] call to puts # 47| getArgument: [StringLiteral] "wibble" # 47| getComponent: [StringTextComponent] wibble @@ -700,7 +700,7 @@ constants/constants.rb: # 14| getAnOperand/getLeftOperand: [ConstantAssignment] GREETING # 14| getAnOperand/getRightOperand: [StringLiteral] "Hello" # 14| getComponent: [StringTextComponent] Hello -# 16| getStmt: [MethodDeclaration] foo +# 16| getStmt: [Method] foo # 17| getStmt: [AssignExpr] ... = ... # 17| getAnOperand/getLeftOperand: [ConstantAssignment] Names # 17| getAnOperand/getRightOperand: [ArrayLiteral] [...] @@ -1096,7 +1096,7 @@ literals/literals.rb: # 154| getStmt: [MethodCall] call to name # 154| getComponent: [StringTextComponent] # 154| -# 157| getStmt: [MethodDeclaration] m +# 157| getStmt: [Method] m # 158| getStmt: [AssignExpr] ... = ... # 158| getAnOperand/getLeftOperand: [LocalVariableAccess] query # 158| getAnOperand/getRightOperand: [HereDoc] <<-BLA @@ -1326,14 +1326,14 @@ modules/modules.rb: # 4| getStmt: [ModuleDeclaration] Foo # 5| getStmt: [ModuleDeclaration] Bar # 6| getStmt: [ClassDeclaration] ClassInFooBar -# 9| getStmt: [MethodDeclaration] method_in_foo_bar +# 9| getStmt: [Method] method_in_foo_bar # 12| getStmt: [MethodCall] call to puts # 12| getArgument: [StringLiteral] "module Foo::Bar" # 12| getComponent: [StringTextComponent] module Foo::Bar # 13| getStmt: [AssignExpr] ... = ... # 13| getAnOperand/getLeftOperand: [GlobalVariableAccess] $global_var # 13| getAnOperand/getRightOperand: [IntegerLiteral] 0 -# 16| getStmt: [MethodDeclaration] method_in_foo +# 16| getStmt: [Method] method_in_foo # 19| getStmt: [ClassDeclaration] ClassInFoo # 22| getStmt: [MethodCall] call to puts # 22| getArgument: [StringLiteral] "module Foo" @@ -1342,7 +1342,7 @@ modules/modules.rb: # 23| getAnOperand/getLeftOperand: [GlobalVariableAccess] $global_var # 23| getAnOperand/getRightOperand: [IntegerLiteral] 1 # 26| getStmt: [ModuleDeclaration] Foo -# 27| getStmt: [MethodDeclaration] method_in_another_definition_of_foo +# 27| getStmt: [Method] method_in_another_definition_of_foo # 30| getStmt: [ClassDeclaration] ClassInAnotherDefinitionOfFoo # 33| getStmt: [MethodCall] call to puts # 33| getArgument: [StringLiteral] "module Foo again" @@ -1351,8 +1351,8 @@ modules/modules.rb: # 34| getAnOperand/getLeftOperand: [GlobalVariableAccess] $global_var # 34| getAnOperand/getRightOperand: [IntegerLiteral] 2 # 37| getStmt: [ModuleDeclaration] Bar -# 38| getStmt: [MethodDeclaration] method_a -# 41| getStmt: [MethodDeclaration] method_b +# 38| getStmt: [Method] method_a +# 41| getStmt: [Method] method_b # 44| getStmt: [MethodCall] call to puts # 44| getArgument: [StringLiteral] "module Bar" # 44| getComponent: [StringTextComponent] module Bar @@ -1362,7 +1362,7 @@ modules/modules.rb: # 48| getStmt: [ModuleDeclaration] Bar # 48| getScopeExpr: [ConstantReadAccess] Foo # 49| getStmt: [ClassDeclaration] ClassInAnotherDefinitionOfFooBar -# 52| getStmt: [MethodDeclaration] method_in_another_definition_of_foo_bar +# 52| getStmt: [Method] method_in_another_definition_of_foo_bar # 55| getStmt: [MethodCall] call to puts # 55| getArgument: [StringLiteral] "module Foo::Bar again" # 55| getComponent: [StringTextComponent] module Foo::Bar again @@ -1603,7 +1603,7 @@ operations/operations.rb: # 84| getAnOperand/getRightOperand: [LocalVariableAccess] qux params/params.rb: # 1| [Toplevel] params.rb -# 4| getStmt: [MethodDeclaration] identifier_method_params +# 4| getStmt: [Method] identifier_method_params # 4| getParameter: [SimpleParameter] foo # 4| getDefiningAccess: [LocalVariableAccess] foo # 4| getParameter: [SimpleParameter] bar @@ -1637,7 +1637,7 @@ params/params.rb: # 14| getStmt: [AddExpr] ... + ... # 14| getAnOperand/getLeftOperand: [LocalVariableAccess] foo # 14| getAnOperand/getRightOperand: [LocalVariableAccess] bar -# 17| getStmt: [MethodDeclaration] destructured_method_param +# 17| getStmt: [Method] destructured_method_param # 17| getParameter: [TuplePatternParameter] (..., ...) # 17| getElement: [LocalVariableAccess] a # 17| getElement: [LocalVariableAccess] b @@ -1671,7 +1671,7 @@ params/params.rb: # 26| getAnOperand/getRightOperand: [LocalVariableAccess] second # 26| getAnOperand/getRightOperand: [LocalVariableAccess] third # 26| getAnOperand/getRightOperand: [LocalVariableAccess] fourth -# 30| getStmt: [MethodDeclaration] method_with_splat +# 30| getStmt: [Method] method_with_splat # 30| getParameter: [SimpleParameter] wibble # 30| getDefiningAccess: [LocalVariableAccess] wibble # 30| getParameter: [SplatParameter] *splat @@ -1696,7 +1696,7 @@ params/params.rb: # 38| getDefiningAccess: [LocalVariableAccess] blah # 38| getParameter: [HashSplatParameter] **wibble # 38| getDefiningAccess: [LocalVariableAccess] wibble -# 41| getStmt: [MethodDeclaration] method_with_keyword_params +# 41| getStmt: [Method] method_with_keyword_params # 41| getParameter: [SimpleParameter] x # 41| getDefiningAccess: [LocalVariableAccess] x # 41| getParameter: [KeywordParameter] foo @@ -1709,7 +1709,7 @@ params/params.rb: # 42| getAnOperand/getLeftOperand: [LocalVariableAccess] x # 42| getAnOperand/getRightOperand: [LocalVariableAccess] foo # 42| getAnOperand/getRightOperand: [LocalVariableAccess] bar -# 46| getStmt: [MethodDeclaration] use_block_with_keyword +# 46| getStmt: [Method] use_block_with_keyword # 46| getParameter: [BlockParameter] &block # 46| getDefiningAccess: [LocalVariableAccess] block # 47| getStmt: [MethodCall] call to puts @@ -1746,7 +1746,7 @@ params/params.rb: # 54| getAnOperand/getLeftOperand: [LocalVariableAccess] x # 54| getAnOperand/getRightOperand: [LocalVariableAccess] y # 54| getAnOperand/getRightOperand: [LocalVariableAccess] z -# 58| getStmt: [MethodDeclaration] method_with_optional_params +# 58| getStmt: [Method] method_with_optional_params # 58| getParameter: [SimpleParameter] val1 # 58| getDefiningAccess: [LocalVariableAccess] val1 # 58| getParameter: [OptionalParameter] val2 @@ -1755,7 +1755,7 @@ params/params.rb: # 58| getParameter: [OptionalParameter] val3 # 58| getDefiningAccess: [LocalVariableAccess] val3 # 58| getDefaultValue: [IntegerLiteral] 100 -# 62| getStmt: [MethodDeclaration] use_block_with_optional +# 62| getStmt: [Method] use_block_with_optional # 62| getParameter: [BlockParameter] &block # 62| getDefiningAccess: [LocalVariableAccess] block # 63| getStmt: [MethodCall] call to call diff --git a/ql/test/library-tests/ast/modules/classes.expected b/ql/test/library-tests/ast/modules/classes.expected index 08543bb9fb0..e81722ecfa2 100644 --- a/ql/test/library-tests/ast/modules/classes.expected +++ b/ql/test/library-tests/ast/modules/classes.expected @@ -23,8 +23,8 @@ classesWithNameScopeExprs classesWithGlobalNameScopeExprs | classes.rb:55:1:56:3 | MyClassInGlobalScope | exprsInClasses -| classes.rb:20:1:37:3 | Wibble | 0 | classes.rb:21:3:23:5 | method_a | MethodDeclaration | -| classes.rb:20:1:37:3 | Wibble | 1 | classes.rb:25:3:27:5 | method_b | MethodDeclaration | +| classes.rb:20:1:37:3 | Wibble | 0 | classes.rb:21:3:23:5 | method_a | Method | +| classes.rb:20:1:37:3 | Wibble | 1 | classes.rb:25:3:27:5 | method_b | Method | | classes.rb:20:1:37:3 | Wibble | 2 | classes.rb:29:3:29:20 | call to some_method_call | MethodCall | | classes.rb:20:1:37:3 | Wibble | 3 | classes.rb:30:3:30:19 | ... = ... | AssignExpr | | classes.rb:20:1:37:3 | Wibble | 4 | classes.rb:32:3:33:5 | ClassInWibble | ClassDeclaration | diff --git a/ql/test/library-tests/ast/modules/classes.ql b/ql/test/library-tests/ast/modules/classes.ql index ed6b8ad45b1..a4b392a32cb 100644 --- a/ql/test/library-tests/ast/modules/classes.ql +++ b/ql/test/library-tests/ast/modules/classes.ql @@ -12,7 +12,7 @@ query predicate exprsInClasses(ClassDeclaration c, int i, Expr e, string eClass) e = c.getStmt(i) and eClass = e.getAPrimaryQlClass() } -query predicate methodsInClasses(ClassDeclaration c, MethodDeclaration m, string name) { +query predicate methodsInClasses(ClassDeclaration c, Method m, string name) { m = c.getMethod(name) } diff --git a/ql/test/library-tests/ast/modules/module_base.ql b/ql/test/library-tests/ast/modules/module_base.ql index 859fe2d8bc7..e95faa62969 100644 --- a/ql/test/library-tests/ast/modules/module_base.ql +++ b/ql/test/library-tests/ast/modules/module_base.ql @@ -4,6 +4,6 @@ query predicate moduleBases(ModuleBase mb, string pClass) { pClass = mb.getAPrim query predicate moduleBaseClasses(ModuleBase mb, ClassDeclaration c) { c = mb.getAClass() } -query predicate moduleBaseMethods(ModuleBase mb, MethodDeclaration m) { m = mb.getAMethod() } +query predicate moduleBaseMethods(ModuleBase mb, Method m) { m = mb.getAMethod() } query predicate moduleBaseModules(ModuleBase mb, ModuleDeclaration m) { m = mb.getAModule() } diff --git a/ql/test/library-tests/ast/modules/modules.expected b/ql/test/library-tests/ast/modules/modules.expected index 89b33b76ef7..61352f7c78e 100644 --- a/ql/test/library-tests/ast/modules/modules.expected +++ b/ql/test/library-tests/ast/modules/modules.expected @@ -36,24 +36,24 @@ modulesWithGlobalNameScopeExprs | modules.rb:60:1:61:3 | MyModuleInGlobalScope | exprsInModules | modules.rb:4:1:24:3 | Foo | 0 | modules.rb:5:3:14:5 | Bar | ModuleDeclaration | -| modules.rb:4:1:24:3 | Foo | 1 | modules.rb:16:3:17:5 | method_in_foo | MethodDeclaration | +| modules.rb:4:1:24:3 | Foo | 1 | modules.rb:16:3:17:5 | method_in_foo | Method | | modules.rb:4:1:24:3 | Foo | 2 | modules.rb:19:3:20:5 | ClassInFoo | ClassDeclaration | | modules.rb:4:1:24:3 | Foo | 3 | modules.rb:22:3:22:19 | call to puts | MethodCall | | modules.rb:4:1:24:3 | Foo | 4 | modules.rb:23:3:23:17 | ... = ... | AssignExpr | | modules.rb:5:3:14:5 | Bar | 0 | modules.rb:6:5:7:7 | ClassInFooBar | ClassDeclaration | -| modules.rb:5:3:14:5 | Bar | 1 | modules.rb:9:5:10:7 | method_in_foo_bar | MethodDeclaration | +| modules.rb:5:3:14:5 | Bar | 1 | modules.rb:9:5:10:7 | method_in_foo_bar | Method | | modules.rb:5:3:14:5 | Bar | 2 | modules.rb:12:5:12:26 | call to puts | MethodCall | | modules.rb:5:3:14:5 | Bar | 3 | modules.rb:13:5:13:19 | ... = ... | AssignExpr | -| modules.rb:26:1:35:3 | Foo | 0 | modules.rb:27:3:28:5 | method_in_another_definition_of_foo | MethodDeclaration | +| modules.rb:26:1:35:3 | Foo | 0 | modules.rb:27:3:28:5 | method_in_another_definition_of_foo | Method | | modules.rb:26:1:35:3 | Foo | 1 | modules.rb:30:3:31:5 | ClassInAnotherDefinitionOfFoo | ClassDeclaration | | modules.rb:26:1:35:3 | Foo | 2 | modules.rb:33:3:33:25 | call to puts | MethodCall | | modules.rb:26:1:35:3 | Foo | 3 | modules.rb:34:3:34:17 | ... = ... | AssignExpr | -| modules.rb:37:1:46:3 | Bar | 0 | modules.rb:38:3:39:5 | method_a | MethodDeclaration | -| modules.rb:37:1:46:3 | Bar | 1 | modules.rb:41:3:42:5 | method_b | MethodDeclaration | +| modules.rb:37:1:46:3 | Bar | 0 | modules.rb:38:3:39:5 | method_a | Method | +| modules.rb:37:1:46:3 | Bar | 1 | modules.rb:41:3:42:5 | method_b | Method | | modules.rb:37:1:46:3 | Bar | 2 | modules.rb:44:3:44:19 | call to puts | MethodCall | | modules.rb:37:1:46:3 | Bar | 3 | modules.rb:45:3:45:17 | ... = ... | AssignExpr | | modules.rb:48:1:57:3 | Bar | 0 | modules.rb:49:3:50:5 | ClassInAnotherDefinitionOfFooBar | ClassDeclaration | -| modules.rb:48:1:57:3 | Bar | 1 | modules.rb:52:3:53:5 | method_in_another_definition_of_foo_bar | MethodDeclaration | +| modules.rb:48:1:57:3 | Bar | 1 | modules.rb:52:3:53:5 | method_in_another_definition_of_foo_bar | Method | | modules.rb:48:1:57:3 | Bar | 2 | modules.rb:55:3:55:30 | call to puts | MethodCall | | modules.rb:48:1:57:3 | Bar | 3 | modules.rb:56:3:56:17 | ... = ... | AssignExpr | | modules.rb:63:1:81:3 | Test | 0 | modules.rb:65:3:68:5 | Foo1 | ModuleDeclaration | diff --git a/ql/test/library-tests/ast/modules/modules.ql b/ql/test/library-tests/ast/modules/modules.ql index e5d1e15b2ca..9ed9c7b4fa2 100644 --- a/ql/test/library-tests/ast/modules/modules.ql +++ b/ql/test/library-tests/ast/modules/modules.ql @@ -12,7 +12,7 @@ query predicate exprsInModules(ModuleDeclaration m, int i, Expr e, string eClass e = m.getStmt(i) and eClass = e.getAPrimaryQlClass() } -query predicate methodsInModules(ModuleDeclaration mod, MethodDeclaration method, string name) { +query predicate methodsInModules(ModuleDeclaration mod, Method method, string name) { method = mod.getMethod(name) } diff --git a/ql/test/library-tests/ast/modules/singleton_classes.expected b/ql/test/library-tests/ast/modules/singleton_classes.expected index 2b11f88cfe1..f1c13b476d6 100644 --- a/ql/test/library-tests/ast/modules/singleton_classes.expected +++ b/ql/test/library-tests/ast/modules/singleton_classes.expected @@ -1,8 +1,8 @@ singletonClasses | classes.rb:41:1:52:3 | class << ... | ClassDeclaration | classes.rb:41:10:41:10 | x | exprsInSingletonClasses -| classes.rb:41:1:52:3 | class << ... | 0 | classes.rb:42:3:44:5 | length | MethodDeclaration | -| classes.rb:41:1:52:3 | class << ... | 1 | classes.rb:46:3:48:5 | wibble | MethodDeclaration | +| classes.rb:41:1:52:3 | class << ... | 0 | classes.rb:42:3:44:5 | length | Method | +| classes.rb:41:1:52:3 | class << ... | 1 | classes.rb:46:3:48:5 | wibble | Method | | classes.rb:41:1:52:3 | class << ... | 2 | classes.rb:50:3:50:21 | call to another_method_call | MethodCall | | classes.rb:41:1:52:3 | class << ... | 3 | classes.rb:51:3:51:20 | ... = ... | AssignExpr | methodsInSingletonClasses diff --git a/ql/test/library-tests/ast/modules/singleton_classes.ql b/ql/test/library-tests/ast/modules/singleton_classes.ql index 850007c0071..d02dd8cb68c 100644 --- a/ql/test/library-tests/ast/modules/singleton_classes.ql +++ b/ql/test/library-tests/ast/modules/singleton_classes.ql @@ -8,6 +8,4 @@ query predicate exprsInSingletonClasses(SingletonClass sc, int i, Expr e, string e = sc.getStmt(i) and eClass = e.getAPrimaryQlClass() } -query predicate methodsInSingletonClasses(SingletonClass sc, MethodDeclaration m) { - m = sc.getAMethod() -} +query predicate methodsInSingletonClasses(SingletonClass sc, Method m) { m = sc.getAMethod() } diff --git a/ql/test/library-tests/ast/params/params.ql b/ql/test/library-tests/ast/params/params.ql index 28913458b9b..eb77ac0e418 100644 --- a/ql/test/library-tests/ast/params/params.ql +++ b/ql/test/library-tests/ast/params/params.ql @@ -28,7 +28,7 @@ query predicate optionalParams(OptionalParameter op, string name, AstNode defaul //////////////////////////////////////////////////////////////////////////////// // Query predicates for various contexts of parameters -query predicate paramsInMethods(MethodDeclaration m, int i, Parameter p, string pClass) { +query predicate paramsInMethods(Method m, int i, Parameter p, string pClass) { p = m.getParameter(i) and pClass = p.getAPrimaryQlClass() }