https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70772
Martin Sebor <msebor at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |msebor at gcc dot gnu.org Known to fail| |4.9.3, 5.3.0, 6.0 --- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> --- This bug affects both the C and the C++ front ends. I mention it because when developing a patch, it would be worth fixing both, and handle other equality and relational operators, as well as the substraction expression. Since the C rules are different than C++, the handling needs to be different for each language. The test case below shows the problems in C and documents the expected output in C++. Note that even in cases where a diagnostic is expected and issued, the text of the diagnostic should change to reflect what the relevant standard says (undefined behavior in C rather than unspecified). Unlike in C, in C++, the behavior of relational expressions is well defined even for pointers pointing to different objects provided one compares either less that or equal to the other (it is unspecified otherwise), so the text of the diagnostic should be adjusted to reflect that. $ cat v.c && gcc -S -Wall -Wextra -Wpedantic -xc v.c void f (void) { const __PTRDIFF_TYPE__ a[] = { "a" == "b", // no warning expected (behavior well defined) "a" != "b", // no warning expected (behavior well defined) "a" < "b", // warning expected in C only (undefined behavior) "a" <= "b", // warning expected in C only (undefined behavior) "a" > "b", // warning expected in C only (undefined behavior) "a" >= "b", // warning expected in C only (undefined behavior) "a" - "b" // warning expected (undefined behavior) }; (void)a; } v.c: In function âfâ: v.c:4:9: warning: comparison with string literal results in unspecified behavior [-Waddress] "a" == "b", // no warning expected (behavior well defined) ^~ v.c:5:9: warning: comparison with string literal results in unspecified behavior [-Waddress] "a" != "b", // no warning expected (behavior well defined) ^~ v.c:6:9: warning: comparison with string literal results in unspecified behavior [-Waddress] "a" < "b", // warning expected in C only (undefined behavior) ^ v.c:7:9: warning: comparison with string literal results in unspecified behavior [-Waddress] "a" <= "b", // warning expected in C only (undefined behavior) ^~ v.c:8:9: warning: comparison with string literal results in unspecified behavior [-Waddress] "a" > "b", // warning expected in C only (undefined behavior) ^ v.c:9:9: warning: comparison with string literal results in unspecified behavior [-Waddress] "a" >= "b", // warning expected in C only (undefined behavior) ^~