Many of the plugin events are called for each function within the source code being compiled. Each time, the plugin passes a gcc.Function instance as a parameter to your callback, so that you can work on it.
You can get at the control flow graph of a gcc.Function via its cfg attribute. This is an instance of gcc.Cfg.
Wrapper around one of GCC’s struct function *
The declaration of this function, as a gcc.FunctionDecl
List of gcc.VarDecl for the function’s local variables. It does not contain arguments; for those see the arguments property of the function’s decl.
Note that for locals with initializers, initial only seems to get set on those local_decls that are static variables. For other locals, it appears that you have to go into the gimple representation to locate assignments.
The gcc.Location of the beginning of the function
The gcc.Location of the end of the function
Integer: a sequence number for profiling, debugging, etc.
A gcc.Cfg is a wrapper around GCC’s struct control_flow_graph.
List of gcc.BasicBlock, giving all of the basic blocks within this CFG
Instance of gcc.BasicBlock: the entrypoint for this CFG
Instance of gcc.BasicBlock: the final one within this CFG
Given a gcc.LabelDecl, get the corresponding gcc.BasicBlock
You can use gccutils.cfg_to_dot to render a gcc.Cfg as a graphviz diagram. It will render the diagram, showing each basic block, with source code on the left-hand side, interleaved with the “gimple” representation on the right-hand side. Each block is labelled with its index, and edges are labelled with appropriate flags.
For example, given this sample C code:
int main(int argc, char **argv) { int i; printf("argc: %i\n", argc); for (i = 0; i < argc; i++) { printf("argv[%i]: %s\n", argv[i]); } helper_function(); return 0; }
then the following Python code:
dot = gccutils.cfg_to_dot(fun.cfg)
gccutils.invoke_dot(dot)
will render a CFG bitmap like this:
A gcc.BasicBlock is a wrapper around GCC’s basic_block type.
The index of the block (an int), as seen in the cfg_to_dot rendering.
The list of gcc.GimplePhi phoney functions at the top of this block, if appropriate for this pass, or None
The list of gcc.Gimple instructions, if appropriate for this pass, or None
A wrapper around GCC’s edge type.
The source gcc.BasicBlock of this edge
The destination gcc.BasicBlock of this edge
Boolean: True if this edge is taken when a gcc.GimpleCond conditional is true, False otherwise
Boolean: True if this edge is taken when a gcc.GimpleCond conditional is false, False otherwise