Merge pull request #9219 from igfoo/igfoo/livelits

Improve LiveLiterals
This commit is contained in:
Ian Lynagh
2022-05-20 11:42:16 +01:00
committed by GitHub

View File

@@ -14,14 +14,22 @@ class LiveLiteral extends MethodAccess {
not this.getEnclosingCallable() instanceof LiveLiteralMethod
}
/** Gets the constant value that backs this live literal. */
/**
* Live literal classes consist of the following:
* - A private field holding the constant value that backs this live literal.
* - A private getter to access the constant value.
* - A public getter that either calls the private getter and returns its result or,
* if live literals are activated, returns the value of a dynamic state object that is initialized with
* the constant value.
*
* This predicate gets the constant value held by the private field.
*/
CompileTimeConstantExpr getValue() {
result =
any(ReturnStmt r | this.getMethod().calls*(r.getEnclosingCallable()))
.getResult()
.(VarAccess)
.getVariable()
.getInitializer()
exists(MethodAccess getterCall, VarAccess va |
methodReturns(this.getMethod(), getterCall) and
methodReturns(getterCall.getMethod(), va) and
result = va.getVariable().getInitializer()
)
}
override string toString() { result = this.getValue().toString() }
@@ -31,3 +39,10 @@ class LiveLiteral extends MethodAccess {
class LiveLiteralMethod extends Method {
LiveLiteralMethod() { this.getDeclaringType().getName().matches("LiveLiterals$%") }
}
private predicate methodReturns(Method m, Expr res) {
exists(ReturnStmt r |
r.getResult() = res and
r.getEnclosingCallable() = m
)
}