Thursday, 12 September 2013

How to prevent shadowing with iterator macros in C

How to prevent shadowing with iterator macros in C

Here's an example of a macro that wraps iterator functions in C,
Macro definition:
/* helper macros for iterating over tree types */
#define NODE_TREE_TYPES_BEGIN(ntype) \
{ \
GHashIterator *__node_tree_type_iter__ = ntreeTypeGetIterator(); \
for (; !BLI_ghashIterator_done(__node_tree_type_iter__);
BLI_ghashIterator_step(__node_tree_type_iter__)) { \
bNodeTreeType *ntype =
BLI_ghashIterator_getValue(__node_tree_type_iter__);
#define NODE_TREE_TYPES_END \
} \
BLI_ghashIterator_free(__node_tree_type_iter__); \
}
Example use:
NODE_TREE_TYPES_BEGIN(nt) {
if (nt->ext.free) {
nt->ext.free(nt->ext.data);
}
}
NODE_TREE_TYPES_END
However nested use (while functional), causes shadowing (Gcc's -Wshadow)
NODE_TREE_TYPES_BEGIN(nt_a) {
NODE_TREE_TYPES_BEGIN(nt_b) {
/* do somthing */
}
NODE_TREE_TYPES_END
}
NODE_TREE_TYPES_END
The only way I can think of to avoid this is to pass a unique identifier
to NODE_TREE_TYPES_BEGIN and NODE_TREE_TYPES_END. So my question is...
Is there there a way to prevent shadowing if variables declared within an
iterator macro when its scope is nested?

No comments:

Post a Comment