diff mbox series

[4/9] pre-process: print macros containing # and ## correctly

Message ID 9704c8a7-1802-efc5-0bea-0e0a9901e72d@ramsayjones.plus.com (mailing list archive)
State Mainlined, archived
Headers show
Series misc sparse patches | expand

Commit Message

Ramsay Jones Nov. 19, 2018, 8:49 p.m. UTC
The dump_macro() function fails to correctly output the definitions of
macros that contain the string operator '#', the concatenation operator
'##' and any macro parameter in the definition token list. For example,
the following macros:

    #define STRING(x) #x
    #define CONCAT(x,y) x ## y

are output like so:

    #define STRING(x) unhandled token type '21'
    #define CONCAT(x, y) unhandled token type '22'  unhandled token type '23'  unhandled token type '22'

Add the code necessary to handle those token types to the dump_macros()
function and add the above macros to the 'dump-macros.c' test file.

Signed-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com>
---
 pre-process.c                         | 7 +++++++
 validation/preprocessor/dump-macros.c | 5 +++++
 2 files changed, 12 insertions(+)

Comments

Luc Van Oostenryck Nov. 20, 2018, 12:03 a.m. UTC | #1
On Mon, Nov 19, 2018 at 08:49:42PM +0000, Ramsay Jones wrote:
> 
> The dump_macro() function fails to correctly output the definitions of
> macros that contain the string operator '#', the concatenation operator
> '##' and any macro parameter in the definition token list. For example,
> the following macros:
> 
>     #define STRING(x) #x
>     #define CONCAT(x,y) x ## y
> 
> are output like so:
> 
>     #define STRING(x) unhandled token type '21'
>     #define CONCAT(x, y) unhandled token type '22'  unhandled token type '23'  unhandled token type '22'

Mmmm, yes. I never thought about these.
 
