Support __COUNTER__ macro
PATCH 1/1 Support __COUNTER__ macro
OUNTER__ macro is expanded to a sequential number starting from 0. This is sometimes used to declare unique variable names.
Implement support for __COUNTER__ in sparse including a small test program for the test suite.
Signed-off-by Sam Ravnborg <[email protected]>
---
I had hit this before - and then I saw a mil about it today. So took a quick look at it.
It seems to work as expected.
ident-list.h | 1 + pre-process.c | 3 +++ validation/preprocessor/preprocessor24.c | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 validation/preprocessor/preprocessor24.c
ident-list.h +1 -0
@@ -108,6 +108,7 @@ __IDENT(__TIME___ident, "__TIME__", 0);__IDENT (__func___ident,"__func__" ,0 );__IDENT (__FUNCTION___ident,"__FUNCTION__" ,0 );__IDENT (__PRETTY_FUNCTION___ident,"__PRETTY_FUNCTION__" ,0 ); +__IDENT (__COUNTER__ident,"__COUNTER__" ,0 );/* Sparse commands */ IDENT_RESERVED (__context__);
pre-process.c +3 -0
@@ -181,6 +181,9 @@ static int expand_one_symbol(struct token **list)time (&t);strftime (buffer,9 ,"%T" ,localtime (&t));replace_with_string (token, buffer); + }else if (token->ident == &__COUNTER__ident) { +static int counter =0 ; +replace_with_integer (token, counter++); }return 1 ; }
validation/preprocessor/preprocessor24.c new file +20 -0
@@ -0,0 +1,20 @@ +#define DO_CONCAT (a, b) a##b +#define CONCAT (a, b) DO_CONCAT(a, b) +#define VARNAME (name) CONCAT(name, __COUNTER__) + +int VARNAME (x ); +int VARNAME (x ); + +/* +* check-name: Preprocessor #24 __COUNTER__ +* check-command: sparse -E $file +* +* check-output-start + +int x0; +int x1; +* check-output-end +* +* check-error-start +* check-error-end +*/
Signature · 1 line
1.9.3
pre-process.c static int expand_one_symbol(struct token **list) · line 186
Quoted 3 lines · Sam Ravnborg
pre-process.c +3
-0+ }else if (token->ident == &__COUNTER__ident) { +static int counter =0 ; +replace_with_integer (token, counter++);
Same comment as with the other version of this patch: this shouldn't use a static counter, because it needs to reset for each top-level source file.
- Josh Triplett