diff mbox

Teach sparse about the __COUNTER__ predefined macro

Message ID 20150123235934.GA42931@macpro.local (mailing list archive)
State Superseded, archived
Headers show

Commit Message

Luc Van Oostenryck Jan. 23, 2015, 11:59 p.m. UTC
On Fri, Jan 23, 2015 at 02:38:56PM -0800, josh@joshtriplett.org wrote:
> On Fri, Jan 23, 2015 at 11:23:32PM +0100, Luc Van Oostenryck wrote:
> > On Fri, Jan 23, 2015 at 08:40:17AM -0800, Christopher Li wrote:
> > > I think sparse haven't implement the __COUNTER__ macro. That is why it emit the
> > > error on duplicate entry.
> > 
> > The following patch should fix that.
> [...]
> > Subject: [PATCH] Teach sparse about the __COUNTER__ predefined macro.
> > 
> > Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
> 
> One issue below.
> 
> > --- a/pre-process.c
> > +++ b/pre-process.c
> > @@ -181,6 +181,10 @@ 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;
> > +
> > +		replace_with_integer(token, counter++);
> 
> This should not use a static counter.  GCC and Sparse can run over more
> than one file in one invocation:
> 
> $ head test1.c test2.c
> ==> test1.c <==
> __COUNTER__
> __COUNTER__
> 
> ==> test2.c <==
> __COUNTER__
> __COUNTER__
> 
> $ gcc -E test1.c test2.c
> # 1 "test1.c"
> # 1 "<built-in>"
> # 1 "<command-line>"
> # 1 "/usr/include/stdc-predef.h" 1 3 4
> # 1 "<command-line>" 2
> # 1 "test1.c"
> 0
> 1
> # 1 "test2.c"
> # 1 "<built-in>"
> # 1 "<command-line>"
> # 1 "/usr/include/stdc-predef.h" 1 3 4
> # 1 "<command-line>" 2
> # 1 "test2.c"
> 0
> 1
> 
> Notice that the second file starts with __COUNTER__ at 0 again.
> 
> The counter *should* keep counting through include files, but needs to
> reset before starting a new top-level compile.
> 
> - Josh Triplett
> --

Yes, indeed.
Thanks for bringing to my attention.

Here is a new version of the patch taking care of that.


Luc

---
Subject: [PATCH] Teach sparse about the __COUNTER__ predefined macro.

This macro expands to sequential integral values starting from 0,
and this for each top-level source file.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 ident-list.h                       |  1 +
 pre-process.c                      |  4 ++++
 validation/preprocessor/counter1.c | 12 ++++++++++++
 validation/preprocessor/counter2.c | 14 ++++++++++++++
 validation/preprocessor/counter2.h |  1 +
 validation/preprocessor/counter3.c | 13 +++++++++++++
 6 files changed, 45 insertions(+)
 create mode 100644 validation/preprocessor/counter1.c
 create mode 100644 validation/preprocessor/counter2.c
 create mode 100644 validation/preprocessor/counter2.h
 create mode 100644 validation/preprocessor/counter3.c

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Josh Triplett Jan. 24, 2015, 1:29 a.m. UTC | #1
On Sat, Jan 24, 2015 at 12:59:35AM +0100, Luc Van Oostenryck wrote:
> Subject: [PATCH] Teach sparse about the __COUNTER__ predefined macro.
> 
> This macro expands to sequential integral values starting from 0,
> and this for each top-level source file.
> 
> Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>

counter3.c seems like a bit of an abuse of the test suite framework, but
I don't have a better suggestion.

Reviewed-by: Josh Triplett <josh@joshtriplett.org>

