-Werror=c++-compat odd behavior

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

-Werror=c++-compat odd behavior

tdwrk
This version of gcc:

 

$ gcc --version

gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0

 

Plus this three-liner:

 

#define badname im-not-happy

#define initializer(a) #a

 

char *only_a_string = initializer(badname);

 

Produces:

 

$ gcc -c -Werror=c++-compat /tmp/foo.c

/tmp/foo.c:1:20: error: identifier "not" is a special operator name in C++
[-Werror=c++-compat]

#define badname im-not-happy

                    ^

cc1: some warnings being treated as errors

 

This seems pretty odd... Can someone explain what's going on?

 

Thanks... Dave Barach

Reply | Threaded
Open this post in threaded view
|

Re: -Werror=c++-compat odd behavior

Jonathan Wakely-4
On Sun, 25 Nov 2018 at 13:08, <[hidden email]> wrote:

>
> This version of gcc:
>
>
>
> $ gcc --version
>
> gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
>
>
>
> Plus this three-liner:
>
>
>
> #define badname im-not-happy
>
> #define initializer(a) #a
>
>
>
> char *only_a_string = initializer(badname);
>
>
>
> Produces:
>
>
>
> $ gcc -c -Werror=c++-compat /tmp/foo.c
>
> /tmp/foo.c:1:20: error: identifier "not" is a special operator name in C++
> [-Werror=c++-compat]
>
> #define badname im-not-happy
>
>                     ^
>
> cc1: some warnings being treated as errors
>
>
>
> This seems pretty odd... Can someone explain what's going on?

What aprt exactly are you asking about?

The preprocessor sees im-not-happy as five preprocessor tokens, "im"
"-" "not" "-" "happy".

"not" is an operator in C++ (effectively, it's a keyword). The
-Wc++-compat diagnostic is implemented in the preprocessor, which sees
the token "not" and so issues a diagnostic. You asked for that
diagnostic to be an error, so that's what you get.