1 // RUN: %clang_analyze_cc1 -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 -analyzer-config eagerly-assume=false %s
3 void clang_analyzer_eval(int);
5 void myWeakFunc() __attribute__((weak_import));
7 void testWeakFuncIsNull()
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}}
14 clang_analyzer_eval(myWeakFunc == NULL); // expected-warning{{FALSE}}
18 void testWeakFuncIsNot()
20 clang_analyzer_eval(myWeakFunc == NULL); // expected-warning{{UNKNOWN}}
22 clang_analyzer_eval(myWeakFunc == NULL); // expected-warning{{TRUE}}
24 clang_analyzer_eval(myWeakFunc == NULL); // expected-warning{{FALSE}}
28 void testWeakFuncIsTrue()
30 clang_analyzer_eval(myWeakFunc == NULL); // expected-warning{{UNKNOWN}}
32 clang_analyzer_eval(myWeakFunc == NULL); // expected-warning{{FALSE}}
34 clang_analyzer_eval(myWeakFunc == NULL); // expected-warning{{TRUE}}
38 //===----------------------------------------------------------------------===
40 //===----------------------------------------------------------------------===
41 void f(void) __attribute__((weak_import));
42 void g(void (*fp)(void)) __attribute__((weak_import));
52 void g(void (*fp)(void));
58 void f3(void (*f)(void), void (*g)(void)) {
59 clang_analyzer_eval(!f); // expected-warning{{UNKNOWN}}
61 clang_analyzer_eval(!f); // expected-warning{{FALSE}}
63 clang_analyzer_eval(!g); // expected-warning{{UNKNOWN}}
65 clang_analyzer_eval(!g); // expected-warning{{FALSE}}
68 //===----------------------------------------------------------------------===
70 //===----------------------------------------------------------------------===
71 void free(void *) __attribute__((weak_import));
74 free((void*)&t10); // expected-warning {{Argument to free() is the address of the function 't10', which is not memory allocated by malloc()}}
77 //===----------------------------------------------------------------------===
78 // string.c : strnlen()
79 //===----------------------------------------------------------------------===
80 typedef typeof(sizeof(int)) size_t;
81 size_t strlen(const char *s) __attribute__((weak_import));
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}}
87 //===----------------------------------------------------------------------===
88 // unix-fns.c : dispatch_once
89 //===----------------------------------------------------------------------===
90 typedef void (^dispatch_block_t)(void);
91 typedef long dispatch_once_t;
92 void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block) __attribute__((weak_import));
94 void 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}}
98 void 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
103 //===----------------------------------------------------------------------===
104 // retain-release-path-notes.m
105 //===----------------------------------------------------------------------===
106 typedef struct CFType *CFTypeRef;
107 CFTypeRef CFCreateSomething() __attribute__((weak_import));
108 CFTypeRef CFGetSomething() __attribute__((weak_import));
110 CFTypeRef 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}}
115 CFTypeRef CFGetRuleViolation () {
116 CFTypeRef object = CFCreateSomething(); // expected-warning{{Potential leak of an object stored into 'object'}}