Skip to content

Commit 27e1a8b

Browse files
committed
Ruby: cache predicates related to getConst
1 parent d4b018f commit 27e1a8b

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

ruby/ql/lib/codeql/ruby/dataflow/internal/DataFlowPublic.qll

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,38 @@ private module Cached {
348348
yield.asExpr().getExpr() = call
349349
)
350350
}
351+
352+
/**
353+
* A place in which a named constant can be looked up during constant lookup.
354+
*/
355+
cached
356+
newtype TConstLookupScope =
357+
/** Look up in a qualified constant name `base::`. */
358+
MkQualifiedLookup(ConstantAccess base) or
359+
/** Look up in the ancestors of `mod`. */
360+
MkAncestorLookup(Module mod) or
361+
/** Look up in a module syntactically nested in a declaration of `mod`. */
362+
MkNestedLookup(Module mod) or
363+
/** Pseudo-scope for accesses that are known to resolve to `mod`. */
364+
MkExactLookup(Module mod)
365+
366+
/**
367+
* Gets a `LocalSourceNode` to represent the constant read or written by `access`.
368+
*/
369+
cached
370+
LocalSourceNode getConstantAccessNode(ConstantAccess access) {
371+
// Namespaces don't evaluate to the constant being accessed, they return the value of their last statement.
372+
// Use the definition of 'self' in the namespace as the representative in this case.
373+
if access instanceof Namespace
374+
then result = getNamespaceSelf(access)
375+
else result.asExpr().getExpr() = access
376+
}
377+
378+
cached
379+
predicate forceCachingInSameStage() { any() }
380+
381+
cached
382+
predicate forceCachingBackref() { exists(any(ConstRef const).getConst(_)) }
351383
}
352384

353385
private import Cached
@@ -1098,30 +1130,6 @@ class ArrayLiteralNode extends LocalSourceNode, ExprNode {
10981130
Node getAnElement() { result = this.(CallNode).getPositionalArgument(_) }
10991131
}
11001132

1101-
/**
1102-
* A place in which a named constant can be looked up during constant lookup.
1103-
*/
1104-
private newtype TConstLookupScope =
1105-
/** Look up in a qualified constant name `base::`. */
1106-
MkQualifiedLookup(ConstantAccess base) or
1107-
/** Look up in the ancestors of `mod`. */
1108-
MkAncestorLookup(Module mod) or
1109-
/** Look up in a module syntactically nested in a declaration of `mod`. */
1110-
MkNestedLookup(Module mod) or
1111-
/** Pseudo-scope for accesses that are known to resolve to `mod`. */
1112-
MkExactLookup(Module mod)
1113-
1114-
/**
1115-
* Gets a `LocalSourceNode` to represent the constant read or written by `access`.
1116-
*/
1117-
private LocalSourceNode getConstantAccessNode(ConstantAccess access) {
1118-
// Namespaces don't evaluate to the constant being accessed, they return the value of their last statement.
1119-
// Use the definition of 'self' in the namespace as the representative in this case.
1120-
if access instanceof Namespace
1121-
then result = getNamespaceSelf(access)
1122-
else result.asExpr().getExpr() = access
1123-
}
1124-
11251133
/**
11261134
* An access to a constant, such as `C`, `C::D`, or a class or module declaration.
11271135
*
@@ -1188,8 +1196,9 @@ class ConstRef extends LocalSourceNode {
11881196
/**
11891197
* Gets a scope in which a constant lookup may access the contents of the module referenced by this constant.
11901198
*/
1191-
pragma[nomagic]
1199+
cached
11921200
private TConstLookupScope getATargetScope() {
1201+
forceCachingInSameStage() and
11931202
result = MkAncestorLookup(this.getAncestryTarget().getAnImmediateDescendent*())
11941203
or
11951204
access = any(ConstantAccess ac).getScopeExpr() and
@@ -1222,8 +1231,9 @@ class ConstRef extends LocalSourceNode {
12221231
/**
12231232
* Holds if this can reference a constant named `name` from `scope` using a lookup of `kind`.
12241233
*/
1225-
pragma[nomagic]
1234+
cached
12261235
private predicate accesses(TConstLookupScope scope, string name) {
1236+
forceCachingInSameStage() and
12271237
scope = this.getLookupScope() and
12281238
name = this.getName()
12291239
or

0 commit comments

Comments
 (0)