[nullability] Define `__assert_nullability` centrally. We now always prepend the definition of `__assert_nullability` in `checkDiagnostics()`, so it will be available in all tests. PiperOrigin-RevId: 522324205
diff --git a/nullability_verification/test/check_diagnostics.cc b/nullability_verification/test/check_diagnostics.cc index d830671..328e7ec 100644 --- a/nullability_verification/test/check_diagnostics.cc +++ b/nullability_verification/test/check_diagnostics.cc
@@ -15,6 +15,17 @@ namespace tidy { namespace nullability { +constexpr char kPreamble[] = R"cc( + enum NullabilityKind { + NK_nonnull, + NK_nullable, + NK_unspecified, + }; + + template <NullabilityKind... NK, typename T> + void __assert_nullability(const T&); +)cc"; + bool checkDiagnostics(llvm::StringRef SourceCode) { std::vector<CFGElement> Diagnostics; PointerNullabilityDiagnoser Diagnoser; @@ -35,8 +46,10 @@ Diagnostics.push_back(EltDiagnostics.value()); } }) + .withASTBuildVirtualMappedFiles({{"preamble.h", kPreamble}}) .withASTBuildArgs({"-fsyntax-only", "-std=c++17", - "-Wno-unused-value", "-Wno-nonnull"}), + "-Wno-unused-value", "-Wno-nonnull", + "-include", "preamble.h"}), [&Diagnostics, &Failed]( const llvm::DenseMap<unsigned, std::string> &Annotations, const dataflow::test::AnalysisOutputs &AnalysisData) {
diff --git a/nullability_verification/test/pointer_nullability_verification_test.cc b/nullability_verification/test/pointer_nullability_verification_test.cc index 91a026c..23989f3 100644 --- a/nullability_verification/test/pointer_nullability_verification_test.cc +++ b/nullability_verification/test/pointer_nullability_verification_test.cc
@@ -1607,22 +1607,9 @@ )cc")); } -// TODO: Move the definitions of `NullabilityKind` and `__assert_nullability()` -// into a preamble that `checkDiagnostics()` prepends to every test. TEST(PointerNullabilityTest, AssertNullability) { - const std::string Declarations = R"cc( - enum NullabilityKind { - NK_nonnull, - NK_nullable, - NK_unspecified, - }; - - template <NullabilityKind... NK, typename T> - void __assert_nullability(const T&); - )cc"; - // Concrete struct. - EXPECT_TRUE(checkDiagnostics(Declarations + R"cc( + EXPECT_TRUE(checkDiagnostics(R"cc( struct StructNonnullNullable { int* _Nonnull nonnull; int* _Nullable nullable; @@ -1641,7 +1628,7 @@ )cc")); // Struct with two template type parameters. - EXPECT_TRUE(checkDiagnostics(Declarations + R"cc( + EXPECT_TRUE(checkDiagnostics(R"cc( template <typename T0, typename T1> struct Struct2Arg {}; @@ -1663,7 +1650,7 @@ )cc")); // Struct with one type and non-type template parameters. - EXPECT_TRUE(checkDiagnostics(Declarations + R"cc( + EXPECT_TRUE(checkDiagnostics(R"cc( template <int I0, typename T1, typename T2> struct Struct3ArgWithInt {}; @@ -1685,7 +1672,7 @@ )cc")); // Nested template arguments. - EXPECT_TRUE(checkDiagnostics(Declarations + R"cc( + EXPECT_TRUE(checkDiagnostics(R"cc( template <typename T0, typename T1> struct Struct2Arg {}; @@ -1708,7 +1695,7 @@ )cc")); // Struct with two template parameters substituted with concrete structs. - EXPECT_TRUE(checkDiagnostics(Declarations + R"cc( + EXPECT_TRUE(checkDiagnostics(R"cc( struct StructUnknownNullable { int* unknown; int* _Nullable nullable; @@ -1739,7 +1726,7 @@ } )cc")); - EXPECT_TRUE(checkDiagnostics(Declarations + R"cc( + EXPECT_TRUE(checkDiagnostics(R"cc( template <typename T0, typename T1> struct Struct2Arg { T0 arg0; @@ -1793,7 +1780,7 @@ } )cc")); - EXPECT_TRUE(checkDiagnostics(Declarations + R"cc( + EXPECT_TRUE(checkDiagnostics(R"cc( void target(int* _Nullable p, int* _Nonnull q, int* r) { __assert_nullability<NK_nonnull, NK_nullable>(&p); __assert_nullability<NK_nonnull, NK_nonnull>(&q); @@ -1974,15 +1961,6 @@ // Call expression with template parameter substituted with a concrete struct. EXPECT_TRUE(checkDiagnostics(R"cc( - enum NullabilityKind { - NK_nonnull, - NK_nullable, - NK_unspecified, - }; - - template <NullabilityKind... NK, typename T> - void __assert_nullability(const T &); - struct StructUnknownNullable { int *var0; int *_Nullable var1;