IMA corner case with forward declarations of statics

Zack Weinberg zack@codesourcery.com
Thu Jun 24 02:58:00 GMT 2004


Consider the following test case:

A.c:
    int foo = 12;
B.c:
    extern int printf(const char *, ...);
    extern int foo;

    int main(void)
    {
        printf("%d\n", foo);
        return 0;
    }

    static int foo = 14;

Taken together, this is a correct C program which should print 14.
However, I've discovered that when compiled with the present
implementation of IMA (i.e. my locally patched tree) it only prints 14
if you give the files in the order B.c A.c.

If you give the files in the order A.c B.c, the 'extern int foo'
declaration is unified with the 'int foo = 12' from A.c the moment
it's encountered.  Then when we encounter the static, it shadows the
declaration in the external scope, but it's too late for main, which
has already been saved with a reference to the original foo in its
parse tree.

Now, GCC 3.4's IMA doesn't do the right thing with this either; no
matter which order the files are given in, you get

Assembler messages:
Error: symbol `foo' is already defined

No one has noticed, which leads me to believe that this will not be a
problem in real life.  So I propose to detect the case where we would
generate incorrect code, and call sorry() instead.

zw



More information about the Gcc mailing list