Is it possible to use the C preprocessor to nest goto labels in C11 or C99? My case is probably best illustrated by looking at the following code. Compiles cleanly with gcc -std=c99 -pedantic -Wall -Wextra
.
#include <stdio.h>
// Macro for mangling the identifier to avoid collisions
#define CTX_ID_(NAME) context_label_ ## NAME ## _
#define CTX_ID(NAME) CTX_ID_(NAME)
// The context keyword starts a block that can be exited with break (ID);
// Just syntactic sugar to keep it structured.
#define context(ID) \
if (0) { CTX_ID(ID): ; } else
// Overloaded break keyword. Doesn't prevent using the plain break;
#define break(ID) \
do { goto CTX_ID(ID); } while (0)
// Example run
int main(void) {
context (c) {
while (1) {
puts("Outer loop, visible.");
while (1) {
puts("Inner loop, visible.");
break (c);
puts("You won't see me.");
}
}
puts("Nor me.");
}
}
I'm trying to do away with the identifier (c in this case). However, unlike variables, goto labels cannot be nested/scoped as they have to be unique within a function. Is it possible to implement unique scoped identifiers in the C preprocessor that can be used as goto labels?
GCC supports taking the address of a label but it is not a part of the ISO standard. Also, I am specifically trying to avoid setjmp due to the overhead and volatileness issues. Finally, if you don't see the usefulness of the above construct, please think of further uses such as try-catch clauses or Python-style with-expressions to enable RAII-like functionality.