xref: /llvm-project/clang/test/Analysis/diagnostics/macros.cpp (revision a746f2b73c39c2043cb75cd4210f87c2b3e347cc)
1 // RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=core,osx -analyzer-output=text -verify %s
2 
3 #include "../Inputs/system-header-simulator.h"
4 #include "../Inputs/system-header-simulator-cxx.h"
5 
testUnsignedIntMacro(unsigned int i)6 void testUnsignedIntMacro(unsigned int i) {
7   if (i == UINT32_MAX) { // expected-note {{Assuming 'i' is equal to UINT32_MAX}}
8                          // expected-note@-1 {{Taking true branch}}
9     char *p = NULL; // expected-note {{'p' initialized to a null pointer value}}
10     *p = 7;  // expected-warning {{Dereference of null pointer (loaded from variable 'p')}}
11              // expected-note@-1 {{Dereference of null pointer (loaded from variable 'p')}}
12   }
13 }
14 
15 
16 // FIXME: 'i' can never be equal to UINT32_MAX - it doesn't even fit into its
17 // type ('int'). This should say "Assuming 'i' is equal to -1".
testIntMacro(int i)18 void testIntMacro(int i) {
19   if (i == UINT32_MAX) { // expected-note {{Assuming 'i' is equal to UINT32_MAX}}
20                          // expected-note@-1 {{Taking true branch}}
21     char *p = NULL; // expected-note {{'p' initialized to a null pointer value}}
22     *p = 7;  // expected-warning {{Dereference of null pointer (loaded from variable 'p')}}
23              // expected-note@-1 {{Dereference of null pointer (loaded from variable 'p')}}
24   }
25 }
26 
27 
28 
testNULLMacro(int * p)29 void testNULLMacro(int *p) {
30   if (p == NULL) { // expected-note {{Assuming 'p' is equal to NULL}}
31                    // expected-note@-1 {{Taking true branch}}
32     *p = 7;  // expected-warning {{Dereference of null pointer (loaded from variable 'p')}}
33              // expected-note@-1 {{Dereference of null pointer (loaded from variable 'p')}}
34   }
35 }
36 
testnullptrMacro(int * p)37 void testnullptrMacro(int *p) {
38   if (p == nullptr) { // expected-note {{Assuming pointer value is null}}
39                       // expected-note@-1 {{Taking true branch}}
40     *p = 7;  // expected-warning {{Dereference of null pointer (loaded from variable 'p')}}
41              // expected-note@-1 {{Dereference of null pointer (loaded from variable 'p')}}
42   }
43 }
44 
45 // There are no path notes on the comparison to float types.
testDoubleMacro(double d)46 void testDoubleMacro(double d) {
47   if (d == DBL_MAX) { // expected-note {{Assuming 'd' is equal to DBL_MAX}}
48                       // expected-note@-1 {{Taking true branch}}
49 
50     char *p = NULL; // expected-note {{'p' initialized to a null pointer value}}
51     *p = 7;         // expected-warning {{Dereference of null pointer (loaded from variable 'p')}}
52                     // expected-note@-1 {{Dereference of null pointer (loaded from variable 'p')}}
53   }
54 }
55 
testboolMacro(bool b,int * p)56 void testboolMacro(bool b, int *p) {
57   p = nullptr;      // expected-note {{Null pointer value stored to 'p'}}
58   if (b == false) { // expected-note {{Assuming the condition is true}}
59                     // expected-note@-1 {{Taking true branch}}
60     *p = 7;         // expected-warning {{Dereference of null pointer (loaded from variable 'p')}}
61                     // expected-note@-1 {{Dereference of null pointer (loaded from variable 'p')}}
62   }
63 }
64 
65 #define nested_null_split(x) if ((x) != UINT32_MAX) {}
66 
testNestedNullSplitMacro(int i,int * p)67 void testNestedNullSplitMacro(int i, int *p) {
68   nested_null_split(i); // expected-note {{Assuming 'i' is equal to -1}}
69                         // expected-note@-1 {{Taking false branch}}
70   if (!p) // expected-note {{Assuming 'p' is null}}
71           // expected-note@-1 {{Taking true branch}}
72     *p = 1; // expected-warning {{Dereference of null pointer (loaded from variable 'p')}}
73             // expected-note@-1 {{Dereference of null pointer (loaded from variable 'p')}}
74 }
75