1 // RUN: %clang_analyze_cc1 -fobjc-arc -analyzer-checker=core,nullability.NullPassedToNonnull,nullability.NullReturnedFromNonnull -DNOSYSTEMHEADERS=0 -verify %s
2 // RUN: %clang_analyze_cc1 -fobjc-arc -analyzer-checker=core,nullability.NullPassedToNonnull,nullability.NullReturnedFromNonnull -analyzer-config nullability:NoDiagnoseCallsToSystemHeaders=true -DNOSYSTEMHEADERS=1 -verify %s
4 #include "Inputs/system-header-simulator-for-nullability.h"
8 typedef struct Dummy { int val; } Dummy;
10 void takesNullable(Dummy *_Nullable);
11 void takesNonnull(Dummy *_Nonnull);
12 Dummy *_Nullable returnsNullable();
14 void testBasicRules() {
15 // The tracking of nullable values is turned off.
16 Dummy *p = returnsNullable();
17 takesNonnull(p); // no warning
21 takesNonnull(q); // expected-warning {{Null passed to a callee that requires a non-null 1st parameter}}
25 Dummy *_Nonnull testNullReturn() {
27 return p; // expected-warning {{Null returned from a function that is expected to return a non-null value}}
30 void onlyReportFirstPreconditionViolationOnPath() {
32 takesNonnull(p); // expected-warning {{Null passed to a callee that requires a non-null 1st parameter}}
33 takesNonnull(p); // No warning.
34 // Passing null to nonnull is a sink. Stop the analysis.
36 i = 5 / i; // no warning
40 Dummy *_Nonnull doNotWarnWhenPreconditionIsViolatedInTopFunc(
44 0; // avoid compiler warning (which is not generated by the analyzer)
46 return ret; // no warning
48 return p; // no warning
54 Dummy *_Nonnull doNotWarnWhenPreconditionIsViolated(Dummy *_Nonnull p) {
57 0; // avoid compiler warning (which is not generated by the analyzer)
59 return ret; // no warning
61 return p; // no warning
67 void testPreconditionViolationInInlinedFunction(Dummy *p) {
68 doNotWarnWhenPreconditionIsViolated(p);
71 void inlinedNullable(Dummy *_Nullable p) {
74 void inlinedNonnull(Dummy *_Nonnull p) {
77 void inlinedUnspecified(Dummy *p) {
81 Dummy *_Nonnull testDefensiveInlineChecks(Dummy * p) {
82 switch (getRandom()) {
83 case 1: inlinedNullable(p); break;
84 case 2: inlinedNonnull(p); break;
85 case 3: inlinedUnspecified(p); break;
92 @interface TestObject : NSObject
95 TestObject *_Nonnull getNonnullTestObject();
97 void testObjCARCImplicitZeroInitialization() {
98 TestObject * _Nonnull implicitlyZeroInitialized; // no-warning
99 implicitlyZeroInitialized = getNonnullTestObject();
102 void testObjCARCExplicitZeroInitialization() {
103 TestObject * _Nonnull explicitlyZeroInitialized = nil; // expected-warning {{nil assigned to a pointer which is expected to have non-null value}}
106 // Under ARC, returned expressions of ObjC objects types are are implicitly
107 // cast to _Nonnull when the functions return type is _Nonnull, so make
108 // sure this doesn't implicit cast doesn't suppress a legitimate warning.
109 TestObject * _Nonnull returnsNilObjCInstanceIndirectly() {
110 TestObject *local = nil;
111 return local; // expected-warning {{nil returned from a function that is expected to return a non-null value}}
114 TestObject * _Nonnull returnsNilObjCInstanceIndirectlyWithSupressingCast() {
115 TestObject *local = nil;
116 return (TestObject * _Nonnull)local; // no-warning
119 TestObject * _Nonnull returnsNilObjCInstanceDirectly() {
120 return nil; // expected-warning {{nil returned from a function that is expected to return a non-null value}}
123 TestObject * _Nonnull returnsNilObjCInstanceDirectlyWithSuppressingCast() {
124 return (TestObject * _Nonnull)nil; // no-warning
127 @interface SomeClass : NSObject
130 @implementation SomeClass (MethodReturn)
131 - (SomeClass * _Nonnull)testReturnsNilInNonnull {
132 SomeClass *local = nil;
133 return local; // expected-warning {{nil returned from a method that is expected to return a non-null value}}
136 - (SomeClass * _Nonnull)testReturnsCastSuppressedNilInNonnull {
137 SomeClass *local = nil;
138 return (SomeClass * _Nonnull)local; // no-warning
141 - (SomeClass * _Nonnull)testReturnsNilInNonnullWhenPreconditionViolated:(SomeClass * _Nonnull) p {
142 SomeClass *local = nil;
143 if (!p) // Pre-condition violated here.
144 return local; // no-warning
146 return p; // no-warning
151 void callFunctionInSystemHeader() {
155 NSSystemFunctionTakingNonnull(s);
157 // expected-warning@-2{{nil passed to a callee that requires a non-null 1st parameter}}
161 void callMethodInSystemHeader() {
165 NSSystemClass *sc = [[NSSystemClass alloc] init];
168 // expected-warning@-2{{nil passed to a callee that requires a non-null 1st parameter}}