Skip to content

Commit e12b6df

Browse files
authored
Merge pull request #8484 from hvitved/ruby/constant-value-rework
Ruby: Rework `getConstantValue` implementation
2 parents 98c0d73 + 4bcd4d7 commit e12b6df

File tree

7 files changed

+990
-779
lines changed

7 files changed

+990
-779
lines changed

ruby/ql/lib/codeql/ruby/ast/Constant.qll

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -224,23 +224,7 @@ class ConstantReadAccess extends ConstantAccess {
224224
*
225225
* the value being read at `M::CONST` is `"const"`.
226226
*/
227-
Expr getValue() {
228-
not exists(this.getScopeExpr()) and
229-
result = lookupConst(this.getEnclosingModule+().getModule(), this.getName()) and
230-
// For now, we restrict the scope of top-level declarations to their file.
231-
// This may remove some plausible targets, but also removes a lot of
232-
// implausible targets
233-
if result.getEnclosingModule() instanceof Toplevel
234-
then result.getFile() = this.getFile()
235-
else any()
236-
or
237-
this.hasGlobalScope() and
238-
result = lookupConst(TResolved("Object"), this.getName())
239-
or
240-
result = lookupConst(resolveConstantReadAccess(this.getScopeExpr()), this.getName())
241-
}
242-
243-
final override ConstantValue getConstantValue() { result = this.getValue().getConstantValue() }
227+
Expr getValue() { result = getConstantReadAccessValue(this) }
244228

245229
final override string getAPrimaryQlClass() { result = "ConstantReadAccess" }
246230
}

ruby/ql/lib/codeql/ruby/ast/Expr.qll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ private import codeql.ruby.AST
22
private import codeql.ruby.CFG
33
private import codeql.ruby.ast.Constant
44
private import internal.AST
5+
private import internal.Constant
56
private import internal.Expr
67
private import internal.TreeSitter
78

@@ -19,9 +20,7 @@ class Expr extends Stmt, TExpr {
1920
deprecated string getValueText() { result = this.getConstantValue().toString() }
2021

2122
/** Gets the constant value of this expression, if any. */
22-
ConstantValue getConstantValue() {
23-
forex(CfgNodes::ExprCfgNode n | n = this.getAControlFlowNode() | result = n.getConstantValue())
24-
}
23+
ConstantValue getConstantValue() { result = getConstantValueExpr(this) }
2524
}
2625

2726
/** DEPRECATED: Use `SelfVariableAccess` instead. */

0 commit comments

Comments
 (0)