From 8280ece0c972db24e51aae5074ca5433002f1071 Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Fri, 9 Apr 2021 11:53:59 -0700 Subject: [Coverage] Support overriding compilation directory When making compilation relocatable, for example in distributed compilation scenarios, we want to set compilation dir to a relative value like `.` but this presents a problem when generating reports because if the file path is relative as well, for example `..`, you may end up writing files outside of the output directory. This change introduces a flag that allows overriding the compilation directory that's stored inside the profile with a different value that is absolute. Differential Revision: https://reviews.llvm.org/D100232 --- llvm/unittests/ProfileData/CoverageMappingTest.cpp | 35 ++++++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'llvm/unittests/ProfileData/CoverageMappingTest.cpp') diff --git a/llvm/unittests/ProfileData/CoverageMappingTest.cpp b/llvm/unittests/ProfileData/CoverageMappingTest.cpp index b10400b..6434474 100644 --- a/llvm/unittests/ProfileData/CoverageMappingTest.cpp +++ b/llvm/unittests/ProfileData/CoverageMappingTest.cpp @@ -900,7 +900,7 @@ INSTANTIATE_TEST_CASE_P(ParameterizedCovMapTest, CoverageMappingTest, std::pair({true, true})),); TEST(CoverageMappingTest, filename_roundtrip) { - std::vector Paths({"", "a", "b", "c", "d", "e"}); + std::vector Paths({"dir", "a", "b", "c", "d", "e"}); for (bool Compress : {false, true}) { std::string EncodedFilenames; @@ -915,8 +915,37 @@ TEST(CoverageMappingTest, filename_roundtrip) { EXPECT_THAT_ERROR(Reader.read(CovMapVersion::CurrentVersion), Succeeded()); ASSERT_EQ(ReadFilenames.size(), Paths.size()); - for (unsigned I = 1; I < Paths.size(); ++I) - ASSERT_TRUE(ReadFilenames[I] == Paths[I]); + for (unsigned I = 1; I < Paths.size(); ++I) { + SmallString<256> P(Paths[0]); + llvm::sys::path::append(P, Paths[I]); + ASSERT_TRUE(ReadFilenames[I] == P); + } + } +} + +TEST(CoverageMappingTest, filename_compilation_dir) { + std::vector Paths({"dir", "a", "b", "c", "d", "e"}); + + for (bool Compress : {false, true}) { + std::string EncodedFilenames; + { + raw_string_ostream OS(EncodedFilenames); + CoverageFilenamesSectionWriter Writer(Paths); + Writer.write(OS, Compress); + } + + StringRef CompilationDir = "out"; + std::vector ReadFilenames; + RawCoverageFilenamesReader Reader(EncodedFilenames, ReadFilenames, + CompilationDir); + EXPECT_THAT_ERROR(Reader.read(CovMapVersion::CurrentVersion), Succeeded()); + + ASSERT_EQ(ReadFilenames.size(), Paths.size()); + for (unsigned I = 1; I < Paths.size(); ++I) { + SmallString<256> P(CompilationDir); + llvm::sys::path::append(P, Paths[I]); + ASSERT_TRUE(ReadFilenames[I] == P); + } } } -- cgit v1.1