Template:GNU C Compiler Internals/When to Instrument 4 1
Add new function and type declarations in hook gem_c_common_nodes_and_builtins.
Instrument an AST after it is parsed in hook gem_finish_function.
Modify attributes of a declaration in hooks gem_start_decl and gem_finish_decl. Let us say we would like to replace local array declarations char arr[10] with a heap array char *arr=(char*)malloc(10);
void l2h_finish_decl(tree decl, tree *init, tree spec) {
...
gem_find_symtab(&t_malloc, "malloc");
BUILD_FUNC_CALL1(t_call, t_malloc, build_int_cst(NULL_TREE, size), NULL_TREE);
*init = build1(NOP_EXPR, build_pointer_type(char_type_node), t_call);
DECL(decl) = build_int_cst(NULL_TREE, 0); // if this field is NULL the init is ignored
}