Search code examples
cvisual-studio-2015clanggoto

labels as values in clang


I'm trying to implement "labels as values" (https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html) in a c program using clang 3.7 in Visual Studio 2015.

As a toy example I had the following code which causes the compiler to crash (internal error "fatal error C1001: An internal error has occurred in the compiler. 1> (compiler file 'c:\agent\build\cache\git\vctools\vctools\compiler\utc\src\p2\main.c', line 246) 1> To work around this problem, try simplifying or changing the program near the locations listed above.").

const void *array_jump[] = {&&S1,&&S2,&&S3,&&S3,&&S4};  

S1:
    goto *array_jump[3];
S2:
    return 2;
S3:
    return 3;
S4:
    return 4;

If I move the array declaration to after all of the labels it works, until I include the array_jump variable in any of the statements.

S1:
    //comment out and add "return 1;" and it will compile fine
    goto *array_jump[3];
S2:
    return 2;
S3:
    return 3;
S4:
    return 4;

const void *array_jump[] = {&&S1,&&S2,&&S3,&&S3,&&S4}; 

Can anyone provide an example like the one above that should work? Is this a problem with clang or with the "codegen" aspect for Visual Studio?

I think this bug is relevant but I'm not sure: https://connect.microsoft.com/VisualStudio/feedback/details/2103400/crash-in-clang-c2-with-address-of-label-extension


Solution

  • The examples you build should work, I've just verified it using Apple LLVM version 7.0.2 (clang-700.1.81).

    The mentioned bug reports seem to report this very same issue, until it's fixed, you can't do anything aside from trying to not use the extension.

    While GNU C has some great extensions, if you want to write portable code, try to avoid using any GNU C extension.