>  ident-list.h                       |  1 +
>  pre-process.c                      |  4 ++++
>  validation/preprocessor/counter1.c | 12 ++++++++++++
>  validation/preprocessor/counter2.c | 14 ++++++++++++++
>  validation/preprocessor/counter2.h |  1 +
>  validation/preprocessor/counter3.c | 13 +++++++++++++
>  6 files changed, 45 insertions(+)
>  create mode 100644 validation/preprocessor/counter1.c
>  create mode 100644 validation/preprocessor/counter2.c
>  create mode 100644 validation/preprocessor/counter2.h
>  create mode 100644 validation/preprocessor/counter3.c
> 
> 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 1aa3d2c4..601e0f26 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;		// __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;
>  }
> @@ -1882,6 +1885,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..1449b2d1
> --- /dev/null
> +++ b/validation/preprocessor/counter3.c
> @@ -0,0 +1,13 @@
> +/*
> + * check-name: __COUNTER__ #3
> + * check-command: sparse -Ipreprocessor -E preprocessor/counter1.c preprocessor/counter2.c
> + *
> + * check-output-start
> +
> +0
> +1
> +"preprocessor/counter2.c" 0
> +"preprocessor/counter2.h" 1
> +"preprocessor/counter2.c" 2
> + * check-output-end
> + */
> --
> To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Christian Borntraeger Jan. 25, 2015, 8:12 p.m. UTC | #2
Am 24.01.2015 um 00:59 schrieb Luc Van Oostenryck:
[...] 
> Yes, indeed.
> Thanks for bringing to my attention.
> 
> Here is a new version of the patch taking care of that.
> 
> 
> Luc
> 
> ---
> Subject: [PATCH] Teach sparse about the __COUNTER__ predefined macro.
> 
> This macro expands to sequential integral values starting from 0,
> and this for each top-level source file.
> 
> Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>

Tested-by: Christian Borntraeger <borntraeger@de.ibm.com>

> ---
>  ident-list.h                       |  1 +
>  pre-process.c                      |  4 ++++
>  validation/preprocessor/counter1.c | 12 ++++++++++++
>  validation/preprocessor/counter2.c | 14 ++++++++++++++
>  validation/preprocessor/counter2.h |  1 +
>  validation/preprocessor/counter3.c | 13 +++++++++++++
>  6 files changed, 45 insertions(+)
>  create mode 100644 validation/preprocessor/counter1.c
>  create mode 100644 validation/preprocessor/counter2.c
>  create mode 100644 validation/preprocessor/counter2.h
>  create mode 100644 validation/preprocessor/counter3.c
> 
> 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 1aa3d2c4..601e0f26 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;		// __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;
>  }
> @@ -1882,6 +1885,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..1449b2d1
> --- /dev/null
> +++ b/validation/preprocessor/counter3.c
> @@ -0,0 +1,13 @@
> +/*
> + * check-name: __COUNTER__ #3
> + * check-command: sparse -Ipreprocessor -E preprocessor/counter1.c preprocessor/counter2.c
> + *
> + * check-output-start
> +
> +0
> +1
> +"preprocessor/counter2.c" 0
> +"preprocessor/counter2.h" 1
> +"preprocessor/counter2.c" 2
> + * check-output-end
> + */
> 

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Christian Borntraeger Jan. 28, 2015, 10:08 a.m. UTC | #3
Am 25.01.2015 um 21:12 schrieb Christian Borntraeger:
> Am 24.01.2015 um 00:59 schrieb Luc Van Oostenryck:
> [...] 
>> Yes, indeed.
>> Thanks for bringing to my attention.
>>
>> Here is a new version of the patch taking care of that.
>>
>>
>> Luc
>>
>> ---
>> Subject: [PATCH] Teach sparse about the __COUNTER__ predefined macro.
>>
>> This macro expands to sequential integral values starting from 0,
>> and this for each top-level source file.
>>
>> Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
> 
> Tested-by: Christian Borntraeger <borntraeger@de.ibm.com>

Christoper,
can you apply that fix as well?

Thans

> 
>> ---
>>  ident-list.h                       |  1 +
>>  pre-process.c                      |  4 ++++
>>  validation/preprocessor/counter1.c | 12 ++++++++++++
>>  validation/preprocessor/counter2.c | 14 ++++++++++++++
>>  validation/preprocessor/counter2.h |  1 +
>>  validation/preprocessor/counter3.c | 13 +++++++++++++
>>  6 files changed, 45 insertions(+)
>>  create mode 100644 validation/preprocessor/counter1.c
>>  create mode 100644 validation/preprocessor/counter2.c
>>  create mode 100644 validation/preprocessor/counter2.h
>>  create mode 100644 validation/preprocessor/counter3.c
>>
>> 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 1aa3d2c4..601e0f26 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;		// __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;
>>  }
>> @@ -1882,6 +1885,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..1449b2d1
>> --- /dev/null
>> +++ b/validation/preprocessor/counter3.c
>> @@ -0,0 +1,13 @@
>> +/*
>> + * check-name: __COUNTER__ #3
>> + * check-command: sparse -Ipreprocessor -E preprocessor/counter1.c preprocessor/counter2.c
>> + *
>> + * check-output-start
>> +
>> +0
>> +1
>> +"preprocessor/counter2.c" 0
>> +"preprocessor/counter2.h" 1
>> +"preprocessor/counter2.c" 2
>> + * check-output-end
>> + */
>>
> 

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Christopher Li Feb. 2, 2015, 5:17 a.m. UTC | #4
On Fri, Jan 23, 2015 at 3:59 PM, Luc Van Oostenryck
<luc.vanoostenryck@gmail.com> wrote:
> Thanks for bringing to my attention.
>
> Here is a new version of the patch taking care of that.
>

