Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Yet another wierd C++ warning

Правка en1, от ifsmirnov, 2016-06-30 23:32:54

Take a look at this snippet.


#include <bits/stdc++.h> using namespace std; void f(int x, int y) { if (x > y) swap(x, y); printf("%d %d\n", x, y); } void caller1(int i) { f(i-1, i); } void caller2(int i) { f(i+1, i); } int main() { caller1(1); caller2(1); }

Have you noticed something strange? Or maybe redundant? Neither did I. But g++ did:

$ g++-4.8 -Wall -O2 a.cpp 
a.cpp: In function ‘void caller1(int)’:
a.cpp:5:5: warning: assuming signed overflow does not occur when assuming that (X - c) > X is always false [-Wstrict-overflow]
     if (x > y) swap(x, y);
     ^

Wait, what the hell? This if is used not only in caller1 but also in caller2, and in both cases the flow continues to the different branch. It seems that the optimizer examines only caller1 and doesn't even think that there could be some other users of that "redundant" line of code.

What is more strange, this error does not reproduce if only caller2 is present (in that case if condition always evaluates to true).

Hopefully, optimizer doesn't completely cut out the body of the conditional and the code works as expected albeit the warning is shown.

The bug reproduces with all g++ versions I have up to 5.0 and doesn't reproduce with clang of any version.

Теги bug, g++

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский ifsmirnov 2016-06-30 23:34:34 4
en1 Английский ifsmirnov 2016-06-30 23:32:54 1356 Initial revision (published)