-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathcheck_macros.cc
More file actions
105 lines (88 loc) · 3.17 KB
/
check_macros.cc
File metadata and controls
105 lines (88 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Part of the Crubit project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// Tests for CHECK_NE and some of its implementation details.
#include <memory>
#include "check.h"
#include "nullability_test.h"
TEST void getReferenceableValueModeledForAbseil(int *_Nullable NullablePtr,
int *_Nonnull NonnullPtr) {
auto &Nullable2 = absl::something::GetReferenceableValue(NullablePtr);
auto &Nonnull2 = absl::something::GetReferenceableValue(NonnullPtr);
// Outputs should share the nullability of inputs.
nullable(Nullable2);
nonnull(Nonnull2);
}
TEST void getReferenceableValueModeledForUtil(int *_Nullable NullablePtr,
int *_Nonnull NonnullPtr) {
auto &Nullable2 = util::something::GetReferenceableValue(NullablePtr);
auto &Nonnull2 = util::something::GetReferenceableValue(NonnullPtr);
// Outputs should share the nullability of inputs.
nullable(Nullable2);
nonnull(Nonnull2);
}
TEST void abslCheckNEImplModelEqualAndNull() {
int *P = nullptr;
// `P` is definitely equal to `nullptr`, so result is nonnull.
nonnull(absl::something::Check_NEImpl(P, nullptr, "foo"));
}
TEST void abslCheckNEImplModelMaybeUnequal(int *P, int *Q) {
// Since we don't know `P`'s relation to `Q`, result may be null.
nullable(absl::something::Check_NEImpl(P, Q, "foo"));
}
TEST void abslCheckNEImPlModelUnequal(int *P) {
if (P != nullptr) {
// `P` is definitely not equal to `nullptr`, so result is null.
nullable(absl::something::Check_NEImpl(P, nullptr, "foo"));
}
}
TEST void checkNELeft(int *_Nullable P) {
CHECK_NE(P, nullptr);
nonnull(P);
}
TEST void checkNERight(int *_Nullable P) {
CHECK_NE(nullptr, P);
nonnull(P);
}
TEST void checkNELeftSmartPointer(std::unique_ptr<int> P) {
CHECK_NE(P, nullptr);
nonnull(P);
}
TEST void checkNERightSmartPointer(std::unique_ptr<int> P) {
CHECK_NE(nullptr, P);
nonnull(P);
}
// Test where a loop can result in Top pointer null states.
// Make sure we can still do a null check after the loop, despite the Top
// null state.
TEST void checkNEAfterLoop(int* _Nullable P, int Bound, bool B) {
int X = 17;
for (int I = 0; I < Bound; ++I) {
if (B) P = &X;
}
CHECK_NE(P, nullptr);
nonnull(P);
}
TEST void checkNEAfterLoopSmartPointer(_Nullable std::unique_ptr<int> P,
int Bound, bool B) {
for (int I = 0; I < Bound; ++I) {
if (B) P = std::make_unique<int>(17);
}
CHECK_NE(P, nullptr);
nonnull(P);
}
TEST void utilCheckNEImplModelEqualAndNull() {
int *P = nullptr;
// `P` is definitely equal to `nullptr`, so result is nonnull.
nonnull(util::something::Check_NEImpl(P, nullptr, "foo"));
}
TEST void utilCheckNEImplModelMaybeUnequal(int *P, int *Q) {
// Since we don't know `P`'s relation to `Q`, result may be null.
nullable(util::something::Check_NEImpl(P, Q, "foo"));
}
TEST void utilCheckNEImPlModelUnequal(int *P) {
if (P != nullptr) {
// `P` is definitely not equal to `nullptr`, so result is null.
nullable(util::something::Check_NEImpl(P, nullptr, "foo"));
}
}