Use cc_embed_data for gtests too

Refactor test headers a bit for reuse across tests, without changing what the
tests themselves have to explicitly include.

PiperOrigin-RevId: 587700267
Change-Id: I4568db3a4a943f30e664da0caf9e37d6d2114ebe
diff --git a/nullability/inference/BUILD b/nullability/inference/BUILD
index d778b28..f249345 100644
--- a/nullability/inference/BUILD
+++ b/nullability/inference/BUILD
@@ -34,7 +34,7 @@
         ":collect_evidence",
         ":inference_cc_proto",
         ":slot_fingerprint",
-        "//nullability/test:headers_for_test",
+        "//nullability/test:test_headers",
         "@llvm-project//clang:ast",
         "@llvm-project//clang:ast_matchers",
         "@llvm-project//clang:basic",
@@ -172,7 +172,7 @@
         ":infer_tu",
         ":inference_cc_proto",
         "//nullability:proto_matchers",
-        "//nullability/test:headers_for_test",
+        "//nullability/test:test_headers",
         "@llvm-project//clang:ast",
         "@llvm-project//clang:ast_matchers",
         "@llvm-project//clang:basic",
diff --git a/nullability/inference/collect_evidence_test.cc b/nullability/inference/collect_evidence_test.cc
index 15f0912..402a2fd 100644
--- a/nullability/inference/collect_evidence_test.cc
+++ b/nullability/inference/collect_evidence_test.cc
@@ -10,7 +10,7 @@
 
 #include "nullability/inference/inference.proto.h"
 #include "nullability/inference/slot_fingerprint.h"
-#include "nullability/test/headers_for_test.h"
+#include "nullability/test/test_headers.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclBase.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
@@ -63,9 +63,11 @@
 
 clang::TestInputs getInputsWithAnnotationDefinitions(llvm::StringRef Source) {
   clang::TestInputs Inputs = Source;
-  Inputs.ExtraFiles = headersForTestAsStringMap();
+  for (const auto& Entry :
+       llvm::ArrayRef(test_headers_create(), test_headers_size()))
+    Inputs.ExtraFiles.try_emplace(Entry.name, Entry.data);
   Inputs.ExtraArgs.push_back("-include");
-  Inputs.ExtraArgs.push_back("preamble.h");
+  Inputs.ExtraArgs.push_back("nullability_annotations.h");
   Inputs.ExtraArgs.push_back("-I.");
   return Inputs;
 }
diff --git a/nullability/inference/infer_tu_test.cc b/nullability/inference/infer_tu_test.cc
index 3e152c0..3748beb 100644
--- a/nullability/inference/infer_tu_test.cc
+++ b/nullability/inference/infer_tu_test.cc
@@ -9,7 +9,7 @@
 
 #include "nullability/inference/inference.proto.h"
 #include "nullability/proto_matchers.h"
-#include "nullability/test/headers_for_test.h"
+#include "nullability/test/test_headers.h"
 #include "clang/AST/Decl.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
@@ -47,9 +47,11 @@
 
   void build(llvm::StringRef Code) {
     TestInputs Inputs = Code;
-    Inputs.ExtraFiles = headersForTestAsStringMap();
+    for (const auto &Entry :
+         llvm::ArrayRef(test_headers_create(), test_headers_size()))
+      Inputs.ExtraFiles.try_emplace(Entry.name, Entry.data);
     Inputs.ExtraArgs.push_back("-include");
-    Inputs.ExtraArgs.push_back("preamble.h");
+    Inputs.ExtraArgs.push_back("nullability_annotations.h");
     Inputs.ExtraArgs.push_back("-I.");
     AST.emplace(Inputs);
   }
diff --git a/nullability/test/BUILD b/nullability/test/BUILD
index 597c969..fcb6841 100644
--- a/nullability/test/BUILD
+++ b/nullability/test/BUILD
@@ -11,10 +11,11 @@
     srcs = ["check_diagnostics.cc"],
     hdrs = ["check_diagnostics.h"],
     deps = [
-        ":headers_for_test",
+        ":test_headers",
         "//nullability:pointer_nullability_analysis",
         "//nullability:pointer_nullability_diagnosis",
         "@llvm-project//clang:analysis",
+        "@llvm-project//clang:tooling",
         "@llvm-project//clang/unittests:dataflow_testing_support",
         "@llvm-project//llvm:Support",
         "@llvm-project//llvm:TestingSupport",
@@ -22,19 +23,6 @@
     ],
 )
 
