Martin Brænne | 2a63b44 | 2023-04-03 08:01:17 -0700 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
| 5 | // Tests for the treatment of the `this` pointer (which is always nonnull). |
| 6 | #include <optional> |
| 7 | #include <set> |
| 8 | #include <string> |
| 9 | |
Googler | 7f19b2b | 2023-05-01 09:44:57 -0700 | [diff] [blame] | 10 | #include "nullability/test/check_diagnostics.h" |
Martin Brænne | 2a63b44 | 2023-04-03 08:01:17 -0700 | [diff] [blame] | 11 | #include "third_party/llvm/llvm-project/third-party/unittest/googletest/include/gtest/gtest.h" |
| 12 | |
Sam McCall | 4f6be42 | 2023-06-27 02:51:22 -0700 | [diff] [blame] | 13 | namespace clang::tidy::nullability { |
Martin Brænne | 2a63b44 | 2023-04-03 08:01:17 -0700 | [diff] [blame] | 14 | namespace { |
| 15 | |
Martin Brænne | 834d4b8 | 2023-05-15 07:07:05 -0700 | [diff] [blame] | 16 | TEST(PointerNullabilityTest, ImplicitThis) { |
Martin Brænne | 2a63b44 | 2023-04-03 08:01:17 -0700 | [diff] [blame] | 17 | EXPECT_TRUE(checkDiagnostics(R"cc( |
| 18 | struct Foo { |
| 19 | void foo(); |
Martin Brænne | 834d4b8 | 2023-05-15 07:07:05 -0700 | [diff] [blame] | 20 | void target() { |
| 21 | __assert_nullability<NK_nonnull>(this); |
| 22 | foo(); |
| 23 | } |
Martin Brænne | 2a63b44 | 2023-04-03 08:01:17 -0700 | [diff] [blame] | 24 | }; |
| 25 | )cc")); |
Martin Brænne | 834d4b8 | 2023-05-15 07:07:05 -0700 | [diff] [blame] | 26 | } |
Martin Brænne | 2a63b44 | 2023-04-03 08:01:17 -0700 | [diff] [blame] | 27 | |
Martin Brænne | 834d4b8 | 2023-05-15 07:07:05 -0700 | [diff] [blame] | 28 | TEST(PointerNullabilityTest, ExplicitThis) { |
Martin Brænne | 2a63b44 | 2023-04-03 08:01:17 -0700 | [diff] [blame] | 29 | // (->) explicit `this` |
| 30 | EXPECT_TRUE(checkDiagnostics(R"cc( |
| 31 | struct Foo { |
| 32 | void foo(); |
| 33 | void target() { this->foo(); } |
| 34 | }; |
| 35 | )cc")); |
| 36 | } |
| 37 | |
Martin Brænne | 834d4b8 | 2023-05-15 07:07:05 -0700 | [diff] [blame] | 38 | TEST(PointerNullabilityTest, ClassWithPointerTemplateArg) { |
| 39 | EXPECT_TRUE(checkDiagnostics(R"cc( |
| 40 | template <class T> |
| 41 | struct S; |
| 42 | template <> |
Sam McCall | 7d9afee | 2023-06-27 01:43:24 -0700 | [diff] [blame] | 43 | struct S<int *_Nullable> { |
Martin Brænne | 834d4b8 | 2023-05-15 07:07:05 -0700 | [diff] [blame] | 44 | void target() { |
| 45 | // `_Nullable` in the specialization is bogus: we can't specialize on |
| 46 | // nullability as it's just sugar. Therefore the correct inner |
| 47 | // nullability here is "unspecified". |
| 48 | __assert_nullability<NK_nonnull, NK_unspecified>(this); |
| 49 | } |
| 50 | }; |
| 51 | )cc")); |
| 52 | } |
| 53 | |
Martin Brænne | 2a63b44 | 2023-04-03 08:01:17 -0700 | [diff] [blame] | 54 | } // namespace |
Sam McCall | 4f6be42 | 2023-06-27 02:51:22 -0700 | [diff] [blame] | 55 | } // namespace clang::tidy::nullability |