2017-03-04 02:02:02 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -fobjc-arc -analyzer-checker=core,nullability.NullPassedToNonnull,nullability.NullReturnedFromNonnull -DNOSYSTEMHEADERS=0 -verify %s
|
|
|
|
// RUN: %clang_analyze_cc1 -fobjc-arc -analyzer-checker=core,nullability.NullPassedToNonnull,nullability.NullReturnedFromNonnull -analyzer-config nullability:NoDiagnoseCallsToSystemHeaders=true -DNOSYSTEMHEADERS=1 -verify %s
|
2015-09-15 02:31:34 +08:00
|
|
|
|
2016-03-05 09:32:43 +08:00
|
|
|
#include "Inputs/system-header-simulator-for-nullability.h"
|
2016-01-16 05:35:40 +08:00
|
|
|
|
2015-09-15 02:31:34 +08:00
|
|
|
int getRandom();
|
|
|
|
|
|
|
|
typedef struct Dummy { int val; } Dummy;
|
|
|
|
|
|
|
|
void takesNullable(Dummy *_Nullable);
|
|
|
|
void takesNonnull(Dummy *_Nonnull);
|
|
|
|
Dummy *_Nullable returnsNullable();
|
|
|
|
|
|
|
|
void testBasicRules() {
|
|
|
|
// The tracking of nullable values is turned off.
|
|
|
|
Dummy *p = returnsNullable();
|
|
|
|
takesNonnull(p); // no warning
|
|
|
|
Dummy *q = 0;
|
|
|
|
if (getRandom()) {
|
|
|
|
takesNullable(q);
|
2016-01-30 02:43:15 +08:00
|
|
|
takesNonnull(q); // expected-warning {{Null passed to a callee that requires a non-null 1st parameter}}
|
2015-09-15 02:31:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Dummy *_Nonnull testNullReturn() {
|
|
|
|
Dummy *p = 0;
|
2016-12-16 06:55:15 +08:00
|
|
|
return p; // expected-warning {{Null returned from a function that is expected to return a non-null value}}
|
2015-09-15 02:31:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void onlyReportFirstPreconditionViolationOnPath() {
|
|
|
|
Dummy *p = 0;
|
2016-01-30 02:43:15 +08:00
|
|
|
takesNonnull(p); // expected-warning {{Null passed to a callee that requires a non-null 1st parameter}}
|
2015-09-15 02:31:34 +08:00
|
|
|
takesNonnull(p); // No warning.
|
|
|
|
// Passing null to nonnull is a sink. Stop the analysis.
|
|
|
|
int i = 0;
|
|
|
|
i = 5 / i; // no warning
|
|
|
|
(void)i;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dummy *_Nonnull doNotWarnWhenPreconditionIsViolatedInTopFunc(
|
|
|
|
Dummy *_Nonnull p) {
|
|
|
|
if (!p) {
|
|
|
|
Dummy *ret =
|
|
|
|
0; // avoid compiler warning (which is not generated by the analyzer)
|
|
|
|
if (getRandom())
|
|
|
|
return ret; // no warning
|
|
|
|
else
|
|
|
|
return p; // no warning
|
|
|
|
} else {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Dummy *_Nonnull doNotWarnWhenPreconditionIsViolated(Dummy *_Nonnull p) {
|
|
|
|
if (!p) {
|
|
|
|
Dummy *ret =
|
|
|
|
0; // avoid compiler warning (which is not generated by the analyzer)
|
|
|
|
if (getRandom())
|
|
|
|
return ret; // no warning
|
|
|
|
else
|
|
|
|
return p; // no warning
|
|
|
|
} else {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testPreconditionViolationInInlinedFunction(Dummy *p) {
|
|
|
|
doNotWarnWhenPreconditionIsViolated(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void inlinedNullable(Dummy *_Nullable p) {
|
|
|
|
if (p) return;
|
|
|
|
}
|
|
|
|
void inlinedNonnull(Dummy *_Nonnull p) {
|
|
|
|
if (p) return;
|
|
|
|
}
|
|
|
|
void inlinedUnspecified(Dummy *p) {
|
|
|
|
if (p) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dummy *_Nonnull testDefensiveInlineChecks(Dummy * p) {
|
|
|
|
switch (getRandom()) {
|
|
|
|
case 1: inlinedNullable(p); break;
|
|
|
|
case 2: inlinedNonnull(p); break;
|
|
|
|
case 3: inlinedUnspecified(p); break;
|
|
|
|
}
|
|
|
|
if (getRandom())
|
|
|
|
takesNonnull(p);
|
|
|
|
return p;
|
|
|
|
}
|
2016-01-16 05:35:40 +08:00
|
|
|
|
2016-01-19 02:53:33 +08:00
|
|
|
@interface TestObject : NSObject
|
|
|
|
@end
|
|
|
|
|
|
|
|
TestObject *_Nonnull getNonnullTestObject();
|
|
|
|
|
|
|
|
void testObjCARCImplicitZeroInitialization() {
|
|
|
|
TestObject * _Nonnull implicitlyZeroInitialized; // no-warning
|
|
|
|
implicitlyZeroInitialized = getNonnullTestObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
void testObjCARCExplicitZeroInitialization() {
|
2016-12-16 06:55:15 +08:00
|
|
|
TestObject * _Nonnull explicitlyZeroInitialized = nil; // expected-warning {{nil assigned to a pointer which is expected to have non-null value}}
|
2016-01-19 02:53:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Under ARC, returned expressions of ObjC objects types are are implicitly
|
|
|
|
// cast to _Nonnull when the functions return type is _Nonnull, so make
|
|
|
|
// sure this doesn't implicit cast doesn't suppress a legitimate warning.
|
|
|
|
TestObject * _Nonnull returnsNilObjCInstanceIndirectly() {
|
2016-12-16 06:55:15 +08:00
|
|
|
TestObject *local = nil;
|
|
|
|
return local; // expected-warning {{nil returned from a function that is expected to return a non-null value}}
|
2016-01-19 02:53:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TestObject * _Nonnull returnsNilObjCInstanceIndirectlyWithSupressingCast() {
|
2016-12-16 06:55:15 +08:00
|
|
|
TestObject *local = nil;
|
2016-01-19 02:53:33 +08:00
|
|
|
return (TestObject * _Nonnull)local; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
TestObject * _Nonnull returnsNilObjCInstanceDirectly() {
|
2016-12-16 06:55:15 +08:00
|
|
|
return nil; // expected-warning {{nil returned from a function that is expected to return a non-null value}}
|
2016-01-19 02:53:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TestObject * _Nonnull returnsNilObjCInstanceDirectlyWithSuppressingCast() {
|
|
|
|
return (TestObject * _Nonnull)nil; // no-warning
|
|
|
|
}
|
2016-01-16 05:35:40 +08:00
|
|
|
|
|
|
|
@interface SomeClass : NSObject
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SomeClass (MethodReturn)
|
|
|
|
- (SomeClass * _Nonnull)testReturnsNilInNonnull {
|
|
|
|
SomeClass *local = nil;
|
2016-12-16 06:55:15 +08:00
|
|
|
return local; // expected-warning {{nil returned from a method that is expected to return a non-null value}}
|
2016-01-16 05:35:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (SomeClass * _Nonnull)testReturnsCastSuppressedNilInNonnull {
|
|
|
|
SomeClass *local = nil;
|
|
|
|
return (SomeClass * _Nonnull)local; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
- (SomeClass * _Nonnull)testReturnsNilInNonnullWhenPreconditionViolated:(SomeClass * _Nonnull) p {
|
|
|
|
SomeClass *local = nil;
|
|
|
|
if (!p) // Pre-condition violated here.
|
|
|
|
return local; // no-warning
|
|
|
|
else
|
|
|
|
return p; // no-warning
|
|
|
|
}
|
|
|
|
@end
|
2016-03-05 09:32:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
void callFunctionInSystemHeader() {
|
|
|
|
NSString *s;
|
|
|
|
s = nil;
|
|
|
|
|
|
|
|
NSSystemFunctionTakingNonnull(s);
|
|
|
|
#if !NOSYSTEMHEADERS
|
2016-12-16 06:55:15 +08:00
|
|
|
// expected-warning@-2{{nil passed to a callee that requires a non-null 1st parameter}}
|
2016-03-05 09:32:43 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void callMethodInSystemHeader() {
|
|
|
|
NSString *s;
|
|
|
|
s = nil;
|
|
|
|
|
|
|
|
NSSystemClass *sc = [[NSSystemClass alloc] init];
|
|
|
|
[sc takesNonnull:s];
|
|
|
|
#if !NOSYSTEMHEADERS
|
2016-12-16 06:55:15 +08:00
|
|
|
// expected-warning@-2{{nil passed to a callee that requires a non-null 1st parameter}}
|
2016-03-05 09:32:43 +08:00
|
|
|
#endif
|
|
|
|
}
|