blob: 6fb0e1cd70b053425fcb3089617ce206f58fb11b [file] [log] [blame]
Dominic Chen184c6242017-03-03 18:02:02 +00001// RUN: %clang_analyze_cc1 -analyzer-checker=core -verify %s
Anna Zaks5497d1a2013-03-09 03:23:19 +00002// expected-no-diagnostics
3
Anna Zaks2672a4c2013-03-14 22:31:56 +00004extern void __assert_fail (__const char *__assertion, __const char *__file,
5 unsigned int __line, __const char *__function)
6__attribute__ ((__noreturn__));
7#define assert(expr) \
8((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__))
9
Anna Zaks5497d1a2013-03-09 03:23:19 +000010class ButterFly {
11private:
12 ButterFly() { }
13public:
14 int triggerderef() {
15 return 0;
16 }
17};
18ButterFly *getInP();
19class X{
20 ButterFly *p;
21 void setP(ButterFly *inP) {
22 if(inP)
23 ;
24 p = inP;
25 };
26 void subtest1() {
27 ButterFly *inP = getInP();
28 setP(inP);
29 }
30 int subtest2() {
31 int c = p->triggerderef(); // no-warning
32 return c;
33 }
34 int test() {
35 subtest1();
36 return subtest2();
37 }
Anna Zaks2672a4c2013-03-14 22:31:56 +000038};
39
40typedef const int *Ty;
41extern
42Ty notNullArg(Ty cf) __attribute__((nonnull));
43typedef const void *CFTypeRef;
44extern Ty getTyVal();
45inline void radar13224271_callee(Ty def, Ty& result ) {
46 result = def;
47 // Clearly indicates that result cannot be 0 if def is not NULL.
48 assert( (result != 0) || (def == 0) );
49}
50void radar13224271_caller()
51{
52 Ty value;
53 radar13224271_callee(getTyVal(), value );
54 notNullArg(value); // no-warning
Anna Zaks94b48bd2013-04-05 23:50:11 +000055}
56
57struct Foo {
58 int *ptr;
59 Foo(int *p) {
60 *p = 1; // no-warning
61 }
62};
63void idc(int *p3) {
64 if (p3)
65 ;
66}
67int *retNull() {
68 return 0;
69}
70void test(int *p1, int *p2) {
71 idc(p1);
72 Foo f(p1);
Artem Dergachev37de8882017-04-24 19:30:33 +000073}
74
75struct Bar {
76 int x;
77};
78void idcBar(Bar *b) {
79 if (b)
80 ;
81}
82void testRefToField(Bar *b) {
83 idcBar(b);
84 int &x = b->x; // no-warning
85 x = 5;
86}
Artem Dergachev57790c52018-06-25 23:55:07 +000087
88namespace get_deref_expr_with_cleanups {
89struct S {
90~S();
91};
92S *conjure();
93// The argument won't be used, but it'll cause cleanups
94// to appear around the call site.
95S *get_conjured(S _) {
96 S *s = conjure();
97 if (s) {}
98 return s;
99}
100void test_conjured() {
101 S &s = *get_conjured(S()); // no-warning
102}
103} // namespace get_deref_expr_with_cleanups