@@ -8,7 +8,13 @@ private import codeql_ruby.ast.Operation
88private import codeql_ruby.ast.Scope
99
1010// Names of built-in modules and classes
11- private string builtin ( ) { result = [ "Object" , "Kernel" , "BasicObject" , "Class" , "Module" ] }
11+ private string builtin ( ) {
12+ result =
13+ [
14+ "Object" , "Kernel" , "BasicObject" , "Class" , "Module" , "NilClass" , "FalseClass" , "TrueClass" ,
15+ "Numeric" , "Integer" , "Float" , "Rational" , "Complex" , "Array" , "Hash" , "Symbol" , "Proc"
16+ ]
17+ }
1218
1319cached
1420private module Cached {
@@ -39,9 +45,14 @@ private module Cached {
3945 Module getSuperClass ( Module cls ) {
4046 cls = TResolved ( "Object" ) and result = TResolved ( "BasicObject" )
4147 or
42- cls = TResolved ( "Module" ) and result = TResolved ( "Object" )
48+ cls = TResolved ( [ "Module" , "Numeric" , "Array" , "Hash" , "FalseClass" , "TrueClass" , "NilClass" ] ) and
49+ result = TResolved ( "Object" )
50+ or
51+ cls = TResolved ( [ "Integer" , "Float" , "Rational" , "Complex" ] ) and
52+ result = TResolved ( "Numeric" )
4353 or
44- cls = TResolved ( "Class" ) and result = TResolved ( "Module" )
54+ cls = TResolved ( "Class" ) and
55+ result = TResolved ( "Module" )
4556 or
4657 not cls = TResolved ( builtin ( ) ) and
4758 (
@@ -86,6 +97,39 @@ private module Cached {
8697 result = resolveScopeExpr ( c .getAnArgument ( ) )
8798 )
8899 }
100+
101+ /**
102+ * Resolve constant read access (typically a scope expression) to a qualified module name.
103+ * `resolveScopeExpr/1` picks the best (lowest priority number) result of
104+ * `resolveScopeExpr/2` that resolves to a constant definition. If the constant
105+ * definition is a Namespace then it is returned, if it's a constant assignment then
106+ * the right-hand side of the assignment is resolved.
107+ */
108+ cached
109+ TResolved resolveScopeExpr ( ConstantReadAccess r ) {
110+ exists ( string qname |
111+ qname =
112+ min ( string qn , int p |
113+ isDefinedConstant ( qn ) and
114+ qn = resolveScopeExpr ( r , p ) and
115+ // prevent classes/modules that contain/extend themselves
116+ not exists ( ConstantWriteAccess w | qn = constantDefinition0 ( w ) |
117+ r = w .getScopeExpr ( )
118+ or
119+ r = w .( ClassDeclaration ) .getSuperclassExpr ( )
120+ )
121+ |
122+ qn order by p
123+ )
124+ |
125+ result = TResolved ( qname )
126+ or
127+ exists ( ConstantAssignment a |
128+ qname = constantDefinition0 ( a ) and
129+ result = resolveScopeExpr ( a .getParent ( ) .( Assignment ) .getRightOperand ( ) )
130+ )
131+ )
132+ }
89133}
90134
91135import Cached
@@ -103,38 +147,6 @@ private predicate isDefinedConstant(string qualifiedModuleName) {
103147 qualifiedModuleName = [ builtin ( ) , constantDefinition0 ( _) ]
104148}
105149
106- /**
107- * Resolve constant read access (typically a scope expression) to a qualified module name.
108- * `resolveScopeExpr/1` picks the best (lowest priority number) result of
109- * `resolveScopeExpr/2` that resolves to a constant definition. If the constant
110- * definition is a Namespace then it is returned, if it's a constant assignment then
111- * the right-hand side of the assignment is resolved.
112- */
113- private TResolved resolveScopeExpr ( ConstantReadAccess r ) {
114- exists ( string qname |
115- qname =
116- min ( string qn , int p |
117- isDefinedConstant ( qn ) and
118- qn = resolveScopeExpr ( r , p ) and
119- // prevent classes/modules that contain/extend themselves
120- not exists ( ConstantWriteAccess w | qn = constantDefinition0 ( w ) |
121- r = w .getScopeExpr ( )
122- or
123- r = w .( ClassDeclaration ) .getSuperclassExpr ( )
124- )
125- |
126- qn order by p
127- )
128- |
129- result = TResolved ( qname )
130- or
131- exists ( ConstantAssignment a |
132- qname = constantDefinition0 ( a ) and
133- result = resolveScopeExpr ( a .getParent ( ) .( Assignment ) .getRightOperand ( ) )
134- )
135- )
136- }
137-
138150private int maxDepth ( ) { result = 1 + max ( int level | exists ( enclosing ( _, level ) ) ) }
139151
140152private ModuleBase enclosing ( ModuleBase m , int level ) {
0 commit comments