R-exts provides some header preamble for using alloca() and suggests that it
"suffices for known R platforms", with the caveat that it "should be included
before standard C headers such as stdlib.h".  But attempting to compile a
minimal program using Xcode 15.3 with macOS 14.4 SDK, including the preamble
followed by stdlib.h, results in an error:

    Source:

    #include <Rconfig.h> // for HAVE_ALLOCA_H
    #ifdef __GNUC__
    // this covers gcc, clang, icc
    # undef alloca
    # define alloca(x) __builtin_alloca((x))
    #elif defined(HAVE_ALLOCA_H)
    // needed for native compilers on Solaris and AIX
    # include <alloca.h>
    #endif
    #include <stdlib.h>
    int main()
    {
        return 0;
    }

    Output:

    $ clang -I"`R RHOME`/include" test.c -o test.o
    In file included from test.c:11:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdlib.h:68:

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/alloca.h:32:7: error: conflicting types for '__builtin_alloca'
    void    *alloca(size_t);                /* built-in for gcc */
             ^
    test.c:5:20: note: expanded from macro 'alloca'
    # define alloca(x) __builtin_alloca((x))
                       ^

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/alloca.h:32:7: note: '__builtin_alloca' is a builtin with type 'void *(unsigned long)'
    test.c:5:20: note: expanded from macro 'alloca'
    # define alloca(x) __builtin_alloca((x))
                       ^
    1 error generated.

where it seems that the macro alloca() defined at line 5 is expanded in
alloca.h, producing a nonsense declaration there.  I conclude that the currently
documented advice is not quite portable here ...

I know that I can just prefix identifier as below:

    #include <Rconfig.h> // for HAVE_ALLOCA_H
    #ifdef __GNUC__
    // this covers gcc, clang, icc
    # define prefixed_alloca(x) __builtin_alloca((x))
    #else
    # ifdef HAVE_ALLOCA_H
    // needed for native compilers on Solaris and AIX
    #  include <alloca.h>
    # endif
    # define prefixed_alloca(x) alloca((x))
    #endif

but if there is another way to remain portable, then perhaps it could be
documented.

Mikael

_______________________________________________
R-SIG-Mac mailing list
R-SIG-Mac@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-mac

Reply via email to