blob: a983f92f81aa45c69ff6df063b23048cec4564c2 [file] [log] [blame]
Stephen Hines651f13c2014-04-23 16:59:28 -07001// RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.core,debug.ExprInspection,unix.Malloc,unix.cstring,alpha.unix.cstring,unix.API,osx.API,osx.cocoa.RetainCount -Wno-null-dereference -Wno-tautological-compare -analyzer-store=region -fblocks -verify %s
Jordan Rose3aa6f432013-08-28 17:07:04 +00002#define NULL 0
3void clang_analyzer_eval(int);
4void myFunc();
5void myWeakFunc() __attribute__((weak_import));
6
7void testWeakFuncIsNull()
8{
9 clang_analyzer_eval(myFunc == NULL); // expected-warning{{FALSE}}
10 clang_analyzer_eval(myWeakFunc == NULL); // expected-warning{{UNKNOWN}}
11 if (myWeakFunc == NULL) {
12 clang_analyzer_eval(myWeakFunc == NULL); // expected-warning{{TRUE}}
13 } else {
14 clang_analyzer_eval(myWeakFunc == NULL); // expected-warning{{FALSE}}
15 }
16}
17
18void testWeakFuncIsNot()
19{
20 clang_analyzer_eval(myWeakFunc == NULL); // expected-warning{{UNKNOWN}}
21 if (!myWeakFunc) {
22 clang_analyzer_eval(myWeakFunc == NULL); // expected-warning{{TRUE}}
23 } else {
24 clang_analyzer_eval(myWeakFunc == NULL); // expected-warning{{FALSE}}
25 }
26}
27
28void testWeakFuncIsTrue()
29{
30 clang_analyzer_eval(myWeakFunc == NULL); // expected-warning{{UNKNOWN}}
31 if (myWeakFunc) {
32 clang_analyzer_eval(myWeakFunc == NULL); // expected-warning{{FALSE}}
33 } else {
34 clang_analyzer_eval(myWeakFunc == NULL); // expected-warning{{TRUE}}
35 }
36}
37
38//===----------------------------------------------------------------------===
39// func.c
40//===----------------------------------------------------------------------===
41void f(void) __attribute__((weak_import));
42void g(void (*fp)(void)) __attribute__((weak_import));
43
44void f(void) {
45 void (*p)(void);
46 p = f;
47 p = &f;
48 p();
49 (*p)();
50}
51
52void g(void (*fp)(void));
53
54void f2() {
55 g(f);
56}
57
58void f3(void (*f)(void), void (*g)(void)) {
59 clang_analyzer_eval(!f); // expected-warning{{UNKNOWN}}
60 f();
61 clang_analyzer_eval(!f); // expected-warning{{FALSE}}
62
63 clang_analyzer_eval(!g); // expected-warning{{UNKNOWN}}
64 (*g)();
65 clang_analyzer_eval(!g); // expected-warning{{FALSE}}
66}
67
68//===----------------------------------------------------------------------===
69// free.c
70//===----------------------------------------------------------------------===
71void free(void *) __attribute__((weak_import));
72
73void t10 () {
74 free((void*)&t10); // expected-warning {{Argument to free() is the address of the function 't10', which is not memory allocated by malloc()}}
75}
76
77//===----------------------------------------------------------------------===
78// string.c : strnlen()
79//===----------------------------------------------------------------------===
80typedef typeof(sizeof(int)) size_t;
81size_t strlen(const char *s) __attribute__((weak_import));
82
83size_t strlen_fn() {
84 return strlen((char*)&strlen_fn); // expected-warning{{Argument to string length function is the address of the function 'strlen_fn', which is not a null-terminated string}}
85}
86
87//===----------------------------------------------------------------------===
88// unix-fns.c : dispatch_once
89//===----------------------------------------------------------------------===
90typedef void (^dispatch_block_t)(void);
91typedef long dispatch_once_t;
92void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block) __attribute__((weak_import));
93
94void test_dispatch_once() {
95 dispatch_once_t pred = 0;
96 do { if (__builtin_expect(*(&pred), ~0l) != ~0l) dispatch_once((&pred), (^() {})); } while (0); // expected-warning{{Call to 'dispatch_once' uses the local variable 'pred' for the predicate value}}
97}
98void test_dispatch_once_neg() {
99 static dispatch_once_t pred = 0;
100 do { if (__builtin_expect(*(&pred), ~0l) != ~0l) dispatch_once((&pred), (^() {})); } while (0); // no-warning
101}
102
103//===----------------------------------------------------------------------===
104// retain-release-path-notes.m
105//===----------------------------------------------------------------------===
106typedef struct CFType *CFTypeRef;
107CFTypeRef CFCreateSomething() __attribute__((weak_import));
108CFTypeRef CFGetSomething() __attribute__((weak_import));
109
110CFTypeRef CFCopyRuleViolation () {
111 CFTypeRef object = CFGetSomething();
112 return object; // expected-warning{{Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected}}
113}
114
115CFTypeRef CFGetRuleViolation () {
116 CFTypeRef object = CFCreateSomething(); // expected-warning{{Potential leak of an object stored into 'object'}}
117 return object; }