I like Sam's commit message better. So I merge a version using his
commit message.

The branch for review is here:
https://git.kernel.org/cgit/devel/sparse/chrisl/sparse.git/log/?h=review-counter

If there is no objects, I will merge it to master.

Chris
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Luc Van Oostenryck Feb. 4, 2015, 2:38 a.m. UTC | #5
On Sun, Feb 01, 2015 at 09:17:38PM -0800, Christopher Li wrote:
> On Fri, Jan 23, 2015 at 3:59 PM, Luc Van Oostenryck
> <luc.vanoostenryck@gmail.com> wrote:
> > Thanks for bringing to my attention.
> >
> > Here is a new version of the patch taking care of that.
> >
> 
> I like Sam's commit message better. So I merge a version using his
> commit message.
> 
> The branch for review is here:
> https://git.kernel.org/cgit/devel/sparse/chrisl/sparse.git/log/?h=review-counter
> 
> If there is no objects, I will merge it to master.
> 
> Chris
> --

Fine for me but Josh had a remark about the third test file (validation/preprocessor/counter3.c)
abusing a bit the test framework and I shared his feeling.

Here is a patch serie fixing this abuse by extending the test framework.


1/3) test-suite: add support for tests case involving several input files
2/3) test-suite: allow filename expansion of the input sections
3/3) test-suite: consolidate tests that require include files into single test files

 Documentation/test-suite                 | 11 +++++++++++
 validation/.gitignore                    |  1 +
 validation/pragma-once.c                 | 13 ++++++++++++-
 validation/preprocessor/counter2.c       | 16 +++++++++++-----
 validation/preprocessor/counter2.h       |  1 -
 validation/preprocessor/counter3.c       | 15 +++++++++++----
 validation/preprocessor/preprocessor20.c | 18 +++++++++++++++---
 validation/preprocessor/preprocessor20.h |  6 ------
 validation/test-suite                    | 11 +++++++++++
 validation/test-suite-file-expansion.c   | 22 ++++++++++++++++++++++
 10 files changed, 94 insertions(+), 20 deletions(-)
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Christopher Li Feb. 12, 2015, 8:16 p.m. UTC | #6
On Tue, Feb 3, 2015 at 6:38 PM, Luc Van Oostenryck
<luc.vanoostenryck@gmail.com> wrote:
>
> Fine for me but Josh had a remark about the third test file (validation/preprocessor/counter3.c)
> abusing a bit the test framework and I shared his feeling.
>
> Here is a patch serie fixing this abuse by extending the test framework.
>

I check in a version with very minor fix for the counter3.c hack.
Counter3.c does a few things unclean, top of which is, it does not invoke
counter3.c at all. It is invoking counter1.c and counter2.c.

I modify coutner3.c to include the couter2.c, that will solve the problem that
counter3.c not using its own source code.

Any way, the change is pushed.

Thanks

Chris
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

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 1aa3d2c4..601e0f26 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;		// __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;
 }
@@ -1882,6 +1885,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..1449b2d1
--- /dev/null
+++ b/validation/preprocessor/counter3.c
@@ -0,0 +1,13 @@ 
+/*
+ * check-name: __COUNTER__ #3
+ * check-command: sparse -Ipreprocessor -E preprocessor/counter1.c preprocessor/counter2.c
+ *
+ * check-output-start
+
+0
+1
+"preprocessor/counter2.c" 0
+"preprocessor/counter2.h" 1
+"preprocessor/counter2.c" 2
+ * check-output-end
+ */