diff options
author | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2015-01-24 00:59:35 +0100 |
---|---|---|
committer | Christopher Li <sparse@chrisli.org> | 2015-02-08 16:54:58 -0800 |
commit | 40791b94c56b1a6da2a0ddeb1f9d5c9d64de8f93 (patch) | |
tree | 1ee8e890740c4a8eec3c8a2ce49fe40e706458bf | |
parent | de1fa7e60d3d179a1b67c47a0429b2d0ac4e4842 (diff) | |
download | sparse-40791b94c56b1a6da2a0ddeb1f9d5c9d64de8f93.tar.gz |
Teach sparse about the __COUNTER__ predefined macro
__COUNTER__ 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 set of
small test programs for the test suite.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Acked-by: Sam Ravnborg <sam@ravnborg.org>
Reviewed-by: Josh Triplett <josh@joshtriplett.org>
Tested-by: Christian Borntraeger <borntraeger@de.ibm.com>
Signed-off-by: Christopher Li <sparse@chrisli.org>
-rw-r--r-- | ident-list.h | 1 | ||||
-rw-r--r-- | pre-process.c | 4 | ||||
-rw-r--r-- | validation/preprocessor/counter1.c | 12 | ||||
-rw-r--r-- | validation/preprocessor/counter2.c | 14 | ||||
-rw-r--r-- | validation/preprocessor/counter2.h | 1 | ||||
-rw-r--r-- | validation/preprocessor/counter3.c | 14 |
6 files changed, 46 insertions, 0 deletions
diff --git a/ident-list.h b/ident-list.h index d5a145f8..b65b667d 100644 --- a/ident-list.h +++ b/ident-list.h @@ -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__); diff --git a/pre-process.c b/pre-process.c index 5c386a85..7c57ba1c 100644 --- a/pre-process.c +++ b/pre-process.c @@ -45,6 +45,7 @@ #include "scope.h" static int false_nesting = 0; +static int counter_macro = 0; // __COUNTER__ expansion #define INCLUDEPATHS 300 const char *includepath[INCLUDEPATHS+1] = { @@ -181,6 +182,8 @@ 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) { + replace_with_integer(token, counter_macro++); } return 1; } @@ -1894,6 +1897,7 @@ static void init_preprocessor(void) sym->normal = 0; } + counter_macro = 0; } static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start) diff --git a/validation/preprocessor/counter1.c b/validation/preprocessor/counter1.c new file mode 100644 index 00000000..98187ee6 --- /dev/null +++ b/validation/preprocessor/counter1.c @@ -0,0 +1,12 @@ +__COUNTER__ +__COUNTER__ +/* + * check-name: __COUNTER__ #1 + * check-command: sparse -E $file + * + * check-output-start + +0 +1 + * check-output-end + */ diff --git a/validation/preprocessor/counter2.c b/validation/preprocessor/counter2.c new file mode 100644 index 00000000..9883b682 --- /dev/null +++ b/validation/preprocessor/counter2.c @@ -0,0 +1,14 @@ +__FILE__ __COUNTER__ +#include <counter2.h> +__FILE__ __COUNTER__ +/* + * check-name: __COUNTER__ #2 + * check-command: sparse -Ipreprocessor -E $file + * + * check-output-start + +"preprocessor/counter2.c" 0 +"preprocessor/counter2.h" 1 +"preprocessor/counter2.c" 2 + * check-output-end + */ diff --git a/validation/preprocessor/counter2.h b/validation/preprocessor/counter2.h new file mode 100644 index 00000000..447b70ab --- /dev/null +++ b/validation/preprocessor/counter2.h @@ -0,0 +1 @@ +__FILE__ __COUNTER__ diff --git a/validation/preprocessor/counter3.c b/validation/preprocessor/counter3.c new file mode 100644 index 00000000..fa3f173b --- /dev/null +++ b/validation/preprocessor/counter3.c @@ -0,0 +1,14 @@ +/* + * check-name: __COUNTER__ #3 + * check-command: sparse -Ipreprocessor -E preprocessor/counter1.c $file + * + * check-output-start + +0 +1 +"preprocessor/counter2.c" 0 +"preprocessor/counter2.h" 1 +"preprocessor/counter2.c" 2 + * check-output-end + */ +#include "counter2.c" |