10

This little code compiles with both GCC and Clang, but gives different results:

#include <stdio.h>

int main(){

  __int128_t test=10;
  while(test>0){
    int myTest=(int)test;
    printf("? %d\n", myTest);
    test--;
  }

}

With GCC this counts from 10 down to 1, the intended behaviour, while for Clang it keeps on counting into negative numbers. With Clang, if I replace test-- with test-=1 then it gives the expected behaviour as well.

__int128_t is a GCC extension, so the above results only apply to non-standard C, so maybe __int128_t is "use at your own risk" in Clang.

Is this a bug in Clang, or did I make some mistake I'm not seeing?

EDIT: I'm using gcc (MacPorts gcc48 4.8-20130411_0) 4.8.1 20130411 (prerelease) and Apple clang version 4.0 (tags/Apple/clang-421.0.60) (based on LLVM 3.1svn).

Douglas B. Staple
  • 10,510
  • 8
  • 31
  • 58
  • 1
    Right, this does just what you explained on OS X 10.7.5 with GCC 4.2 and Clang 4.0. –  May 08 '13 at 18:37
  • 1
    Works fine here on my machine, "Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)". – Carl Norum May 08 '13 at 18:38
  • @Carl Maybe Apple fixed it between clang-421.0.60 and clang-425.0.28. I should update to check. – Douglas B. Staple May 08 '13 at 18:46
  • Seems that way, yup. clang is all open-source, so I'm sure you can hunt down the patch if you really want to. – Carl Norum May 08 '13 at 18:47
  • I think the updates come through the Mac App Store these days. – Carl Norum May 08 '13 at 18:49
  • @Carl Yes your right. Apparently I wasn't getting Xcode updates [because I had Spotlight disabled on Macintosh HD](https://discussions.apple.com/thread/2794280?start=15&tstart=0). I updated Xcode and the command line tools to Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn) and now the code works properly with Clang. – Douglas B. Staple May 08 '13 at 19:37

1 Answers1

9

This was a bug in Clang, which was resolved somewhere between Apple clang version 4.0 (tags/Apple/clang-421.0.60) (based on LLVM 3.1svn) and Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn), see the comments -- thanks Carl and H2CO3.

Douglas B. Staple
  • 10,510
  • 8
  • 31
  • 58