steakhal wrote:

This is what I had in mind to avoid this unittest-only test case:
```c++
class UnsafeBufferUsageRoundtripTest : public UnsafeBufferUsageTest {
public:
  using PathString = llvm::SmallString<128>;

protected:
  llvm::SmallString<128> TestDir;

  void SetUp() override {
    std::error_code EC = llvm::sys::fs::createUniqueDirectory(
        "unsafe-buffers-usage-test", TestDir);
    ASSERT_FALSE(EC) << "Failed to create temp directory: " << EC.message();
  }

  void TearDown() override { llvm::sys::fs::remove_directories(TestDir); }
};

TEST_F(UnsafeBufferUsageRoundtripTest, UnsafeBufferUsageSerializeTest) {
  auto Sum = setUpTest(R"cpp(
    void foo(int ***p, int ****q, int x) {
      p[5][5][5];
      q[5][5][5][5];
    }
  )cpp",
                       "foo");
  ASSERT_NE(Sum, nullptr);
  EXPECT_EQ(*Sum, makeSet(__LINE__, {{"p", 1U},
                                     {"p", 2U},
                                     {"p", 3U},
                                     {"q", 1U},
                                     {"q", 2U},
                                     {"q", 3U},
                                     {"q", 4U}}));
  const UnsafeBufferUsageEntitySummary OriginalEntitySummary = *Sum;

  auto Fmt = ssaf::makeFormat("json");
  ASSERT_TRUE(Fmt);

  BuildNamespace NS(BuildNamespaceKind::CompilationUnit, "Mock.cpp");
  TUSummary Summary(NS);
  TUSummaryBuilder Builder(Summary);

  // Fill up this empty TUSummary with this single entity summary.
  auto FooName = getEntityName(findFnByName("foo", AST->getASTContext()));
  ASSERT_TRUE(FooName.has_value());
  EntityId FooId = Builder.addEntity(FooName.value());
  Builder.addSummary(FooId, std::move(Sum));

  // Add Linkage to this entity.
  getLinkageTable(Summary).emplace(FooId, EntityLinkageType::External);

  // Write it to a file.
  std::string OutputPath = (TestDir + "/output.summary.json").str();
  llvm::Error Err = Fmt->writeTUSummary(Summary, OutputPath);
  ASSERT_THAT_ERROR(std::move(Err), llvm::Succeeded());

  // Load it back from the file.
  llvm::Expected<TUSummary> LoadedSummary = Fmt->readTUSummary(OutputPath);
  ASSERT_THAT_ERROR(LoadedSummary.takeError(), llvm::Succeeded());

  // Dig up the single entity summary we saved there.
  const auto &DataMap = getData(*LoadedSummary);
  auto DataIt = DataMap.find(UnsafeBufferUsageEntitySummary::summaryName());
  ASSERT_NE(DataIt, DataMap.end());
  const auto &EntitySumms = DataIt->second;
  ASSERT_EQ(EntitySumms.size(), 1U);
  const auto &LoadedEntitySummary =
      static_cast<const UnsafeBufferUsageEntitySummary &>(
          *EntitySumms.begin()->second.get());

  // Check if it's equal to the original entity summary.
  ASSERT_EQ(LoadedEntitySummary, OriginalEntitySummary);
}
```

It's more verbose than what I thought it would be but all-in-all this would 
save you from creating this test-only header, create the definition to that 
function. This works, compiles and passes. I've checked.

https://github.com/llvm/llvm-project/pull/187156
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to