xref: /llvm-project/clang/test/Analysis/plist-macros.cpp (revision 893a303962608469ec5bd01fe44e82c935152e9c)
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,unix -verify %s
2 // RUN: %clang_analyze_cc1 -analyzer-checker=core,unix -analyzer-output=plist-multi-file %s -o %t.plist
3 // RUN: %normalize_plist <%t.plist | diff -ub %S/Inputs/expected-plists/plist-macros.cpp.plist -
4 
5 
6 typedef __typeof(sizeof(int)) size_t;
7 void *malloc(size_t);
8 
9 #define mallocmemory int *x = (int*)malloc(12);
10 void noteOnMacro(int y) {
11   y++;
12   y--;
13   mallocmemory
14   y++;
15   y++;
16   delete x; // expected-warning {{Memory allocated by 'malloc()' should be deallocated by 'free()', not 'delete'}}
17 }
18 
19 void macroIsFirstInFunction(int y) {
20   mallocmemory
21   y++; // expected-warning {{Potential leak of memory pointed to by 'x'}}
22 }
23 
24 #define checkmacro p==0
25 void macroInExpressionAux(bool b);
26 int macroInExpression(int *p, int y) {;
27   y++;
28   macroInExpressionAux(checkmacro);
29 
30   return *p; // expected-warning {{Dereference of null pointer}}
31 }
32 
33 #define noPathNoteMacro y+y
34 int macroInExpressionNoNote(int *p, int y) {;
35   y++;
36   if (5 + noPathNoteMacro)
37     if (p)
38       ;
39   return *p; // expected-warning {{Dereference of null pointer}}
40 }
41 
42 #define macroWithArg(mp) mp==0
43 int macroWithArgInExpression(int *p, int y) {;
44   y++;
45   if (macroWithArg(p))
46     ;
47   return *p; // expected-warning {{Dereference of null pointer}}
48 }
49 
50 #define multiNoteMacroWithError \
51   if (p) \
52     ;\
53   *p = 5;
54 int useMultiNoteMacroWithError(int *p, int y) {;
55   y++;
56   multiNoteMacroWithError  // expected-warning {{Dereference of null pointer}}
57 
58   return *p;
59 }
60 
61 #define multiNoteMacro \
62 if (p) \
63   ;\
64 if (y) \
65   ;
66 int useMultiNote(int *p, int y) {;
67   y++;
68   if (p) {}
69   multiNoteMacro
70 
71   return *p; // expected-warning {{Dereference of null pointer}}
72 }
73 
74 #define CALL_FN(a) null_deref(a)
75 
76 void null_deref(int *a) {
77   if (a)
78     return;
79   *a = 1; // expected-warning {{Dereference of null pointer}}
80 }
81 
82 void test1() {
83   CALL_FN(0);
84 }
85 
86 void test2(int *p) {
87   CALL_FN(p);
88 }
89