xref: /llvm-project/clang/docs/analyzer/checkers/callandmessage_example.c (revision 1a17032b788016299ea4e3c4b53670c6dcd94b4f)
1*1a17032bSKristof Umann //C
test()2*1a17032bSKristof Umann void test() {
3*1a17032bSKristof Umann    void (*foo)(void);
4*1a17032bSKristof Umann    foo = 0;
5*1a17032bSKristof Umann    foo(); // warn: function pointer is null
6*1a17032bSKristof Umann  }
7*1a17032bSKristof Umann 
8*1a17032bSKristof Umann  // C++
9*1a17032bSKristof Umann  class C {
10*1a17032bSKristof Umann  public:
11*1a17032bSKristof Umann    void f();
12*1a17032bSKristof Umann  };
13*1a17032bSKristof Umann 
test()14*1a17032bSKristof Umann  void test() {
15*1a17032bSKristof Umann    C *pc;
16*1a17032bSKristof Umann    pc->f(); // warn: object pointer is uninitialized
17*1a17032bSKristof Umann  }
18*1a17032bSKristof Umann 
19*1a17032bSKristof Umann  // C++
20*1a17032bSKristof Umann  class C {
21*1a17032bSKristof Umann  public:
22*1a17032bSKristof Umann    void f();
23*1a17032bSKristof Umann  };
24*1a17032bSKristof Umann 
test()25*1a17032bSKristof Umann  void test() {
26*1a17032bSKristof Umann    C *pc = 0;
27*1a17032bSKristof Umann    pc->f(); // warn: object pointer is null
28*1a17032bSKristof Umann  }
29*1a17032bSKristof Umann 
30*1a17032bSKristof Umann  // Objective-C
31*1a17032bSKristof Umann  @interface MyClass : NSObject
property(readwrite,assign)32*1a17032bSKristof Umann  @property (readwrite,assign) id x;
33*1a17032bSKristof Umann  - (long double)longDoubleM;
34*1a17032bSKristof Umann  @end
35*1a17032bSKristof Umann 
36*1a17032bSKristof Umann  void test() {
37*1a17032bSKristof Umann    MyClass *obj1;
38*1a17032bSKristof Umann    long double ld1 = [obj1 longDoubleM];
39*1a17032bSKristof Umann      // warn: receiver is uninitialized
40*1a17032bSKristof Umann  }
41*1a17032bSKristof Umann 
42*1a17032bSKristof Umann  // Objective-C
43*1a17032bSKristof Umann  @interface MyClass : NSObject
property(readwrite,assign)44*1a17032bSKristof Umann  @property (readwrite,assign) id x;
45*1a17032bSKristof Umann  - (long double)longDoubleM;
46*1a17032bSKristof Umann  @end
47*1a17032bSKristof Umann 
48*1a17032bSKristof Umann  void test() {
49*1a17032bSKristof Umann    MyClass *obj1;
50*1a17032bSKristof Umann    id i = obj1.x; // warn: uninitialized object pointer
51*1a17032bSKristof Umann  }
52*1a17032bSKristof Umann 
53*1a17032bSKristof Umann  // Objective-C
54*1a17032bSKristof Umann  @interface Subscriptable : NSObject
55*1a17032bSKristof Umann  - (id)objectAtIndexedSubscript:(unsigned int)index;
56*1a17032bSKristof Umann  @end
57*1a17032bSKristof Umann 
58*1a17032bSKristof Umann  @interface MyClass : Subscriptable
property(readwrite,assign)59*1a17032bSKristof Umann  @property (readwrite,assign) id x;
60*1a17032bSKristof Umann  - (long double)longDoubleM;
61*1a17032bSKristof Umann  @end
62*1a17032bSKristof Umann 
63*1a17032bSKristof Umann  void test() {
64*1a17032bSKristof Umann    MyClass *obj1;
65*1a17032bSKristof Umann    id i = obj1[0]; // warn: uninitialized object pointer
66*1a17032bSKristof Umann  }
67