Clang Project

clang_source_code/test/Analysis/plist-macros.cpp
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: cat %t.plist | %diff_plist %S/Inputs/expected-plists/plist-macros.cpp.plist -
4
5
6typedef __typeof(sizeof(int)) size_t;
7void *malloc(size_t);
8
9#define mallocmemory int *x = (int*)malloc(12);
10void 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
19void macroIsFirstInFunction(int y) {
20  mallocmemory 
21  y++; // expected-warning {{Potential leak of memory pointed to by 'x'}}
22}
23
24#define checkmacro p==0
25void macroInExpressionAux(bool b);
26int 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
34int 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 
43int 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;
54int useMultiNoteMacroWithError(int *p, int y) {;
55  y++;
56  multiNoteMacroWithError  // expected-warning {{Dereference of null pointer}}
57
58  return *p;
59}
60
61#define multiNoteMacro \
62if (p) \
63  ;\
64if (y) \
65  ;
66int 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
76void null_deref(int *a) {
77  if (a)
78    return;
79  *a = 1; // expected-warning {{Dereference of null pointer}}
80}
81
82void test1() {
83  CALL_FN(0);
84}
85
86void test2(int *p) {
87  CALL_FN(p);
88}
89