-cc_library(
-    name = "headers_for_test",
-    testonly = 1,
-    srcs = ["headers_for_test.cc"],
-    hdrs = ["headers_for_test.h"],
-    visibility = [
-        "//nullability/inference:__pkg__",
-    ],
-    deps = [
-        "@llvm-project//clang:tooling",
-    ],
-)
-
 cc_test(
     name = "assert_nullability_test",
     srcs = ["assert_nullability_test.cc"],
@@ -312,11 +300,18 @@
 
 cc_embed_data(
     name = "test_headers",
-    srcs = ["nullability_test.h"],
+    srcs = [
+        "nullability_annotations.h",
+        "nullability_test.h",
+        "check_diagnostics_preamble.h",
+        "new",
+        "memory",
+    ],
     outs = [
         "test_headers.cc",
         "test_headers.h",
         "test_headers.o",
     ],
     embedopts = ["--namespace=clang::tidy::nullability"],
+    visibility = ["//nullability:__subpackages__"],
 )
diff --git a/nullability/test/check_diagnostics.cc b/nullability/test/check_diagnostics.cc
index 2ff88ad..70c9a09 100644
--- a/nullability/test/check_diagnostics.cc
+++ b/nullability/test/check_diagnostics.cc
@@ -9,9 +9,11 @@
 
 #include "nullability/pointer_nullability_analysis.h"
 #include "nullability/pointer_nullability_diagnosis.h"
-#include "nullability/test/headers_for_test.h"
+#include "nullability/test/test_headers.h"
 #include "clang/Analysis/CFG.h"
+#include "clang/Tooling/Tooling.h"
 #include "third_party/llvm/llvm-project/clang/unittests/Analysis/FlowSensitive/TestingSupport.h"
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Testing/Support/Error.h"
 #include "third_party/llvm/llvm-project/third-party/unittest/googletest/include/gtest/gtest.h"
@@ -22,6 +24,10 @@
   std::vector<PointerNullabilityDiagnostic> Diagnostics;
   PointerNullabilityDiagnoser Diagnoser = pointerNullabilityDiagnoser();
   bool Failed = false;
+  tooling::FileContentMappings TestHeaders;
+  for (const auto &Entry :
+       llvm::ArrayRef(test_headers_create(), test_headers_size()))
+    TestHeaders.emplace_back(Entry.name, Entry.data);
   EXPECT_THAT_ERROR(
       dataflow::test::checkDataflow<PointerNullabilityAnalysis>(
           dataflow::test::AnalysisInputs<PointerNullabilityAnalysis>(
@@ -36,10 +42,11 @@
                 auto EltDiagnostics = Diagnoser(Elt, Ctx, State);
                 llvm::move(EltDiagnostics, std::back_inserter(Diagnostics));
               })
-              .withASTBuildVirtualMappedFiles(headersForTest())
+              .withASTBuildVirtualMappedFiles(std::move(TestHeaders))
               .withASTBuildArgs({"-fsyntax-only", "-std=c++17",
                                  "-Wno-unused-value", "-Wno-nonnull",
-                                 "-include", "preamble.h", "-I."}),
+                                 "-include", "check_diagnostics_preamble.h",
+                                 "-I."}),
           [&Diagnostics, &Failed](
               const llvm::DenseMap<unsigned, std::string> &Annotations,
               const dataflow::test::AnalysisOutputs &AnalysisData) {
diff --git a/nullability/test/check_diagnostics_preamble.h b/nullability/test/check_diagnostics_preamble.h
new file mode 100644
index 0000000..ad21901
--- /dev/null
+++ b/nullability/test/check_diagnostics_preamble.h
@@ -0,0 +1,19 @@
+// 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
+
+// This header is included in all code passed to checkDiagnostics.
+
+#include "nullability_annotations.h"  // IWYU pragma: export
+
+enum NullabilityKind {
+  NK_nonnull,
+  NK_nullable,
+  NK_unspecified,
+};
+
+template <NullabilityKind... NK, typename T>
+void __assert_nullability(const T &);
+
+template <typename T>
+T value();
diff --git a/nullability/test/headers_for_test.cc b/nullability/test/headers_for_test.cc
deleted file mode 100644
index 74587e2..0000000
--- a/nullability/test/headers_for_test.cc
+++ /dev/null
@@ -1,71 +0,0 @@
-// 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
-
-#include "nullability/test/headers_for_test.h"
-
-namespace clang::tidy::nullability {
-
-constexpr char kPreamble[] = R"cc(
-  enum NullabilityKind {
-    NK_nonnull,
-    NK_nullable,
-    NK_unspecified,
-  };
-
-  template <NullabilityKind... NK, typename T>
-  void __assert_nullability(const T &);
-
-  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(
-  namespace std {
-  struct nothrow_t {
-    explicit nothrow_t() = default;
-  };
-  extern const nothrow_t nothrow;
-  using size_t = decltype(sizeof(int));
-  }  // namespace std
-  void *operator new(std::size_t size, const std::nothrow_t &) noexcept;
-)cc";
-
-constexpr char kMemoryHeader[] = R"cc(
-  namespace std {
-  template <class T>
-  class unique_ptr {
-   public:
-    unique_ptr() {}
-    T &operator*() const;
-  };
-
-  template <class T, class... Args>
-  unique_ptr<T> make_unique(Args &&...args);
-  }  // namespace std
-)cc";
-
-tooling::FileContentMappings headersForTest() {
-  return {{"preamble.h", kPreamble},
-          {"new", kNewHeader},
-          {"memory", kMemoryHeader}};
-}
-
-llvm::StringMap<std::string> headersForTestAsStringMap() {
-  llvm::StringMap<std::string> Result;
-  for (const auto &[Name, Content] : headersForTest()) {
-    Result.insert_or_assign(Name, Content);
-  }
-  return Result;
-}
-
-}  // namespace clang::tidy::nullability
diff --git a/nullability/test/headers_for_test.h b/nullability/test/headers_for_test.h
deleted file mode 100644
index d79be3c..0000000
--- a/nullability/test/headers_for_test.h
+++ /dev/null
@@ -1,22 +0,0 @@
-// 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
-
-#ifndef CRUBIT_NULLABILITY_TEST_HEADERS_FOR_TEST_H_
-#define CRUBIT_NULLABILITY_TEST_HEADERS_FOR_TEST_H_
-
-#include "clang/Tooling/Tooling.h"
-
-namespace clang::tidy::nullability {
-
-/// Returns headers containing mock definitions to be used in tests.
-/// This includes various standard library headers as well as a header
-/// `preamble.h` containing test-specific definitions.
-tooling::FileContentMappings headersForTest();
-
-/// See `headersForTest()`, but the result is returned as a `StringMap`.
-llvm::StringMap<std::string> headersForTestAsStringMap();
-
-}  // namespace clang::tidy::nullability
-
-#endif  // CRUBIT_NULLABILITY_TEST_HEADERS_FOR_TEST_H_
diff --git a/nullability/test/memory b/nullability/test/memory
new file mode 100644
index 0000000..ac4bcba
--- /dev/null
+++ b/nullability/test/memory
@@ -0,0 +1,17 @@
+// 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
+
+// Mock of standard library <memory> header.
+
+namespace std {
+template <class T>
+class unique_ptr {
+public:
+unique_ptr() {}
+T &operator*() const;
+};
+
+template <class T, class... Args>
+unique_ptr<T> make_unique(Args &&...args);
+}  // namespace std
diff --git a/nullability/test/new b/nullability/test/new
new file mode 100644
index 0000000..3394835
--- /dev/null
+++ b/nullability/test/new
@@ -0,0 +1,14 @@
+// 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
+
+// Mock of standard library <new> header.
+
+namespace std {
+struct nothrow_t {
+  explicit nothrow_t() = default;
+};
+extern const nothrow_t nothrow;
+using size_t = decltype(sizeof(int));
+}  // namespace std
+void *operator new(std::size_t size, const std::nothrow_t &) noexcept;
diff --git a/nullability/test/nullability_annotations.h b/nullability/test/nullability_annotations.h
new file mode 100644
index 0000000..6c5af9f
--- /dev/null
+++ b/nullability/test/nullability_annotations.h
@@ -0,0 +1,12 @@
+// 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
+
+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;
diff --git a/nullability/test/nullability_test.h b/nullability/test/nullability_test.h
index dbd2a25..c8512ca 100644
--- a/nullability/test/nullability_test.h
+++ b/nullability/test/nullability_test.h
@@ -25,6 +25,8 @@
 #ifndef CRUBIT_NULLABILITY_TEST_NULLABILITY_TEST_H_
 #define CRUBIT_NULLABILITY_TEST_NULLABILITY_TEST_H_
 
+#include "nullability_annotations.h"  // IWYU pragma: export
+
 namespace preamble_detail {
 template <typename, typename>
 struct require_same;
@@ -68,11 +70,6 @@
 
 ///////////////// Helpers to make writing tests more convenient ////////////////
 
-template <typename T>
-using Nullable [[clang::annotate("Nullable")]] = T;
-template <typename T>
-using Nonnull [[clang::annotate("Nonnull")]] = T;
-
 // Marker annotations for pointer types whose nullability is symbolic.
 // This means we track it as a variable: without assuming a specific value.
 //