0

I am trying to compile a simple code with gcc and clang. the gcc generates a warning for an incomparable casting (great!). However, clang didn't generate any warnings! I have passed the same arguments for both:

cc -Wall -Wextra tmp3.c
gcc -Wall -Wextra tmp3.c

Am I passing all the necessary options to clang compiler or missing something? The clang documentation isn't a great help!

Code:

int main(void)
{
    void *b = (void *)0x12345678;
    int   a = (int)(unsigned long)b;
    int   c = (int)b;
    return a + c;
}

Clang version 3.8

DJJ
  • 189
  • 3
  • 14
  • Why do you _expect_ clang to emit a warning? – Siguza Jul 16 '17 at 16:19
  • The best way to find warnings you may not know about is `-Weverything`. This option (only on Clang, sadly) turns on literally every warning. You're right, the documentation is not terribly helpful. – Kyle Strand Jul 16 '17 at 16:19
  • @Siguza: Why not? This is the most fundamental thing in any compiler for any programming language--a warning for an incompatible casting! – DJJ Jul 16 '17 at 16:22
  • @KyleStrand: I didn't know about `-Weverything`, thanks for sharing. It didn't generate the necessary warning, instead it gave me: `tmp3.c:7:2: warning: no newline at end of file [-Wnewline-eof]` So impressed by clang so far! – DJJ Jul 16 '17 at 16:24
  • @DavidJohn I would agree with both "incompatible assignments" as well as "anything that's obviously wrong", but casts are often neither, as the sole need for `(int)(unsigned long)` demonstrates. With explicit casts you basically tell the compiler that you know what you're doing. – Siguza Jul 16 '17 at 16:26
  • @Siguza understood. If I add `(int)(unsigned long)` then I tell the compiler that I know what I am doing. However, if I cast a 64-bit value to a 32-bit value (without any hints to the compiler) then I expect the compiler to warn me. Anyway, my question was is there a way to make clang generate a warning for this situation or not? – DJJ Jul 16 '17 at 16:30
  • Since you've tried `-Weverything`, you can now answer your own question (and mark it as accepted). – Kyle Strand Jul 25 '17 at 19:42

1 Answers1

1

I have reached out to the clang developers (mailing list). I have got this response:

In C++ mode, Clang errors on the line, same as everyone else. In C mode, however, conversions are typically more permissive. In this case, I suspect Clang should generate this warning as well. It’ll likely require a patch however.

DJJ
  • 189
  • 3
  • 14