Bug 52998 - different macro unwinder for C and C++ (-ftrack-macro-expansion)
different macro unwinder for C and C++ (-ftrack-macro-expansion)
Status: NEW
Product: gcc
Classification: Unclassified
Component: preprocessor
4.8.0
: P4 normal
: ---
Assigned To: Not yet assigned to anyone
: diagnostic
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2012-04-15 20:21 UTC by Manuel López-Ibáñez
Modified: 2012-04-30 17:28 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-04-24 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Manuel López-Ibáñez 2012-04-15 20:21:19 UTC
With GCC 4.8 and -ftrack-macro-expansion, we get:

manuel@gcc12:~$ cat macro-caret.c
#define OPERATE(OPRD1, OPRT, OPRD2) \
  OPRD1 OPRT OPRD2;

#define SHIFTL(A,B) \
  OPERATE (A,<<,B)

#define MULT(A) \
  SHIFTL (A,1)

void
g ()
{
  MULT (1.0);// 1.0 << 1; <-- so this is an error.
}

manuel@gcc12:~$ ~/trunk/186353M/build/gcc/cc1plus macro-caret.c -ftrack-macro-expansion
macro-caret.c:8:13: error: invalid operands of types ‘double’ and ‘int’ to binary ‘operator<<’
   SHIFTL (A,1)
             ^
macro-caret.c:2:14: note: in expansion of macro 'OPERATE'
   OPRD1 OPRT OPRD2;
              ^
macro-caret.c:5:3: note: expanded from here
   OPERATE (A,<<,B)
   ^
macro-caret.c:5:17: note: in expansion of macro 'SHIFTL'
   OPERATE (A,<<,B)
                 ^
macro-caret.c:8:3: note: expanded from here
   SHIFTL (A,1)
   ^
macro-caret.c:8:13: note: in expansion of macro 'MULT'
   SHIFTL (A,1)
             ^
macro-caret.c:13:3: note: expanded from here
   MULT (1.0);// 1.0 << 1; <-- so this is an error.
   ^

And with GCC:

manuel@gcc12:~$ ~/trunk/186353M/build/gcc/cc1 macro-caret.c -ftrack-macro-expansion
macro-caret.c: In function ‘g’:
macro-caret.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
   OPERATE (A,<<,B)
              ^
macro-caret.c:2:9: note: in expansion of macro 'OPERATE'
   OPRD1 OPRT OPRD2;
         ^
macro-caret.c:5:3: note: expanded from here
   OPERATE (A,<<,B)
   ^
macro-caret.c:5:14: note: in expansion of macro 'SHIFTL'
   OPERATE (A,<<,B)
              ^
macro-caret.c:8:3: note: expanded from here
   SHIFTL (A,1)
   ^
macro-caret.c:8:3: note: in expansion of macro 'MULT'
   SHIFTL (A,1)
   ^
macro-caret.c:13:3: note: expanded from here
   MULT (1.0);// 1.0 << 1; <-- so this is an error.
   ^

Note that cc1plus prints the error at 8:13 and cc1 at 5:14. Without -ftrac-macro-expansion, both give 13:3.
Comment 1 Manuel López-Ibáñez 2012-04-15 20:35:08 UTC
I think with and without macro expansion, the error should be given at 13:3, and unwind from outermost to innermost. Innermost to outermost makes sense if we show preprocessed lines:

macro-caret.c:5:14: error: invalid operands to binary << (have ‘double’ and
‘int’)
   1.0 << 1
       ^
macro-caret.c:2:9: note: in expansion of macro 'OPERATE' [with OPRD1=0.1, OPRT=<<, OPRD2=1]
   OPRD1 OPRT OPRD2;
         ^
macro-caret.c:5:3: note: expanded from here
   OPERATE (1.0,<<,1)
   ^
macro-caret.c:5:14: note: in expansion of macro 'SHIFTL' [with A=1.0, B=1]
   OPERATE (A,<<,B)
              ^
macro-caret.c:8:3: note: expanded from here
   SHIFTL (1.0,1)
   ^
macro-caret.c:8:3: note: in expansion of macro 'MULT' [with A=1.0]
   SHIFTL (A,1)
   ^
macro-caret.c:13:3: note: expanded from here
   MULT (1.0);// 1.0 << 1; <-- so this is an error.


Otherwise, the error is shown at some code the user has not typed, and that shows no obvious error (OPERATE (A,<<,B)) . Hence, the user has to read the output once to find the source of the error (MULT), and then again to unwind the macro expansion. In that case, it is better to go outermost to innermost, so the user can follow the macro expansion as it occurs.

What do you think Dodji?
Comment 2 Manuel López-Ibáñez 2012-04-15 20:37:44 UTC
(In reply to comment #1)
> I think with and without macro expansion, the error should be given at 13:3,
> and unwind from outermost to innermost. Innermost to outermost makes sense if
> we show preprocessed lines:
> 
> macro-caret.c:5:14: error: invalid operands to binary << (have ‘double’ and
> ‘int’)
>    1.0 << 1
>        ^

But with preprocessed source lines, the best location is 5:14, because this way the user can compare what is in that line with the preprocessed line shown by GCC. So, in some sense, cc1 is right, and cc1plus is wrong.
Comment 3 Andrew Pinski 2012-04-24 04:36:42 UTC
Confirmed.
Comment 4 Manuel López-Ibáñez 2012-04-30 17:28:05 UTC
Just a note that the recent commits about tracking macro locations haven't changed this issue.

I change the summary to reflect that this has nothing to do with caret diagnostics, just that it becomes evident thanks to it.