Diagnostics related to redefinition errors that point to the same header file do not provide much information that helps fixing the issue. In modules context it usually happens because of a non modular include, in non-module context it might happen because of the lack of header guardas. This patch tries to improve this situation by enhancing diagnostics in this particular scenario. If the approach seems reasonable, I can extend it to other relevant redefinition_error diagnostic call sites.
Modules
In file included from x.c:2:
Inputs4/C.h:3:5: error: redefinition of 'c'
int c = 1;
^
In module 'X' imported from x.c:1:
Inputs4/C.h:3:5: note: previous definition is here
int c = 1;
^
1 warning and 1 error generated.
After this patch
In file included from x.c:2:
Inputs4/C.h:3:5: error: redefinition of 'c'
int c = 1;
^
In module 'X' imported from x.c:1:
Inputs4/B.h:3:10: note: 'Inputs4/C.h' included multiple times, additional (likely non-modular) include site in module 'X.B'
#include "C.h"
^
1 error generated.
Inputs4/module.modulemap:6:10: note: consider adding 'Inputs4/C.h' as part of 'X.B' definition in
module B { ^
1 error generated.
Without Modules
In file included from x.c:2:
./a.h:1:5: error: redefinition of 'yyy'
int yyy = 42;
^
./a.h:1:5: note: previous definition is here
int yyy = 42;
^
1 error generated.
After this patch
In file included from x.c:2:
./a.h:1:5: error: redefinition of 'yyy'
int yyy = 42;
^
x.c:1:10: note: './a.h' included multiple times, consider augmenting this header with #ifdef guards
#include "a.h"
^
1 error generated.