Skip to content

Commit 9f32816

Browse files
committed
[analyzer] Refine the diagnostics in the nullability checker to differentiate between nil and null
This is a big deal for ObjC, where nullability annotations are extensively used. I've also changed "Null" -> "null" and removed "is" as this is the pattern that Sema is using. Differential Revision: https://reviews.llvm.org/D27600 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@289885 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 2c779f3 commit 9f32816

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,9 @@ void NullabilityChecker::checkPreStmt(const ReturnStmt *S,
610610

611611
SmallString<256> SBuf;
612612
llvm::raw_svector_ostream OS(SBuf);
613-
OS << "Null is returned from a " << C.getDeclDescription(D) <<
613+
OS << (RetExpr->getType()->isObjCObjectPointerType() ? "nil" : "Null");
614+
OS << " returned from a " << C.getDeclDescription(D) <<
614615
" that is expected to return a non-null value";
615-
616616
reportBugIfInvariantHolds(OS.str(),
617617
ErrorKind::NilReturnedToNonnull, N, nullptr, C,
618618
RetExpr);
@@ -707,9 +707,11 @@ void NullabilityChecker::checkPreCall(const CallEvent &Call,
707707
ExplodedNode *N = C.generateErrorNode(State);
708708
if (!N)
709709
return;
710+
710711
SmallString<256> SBuf;
711712
llvm::raw_svector_ostream OS(SBuf);
712-
OS << "Null passed to a callee that requires a non-null " << ParamIdx
713+
OS << (Param->getType()->isObjCObjectPointerType() ? "nil" : "Null");
714+
OS << " passed to a callee that requires a non-null " << ParamIdx
713715
<< llvm::getOrdinalSuffix(ParamIdx) << " parameter";
714716
reportBugIfInvariantHolds(OS.str(), ErrorKind::NilPassedToNonnull, N,
715717
nullptr, C,
@@ -1128,8 +1130,11 @@ void NullabilityChecker::checkBind(SVal L, SVal V, const Stmt *S,
11281130
if (ValueExpr)
11291131
ValueStmt = ValueExpr;
11301132

1131-
reportBugIfInvariantHolds("Null is assigned to a pointer which is "
1132-
"expected to have non-null value",
1133+
SmallString<256> SBuf;
1134+
llvm::raw_svector_ostream OS(SBuf);
1135+
OS << (LocType->isObjCObjectPointerType() ? "nil" : "Null");
1136+
OS << " assigned to a pointer which is expected to have non-null value";
1137+
reportBugIfInvariantHolds(OS.str(),
11331138
ErrorKind::NilAssignedToNonnull, N, nullptr, C,
11341139
ValueStmt);
11351140
return;

test/Analysis/nullability-no-arc.mm

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ - (void)release;
1717
@interface TestObject : NSObject
1818
@end
1919

20-
TestObject * _Nonnull returnsNilObjCInstanceIndirectly() {
21-
TestObject *local = 0;
22-
return local; // expected-warning {{Null is returned from a function that is expected to return a non-null value}}
20+
TestObject *_Nonnull returnsNilObjCInstanceIndirectly() {
21+
TestObject *local = nil;
22+
return local; // expected-warning {{nil returned from a function that is expected to return a non-null value}}
2323
}
2424

2525
TestObject * _Nonnull returnsNilObjCInstanceIndirectlyWithSupressingCast() {
26-
TestObject *local = 0;
26+
TestObject *local = nil;
2727
return (TestObject * _Nonnull)local; // no-warning
2828
}
2929

3030
TestObject * _Nonnull returnsNilObjCInstanceDirectly() {
3131
// The first warning is from Sema. The second is from the static analyzer.
3232
return nil; // expected-warning {{null returned from function that requires a non-null return value}}
33-
// expected-warning@-1 {{Null is returned from a function that is expected to return a non-null value}}
33+
// expected-warning@-1 {{nil returned from a function that is expected to return a non-null value}}
3434
}
3535

3636
TestObject * _Nonnull returnsNilObjCInstanceDirectlyWithSuppressingCast() {
@@ -43,7 +43,7 @@ void testObjCNonARCNoInitialization(TestObject * _Nonnull p) {
4343
}
4444

4545
void testObjCNonARCExplicitZeroInitialization() {
46-
TestObject * _Nonnull explicitlyZeroInitialized = nil; // expected-warning {{Null is assigned to a pointer which is expected to have non-null value}}
46+
TestObject * _Nonnull explicitlyZeroInitialized = nil; // expected-warning {{nil assigned to a pointer which is expected to have non-null value}}
4747
}
4848

4949
@interface ClassWithInitializers : NSObject

test/Analysis/nullability.mm

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void testBasicRules() {
7575
}
7676
Dummy a;
7777
Dummy *_Nonnull nonnull = &a;
78-
nonnull = q; // expected-warning {{Null is assigned to a pointer which is expected to have non-null value}}
78+
nonnull = q; // expected-warning {{Null assigned to a pointer which is expected to have non-null value}}
7979
q = &a;
8080
takesNullable(q);
8181
takesNonnull(q);
@@ -107,7 +107,7 @@ void testArgumentTracking(Dummy *_Nonnull nonnull, Dummy *_Nullable nullable) {
107107

108108
Dummy *_Nonnull testNullReturn() {
109109
Dummy *p = 0;
110-
return p; // expected-warning {{Null is returned from a function that is expected to return a non-null value}}
110+
return p; // expected-warning {{Null returned from a function that is expected to return a non-null value}}
111111
}
112112

113113
void testObjCMessageResultNullability() {
@@ -229,7 +229,7 @@ void testConditionalNilPassToNonnull(Dummy *p) {
229229
Dummy * _Nonnull testIndirectCastNilToNonnullAndReturn() {
230230
Dummy *p = (Dummy * _Nonnull)0;
231231
// FIXME: Ideally the cast above would suppress this warning.
232-
return p; // expected-warning {{Null is returned from a function that is expected to return a non-null value}}
232+
return p; // expected-warning {{Null returned from a function that is expected to return a non-null value}}
233233
}
234234

235235
void testInvalidPropagation() {

test/Analysis/nullability_nullonly.mm

+9-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void testBasicRules() {
2424

2525
Dummy *_Nonnull testNullReturn() {
2626
Dummy *p = 0;
27-
return p; // expected-warning {{Null is returned from a function that is expected to return a non-null value}}
27+
return p; // expected-warning {{Null returned from a function that is expected to return a non-null value}}
2828
}
2929

3030
void onlyReportFirstPreconditionViolationOnPath() {
@@ -100,24 +100,24 @@ void testObjCARCImplicitZeroInitialization() {
100100
}
101101

102102
void testObjCARCExplicitZeroInitialization() {
103-
TestObject * _Nonnull explicitlyZeroInitialized = nil; // expected-warning {{Null is assigned to a pointer which is expected to have non-null value}}
103+
TestObject * _Nonnull explicitlyZeroInitialized = nil; // expected-warning {{nil assigned to a pointer which is expected to have non-null value}}
104104
}
105105

106106
// Under ARC, returned expressions of ObjC objects types are are implicitly
107107
// cast to _Nonnull when the functions return type is _Nonnull, so make
108108
// sure this doesn't implicit cast doesn't suppress a legitimate warning.
109109
TestObject * _Nonnull returnsNilObjCInstanceIndirectly() {
110-
TestObject *local = 0;
111-
return local; // expected-warning {{Null is returned from a function that is expected to return a non-null value}}
110+
TestObject *local = nil;
111+
return local; // expected-warning {{nil returned from a function that is expected to return a non-null value}}
112112
}
113113

114114
TestObject * _Nonnull returnsNilObjCInstanceIndirectlyWithSupressingCast() {
115-
TestObject *local = 0;
115+
TestObject *local = nil;
116116
return (TestObject * _Nonnull)local; // no-warning
117117
}
118118

119119
TestObject * _Nonnull returnsNilObjCInstanceDirectly() {
120-
return nil; // expected-warning {{Null is returned from a function that is expected to return a non-null value}}
120+
return nil; // expected-warning {{nil returned from a function that is expected to return a non-null value}}
121121
}
122122

123123
TestObject * _Nonnull returnsNilObjCInstanceDirectlyWithSuppressingCast() {
@@ -130,7 +130,7 @@ @interface SomeClass : NSObject
130130
@implementation SomeClass (MethodReturn)
131131
- (SomeClass * _Nonnull)testReturnsNilInNonnull {
132132
SomeClass *local = nil;
133-
return local; // expected-warning {{Null is returned from a method that is expected to return a non-null value}}
133+
return local; // expected-warning {{nil returned from a method that is expected to return a non-null value}}
134134
}
135135

136136
- (SomeClass * _Nonnull)testReturnsCastSuppressedNilInNonnull {
@@ -154,7 +154,7 @@ void callFunctionInSystemHeader() {
154154

155155
NSSystemFunctionTakingNonnull(s);
156156
#if !NOSYSTEMHEADERS
157-
// expected-warning@-2{{Null passed to a callee that requires a non-null 1st parameter}}
157+
// expected-warning@-2{{nil passed to a callee that requires a non-null 1st parameter}}
158158
#endif
159159
}
160160

@@ -165,6 +165,6 @@ void callMethodInSystemHeader() {
165165
NSSystemClass *sc = [[NSSystemClass alloc] init];
166166
[sc takesNonnull:s];
167167
#if !NOSYSTEMHEADERS
168-
// expected-warning@-2{{Null passed to a callee that requires a non-null 1st parameter}}
168+
// expected-warning@-2{{nil passed to a callee that requires a non-null 1st parameter}}
169169
#endif
170170
}

0 commit comments

Comments
 (0)