Macros active in error messages

emre brookes brookes@uthscsa.edu
Wed Nov 25 01:14:30 GMT 2020


Martin Sebor wrote:
> On 11/22/20 8:34 AM, emre brookes wrote:
>> I was compiling a project and received an odd message that took some 
>> digging.
>> e.g.
>>
>> file.cpp:#:#: error: expected unqualified-id before numeric constant
>> # | if ( inttype == MyClass::ERROR )
>>                               ^~~~~
>> where MyClass::ERROR is an enum.
>>
>> Took some digging to find out ERROR was somewhere #define'd and the 
>> cure was to #undef ERROR
>> Could never find out *where* it was defined - recursively searched 
>> all include files of the project and /usr/include etc.
>> Regardless, it would have been very helpful to know in the error 
>> messages that a #define was active or to have the ERROR replaced by 
>> the macro substitution.
>>
>> Is it possible to get any macro expansion information in error messages?
>> I didn't see a compiler flag for to get this information, but I might 
>> have missed it.
>> If not, it would be a nice feature to have.
>
> I'd expect to see output similar to the below where the error points
> to the macro.  I'm not sure under what conditions it won't (with some
> specific options perhaps?)  If you can create an isolated test case
> showing it doesn't I'd recommend to open a bug in Bugzilla (or just
> follow up here).
>
> Martin
>
> $ cat a.C && gcc -S a.C
> struct MyClass { enum { ERROR = 1 }; };
>
> #define ERROR 1
>
> int f (int inttype)
> {
>   if (inttype == MyClass::ERROR)
>     return 0;
>   return 1;
> }
> a.C: In function ‘int f(int)’:
> a.C:3:15: error: expected unqualified-id before numeric constant
>     3 | #define ERROR 1
>       |               ^
> a.C:7:27: note: in expansion of macro ‘ERROR’
>     7 |   if (inttype == MyClass::ERROR)
>       |                           ^~~~~
> a.C:7:25: error: expected ‘)’ before numeric constant
>     7 |   if (inttype == MyClass::ERROR)
>       |      ~                  ^
>       |                         )
> .
>
I get the same results for your example code. Here gcc nicely identifies 
the macro expansion :)
Will test later with compiler flags from the project's Makefile add see 
if I can reproduce the issue I observed on a simple case.

Thanks,
Emre




More information about the Gcc-help mailing list