Fraunhofer-AISEC / cpg

A library to extract Code Property Graphs from C/C++, Java, Go, Python, Ruby and every other language through LLVM-IR.
https://fraunhofer-aisec.github.io/cpg/
Apache License 2.0
278 stars 62 forks source link

Provide a walker strategy for *all* node types #246

Open peckto opened 4 years ago

peckto commented 4 years ago

Graph traversal is a basic task when working with graphs. It would be helpful to have such an algorithm build into the cpg library. Ideally, the user could specify a visitor function and this function is called for every edge or vertex. To discuss: traversing by edge or by vertex?

A vertex traversal algorithm is already part of codyze: OverflowDatabase.java

Maybe we can move this to cpg?

JulianSchuette commented 4 years ago

There is already a Visitor pattern that you can use to traverse nodes of the graph using any "strategy" that you like (bfs, dfs, etc).

https://github.com/Fraunhofer-AISEC/cpg/blob/6adb9d8505def3f834455ba3b736313b21a347b8/src/main/java/de/fraunhofer/aisec/cpg/processing/IVisitable.java

See VisitorTest for usage: https://github.com/Fraunhofer-AISEC/cpg/blob/f0dfe140418a2f5ac7f6bbb26afa28d967b8046e/src/test/java/de/fraunhofer/aisec/cpg/enhancements/VisitorTest.java

Does that suit your needs?

peckto commented 4 years ago

Thank you for the hint! That's basically what I'm looking for. As far as I can see, there are certain strategies defined for the visitor in Strategy.java: walking the EOG, DFG and AST. What I'm looking for would be a "walk all edges" strategy. This seems not so trivial, as every Node type can have its own list of edges. So all this lists must be walked successive to visit all Nodes in the cpg. From my understanding, thats what codyze is doing in OverflowDatabase.java. Is this understanding correct? In my opinion, the "walk all edges" strategy would be nice to have as part of the cpg library.

Masrepus commented 4 years ago

Yes that is what codyze is doing when persisting the graph. I think such a reflection-based visiting strategy could be incorporated into the visitor that Julian created

oxisto commented 3 years ago

276 might also be relevant for this. However, what is still missing is an easy way to actually traverse all nodes in the graph, not just AST nodes.