Clang Project

clang_source_code/test/Analysis/diagnostics/macros.m
1// RUN: %clang_analyze_cc1 -analyzer-checker=core,osx -fblocks -analyzer-output=text -verify %s
2
3#include "../Inputs/system-header-simulator-objc.h"
4
5@interface NSDictionary : NSObject
6- (NSUInteger)count;
7- (id)objectForKey:(id)aKey;
8- (NSEnumerator *)keyEnumerator;
9@end
10@interface NSMutableDictionary : NSDictionary
11- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
12@end
13
14void testBOOLMacro(BOOL b) {
15  if (b == YES) { // expected-note {{Assuming 'b' is equal to YES}}
16                  // expected-note@-1 {{Taking true branch}}
17    char *p = NULL;// expected-note {{'p' initialized to a null pointer value}}
18    *p = 7;  // expected-warning {{Dereference of null pointer (loaded from variable 'p')}}
19             // expected-note@-1 {{Dereference of null pointer (loaded from variable 'p')}}
20  }
21}
22
23void testNilMacro(NSMutableDictionary *d, NSObject *o) {
24  if (o == nil) // expected-note {{Assuming 'o' is equal to nil}}
25                // expected-note@-1 {{Taking true branch}}
26    [d setObject:o forKey:[o description]]; // expected-warning {{Key argument to 'setObject:forKey:' cannot be nil}}
27                                            // expected-note@-1 {{'description' not called because the receiver is nil}}
28                                            // expected-note@-2 {{Key argument to 'setObject:forKey:' cannot be nil}}
29
30  return;
31}
32