Saturday, 7 September 2013

How do I get C to understand my code :)

How do I get C to understand my code :)

Writing a simple evaluate I came across a funny issue.
Given 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_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 that gcc is complaining about a missing control
flow option :
example.c: In function 'parse_tree':
example.c:54: warning: control reaches end of non-void function
the flow problem can be solved by storing the return value, in a variable
like so:
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;
}
I do however find the original code alot cleaner, is there a way of making
gcc accept the original code - (I want to static analysis to realize that
my code is valid, and clean).

No comments:

Post a Comment