Dominic Chen | 184c624 | 2017-03-03 18:02:02 +0000 | [diff] [blame] | 1 | // RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -w -analyzer-checker=osx.NumberObjectConversion %s -verify |
| 2 | // RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -w -analyzer-checker=osx.NumberObjectConversion -analyzer-config osx.NumberObjectConversion:Pedantic=true -DPEDANTIC %s -verify |
Artem Dergachev | e14d881 | 2016-10-31 03:08:48 +0000 | [diff] [blame] | 3 | |
| 4 | #define NULL ((void *)0) |
| 5 | |
| 6 | typedef const struct __CFNumber *CFNumberRef; |
| 7 | |
| 8 | void takes_int(int); |
| 9 | |
| 10 | void bad(CFNumberRef p) { |
| 11 | #ifdef PEDANTIC |
| 12 | if (p) {} // expected-warning{{Converting a pointer value of type 'CFNumberRef' to a primitive boolean value; instead, either compare the pointer to NULL or call CFNumberGetValue()}} |
| 13 | if (!p) {} // expected-warning{{Converting a pointer value of type 'CFNumberRef' to a primitive boolean value; instead, either compare the pointer to NULL or call CFNumberGetValue()}} |
| 14 | p ? 1 : 2; // expected-warning{{Converting a pointer value of type 'CFNumberRef' to a primitive boolean value; instead, either compare the pointer to NULL or call CFNumberGetValue()}} |
| 15 | if (p == 0) {} // expected-warning{{Comparing a pointer value of type 'CFNumberRef' to a primitive integer value; instead, either compare the pointer to NULL or compare the result of calling CFNumberGetValue()}} |
| 16 | #else |
| 17 | if (p) {} // no-warning |
| 18 | if (!p) {} // no-warning |
| 19 | p ? 1 : 2; // no-warning |
| 20 | if (p == 0) {} // no-warning |
| 21 | #endif |
| 22 | if (p > 0) {} // expected-warning{{Comparing a pointer value of type 'CFNumberRef' to a primitive integer value; did you mean to compare the result of calling CFNumberGetValue()?}} |
| 23 | int x = p; // expected-warning{{Converting a pointer value of type 'CFNumberRef' to a primitive integer value; did you mean to call CFNumberGetValue()?}} |
| 24 | x = p; // expected-warning{{Converting a pointer value of type 'CFNumberRef' to a primitive integer value; did you mean to call CFNumberGetValue()?}} |
| 25 | takes_int(p); // expected-warning{{Converting a pointer value of type 'CFNumberRef' to a primitive integer value; did you mean to call CFNumberGetValue()?}} |
| 26 | takes_int(x); // no-warning |
| 27 | } |
| 28 | |
| 29 | // Conversion of a pointer to an intptr_t is fine. |
| 30 | typedef long intptr_t; |
| 31 | typedef unsigned long uintptr_t; |
| 32 | typedef long fintptr_t; // Fake, for testing the regex. |
| 33 | void test_intptr_t(CFNumberRef p) { |
| 34 | (long)p; // expected-warning{{Converting a pointer value of type 'CFNumberRef' to a primitive integer value; did you mean to call CFNumberGetValue()?}} |
| 35 | (intptr_t)p; // no-warning |
| 36 | (unsigned long)p; // expected-warning{{Converting a pointer value of type 'CFNumberRef' to a primitive integer value; did you mean to call CFNumberGetValue()?}} |
| 37 | (uintptr_t)p; // no-warning |
| 38 | (fintptr_t)p; // expected-warning{{Converting a pointer value of type 'CFNumberRef' to a primitive integer value; did you mean to call CFNumberGetValue()?}} |
| 39 | } |
| 40 | |