Is it possible to create a macro that expands the following code
int error = 0;
struct parse_tree res = CLEANUP_parse(file_hdl);
// ...
cleanup:
return error
into something semantically equivalent (enough) to
int error = 0;
struct parse_tree res;
struct parse_tree tmp = parse(file_hdl);
if(global_error_variable != 0) {
error = global_error_variable;
goto cleanup;
} else {
res = tmp;
}
// ...
cleanup:
return error
The reason I fear it's impossible is that goto label;
is a statement, and I really need an expression. E.g. something like
#define CLEANUP_parse(file_hdl) ((tmp=parse(file_hdl)) ^ tmp ^ global_error_variable) ? goto cleanup : tmp
doesn't work for that reason (and because tmp
is not declared and possibly more).
Unfortunately compiler extensions are only possible if GCC, VSC and Texas Instrument's C compiler support them.
Background
We have an existing codebase where each function returns an integer error code and the caller jumps to cleanup:
on non-zero return code. I consider introducing functions that use a global error code to enable the use of their return value, but want to do to in a safe and backwards compatible manner. The full picture on SE Code Review