[nullability] Add support for type alias nullability annotations in tests.
These will be needed to add smart pointer support, as smart pointers cannot be
annotated with nullability attributes (`_Nullable` etc.).
Also convert one test to demonstrate that the type alias annotations work.
PiperOrigin-RevId: 578798169
Change-Id: If996de917bed1461bfdec858cbd6016fb1fbd6aa
diff --git a/nullability/test/check_diagnostics.cc b/nullability/test/check_diagnostics.cc
index 291237a..8f01913 100644
--- a/nullability/test/check_diagnostics.cc
+++ b/nullability/test/check_diagnostics.cc
@@ -29,6 +29,15 @@
template <typename T>
T value();
+
+ template <typename T>
+ using Nullable [[clang::annotate("Nullable")]] = T;
+
+ template <typename T>
+ using Nonnull [[clang::annotate("Nonnull")]] = T;
+
+ template <typename T>
+ using NullabilityUnknown [[clang::annotate("Nullability_Unspecified")]] = T;
)cc";
constexpr char kNewHeader[] = R"cc(
diff --git a/nullability/test/convergence.cc b/nullability/test/convergence.cc
index 581289d..9bad2a9 100644
--- a/nullability/test/convergence.cc
+++ b/nullability/test/convergence.cc
@@ -19,8 +19,8 @@
TEST(PointerNullabilityTest, PointerLoop_Nullable_Nullable) {
EXPECT_TRUE(checkDiagnostics(R"cc(
- int* _Nullable GetFirst();
- int* _Nullable GetNext();
+ Nullable<int*> GetFirst();
+ Nullable<int*> GetNext();
void target() {
for (int* p = GetFirst();; p = GetNext()) {
*p; // [[unsafe]]
@@ -31,8 +31,8 @@
TEST(PointerNullabilityTest, PointerLoop_Nonnull_Nullable) {
EXPECT_TRUE(checkDiagnostics(R"cc(
- int* _Nonnull GetFirst();
- int* _Nullable GetNext();
+ Nonnull<int*> GetFirst();
+ Nullable<int*> GetNext();
void target() {
for (int* p = GetFirst();; p = GetNext()) {
*p; // [[unsafe]]
@@ -43,8 +43,8 @@
TEST(PointerNullabilityTest, PointerLoop_Nullable_Nonnull) {
EXPECT_TRUE(checkDiagnostics(R"cc(
- int* _Nullable GetFirst();
- int* _Nonnull GetNext();
+ Nullable<int*> GetFirst();
+ Nonnull<int*> GetNext();
void target() {
for (int* p = GetFirst();; p = GetNext()) {
*p; // [[unsafe]]
@@ -55,8 +55,8 @@
TEST(PointerNullabilityTest, PointerLoop_Nonnull_Nonnull) {
EXPECT_TRUE(checkDiagnostics(R"cc(
- int* _Nonnull GetFirst();
- int* _Nonnull GetNext();
+ Nonnull<int*> GetFirst();
+ Nonnull<int*> GetNext();
void target() {
for (int* p = GetFirst();; p = GetNext()) {
*p;
@@ -68,7 +68,7 @@
TEST(PointerNullabilityTest, PointerLoop_Unknown_Nullable) {
EXPECT_TRUE(checkDiagnostics(R"cc(
int* GetFirst();
- int* _Nullable GetNext();
+ Nullable<int*> GetNext();
void target() {
for (int* p = GetFirst();; p = GetNext()) {
*p; // [[unsafe]]
@@ -80,7 +80,7 @@
TEST(PointerNullabilityTest, PointerLoop_Unknown_Nonnull) {
EXPECT_TRUE(checkDiagnostics(R"cc(
int* GetFirst();
- int* _Nonnull GetNext();
+ Nonnull<int*> GetNext();
void target() {
for (int* p = GetFirst();; p = GetNext()) {
*p;
@@ -91,7 +91,7 @@
TEST(PointerNullabilityTest, PointerLoop_Nullable_Unknown) {
EXPECT_TRUE(checkDiagnostics(R"cc(
- int* _Nullable GetFirst();
+ Nullable<int*> GetFirst();
int* GetNext();
void target() {
for (int* p = GetFirst();; p = GetNext()) {
@@ -103,7 +103,7 @@
TEST(PointerNullabilityTest, PointerLoop_Nonnull_Unknown) {
EXPECT_TRUE(checkDiagnostics(R"cc(
- int* _Nonnull GetFirst();
+ Nonnull<int*> GetFirst();
int* GetNext();
void target() {
for (int* p = GetFirst();; p = GetNext()) {
@@ -128,8 +128,8 @@
// If we check that the pointer is non-null, don't warn.
TEST(PointerNullabilityTest, PointerLoop_Checked) {
EXPECT_TRUE(checkDiagnostics(R"cc(
- int* _Nullable GetFirst();
- int* _Nullable GetNext();
+ Nullable<int*> GetFirst();
+ Nullable<int*> GetNext();
void target() {
for (int* p = GetFirst(); p != nullptr; p = GetNext()) {
*p;
@@ -143,8 +143,8 @@
// than just the first iteration.
TEST(PointerNullabilityTest, PointerLoop_UnrelatedCondition) {
EXPECT_TRUE(checkDiagnostics(R"cc(
- int* _Nonnull GetFirst();
- int* _Nullable GetNext();
+ Nonnull<int*> GetFirst();
+ Nullable<int*> GetNext();
bool cond();
void target() {
for (int* p = GetFirst(); cond(); p = GetNext()) {
@@ -157,8 +157,8 @@
// Similar to `PointerLoop_UnrelatedCondition`, but we use a counted loop.
TEST(PointerNullabilityTest, PointerLoop_Counted) {
EXPECT_TRUE(checkDiagnostics(R"cc(
- int* _Nonnull GetFirst();
- int* _Nullable GetNext();
+ Nonnull<int*> GetFirst();
+ Nullable<int*> GetNext();
void target() {
int* p = GetFirst();
for (int i = 0; i < 10; ++i, p = GetNext()) {
@@ -246,7 +246,7 @@
// need an extra narrowing step to fix this.
TEST(PointerNullabilityTest, WhileAssignment) {
EXPECT_TRUE(checkDiagnostics(R"cc(
- int* _Nullable GetNext();
+ Nullable<int*> GetNext();
void target() {
int* p;
while ((p = GetNext())) {
@@ -259,9 +259,9 @@
TEST(PointerNullabilityTest, WhileAssignment2) {
EXPECT_TRUE(checkDiagnostics(R"cc(
int* GetFirst();
- int* _Nullable GetNext();
+ Nullable<int*> GetNext();
void target() {
- int* _Nullable p = GetFirst();
+ Nullable<int*> p = GetFirst();
while ((p = GetNext()) != nullptr) {
*p;
}
@@ -273,7 +273,7 @@
// in a loop.
TEST(PointerNullabilityTest, InconsistentLoopStateRepro) {
EXPECT_TRUE(checkDiagnostics(R"cc(
- int* _Nullable GetNullable();
+ Nullable<int*> GetNullable();
bool cond();
void target(int* b, int* e) {