clang中是否存在“积分常数溢出”警告?

时间:2014-01-13 14:06:28

标签: c clang constants integer-overflow compile-time-constant

请考虑以下代码段:

short x = 2000000000;

short x = (short)2000000000;

int x = 1000000000 * 1000000000;

我们可以在Clang中收到警告(/错误)吗?怎么样?从什么版本开始?

谢谢, 西普里安。

1 个答案:

答案 0 :(得分:2)

至于clang 3.3,至少在两种情况下都会收到警告,甚至没有尝试:

/* main.c */
short x = 2000000000;
int y = 1000000000 * 1000000000;

int main()
{
    return 0;
}

编译:

$ clang -c main.c
main.c:1:11: warning: implicit conversion from 'int' to 'short' changes value
      from 2000000000 to -27648 [-Wconstant-conversion]
short x = 2000000000;
      ~   ^~~~~~~~~~
main.c:2:20: warning: overflow in expression; result is -1486618624 with type
      'int' [-Winteger-overflow]
int y = 1000000000 * 1000000000;
                   ^
2 warnings generated.