Skip to content

Support for constant propagation #1831

Answered by oxisto
llooFlashooll asked this question in Q&A
Discussion options

You must be logged in to vote

You need to include the cpg-analysis module. You can then use the ValueEvaluator class to evaluate simple constants such as strings and numbers in most languages. You can also override the default behaviour if you specify your own implementation of notDefined. There is also a shortcut you can use: evaluate(), so if you have a node that you suspect will contain a constant you can do

import de.fraunhofer.aisec.cpg.graph.*

val node = /* ... */
val value = node.evaluate()

This is equivalent to ValueEvaluator().evaluate(node)

You can find some more test cases here: https://github.com/Fraunhofer-AISEC/cpg/blob/main/cpg-analysis/src/test/kotlin/de/fraunhofer/aisec/cpg/analysis/ValueEvaluatorTes…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@llooFlashooll
Comment options

@oxisto
Comment options

Answer selected by oxisto
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants