On Apr 16, 2009, at 8:44 PM, Joe Buck wrote:
On Thu, Apr 16, 2009 at 03:40:47PM -0700, Arthur Schwarz wrote:
The rock has dropped. The answer is quoted below:
"My best guess is that a header file is included twice, and lacks
guards, hence the message is correct: the function is being defined
twice, from the same source location."
Yes, I've had to diagnose this one before; it doesn't happen with my
own code because I use include guards, but I've had to help others.
It would be cool if there were a way of distinguishing the case where
the same header is being processed twice.
We might see
foo.h:11 error: redefinition of `a'
foo.h:11 error: `a' previously defined here
but the first "foo.h:11" might represent the 2300'th line read by the
compiler, while the second "foo.h:11" might represent the 2194'th
line.
If, for definitions, the compiler keeps track of this detail, it
would be possible to reliably print
foo.h:11 error: redefinition of `a' (file was included more than once)
Clang just prints the include stack information when anything in the
include stack differs between two consecutive diagnostics.
$ cat t.h
int x = 4;
$ cat t.c
#include "t.h"
#include "t.h"
$ clang t.c
In file included from t.c:3:
./t.h:1:5: error: redefinition of 'x'
int x = 4;
^
In file included from t.c:2:
./t.h:1:5: note: previous definition is here
int x = 4;
^
This clearly says that the first definition was coming from t.c:2 and
the second from t.c:3. Of course, if you get multiple diagnostics
from the same header, you only show the include stack for the first.
-Chris