> Add the code necessary to handle those token types to the dump_macros()
> function and add the above macros to the 'dump-macros.c' test file.
> 
> Signed-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com>
> ---
>  pre-process.c                         | 7 +++++++
>  validation/preprocessor/dump-macros.c | 5 +++++
>  2 files changed, 12 insertions(+)
> 
> diff --git a/pre-process.c b/pre-process.c
> index 8abd5e6..eca7ab5 100644
> --- a/pre-process.c
> +++ b/pre-process.c
> @@ -2196,6 +2196,13 @@ static void dump_macro(struct symbol *sym)
>  		switch (token_type(token)) {
>  		case TOKEN_UNTAINT:
>  			break;
> +		case TOKEN_STR_ARGUMENT:
> +			printf("#%s", show_token(args[token->argnum]));
> +			break;
> +		case TOKEN_CONCAT:
> +			printf("## ");
> +			break;
> +		case TOKEN_QUOTED_ARGUMENT:
>  		case TOKEN_MACRO_ARGUMENT:

I'm wondering if there could be some other TOKEN type that would
need to be handled here.

-- Luc
Ramsay Jones Nov. 20, 2018, 12:42 a.m. UTC | #2
On 20/11/2018 00:03, Luc Van Oostenryck wrote:
> On Mon, Nov 19, 2018 at 08:49:42PM +0000, Ramsay Jones wrote:
>>
>> The dump_macro() function fails to correctly output the definitions of
>> macros that contain the string operator '#', the concatenation operator
>> '##' and any macro parameter in the definition token list. For example,
>> the following macros:
>>
>>     #define STRING(x) #x
>>     #define CONCAT(x,y) x ## y
>>
>> are output like so:
>>
>>     #define STRING(x) unhandled token type '21'
>>     #define CONCAT(x, y) unhandled token type '22'  unhandled token type '23'  unhandled token type '22'
> 
> Mmmm, yes. I never thought about these.
>  
>> Add the code necessary to handle those token types to the dump_macros()
>> function and add the above macros to the 'dump-macros.c' test file.
>>
>> Signed-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com>
>> ---
>>  pre-process.c                         | 7 +++++++
>>  validation/preprocessor/dump-macros.c | 5 +++++
>>  2 files changed, 12 insertions(+)
>>
>> diff --git a/pre-process.c b/pre-process.c
>> index 8abd5e6..eca7ab5 100644
>> --- a/pre-process.c
>> +++ b/pre-process.c
>> @@ -2196,6 +2196,13 @@ static void dump_macro(struct symbol *sym)
>>  		switch (token_type(token)) {
>>  		case TOKEN_UNTAINT:
>>  			break;
>> +		case TOKEN_STR_ARGUMENT:
>> +			printf("#%s", show_token(args[token->argnum]));
>> +			break;
>> +		case TOKEN_CONCAT:
>> +			printf("## ");
>> +			break;
>> +		case TOKEN_QUOTED_ARGUMENT:
>>  		case TOKEN_MACRO_ARGUMENT:
> 
> I'm wondering if there could be some other TOKEN type that would
> need to be handled here.

Good question, but I don't know the answer! ;-)

These patches were written months ago and I was being driven by
the need to harmonize the output of the 'meld ggc-macs sp-macs'
command! I quite literally wrote each of these patches, in the
given order, to address each irritation as I noticed it!

ATB,
Ramsay Jones
Luc Van Oostenryck Nov. 21, 2018, 12:27 a.m. UTC | #3
On Tue, Nov 20, 2018 at 12:42:20AM +0000, Ramsay Jones wrote:
> 
> 
> On 20/11/2018 00:03, Luc Van Oostenryck wrote:
> > On Mon, Nov 19, 2018 at 08:49:42PM +0000, Ramsay Jones wrote:
> >>
> >> The dump_macro() function fails to correctly output the definitions of
> >> macros that contain the string operator '#', the concatenation operator
> >> '##' and any macro parameter in the definition token list. For example,
> >> the following macros:
> >>
> >>     #define STRING(x) #x
> >>     #define CONCAT(x,y) x ## y
> >>
> >> are output like so:
> >>
> >>     #define STRING(x) unhandled token type '21'
> >>     #define CONCAT(x, y) unhandled token type '22'  unhandled token type '23'  unhandled token type '22'
> > 
> > Mmmm, yes. I never thought about these.
> >  
> >> Add the code necessary to handle those token types to the dump_macros()
> >> function and add the above macros to the 'dump-macros.c' test file.
> >>
> >> Signed-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com>
> >> ---
> >>  pre-process.c                         | 7 +++++++
> >>  validation/preprocessor/dump-macros.c | 5 +++++
> >>  2 files changed, 12 insertions(+)
> >>
> >> diff --git a/pre-process.c b/pre-process.c
> >> index 8abd5e6..eca7ab5 100644
> >> --- a/pre-process.c
> >> +++ b/pre-process.c
> >> @@ -2196,6 +2196,13 @@ static void dump_macro(struct symbol *sym)
> >>  		switch (token_type(token)) {
> >>  		case TOKEN_UNTAINT:
> >>  			break;
> >> +		case TOKEN_STR_ARGUMENT:
> >> +			printf("#%s", show_token(args[token->argnum]));
> >> +			break;
> >> +		case TOKEN_CONCAT:
> >> +			printf("## ");
> >> +			break;
> >> +		case TOKEN_QUOTED_ARGUMENT:
> >>  		case TOKEN_MACRO_ARGUMENT:
> > 
> > I'm wondering if there could be some other TOKEN type that would
> > need to be handled here.
> 
> Good question, but I don't know the answer! ;-)
> 
> These patches were written months ago and I was being driven by
> the need to harmonize the output of the 'meld ggc-macs sp-macs'
> command! I quite literally wrote each of these patches, in the
> given order, to address each irritation as I noticed it!

Yes, nervermind. It was as much a note to myself than a real question.

I quickly checked and I think only TOKEN_GNU_KLUDGE may need some
extra processing.

Best regards,
-- Luc
Luc Van Oostenryck Nov. 24, 2018, 5:28 p.m. UTC | #4
On Mon, Nov 19, 2018 at 08:49:42PM +0000, Ramsay Jones wrote:
> 
> The dump_macro() function fails to correctly output the definitions of
> macros that contain the string operator '#', the concatenation operator
> '##' and any macro parameter in the definition token list. For example,
> the following macros:
> 
>     #define STRING(x) #x
>     #define CONCAT(x,y) x ## y
> 
> are output like so:
> 
>     #define STRING(x) unhandled token type '21'
>     #define CONCAT(x, y) unhandled token type '22'  unhandled token type '23'  unhandled token type '22'
> 
> Add the code necessary to handle those token types to the dump_macros()
> function and add the above macros to the 'dump-macros.c' test file.
> 
> Signed-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com>
> ---
>  pre-process.c                         | 7 +++++++
>  validation/preprocessor/dump-macros.c | 5 +++++
>  2 files changed, 12 insertions(+)
> 
> diff --git a/pre-process.c b/pre-process.c
> index 8abd5e6..eca7ab5 100644
> --- a/pre-process.c
> +++ b/pre-process.c
> @@ -2196,6 +2196,13 @@ static void dump_macro(struct symbol *sym)
>  		switch (token_type(token)) {
>  		case TOKEN_UNTAINT:
>  			break;
> +		case TOKEN_STR_ARGUMENT:
> +			printf("#%s", show_token(args[token->argnum]));
> +			break;
> +		case TOKEN_CONCAT:
> +			printf("## ");
> +			break;
> +		case TOKEN_QUOTED_ARGUMENT:
>  		case TOKEN_MACRO_ARGUMENT:
>  			token = args[token->argnum];
>  			/* fall-through */

I'll squash the following in as it will facilitate some later patches:
 		switch (token_type(token)) {
 		case TOKEN_UNTAINT:
 			break;
-		case TOKEN_STR_ARGUMENT:
-			printf("#%s", show_token(args[token->argnum]));
-			break;
 		case TOKEN_CONCAT:
 			printf("## ");
 			break;
+		case TOKEN_STR_ARGUMENT:
+			printf("#");
+			/* fall-through */
 		case TOKEN_QUOTED_ARGUMENT:
 		case TOKEN_MACRO_ARGUMENT:
 			token = args[token->argnum];

Best regards,
-- Luc
diff mbox series

Patch

diff --git a/pre-process.c b/pre-process.c
index 8abd5e6..eca7ab5 100644
--- a/pre-process.c
+++ b/pre-process.c
@@ -2196,6 +2196,13 @@  static void dump_macro(struct symbol *sym)
 		switch (token_type(token)) {
 		case TOKEN_UNTAINT:
 			break;
+		case TOKEN_STR_ARGUMENT:
+			printf("#%s", show_token(args[token->argnum]));
+			break;
+		case TOKEN_CONCAT:
+			printf("## ");
+			break;
+		case TOKEN_QUOTED_ARGUMENT:
 		case TOKEN_MACRO_ARGUMENT:
 			token = args[token->argnum];
 			/* fall-through */
diff --git a/validation/preprocessor/dump-macros.c b/validation/preprocessor/dump-macros.c
index 4dbb962..5a96464 100644
--- a/validation/preprocessor/dump-macros.c
+++ b/validation/preprocessor/dump-macros.c
@@ -6,6 +6,9 @@ 
 #define DEF xyz
 
 #define NYDEF ydef
+
+#define STRING(x) #x
+#define CONCAT(x,y) x ## y
 /*
  * check-name: dump-macros
  * check-command: sparse -E -dD -DIJK=ijk -UNDEF -UNYDEF $file
@@ -15,4 +18,6 @@  check-output-pattern(1): #define __CHECKER__ 1
 check-output-contains: #define IJK ijk
 check-output-contains: #define DEF xyz
 check-output-contains: #define NYDEF ydef
+check-output-contains: #define STRING(x) #x
+check-output-contains: #define CONCAT(x, y) x ## y
  */