Hello! I am looking to make a compiler plugin to a...
# compiler
o
Hello! I am looking to make a compiler plugin to analyse Kotlin code as a control flow graph. I am aiming to analyse Android code in specific. Since there exist a specification for constructing a CFG and analysing for smart casting, I am guessing Kotlin compiler plugin should also be able to construct a CFG and a builder for it should already be available somewhere. Currently I have managed to get a simple compiler plugin to work with a pre-K2 guide, meaning I am using IR. I found multiple implementations of CFG available but these don't seem to work with IR and I am not sure how to even use or build these implementations. Just so it is clear, I do not need to transform code, I only need to analyse it and throw errors and warnings. Should I look into using FIR instead? What is the suggested way of constructing a CFG and where can I find more documentation about it?
Seems a few days ago someone (not me) also asked similar question of Stackoverflow but quickly got the question closed. https://kotlinlang.slack.com/archives/C542V467L/p1707688888044149
h
I am writing a similar plugin and I use FIR:
FirAdditionalCheckersExtension
👀 1
👍 1
o
Do you also know where I could find documentation about this extension?
h
Unfortunately, documentation is rare, best luck is checking out other repos or the Kotlin source. Or use the debugger or println “debugging” 😅
d
The best way to access CFA analysis from the compiler itself is to implement your own FirControlFlowChecker and register it via FirAdditionalCheckersExtension -> DeclarationCheckers.controlFlowAnalyserCheckers You can use following compiler checkers for reference: • FirCallsEffectAnalyzer verifies
callsInPlace
contract (easy one) • FirPropertyInitializationAnalyzer checks property initialization (hard one) If you setup compiler test framework for your plugin, you will be able to generate dumps of CFG in graphviz format out of box (test and corresponding graph as an example)
kodee happy 1
You can use this repository of an example of setup project for compiler plugin and compiler test framework for it
kodee happy 1
o
Thank you very much! I will check this out kodee electrified
👌 1