1 // RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.nullability.NullPassedToNonnull,alpha.nullability.NullReturnedFromNonnull -verify %s
5 typedef struct Dummy { int val; } Dummy;
7 void takesNullable(Dummy *_Nullable);
8 void takesNonnull(Dummy *_Nonnull);
9 Dummy *_Nullable returnsNullable();
11 void testBasicRules() {
12 // The tracking of nullable values is turned off.
13 Dummy *p = returnsNullable();
14 takesNonnull(p); // no warning
18 takesNonnull(q); // expected-warning {{}}
22 Dummy *_Nonnull testNullReturn() {
24 return p; // expected-warning {{}}
27 void onlyReportFirstPreconditionViolationOnPath() {
29 takesNonnull(p); // expected-warning {{}}
30 takesNonnull(p); // No warning.
31 // Passing null to nonnull is a sink. Stop the analysis.
33 i = 5 / i; // no warning
37 Dummy *_Nonnull doNotWarnWhenPreconditionIsViolatedInTopFunc(
41 0; // avoid compiler warning (which is not generated by the analyzer)
43 return ret; // no warning
45 return p; // no warning
51 Dummy *_Nonnull doNotWarnWhenPreconditionIsViolated(Dummy *_Nonnull p) {
54 0; // avoid compiler warning (which is not generated by the analyzer)
56 return ret; // no warning
58 return p; // no warning
64 void testPreconditionViolationInInlinedFunction(Dummy *p) {
65 doNotWarnWhenPreconditionIsViolated(p);
68 void inlinedNullable(Dummy *_Nullable p) {
71 void inlinedNonnull(Dummy *_Nonnull p) {
74 void inlinedUnspecified(Dummy *p) {
78 Dummy *_Nonnull testDefensiveInlineChecks(Dummy * p) {
79 switch (getRandom()) {
80 case 1: inlinedNullable(p); break;
81 case 2: inlinedNonnull(p); break;
82 case 3: inlinedUnspecified(p); break;