Thread: LLVM __COUNTER__ Macro not expanding

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262

    LLVM __COUNTER__ Macro not expanding

    Hi everybody,

    I'm using the LLVM compiler at the moment. I've written the following (test) code:
    Code:
    #define F2(x, y) x##y 
    #define F1(x, y) F2(x, y)
    #define TEST(x) F1(x, __COUNTER__)
    
    TEST(abc) 
    TEST(abc)
    I don't actually compile this code, but I preprocess it using "gcc -E". The output becomes:
    Code:
    abc__COUNTER__
    abc__COUNTER__
    However, when I change __COUNTER__ to __LINE__, it does work:
    Code:
    abc5
    abc6
    Am I doing something wrong here? __LINE__ just doesn't cut it for me in this situation...

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Actually, it seems that __COUNTER__ doesn't work at all. It seemed to work when I compiled with "make", as my Mac is a bit of a mess with several compilers installed. But even a basic app like this:
    Code:
    #include <stdio.h>
    
    int main()
    {
      printf("%d %d\n", __COUNTER__, __COUNTER__);
    }
    Doesn't seem to expand __COUNTER__. Any idea why? My compiler version is:
    $ gcc -v
    Using built-in specs.
    Target: i686-apple-darwin11
    Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~67/src/configure --disable-checking --enable-werror --prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~67/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
    Thread model: posix
    gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
    As provided by Apple's command line tools.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    IMO, it's working as __LINE__, is a global directive and __COUNTER__ is being expanded to the __LINE__ number of the code, as in
    Code:
    abc5
    abc6
    Or if I didn't get it then explain yourself better

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by itCbitC View Post
    IMO, it's working as __LINE__, is a global directive and __COUNTER__ is being expanded to the __LINE__ number of the code, as in
    Code:
    abc5
    abc6
    Or if I didn't get it then explain yourself better
    Yes, __LINE__ worked, __COUNTER__ didn't. As I explained in the second post, __COUNTER__ isn't expanded at all (it did work with make as it used gcc, it didn't with gcc as it used llvm). But according to the documents, LLVM should support __COUNTER__.

    __LINE__ isn't good enough for a unique identifier, as that may still cause conflicting names through multiple files.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Take a look here, which says that __COUNTER__ macro is found only in gcc release 4.3 and above.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by itCbitC View Post
    Take a look here, which says that __COUNTER__ macro is found only in gcc release 4.3 and above.
    I don't think that is correct. Clang Language Extensions

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> I don't think that is correct. ...

    >> $ gcc -v ...

    Never used it myself, but if clang were being used, I would expect the command line to use "clang" instead of "gcc".

    gg

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    $ gcc --version
    gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
    $ cat test.c
    #define F2(x, y) x##y
    #define F1(x, y) F2(x, y)
    #define TEST(x) F1(x, __COUNTER__)
     
    TEST(abc)
    TEST(abc)
    $ gcc -E test.c | tail -n 2
    abc0
    abc1
    Bye, Andreas

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Codeplug View Post
    >> I don't think that is correct. ...

    >> $ gcc -v ...

    Never used it myself, but if clang were being used, I would expect the command line to use "clang" instead of "gcc".

    gg

    Right. I didn't even know I had the "clang" command. Mac's a bit of a mess compiler wise. See:
    Code:
    $ cat /tmp/a.c 
    #define F2(x, y) x##y 
    #define F1(x, y) F2(x, y)
    #define TEST(x) F1(x, __COUNTER__)
     
    TEST(abc) 
    TEST(abc)
    Now the info about the available compilers:
    Code:
    $ ls -al /usr/bin/gcc 
    lrwxr-xr-x  1 root  wheel  12 24 Oct 13:52 /usr/bin/gcc -> llvm-gcc-4.2
    $ ls -al /usr/bin/clang
    -rwxr-xr-x  1 root  wheel  21622640 24 Oct 13:52 /usr/bin/clang
    $ clang -v
    Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
    Target: x86_64-apple-darwin12.2.0
    Thread model: posix
    $ gcc -v
    Using built-in specs.
    Target: i686-apple-darwin11
    Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~67/src/configure --disable-checking --enable-werror --prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~67/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
    Thread model: posix
    gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
    As for the compilation:
    Code:
    $ clang -E /tmp/a.c 
    # 1 "/tmp/a.c"
    # 1 "/tmp/a.c" 1
    # 1 "<built-in>" 1
    # 1 "<built-in>" 3
    # 147 "<built-in>" 3
    # 1 "<command line>" 1
    # 1 "<built-in>" 2
    # 1 "/tmp/a.c" 2
    
    
    
    
    abc0
    abc1
    And:
    Code:
    $ llvm-gcc -E /tmp/a.c 
    # 1 "/tmp/a.c"
    # 1 "<built-in>"
    # 1 "<command-line>"
    # 1 "/tmp/a.c"
    
    
    
    
    abc__COUNTER__
    abc__COUNTER__
    So I do have "LLVM" and "clang" separately, where clang works fine and LLVM doesn't.

    I thought clang was part of LLVM, but now I don't know what LLVM is even supposed to be anymore. It seems to simply be an old version of gcc, though... Apple's compiler infrastructure is a mess... I guess I'll just stop using that compiler (or whatever it may be).


    Thanks for your replies, guys!

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    llvm-gcc is a front end for LLVM based on gcc 4.2.x (where I can't remember the x). That puts it as older than the 4.3 needed to have the __COUNTER__ macro added to gcc. Clang is a gcc-alike front end for LLVM that's actively being developed, so it probably copied GCC4.3's addition of __COUNTER__ development.

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Your gcc and clang installs are "front ends" which make use of LLVM for optimization, using a language-agnostic "intermediate form" (IF).
    LLVM - Wikipedia, the free encyclopedia

    So for gcc, you'll need a version based on 4.3 or higher to use __COUNTER__, as itCbitC pointed out.

    gg

    [edit]meh, what KCfromNC said...[/edit]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. llvm library linking issues
    By King Mir in forum C++ Programming
    Replies: 3
    Last Post: 07-14-2012, 06:58 PM
  2. Array Expanding
    By Mcdom34 in forum C Programming
    Replies: 1
    Last Post: 06-26-2012, 10:59 PM
  3. expanding function
    By Tool in forum C Programming
    Replies: 2
    Last Post: 12-21-2009, 12:58 PM
  4. Expanding my arraysize
    By hansel13 in forum C Programming
    Replies: 3
    Last Post: 05-20-2009, 01:54 AM
  5. Expanding Inventory
    By Sentral in forum Game Programming
    Replies: 11
    Last Post: 08-23-2006, 05:28 AM