1 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin10 -analyzer-checker=core,deadcode.DeadStores,osx.cocoa.RetainCount,unix.Malloc,unix.MismatchedDeallocator -analyzer-output=plist -analyzer-config deadcode.DeadStores:ShowFixIts=true -o %t -w %s
2 // RUN: %normalize_plist <%t | diff -ub %S/Inputs/expected-plists/edges-new.mm.plist -
4 //===----------------------------------------------------------------------===//
5 // Forward declarations (from headers).
6 //===----------------------------------------------------------------------===//
8 typedef const struct __CFNumber * CFNumberRef;
9 typedef const struct __CFAllocator * CFAllocatorRef;
10 extern const CFAllocatorRef kCFAllocatorDefault;
11 typedef signed long CFIndex;
13 kCFNumberSInt8Type = 1,
14 kCFNumberSInt16Type = 2,
15 kCFNumberSInt32Type = 3,
16 kCFNumberSInt64Type = 4,
17 kCFNumberFloat32Type = 5,
18 kCFNumberFloat64Type = 6,
19 kCFNumberCharType = 7,
20 kCFNumberShortType = 8,
22 kCFNumberLongType = 10,
23 kCFNumberLongLongType = 11,
24 kCFNumberFloatType = 12,
25 kCFNumberDoubleType = 13,
26 kCFNumberCFIndexType = 14,
27 kCFNumberNSIntegerType = 15,
28 kCFNumberCGFloatType = 16,
31 typedef CFIndex CFNumberType;
32 CFNumberRef CFNumberCreate(CFAllocatorRef allocator, CFNumberType theType, const void *valuePtr);
36 __attribute__((objc_root_class))
38 + (instancetype) alloc;
39 - (instancetype) init;
40 - (instancetype)retain;
44 @interface NSArray : NSObject
47 //===----------------------------------------------------------------------===//
48 // Basic tracking of null and tests for null.
49 //===----------------------------------------------------------------------===//
51 void test_null_init(void) {
56 void test_null_assign(void) {
62 void test_null_assign_transitive(void) {
69 void test_null_cond(int *p) {
75 void test_null_cond_transitive(int *q) {
82 void test_null_field(void) {
83 struct s { int *p; } x;
88 void test_assumptions(int a, int b)
100 int *bar_cond_assign();
101 int test_cond_assign() {
103 if ((p = bar_cond_assign()))
108 //===----------------------------------------------------------------------===//
109 // Diagnostics for leaks and "noreturn" paths.
110 //===----------------------------------------------------------------------===//
113 // leak reports should not show paths that end with exit() (but ones that don't end with exit())
115 void stop() __attribute__((noreturn));
117 void rdar8331641(int x) {
119 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // expected-warning{{leak}}
125 //===----------------------------------------------------------------------===//
126 // Test loops and control-flow.
127 //===----------------------------------------------------------------------===//
129 void test_objc_fast_enumeration(NSArray *x) {
132 *(volatile int *)0 = 0;
135 void test_objc_fast_enumeration_2(id arr) {
137 for (id obj in arr) {
143 // Test that loops are documented in the path.
144 void rdar12280665() {
145 for (unsigned i = 0; i < 2; ++i) {
148 *p = 0xDEADBEEF; // expected-warning {{dereference}}
153 // Test for a "loop executed 0 times" diagnostic.
154 int *radar12322528_bar();
156 void radar12322528_for(int x) {
159 for (unsigned i = 0; i < x; ++i) {
160 p = radar12322528_bar();
165 void radar12322528_while(int x) {
170 p = radar12322528_bar();
175 void radar12322528_foo_2() {
177 for (unsigned i = 0; i < 2; ++i) {
189 void test_loop_diagnostics() {
191 for (int i = 0; i < 2; ++i) { p = 0; }
195 void test_loop_diagnostics_2() {
198 for (int i = 0; i < 2; ) {
209 void test_loop_diagnostics_3() {
222 void test_do_while() {
238 void test_logical_and() {
245 void test_logical_or() {
252 void test_logical_or_call() {
253 extern int call(int);
260 void test_nested_logicals(int coin) {
263 if ((0 || 0) || coin) {
267 if (0 || (0 || !coin)) {
272 void test_deeply_nested_logicals() {
273 extern int call(int);
276 if ((0 || (5 && 0)) ? 0 : ((0 || 4) ? call(1 && 5) : 0)) {
282 void test_ternary(int x, int *y) {
290 void testUseless(int *y) {
301 //===----------------------------------------------------------------------===//
302 // Interprocedural tests.
303 //===----------------------------------------------------------------------===//
306 - (int *) returnsPointer;
309 int testFoo(IPA_Foo *x) {
312 return *[x returnsPointer];
315 @interface IPA_X : NSObject
321 *[x getPointer] = 1; // here
325 @interface IPA_Y : NSObject
330 @implementation IPA_Y
336 void test_IPA_Y(IPA_Y *y) {
340 IPA_X *x = [[y opaque] getX]; // here
344 // From diagnostics/report-issues-within-main-file.cpp:
345 void causeDivByZeroInMain(int in) {
351 void mainPlusMain() {
354 causeDivByZeroInMain(i);
358 // From inlining/path-notes.c:
364 void usePointer(int *p) {
368 void testUseOfNullPointer() {
369 // Test the case where an argument expression is itself a call.
370 usePointer(getZero());
374 //===----------------------------------------------------------------------===//
376 //===----------------------------------------------------------------------===//
378 // Test for tracking null state of ivars.
379 @interface RDar12114812 : NSObject { char *p; }
381 @implementation RDar12114812
388 // Test diagnostics for initialization of structs.
389 void RDar13295437_f(void *i) __attribute__((__nonnull__));
390 struct RDar13295437_S { int *i; };
392 struct RDar13295437_S s = {0};
393 struct RDar13295437_S *sp = &s;
394 RDar13295437_f(sp->i);
399 void testCast(int coin) {
404 *(volatile int *)0 = 1;
408 // The following previously crashed when generating extensive diagnostics.
409 @interface RDar10797980_help
410 @property (readonly) int x;
412 @interface RDar10797980 : NSObject {
413 RDar10797980_help *y;
417 @implementation RDar10797980
421 *p = 0xDEADBEEF; // expected-warning {{deference}}
425 // The original source for the above Radar contains another problem:
426 // if the end-of-path node is an implicit statement, it may not have a valid
429 if (bar_cond_assign()) {
430 id foo = [[RDar10797980 alloc] init]; // leak
432 (void)y; // first statement after the 'if' is an implicit 'self' DeclRefExpr
437 void variousLoops(id input) {
448 *(volatile int *)0 = 1;
457 *(volatile int *)0 = 1;
461 for (int i = 0; i != b(); ++i) {
463 *(volatile int *)0 = 1;
466 for (id x in input) {
471 *(volatile int *)0 = 1;
483 for (int y : empty) {
490 for (int i = 0; ; ++i) {
497 for (i = 0; i != b(); ++i) {
499 *(volatile int *)0 = 1;
502 for (; i != b(); ++i) {
504 *(volatile int *)0 = 1;
511 *(volatile int *)0 = 1;
520 *(volatile int *)0 = 1;
523 void *malloc(unsigned long);
524 void *realloc(void *, unsigned long);
527 void reallocDiagnostics() {
528 char * buf = (char*)malloc(100);
530 tmp = (char*)realloc(buf, 0x1000000);
532 return;// expected-warning {{leak}}
538 template <typename T>
542 explicit unique_ptr(T *p) : ptr(p) {}
543 ~unique_ptr() { delete ptr; }
550 unique_ptr<int> p(new int[4]);
557 id foo = [[NSObject alloc] init]; // leak
564 // This next line is intentionally longer than 80 characters.
565 id garply = [foo retain];
569 #define POINTER(T) T*
570 POINTER(void) testMacroInFunctionDecl(void *q) {
576 namespace rdar14960554 {
585 // Check that we don't have an edge to the in-class initializer for 'b'.
587 *(volatile int *)0 = 1;