Writing a simple grade I ran into a fun problem.
Based on the code:
enum node_type {LEAF, NODE}; struct tree_elm_t { enum node_type type; union { struct tree_node_t node; struct tree_leaf_t leaf; } datum; }; int parse_leaf(struct tree_leaf_t leaf); int parse_node( struct tree_node_t node ); int parse_tree( struct tree_elm_t* tree ); .... int parse_tree( struct tree_elm_t* tree ) { switch( tree->type ) { case NODE: return parse_node(tree->datum.node); case LEAF: return parse_leaf(tree->datum.leaf); } }
I was surprised to see gcc complaining about a missing control flow option:
example.c: In function 'parse_tree': example.c:54: warning: control reaches end of non-void function
a thread problem can be solved by storing the return value in a variable like this:
int parse_tree( struct tree_elm_t* tree ) { int sum; switch( tree->type ) { case NODE: sum = parse_node(tree->datum.node); break; case LEAF: sum = parse_leaf(tree->datum.leaf); break; } return sum; }
However, I found the alot cleaner source code, there is a way to make gcc accept the source code - (I want static analysis to understand that my code is valid and clean).
EDIT:
Perhaps I was a little obscure.
allows you to compile the following code:
int parse_tree( struct tree_elm_t* tree ) { int sum; switch( tree->type ) { case NODE: sum = parse_node(tree->datum.node); break;
gcc will give me a warning:
example.c: In function 'parse_tree': example.c:51: warning: enumeration value 'LEAF' not handled in switch
means that gcc makes sense of options for values ββin the switch and the fact that I hav commented on the case of LEAF. This would mean that gcc also knows that when moving through the switch, each case is considered. therefore why the statement:
control reaches end of non-void function
Is there no static analysis system in gcc or language function?