2015-02-04 01:15:12 +01:00
|
|
|
//===- unittest/ProfileData/CoverageMappingTest.cpp -------------------------=//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-02-04 01:15:12 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-04-29 20:53:05 +02:00
|
|
|
#include "llvm/ProfileData/Coverage/CoverageMapping.h"
|
|
|
|
#include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
|
|
|
|
#include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
|
2015-02-18 19:01:14 +01:00
|
|
|
#include "llvm/ProfileData/InstrProfReader.h"
|
|
|
|
#include "llvm/ProfileData/InstrProfWriter.h"
|
2015-02-04 01:15:12 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-07-07 23:02:59 +02:00
|
|
|
#include "llvm/Testing/Support/Error.h"
|
|
|
|
#include "llvm/Testing/Support/SupportHelpers.h"
|
2015-02-04 01:15:12 +01:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2016-04-18 11:17:29 +02:00
|
|
|
#include <ostream>
|
2016-10-14 19:16:53 +02:00
|
|
|
#include <utility>
|
2015-02-04 12:19:16 +01:00
|
|
|
|
2015-02-04 01:15:12 +01:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace coverage;
|
|
|
|
|
2017-07-07 23:02:59 +02:00
|
|
|
LLVM_NODISCARD static ::testing::AssertionResult
|
|
|
|
ErrorEquals(coveragemap_error Expected, Error E) {
|
2017-06-20 04:05:35 +02:00
|
|
|
coveragemap_error Found;
|
|
|
|
std::string FoundMsg;
|
|
|
|
handleAllErrors(std::move(E), [&](const CoverageMapError &CME) {
|
|
|
|
Found = CME.get();
|
|
|
|
FoundMsg = CME.message();
|
|
|
|
});
|
|
|
|
if (Expected == Found)
|
|
|
|
return ::testing::AssertionSuccess();
|
|
|
|
return ::testing::AssertionFailure() << "error: " << FoundMsg << "\n";
|
|
|
|
}
|
|
|
|
|
2015-02-04 01:15:12 +01:00
|
|
|
namespace llvm {
|
|
|
|
namespace coverage {
|
|
|
|
void PrintTo(const Counter &C, ::std::ostream *os) {
|
|
|
|
if (C.isZero())
|
|
|
|
*os << "Zero";
|
|
|
|
else if (C.isExpression())
|
|
|
|
*os << "Expression " << C.getExpressionID();
|
|
|
|
else
|
|
|
|
*os << "Counter " << C.getCounterID();
|
|
|
|
}
|
2015-02-18 19:01:14 +01:00
|
|
|
|
|
|
|
void PrintTo(const CoverageSegment &S, ::std::ostream *os) {
|
|
|
|
*os << "CoverageSegment(" << S.Line << ", " << S.Col << ", ";
|
|
|
|
if (S.HasCount)
|
|
|
|
*os << S.Count << ", ";
|
|
|
|
*os << (S.IsRegionEntry ? "true" : "false") << ")";
|
|
|
|
}
|
2015-02-04 01:15:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
struct OutputFunctionCoverageData {
|
2015-02-18 19:01:14 +01:00
|
|
|
StringRef Name;
|
|
|
|
uint64_t Hash;
|
|
|
|
std::vector<StringRef> Filenames;
|
2016-04-15 16:46:31 +02:00
|
|
|
std::vector<CounterMappingRegion> Regions;
|
2015-02-18 19:01:14 +01:00
|
|
|
|
2016-10-13 00:27:54 +02:00
|
|
|
OutputFunctionCoverageData() : Hash(0) {}
|
|
|
|
|
|
|
|
OutputFunctionCoverageData(OutputFunctionCoverageData &&OFCD)
|
|
|
|
: Name(OFCD.Name), Hash(OFCD.Hash), Filenames(std::move(OFCD.Filenames)),
|
|
|
|
Regions(std::move(OFCD.Regions)) {}
|
|
|
|
|
|
|
|
OutputFunctionCoverageData(const OutputFunctionCoverageData &) = delete;
|
|
|
|
OutputFunctionCoverageData &
|
|
|
|
operator=(const OutputFunctionCoverageData &) = delete;
|
|
|
|
OutputFunctionCoverageData &operator=(OutputFunctionCoverageData &&) = delete;
|
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
void fillCoverageMappingRecord(CoverageMappingRecord &Record) const {
|
2015-02-18 19:01:14 +01:00
|
|
|
Record.FunctionName = Name;
|
|
|
|
Record.FunctionHash = Hash;
|
|
|
|
Record.Filenames = Filenames;
|
|
|
|
Record.Expressions = {};
|
|
|
|
Record.MappingRegions = Regions;
|
2016-04-15 16:46:31 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CoverageMappingReaderMock : CoverageMappingReader {
|
|
|
|
ArrayRef<OutputFunctionCoverageData> Functions;
|
|
|
|
|
|
|
|
CoverageMappingReaderMock(ArrayRef<OutputFunctionCoverageData> Functions)
|
|
|
|
: Functions(Functions) {}
|
|
|
|
|
2016-05-19 05:54:45 +02:00
|
|
|
Error readNextRecord(CoverageMappingRecord &Record) override {
|
2016-04-15 16:46:31 +02:00
|
|
|
if (Functions.empty())
|
2016-05-19 05:54:45 +02:00
|
|
|
return make_error<CoverageMapError>(coveragemap_error::eof);
|
2016-04-15 16:46:31 +02:00
|
|
|
|
|
|
|
Functions.front().fillCoverageMappingRecord(Record);
|
|
|
|
Functions = Functions.slice(1);
|
|
|
|
|
2016-05-19 05:54:45 +02:00
|
|
|
return Error::success();
|
2015-02-18 19:01:14 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
struct InputFunctionCoverageData {
|
|
|
|
// Maps the global file index from CoverageMappingTest.Files
|
|
|
|
// to the index of that file within this function. We can't just use
|
|
|
|
// global file indexes here because local indexes have to be dense.
|
|
|
|
// This map is used during serialization to create the virtual file mapping
|
|
|
|
// (from local fileId to global Index) in the head of the per-function
|
|
|
|
// coverage mapping data.
|
|
|
|
SmallDenseMap<unsigned, unsigned> ReverseVirtualFileMapping;
|
|
|
|
std::string Name;
|
|
|
|
uint64_t Hash;
|
|
|
|
std::vector<CounterMappingRegion> Regions;
|
|
|
|
|
|
|
|
InputFunctionCoverageData(std::string Name, uint64_t Hash)
|
|
|
|
: Name(std::move(Name)), Hash(Hash) {}
|
2016-10-13 00:44:50 +02:00
|
|
|
|
|
|
|
InputFunctionCoverageData(InputFunctionCoverageData &&IFCD)
|
|
|
|
: ReverseVirtualFileMapping(std::move(IFCD.ReverseVirtualFileMapping)),
|
|
|
|
Name(std::move(IFCD.Name)), Hash(IFCD.Hash),
|
|
|
|
Regions(std::move(IFCD.Regions)) {}
|
|
|
|
|
|
|
|
InputFunctionCoverageData(const InputFunctionCoverageData &) = delete;
|
|
|
|
InputFunctionCoverageData &
|
|
|
|
operator=(const InputFunctionCoverageData &) = delete;
|
|
|
|
InputFunctionCoverageData &operator=(InputFunctionCoverageData &&) = delete;
|
2016-04-15 16:46:31 +02:00
|
|
|
};
|
|
|
|
|
2016-10-14 19:16:53 +02:00
|
|
|
struct CoverageMappingTest : ::testing::TestWithParam<std::pair<bool, bool>> {
|
|
|
|
bool UseMultipleReaders;
|
2015-02-17 22:33:43 +01:00
|
|
|
StringMap<unsigned> Files;
|
2016-04-15 16:46:31 +02:00
|
|
|
std::vector<InputFunctionCoverageData> InputFunctions;
|
|
|
|
std::vector<OutputFunctionCoverageData> OutputFunctions;
|
2015-02-17 14:18:43 +01:00
|
|
|
|
2015-02-18 19:01:14 +01:00
|
|
|
InstrProfWriter ProfileWriter;
|
|
|
|
std::unique_ptr<IndexedInstrProfReader> ProfileReader;
|
|
|
|
|
|
|
|
std::unique_ptr<CoverageMapping> LoadedCoverage;
|
|
|
|
|
2015-02-17 22:33:43 +01:00
|
|
|
void SetUp() override {
|
2016-10-14 19:16:53 +02:00
|
|
|
ProfileWriter.setOutputSparse(GetParam().first);
|
|
|
|
UseMultipleReaders = GetParam().second;
|
2015-02-17 22:33:43 +01:00
|
|
|
}
|
2015-02-04 01:15:12 +01:00
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
unsigned getGlobalFileIndex(StringRef Name) {
|
2015-02-17 22:33:43 +01:00
|
|
|
auto R = Files.find(Name);
|
|
|
|
if (R != Files.end())
|
|
|
|
return R->second;
|
2016-04-14 12:43:37 +02:00
|
|
|
unsigned Index = Files.size();
|
2016-07-21 15:37:48 +02:00
|
|
|
Files.try_emplace(Name, Index);
|
2016-04-14 12:43:37 +02:00
|
|
|
return Index;
|
2015-02-17 07:56:49 +01:00
|
|
|
}
|
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
// Return the file index of file 'Name' for the current function.
|
2016-11-20 14:31:13 +01:00
|
|
|
// Add the file into the global map if necessary.
|
2016-04-15 16:46:31 +02:00
|
|
|
// See also InputFunctionCoverageData::ReverseVirtualFileMapping
|
|
|
|
// for additional comments.
|
|
|
|
unsigned getFileIndexForFunction(StringRef Name) {
|
|
|
|
unsigned GlobalIndex = getGlobalFileIndex(Name);
|
|
|
|
auto &CurrentFunctionFileMapping =
|
|
|
|
InputFunctions.back().ReverseVirtualFileMapping;
|
|
|
|
auto R = CurrentFunctionFileMapping.find(GlobalIndex);
|
|
|
|
if (R != CurrentFunctionFileMapping.end())
|
|
|
|
return R->second;
|
|
|
|
unsigned IndexInFunction = CurrentFunctionFileMapping.size();
|
|
|
|
CurrentFunctionFileMapping.insert(
|
|
|
|
std::make_pair(GlobalIndex, IndexInFunction));
|
|
|
|
return IndexInFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void startFunction(StringRef FuncName, uint64_t Hash) {
|
|
|
|
InputFunctions.emplace_back(FuncName.str(), Hash);
|
|
|
|
}
|
|
|
|
|
2015-02-17 22:33:43 +01:00
|
|
|
void addCMR(Counter C, StringRef File, unsigned LS, unsigned CS, unsigned LE,
|
2017-09-08 20:44:50 +02:00
|
|
|
unsigned CE, bool Skipped = false) {
|
|
|
|
auto &Regions = InputFunctions.back().Regions;
|
|
|
|
unsigned FileID = getFileIndexForFunction(File);
|
|
|
|
Regions.push_back(
|
|
|
|
Skipped ? CounterMappingRegion::makeSkipped(FileID, LS, CS, LE, CE)
|
|
|
|
: CounterMappingRegion::makeRegion(C, FileID, LS, CS, LE, CE));
|
2015-02-17 22:33:43 +01:00
|
|
|
}
|
2015-02-17 07:56:49 +01:00
|
|
|
|
2015-02-17 22:33:43 +01:00
|
|
|
void addExpansionCMR(StringRef File, StringRef ExpandedFile, unsigned LS,
|
|
|
|
unsigned CS, unsigned LE, unsigned CE) {
|
2016-04-15 16:46:31 +02:00
|
|
|
InputFunctions.back().Regions.push_back(CounterMappingRegion::makeExpansion(
|
|
|
|
getFileIndexForFunction(File), getFileIndexForFunction(ExpandedFile),
|
|
|
|
LS, CS, LE, CE));
|
2015-02-17 22:33:43 +01:00
|
|
|
}
|
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
std::string writeCoverageRegions(InputFunctionCoverageData &Data) {
|
|
|
|
SmallVector<unsigned, 8> FileIDs(Data.ReverseVirtualFileMapping.size());
|
|
|
|
for (const auto &E : Data.ReverseVirtualFileMapping)
|
|
|
|
FileIDs[E.second] = E.first;
|
2015-02-17 22:33:43 +01:00
|
|
|
std::string Coverage;
|
|
|
|
llvm::raw_string_ostream OS(Coverage);
|
2016-04-15 16:46:31 +02:00
|
|
|
CoverageMappingWriter(FileIDs, None, Data.Regions).write(OS);
|
2015-02-17 22:33:43 +01:00
|
|
|
return OS.str();
|
|
|
|
}
|
|
|
|
|
2016-10-13 00:27:52 +02:00
|
|
|
void readCoverageRegions(const std::string &Coverage,
|
2016-04-15 16:46:31 +02:00
|
|
|
OutputFunctionCoverageData &Data) {
|
2016-04-14 12:43:37 +02:00
|
|
|
SmallVector<StringRef, 8> Filenames(Files.size());
|
2015-02-17 22:33:43 +01:00
|
|
|
for (const auto &E : Files)
|
2016-04-14 12:43:37 +02:00
|
|
|
Filenames[E.getValue()] = E.getKey();
|
2016-04-15 16:46:31 +02:00
|
|
|
std::vector<CounterExpression> Expressions;
|
|
|
|
RawCoverageMappingReader Reader(Coverage, Filenames, Data.Filenames,
|
|
|
|
Expressions, Data.Regions);
|
2017-07-07 23:02:59 +02:00
|
|
|
EXPECT_THAT_ERROR(Reader.read(), Succeeded());
|
2015-02-17 22:33:43 +01:00
|
|
|
}
|
2015-02-18 19:01:14 +01:00
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
void writeAndReadCoverageRegions(bool EmitFilenames = true) {
|
|
|
|
OutputFunctions.resize(InputFunctions.size());
|
|
|
|
for (unsigned I = 0; I < InputFunctions.size(); ++I) {
|
|
|
|
std::string Regions = writeCoverageRegions(InputFunctions[I]);
|
|
|
|
readCoverageRegions(Regions, OutputFunctions[I]);
|
|
|
|
OutputFunctions[I].Name = InputFunctions[I].Name;
|
|
|
|
OutputFunctions[I].Hash = InputFunctions[I].Hash;
|
|
|
|
if (!EmitFilenames)
|
|
|
|
OutputFunctions[I].Filenames.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 19:01:14 +01:00
|
|
|
void readProfCounts() {
|
|
|
|
auto Profile = ProfileWriter.writeBuffer();
|
|
|
|
auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
|
2017-07-07 23:02:59 +02:00
|
|
|
EXPECT_THAT_ERROR(ReaderOrErr.takeError(), Succeeded());
|
2015-02-18 19:01:14 +01:00
|
|
|
ProfileReader = std::move(ReaderOrErr.get());
|
|
|
|
}
|
|
|
|
|
2016-10-14 19:16:53 +02:00
|
|
|
Expected<std::unique_ptr<CoverageMapping>> readOutputFunctions() {
|
|
|
|
std::vector<std::unique_ptr<CoverageMappingReader>> CoverageReaders;
|
2017-06-30 02:45:26 +02:00
|
|
|
if (UseMultipleReaders) {
|
|
|
|
for (const auto &OF : OutputFunctions) {
|
|
|
|
ArrayRef<OutputFunctionCoverageData> Funcs(OF);
|
|
|
|
CoverageReaders.push_back(
|
2019-08-15 17:54:37 +02:00
|
|
|
std::make_unique<CoverageMappingReaderMock>(Funcs));
|
2017-06-30 02:45:26 +02:00
|
|
|
}
|
|
|
|
} else {
|
2017-06-30 06:04:44 +02:00
|
|
|
ArrayRef<OutputFunctionCoverageData> Funcs(OutputFunctions);
|
2017-06-30 02:45:26 +02:00
|
|
|
CoverageReaders.push_back(
|
2019-08-15 17:54:37 +02:00
|
|
|
std::make_unique<CoverageMappingReaderMock>(Funcs));
|
2016-10-14 19:16:53 +02:00
|
|
|
}
|
|
|
|
return CoverageMapping::load(CoverageReaders, *ProfileReader);
|
|
|
|
}
|
|
|
|
|
2017-06-20 04:05:35 +02:00
|
|
|
Error loadCoverageMapping(bool EmitFilenames = true) {
|
2016-04-15 16:46:31 +02:00
|
|
|
readProfCounts();
|
|
|
|
writeAndReadCoverageRegions(EmitFilenames);
|
2016-10-14 19:16:53 +02:00
|
|
|
auto CoverageOrErr = readOutputFunctions();
|
2017-06-20 04:05:35 +02:00
|
|
|
if (!CoverageOrErr)
|
|
|
|
return CoverageOrErr.takeError();
|
2015-02-18 19:01:14 +01:00
|
|
|
LoadedCoverage = std::move(CoverageOrErr.get());
|
2017-06-20 04:05:35 +02:00
|
|
|
return Error::success();
|
2015-02-18 19:01:14 +01:00
|
|
|
}
|
2015-02-17 22:33:43 +01:00
|
|
|
};
|
|
|
|
|
2016-10-13 19:22:44 +02:00
|
|
|
TEST_P(CoverageMappingTest, basic_write_read) {
|
2016-04-15 16:46:31 +02:00
|
|
|
startFunction("func", 0x1234);
|
2015-02-17 22:33:43 +01:00
|
|
|
addCMR(Counter::getCounter(0), "foo", 1, 1, 1, 1);
|
|
|
|
addCMR(Counter::getCounter(1), "foo", 2, 1, 2, 2);
|
|
|
|
addCMR(Counter::getZero(), "foo", 3, 1, 3, 4);
|
|
|
|
addCMR(Counter::getCounter(2), "foo", 4, 1, 4, 8);
|
|
|
|
addCMR(Counter::getCounter(3), "bar", 1, 2, 3, 4);
|
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
writeAndReadCoverageRegions();
|
|
|
|
ASSERT_EQ(1u, InputFunctions.size());
|
|
|
|
ASSERT_EQ(1u, OutputFunctions.size());
|
|
|
|
InputFunctionCoverageData &Input = InputFunctions.back();
|
|
|
|
OutputFunctionCoverageData &Output = OutputFunctions.back();
|
|
|
|
|
|
|
|
size_t N = makeArrayRef(Input.Regions).size();
|
|
|
|
ASSERT_EQ(N, Output.Regions.size());
|
2015-02-04 01:15:12 +01:00
|
|
|
for (size_t I = 0; I < N; ++I) {
|
2016-04-15 16:46:31 +02:00
|
|
|
ASSERT_EQ(Input.Regions[I].Count, Output.Regions[I].Count);
|
|
|
|
ASSERT_EQ(Input.Regions[I].FileID, Output.Regions[I].FileID);
|
|
|
|
ASSERT_EQ(Input.Regions[I].startLoc(), Output.Regions[I].startLoc());
|
|
|
|
ASSERT_EQ(Input.Regions[I].endLoc(), Output.Regions[I].endLoc());
|
|
|
|
ASSERT_EQ(Input.Regions[I].Kind, Output.Regions[I].Kind);
|
2015-02-04 01:15:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 19:22:44 +02:00
|
|
|
TEST_P(CoverageMappingTest, correct_deserialize_for_more_than_two_files) {
|
2016-04-14 12:43:37 +02:00
|
|
|
const char *FileNames[] = {"bar", "baz", "foo"};
|
|
|
|
static const unsigned N = array_lengthof(FileNames);
|
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
startFunction("func", 0x1234);
|
2016-04-14 12:43:37 +02:00
|
|
|
for (unsigned I = 0; I < N; ++I)
|
|
|
|
// Use LineStart to hold the index of the file name
|
|
|
|
// in order to preserve that information during possible sorting of CMRs.
|
|
|
|
addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
|
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
writeAndReadCoverageRegions();
|
|
|
|
ASSERT_EQ(1u, OutputFunctions.size());
|
|
|
|
OutputFunctionCoverageData &Output = OutputFunctions.back();
|
2016-04-14 12:43:37 +02:00
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
ASSERT_EQ(N, Output.Regions.size());
|
|
|
|
ASSERT_EQ(N, Output.Filenames.size());
|
2016-04-14 12:43:37 +02:00
|
|
|
|
|
|
|
for (unsigned I = 0; I < N; ++I) {
|
2016-04-15 16:46:31 +02:00
|
|
|
ASSERT_GT(N, Output.Regions[I].FileID);
|
|
|
|
ASSERT_GT(N, Output.Regions[I].LineStart);
|
|
|
|
EXPECT_EQ(FileNames[Output.Regions[I].LineStart],
|
|
|
|
Output.Filenames[Output.Regions[I].FileID]);
|
2016-04-14 12:43:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 05:04:59 +02:00
|
|
|
static const auto Err = [](Error E) { FAIL(); };
|
|
|
|
|
2016-10-13 19:22:44 +02:00
|
|
|
TEST_P(CoverageMappingTest, load_coverage_for_more_than_two_files) {
|
2017-07-10 05:04:59 +02:00
|
|
|
ProfileWriter.addRecord({"func", 0x1234, {0}}, Err);
|
2016-04-14 12:43:37 +02:00
|
|
|
|
|
|
|
const char *FileNames[] = {"bar", "baz", "foo"};
|
|
|
|
static const unsigned N = array_lengthof(FileNames);
|
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
startFunction("func", 0x1234);
|
2016-04-14 12:43:37 +02:00
|
|
|
for (unsigned I = 0; I < N; ++I)
|
|
|
|
// Use LineStart to hold the index of the file name
|
|
|
|
// in order to preserve that information during possible sorting of CMRs.
|
|
|
|
addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
|
|
|
|
|
2017-07-07 23:02:59 +02:00
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
2016-04-14 12:43:37 +02:00
|
|
|
|
|
|
|
for (unsigned I = 0; I < N; ++I) {
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFile(FileNames[I]);
|
|
|
|
ASSERT_TRUE(!Data.empty());
|
|
|
|
EXPECT_EQ(I, Data.begin()->Line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 04:05:35 +02:00
|
|
|
TEST_P(CoverageMappingTest, load_coverage_with_bogus_function_name) {
|
2017-07-10 05:04:59 +02:00
|
|
|
ProfileWriter.addRecord({"", 0x1234, {10}}, Err);
|
2017-06-20 04:05:35 +02:00
|
|
|
startFunction("", 0x1234);
|
|
|
|
addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
|
2017-07-07 23:02:59 +02:00
|
|
|
EXPECT_TRUE(ErrorEquals(coveragemap_error::malformed, loadCoverageMapping()));
|
2017-06-20 04:05:35 +02:00
|
|
|
}
|
|
|
|
|
2016-10-13 19:22:44 +02:00
|
|
|
TEST_P(CoverageMappingTest, load_coverage_for_several_functions) {
|
2017-07-10 05:04:59 +02:00
|
|
|
ProfileWriter.addRecord({"func1", 0x1234, {10}}, Err);
|
|
|
|
ProfileWriter.addRecord({"func2", 0x2345, {20}}, Err);
|
2016-04-15 16:46:31 +02:00
|
|
|
|
|
|
|
startFunction("func1", 0x1234);
|
|
|
|
addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
|
|
|
|
|
|
|
|
startFunction("func2", 0x2345);
|
|
|
|
addCMR(Counter::getCounter(0), "bar", 2, 2, 6, 6);
|
|
|
|
|
2017-07-07 23:02:59 +02:00
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
2016-04-15 16:46:31 +02:00
|
|
|
|
|
|
|
const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
|
2017-03-11 20:38:22 +01:00
|
|
|
EXPECT_EQ(2, std::distance(FunctionRecords.begin(), FunctionRecords.end()));
|
2016-04-15 16:46:31 +02:00
|
|
|
for (const auto &FunctionRecord : FunctionRecords) {
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
ASSERT_EQ(2U, Segments.size());
|
|
|
|
if (FunctionRecord.Name == "func1") {
|
|
|
|
EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
|
|
|
|
EXPECT_EQ(CoverageSegment(5, 5, false), Segments[1]);
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ("func2", FunctionRecord.Name);
|
|
|
|
EXPECT_EQ(CoverageSegment(2, 2, 20, true), Segments[0]);
|
|
|
|
EXPECT_EQ(CoverageSegment(6, 6, false), Segments[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 20:44:50 +02:00
|
|
|
TEST_P(CoverageMappingTest, create_combined_regions) {
|
|
|
|
ProfileWriter.addRecord({"func1", 0x1234, {1, 2, 3}}, Err);
|
|
|
|
startFunction("func1", 0x1234);
|
|
|
|
|
|
|
|
// Given regions which start at the same location, emit a segment for the
|
|
|
|
// last region.
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 2, 2);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 1, 1, 2, 2);
|
|
|
|
addCMR(Counter::getCounter(2), "file1", 1, 1, 2, 2);
|
|
|
|
|
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
|
|
|
const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
|
|
|
|
const auto &FunctionRecord = *FunctionRecords.begin();
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
|
|
|
|
ASSERT_EQ(2U, Segments.size());
|
|
|
|
EXPECT_EQ(CoverageSegment(1, 1, 6, true), Segments[0]);
|
|
|
|
EXPECT_EQ(CoverageSegment(2, 2, false), Segments[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(CoverageMappingTest, skipped_segments_have_no_count) {
|
|
|
|
ProfileWriter.addRecord({"func1", 0x1234, {1}}, Err);
|
|
|
|
startFunction("func1", 0x1234);
|
|
|
|
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 5, 5);
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 5, 1, 5, 5, /*Skipped=*/true);
|
|
|
|
|
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
|
|
|
const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
|
|
|
|
const auto &FunctionRecord = *FunctionRecords.begin();
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
|
|
|
|
ASSERT_EQ(3U, Segments.size());
|
|
|
|
EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
|
|
|
|
EXPECT_EQ(CoverageSegment(5, 1, true), Segments[1]);
|
|
|
|
EXPECT_EQ(CoverageSegment(5, 5, false), Segments[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(CoverageMappingTest, multiple_regions_end_after_parent_ends) {
|
|
|
|
ProfileWriter.addRecord({"func1", 0x1234, {1, 0}}, Err);
|
|
|
|
startFunction("func1", 0x1234);
|
|
|
|
|
|
|
|
// 1| F{ a{
|
|
|
|
// 2|
|
|
|
|
// 3| a} b{ c{
|
|
|
|
// 4|
|
|
|
|
// 5| b}
|
|
|
|
// 6|
|
|
|
|
// 7| c} d{ e{
|
|
|
|
// 8|
|
|
|
|
// 9| d} e} F}
|
2017-09-09 08:19:53 +02:00
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); // < F
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 3, 5); // < a
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 3, 5, 5, 4); // < b
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 3, 5, 7, 3); // < c
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 7, 3, 9, 2); // < d
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 7, 7, 9, 7); // < e
|
2017-09-08 20:44:50 +02:00
|
|
|
|
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
|
|
|
const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
|
|
|
|
const auto &FunctionRecord = *FunctionRecords.begin();
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
|
|
|
|
// Old output (not sorted or unique):
|
|
|
|
// Segment at 1:1 with count 1
|
|
|
|
// Segment at 1:1 with count 1
|
|
|
|
// Segment at 3:5 with count 1
|
|
|
|
// Segment at 3:5 with count 0
|
|
|
|
// Segment at 3:5 with count 1
|
|
|
|
// Segment at 5:4 with count 0
|
|
|
|
// Segment at 7:3 with count 1
|
|
|
|
// Segment at 7:3 with count 0
|
|
|
|
// Segment at 7:7 with count 0
|
|
|
|
// Segment at 9:7 with count 0
|
|
|
|
// Segment at 9:2 with count 1
|
|
|
|
// Top level segment at 9:9
|
|
|
|
|
|
|
|
// New output (sorted and unique):
|
|
|
|
// Segment at 1:1 (count = 1), RegionEntry
|
|
|
|
// Segment at 3:5 (count = 1), RegionEntry
|
|
|
|
// Segment at 5:4 (count = 0)
|
|
|
|
// Segment at 7:3 (count = 0), RegionEntry
|
|
|
|
// Segment at 7:7 (count = 0), RegionEntry
|
|
|
|
// Segment at 9:2 (count = 0)
|
|
|
|
// Segment at 9:7 (count = 1)
|
|
|
|
// Segment at 9:9 (count = 0), Skipped
|
|
|
|
|
|
|
|
ASSERT_EQ(8U, Segments.size());
|
|
|
|
EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
|
|
|
|
EXPECT_EQ(CoverageSegment(3, 5, 1, true), Segments[1]);
|
|
|
|
EXPECT_EQ(CoverageSegment(5, 4, 0, false), Segments[2]);
|
|
|
|
EXPECT_EQ(CoverageSegment(7, 3, 0, true), Segments[3]);
|
|
|
|
EXPECT_EQ(CoverageSegment(7, 7, 0, true), Segments[4]);
|
|
|
|
EXPECT_EQ(CoverageSegment(9, 2, 0, false), Segments[5]);
|
|
|
|
EXPECT_EQ(CoverageSegment(9, 7, 1, false), Segments[6]);
|
|
|
|
EXPECT_EQ(CoverageSegment(9, 9, false), Segments[7]);
|
|
|
|
}
|
|
|
|
|
2017-11-30 01:28:23 +01:00
|
|
|
TEST_P(CoverageMappingTest, multiple_completed_segments_at_same_loc) {
|
|
|
|
ProfileWriter.addRecord({"func1", 0x1234, {0, 1, 2}}, Err);
|
|
|
|
startFunction("func1", 0x1234);
|
|
|
|
|
2017-12-07 01:01:15 +01:00
|
|
|
// PR35495
|
2017-11-30 01:28:23 +01:00
|
|
|
addCMR(Counter::getCounter(1), "file1", 2, 1, 18, 2);
|
2017-12-07 01:01:15 +01:00
|
|
|
addCMR(Counter::getCounter(0), "file1", 8, 10, 14, 6);
|
2017-11-30 01:28:23 +01:00
|
|
|
addCMR(Counter::getCounter(0), "file1", 8, 12, 14, 6);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 9, 1, 14, 6);
|
|
|
|
addCMR(Counter::getCounter(2), "file1", 11, 13, 11, 14);
|
|
|
|
|
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
|
|
|
const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
|
|
|
|
const auto &FunctionRecord = *FunctionRecords.begin();
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
|
2017-12-07 01:01:15 +01:00
|
|
|
ASSERT_EQ(7U, Segments.size());
|
2017-11-30 01:28:23 +01:00
|
|
|
EXPECT_EQ(CoverageSegment(2, 1, 1, true), Segments[0]);
|
2017-12-07 01:01:15 +01:00
|
|
|
EXPECT_EQ(CoverageSegment(8, 10, 0, true), Segments[1]);
|
|
|
|
EXPECT_EQ(CoverageSegment(8, 12, 0, true), Segments[2]);
|
|
|
|
EXPECT_EQ(CoverageSegment(9, 1, 1, true), Segments[3]);
|
|
|
|
EXPECT_EQ(CoverageSegment(11, 13, 2, true), Segments[4]);
|
2017-11-30 01:28:23 +01:00
|
|
|
// Use count=1 (from 9:1 -> 14:6), not count=0 (from 8:12 -> 14:6).
|
2017-12-07 01:01:15 +01:00
|
|
|
EXPECT_EQ(CoverageSegment(11, 14, 1, false), Segments[5]);
|
|
|
|
EXPECT_EQ(CoverageSegment(18, 2, false), Segments[6]);
|
2017-11-30 01:28:23 +01:00
|
|
|
}
|
|
|
|
|
2017-09-08 20:44:50 +02:00
|
|
|
TEST_P(CoverageMappingTest, dont_emit_redundant_segments) {
|
|
|
|
ProfileWriter.addRecord({"func1", 0x1234, {1, 1}}, Err);
|
|
|
|
startFunction("func1", 0x1234);
|
|
|
|
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 4, 4);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 2, 2, 5, 5);
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 3, 3, 6, 6);
|
|
|
|
|
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
|
|
|
const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
|
|
|
|
const auto &FunctionRecord = *FunctionRecords.begin();
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
|
|
|
|
ASSERT_EQ(5U, Segments.size());
|
|
|
|
EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
|
|
|
|
EXPECT_EQ(CoverageSegment(2, 2, 1, true), Segments[1]);
|
|
|
|
EXPECT_EQ(CoverageSegment(3, 3, 1, true), Segments[2]);
|
|
|
|
EXPECT_EQ(CoverageSegment(4, 4, 1, false), Segments[3]);
|
|
|
|
// A closing segment starting at 5:5 would be redundant: it would have the
|
|
|
|
// same count as the segment starting at 4:4, and has all the same metadata.
|
|
|
|
EXPECT_EQ(CoverageSegment(6, 6, false), Segments[4]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(CoverageMappingTest, dont_emit_closing_segment_at_new_region_start) {
|
|
|
|
ProfileWriter.addRecord({"func1", 0x1234, {1}}, Err);
|
|
|
|
startFunction("func1", 0x1234);
|
|
|
|
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 6, 5);
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 2, 2, 6, 5);
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 3, 3, 6, 5);
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 6, 5, 7, 7);
|
|
|
|
|
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
|
|
|
const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
|
|
|
|
const auto &FunctionRecord = *FunctionRecords.begin();
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
|
|
|
|
ASSERT_EQ(5U, Segments.size());
|
|
|
|
EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
|
|
|
|
EXPECT_EQ(CoverageSegment(2, 2, 1, true), Segments[1]);
|
|
|
|
EXPECT_EQ(CoverageSegment(3, 3, 1, true), Segments[2]);
|
|
|
|
EXPECT_EQ(CoverageSegment(6, 5, 1, true), Segments[3]);
|
|
|
|
// The old segment builder would get this wrong by emitting multiple segments
|
|
|
|
// which start at 6:5 (a few of which were skipped segments). We should just
|
|
|
|
// get a segment for the region entry.
|
|
|
|
EXPECT_EQ(CoverageSegment(7, 7, false), Segments[4]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(CoverageMappingTest, handle_consecutive_regions_with_zero_length) {
|
|
|
|
ProfileWriter.addRecord({"func1", 0x1234, {1, 2}}, Err);
|
|
|
|
startFunction("func1", 0x1234);
|
|
|
|
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 1, 1, 1, 1);
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 1, 1, 1, 1);
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
|
|
|
|
|
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
|
|
|
const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
|
|
|
|
const auto &FunctionRecord = *FunctionRecords.begin();
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
|
|
|
|
ASSERT_EQ(1U, Segments.size());
|
|
|
|
EXPECT_EQ(CoverageSegment(1, 1, true), Segments[0]);
|
|
|
|
// We need to get a skipped segment starting at 1:1. In this case there is
|
|
|
|
// also a region entry at 1:1.
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(CoverageMappingTest, handle_sandwiched_zero_length_region) {
|
|
|
|
ProfileWriter.addRecord({"func1", 0x1234, {2, 1}}, Err);
|
|
|
|
startFunction("func1", 0x1234);
|
|
|
|
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 5, 4, 4);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 1, 9, 1, 50);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 2, 7, 2, 34);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 3, 5, 3, 21);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 3, 21, 3, 21);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 4, 12, 4, 17);
|
|
|
|
|
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
|
|
|
const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
|
|
|
|
const auto &FunctionRecord = *FunctionRecords.begin();
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
|
|
|
|
ASSERT_EQ(10U, Segments.size());
|
|
|
|
EXPECT_EQ(CoverageSegment(1, 5, 2, true), Segments[0]);
|
|
|
|
EXPECT_EQ(CoverageSegment(1, 9, 1, true), Segments[1]);
|
|
|
|
EXPECT_EQ(CoverageSegment(1, 50, 2, false), Segments[2]);
|
|
|
|
EXPECT_EQ(CoverageSegment(2, 7, 1, true), Segments[3]);
|
|
|
|
EXPECT_EQ(CoverageSegment(2, 34, 2, false), Segments[4]);
|
|
|
|
EXPECT_EQ(CoverageSegment(3, 5, 1, true), Segments[5]);
|
|
|
|
EXPECT_EQ(CoverageSegment(3, 21, 2, true), Segments[6]);
|
|
|
|
// Handle the zero-length region by creating a segment with its predecessor's
|
|
|
|
// count (i.e the count from 1:5 -> 4:4).
|
|
|
|
EXPECT_EQ(CoverageSegment(4, 4, false), Segments[7]);
|
|
|
|
// The area between 4:4 and 4:12 is skipped.
|
|
|
|
EXPECT_EQ(CoverageSegment(4, 12, 1, true), Segments[8]);
|
|
|
|
EXPECT_EQ(CoverageSegment(4, 17, false), Segments[9]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(CoverageMappingTest, handle_last_completed_region) {
|
|
|
|
ProfileWriter.addRecord({"func1", 0x1234, {1, 2, 3, 4}}, Err);
|
|
|
|
startFunction("func1", 0x1234);
|
|
|
|
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 8, 8);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 2, 2, 5, 5);
|
|
|
|
addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
|
|
|
|
addCMR(Counter::getCounter(3), "file1", 6, 6, 7, 7);
|
|
|
|
|
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
|
|
|
const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
|
|
|
|
const auto &FunctionRecord = *FunctionRecords.begin();
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
|
|
|
|
ASSERT_EQ(8U, Segments.size());
|
|
|
|
EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
|
|
|
|
EXPECT_EQ(CoverageSegment(2, 2, 2, true), Segments[1]);
|
|
|
|
EXPECT_EQ(CoverageSegment(3, 3, 3, true), Segments[2]);
|
|
|
|
EXPECT_EQ(CoverageSegment(4, 4, 2, false), Segments[3]);
|
|
|
|
EXPECT_EQ(CoverageSegment(5, 5, 1, false), Segments[4]);
|
|
|
|
EXPECT_EQ(CoverageSegment(6, 6, 4, true), Segments[5]);
|
|
|
|
EXPECT_EQ(CoverageSegment(7, 7, 1, false), Segments[6]);
|
|
|
|
EXPECT_EQ(CoverageSegment(8, 8, false), Segments[7]);
|
|
|
|
}
|
|
|
|
|
2016-10-13 19:22:44 +02:00
|
|
|
TEST_P(CoverageMappingTest, expansion_gets_first_counter) {
|
2016-04-15 16:46:31 +02:00
|
|
|
startFunction("func", 0x1234);
|
2015-02-17 22:33:43 +01:00
|
|
|
addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2);
|
|
|
|
// This starts earlier in "foo", so the expansion should get its counter.
|
|
|
|
addCMR(Counter::getCounter(2), "foo", 1, 1, 20, 1);
|
|
|
|
addExpansionCMR("bar", "foo", 3, 3, 3, 3);
|
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
writeAndReadCoverageRegions();
|
|
|
|
ASSERT_EQ(1u, OutputFunctions.size());
|
|
|
|
OutputFunctionCoverageData &Output = OutputFunctions.back();
|
|
|
|
|
|
|
|
ASSERT_EQ(CounterMappingRegion::ExpansionRegion, Output.Regions[2].Kind);
|
|
|
|
ASSERT_EQ(Counter::getCounter(2), Output.Regions[2].Count);
|
|
|
|
ASSERT_EQ(3U, Output.Regions[2].LineStart);
|
2015-02-04 01:15:12 +01:00
|
|
|
}
|
|
|
|
|
2016-10-13 19:22:44 +02:00
|
|
|
TEST_P(CoverageMappingTest, basic_coverage_iteration) {
|
2017-07-10 05:04:59 +02:00
|
|
|
ProfileWriter.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err);
|
2015-02-18 19:01:14 +01:00
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
startFunction("func", 0x1234);
|
2015-02-18 19:01:14 +01:00
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
|
|
|
|
addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
|
|
|
|
addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
|
2017-07-07 23:02:59 +02:00
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
2015-02-18 19:01:14 +01:00
|
|
|
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
ASSERT_EQ(7U, Segments.size());
|
|
|
|
ASSERT_EQ(CoverageSegment(1, 1, 20, true), Segments[0]);
|
|
|
|
ASSERT_EQ(CoverageSegment(4, 7, 30, false), Segments[1]);
|
|
|
|
ASSERT_EQ(CoverageSegment(5, 8, 10, true), Segments[2]);
|
|
|
|
ASSERT_EQ(CoverageSegment(9, 1, 30, false), Segments[3]);
|
|
|
|
ASSERT_EQ(CoverageSegment(9, 9, false), Segments[4]);
|
|
|
|
ASSERT_EQ(CoverageSegment(10, 10, 0, true), Segments[5]);
|
|
|
|
ASSERT_EQ(CoverageSegment(11, 11, false), Segments[6]);
|
|
|
|
}
|
2015-02-04 01:15:12 +01:00
|
|
|
|
2017-10-19 01:58:28 +02:00
|
|
|
TEST_P(CoverageMappingTest, test_line_coverage_iterator) {
|
|
|
|
ProfileWriter.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err);
|
|
|
|
|
|
|
|
startFunction("func", 0x1234);
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
|
|
|
|
addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
|
|
|
|
addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
|
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
|
|
|
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
|
|
|
|
|
2017-10-19 08:16:23 +02:00
|
|
|
unsigned Line = 0;
|
2017-11-09 03:33:43 +01:00
|
|
|
unsigned LineCounts[] = {20, 20, 20, 20, 30, 10, 10, 10, 10, 0, 0};
|
2017-10-19 01:58:28 +02:00
|
|
|
for (const auto &LCS : getLineCoverageStats(Data)) {
|
2017-10-19 08:16:23 +02:00
|
|
|
ASSERT_EQ(Line + 1, LCS.getLine());
|
2017-11-09 03:33:43 +01:00
|
|
|
errs() << "Line: " << Line + 1 << ", count = " << LCS.getExecutionCount() << "\n";
|
2017-10-19 08:16:23 +02:00
|
|
|
ASSERT_EQ(LineCounts[Line], LCS.getExecutionCount());
|
|
|
|
++Line;
|
2017-10-19 01:58:28 +02:00
|
|
|
}
|
2017-10-19 08:16:23 +02:00
|
|
|
ASSERT_EQ(11U, Line);
|
2017-10-19 01:58:28 +02:00
|
|
|
}
|
|
|
|
|
2016-10-13 19:22:44 +02:00
|
|
|
TEST_P(CoverageMappingTest, uncovered_function) {
|
2016-04-15 16:46:31 +02:00
|
|
|
startFunction("func", 0x1234);
|
2015-02-18 19:40:46 +01:00
|
|
|
addCMR(Counter::getZero(), "file1", 1, 2, 3, 4);
|
2017-07-07 23:02:59 +02:00
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
2015-02-18 19:40:46 +01:00
|
|
|
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
ASSERT_EQ(2U, Segments.size());
|
|
|
|
ASSERT_EQ(CoverageSegment(1, 2, 0, true), Segments[0]);
|
|
|
|
ASSERT_EQ(CoverageSegment(3, 4, false), Segments[1]);
|
|
|
|
}
|
|
|
|
|
2016-10-13 19:22:44 +02:00
|
|
|
TEST_P(CoverageMappingTest, uncovered_function_with_mapping) {
|
2016-04-15 16:46:31 +02:00
|
|
|
startFunction("func", 0x1234);
|
2015-05-14 00:03:04 +02:00
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
|
2017-07-07 23:02:59 +02:00
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
2015-05-14 00:03:04 +02:00
|
|
|
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
ASSERT_EQ(3U, Segments.size());
|
|
|
|
ASSERT_EQ(CoverageSegment(1, 1, 0, true), Segments[0]);
|
|
|
|
ASSERT_EQ(CoverageSegment(4, 7, 0, false), Segments[1]);
|
|
|
|
ASSERT_EQ(CoverageSegment(9, 9, false), Segments[2]);
|
|
|
|
}
|
|
|
|
|
2016-10-13 19:22:44 +02:00
|
|
|
TEST_P(CoverageMappingTest, combine_regions) {
|
2017-07-10 05:04:59 +02:00
|
|
|
ProfileWriter.addRecord({"func", 0x1234, {10, 20, 30}}, Err);
|
2015-02-18 20:01:06 +01:00
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
startFunction("func", 0x1234);
|
2015-02-18 20:01:06 +01:00
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
|
|
|
|
addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
|
2017-07-07 23:02:59 +02:00
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
2015-02-18 20:01:06 +01:00
|
|
|
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
ASSERT_EQ(4U, Segments.size());
|
|
|
|
ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
|
|
|
|
ASSERT_EQ(CoverageSegment(3, 3, 50, true), Segments[1]);
|
|
|
|
ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
|
|
|
|
ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
|
|
|
|
}
|
|
|
|
|
2016-10-13 19:22:44 +02:00
|
|
|
TEST_P(CoverageMappingTest, restore_combined_counter_after_nested_region) {
|
2017-07-10 05:04:59 +02:00
|
|
|
ProfileWriter.addRecord({"func", 0x1234, {10, 20, 40}}, Err);
|
2016-04-25 11:43:37 +02:00
|
|
|
|
|
|
|
startFunction("func", 0x1234);
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 1, 1, 9, 9);
|
|
|
|
addCMR(Counter::getCounter(2), "file1", 3, 3, 5, 5);
|
2017-07-07 23:02:59 +02:00
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
2016-04-25 11:43:37 +02:00
|
|
|
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
ASSERT_EQ(4U, Segments.size());
|
|
|
|
EXPECT_EQ(CoverageSegment(1, 1, 30, true), Segments[0]);
|
|
|
|
EXPECT_EQ(CoverageSegment(3, 3, 40, true), Segments[1]);
|
|
|
|
EXPECT_EQ(CoverageSegment(5, 5, 30, false), Segments[2]);
|
|
|
|
EXPECT_EQ(CoverageSegment(9, 9, false), Segments[3]);
|
|
|
|
}
|
|
|
|
|
2016-05-05 11:39:45 +02:00
|
|
|
// If CodeRegions and ExpansionRegions cover the same area,
|
|
|
|
// only counts of CodeRegions should be used.
|
2016-10-13 19:22:44 +02:00
|
|
|
TEST_P(CoverageMappingTest, dont_combine_expansions) {
|
2017-07-10 05:04:59 +02:00
|
|
|
ProfileWriter.addRecord({"func", 0x1234, {10, 20}}, Err);
|
|
|
|
ProfileWriter.addRecord({"func", 0x1234, {0, 0}}, Err);
|
2015-02-18 20:01:06 +01:00
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
startFunction("func", 0x1234);
|
2015-02-18 20:01:06 +01:00
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
|
|
|
|
addCMR(Counter::getCounter(1), "include1", 6, 6, 7, 7);
|
|
|
|
addExpansionCMR("file1", "include1", 3, 3, 4, 4);
|
2017-07-07 23:02:59 +02:00
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
2015-02-18 20:01:06 +01:00
|
|
|
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
ASSERT_EQ(4U, Segments.size());
|
|
|
|
ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
|
|
|
|
ASSERT_EQ(CoverageSegment(3, 3, 20, true), Segments[1]);
|
|
|
|
ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
|
|
|
|
ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
|
|
|
|
}
|
|
|
|
|
2016-05-05 11:39:45 +02:00
|
|
|
// If an area is covered only by ExpansionRegions, they should be combinated.
|
2016-10-13 19:22:44 +02:00
|
|
|
TEST_P(CoverageMappingTest, combine_expansions) {
|
2017-07-10 05:04:59 +02:00
|
|
|
ProfileWriter.addRecord({"func", 0x1234, {2, 3, 7}}, Err);
|
2016-05-05 11:39:45 +02:00
|
|
|
|
|
|
|
startFunction("func", 0x1234);
|
|
|
|
addCMR(Counter::getCounter(1), "include1", 1, 1, 1, 10);
|
|
|
|
addCMR(Counter::getCounter(2), "include2", 1, 1, 1, 10);
|
|
|
|
addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5);
|
|
|
|
addExpansionCMR("file", "include1", 3, 1, 3, 5);
|
|
|
|
addExpansionCMR("file", "include2", 3, 1, 3, 5);
|
|
|
|
|
2017-07-07 23:02:59 +02:00
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
2016-05-05 11:39:45 +02:00
|
|
|
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFile("file");
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
ASSERT_EQ(4U, Segments.size());
|
|
|
|
EXPECT_EQ(CoverageSegment(1, 1, 2, true), Segments[0]);
|
|
|
|
EXPECT_EQ(CoverageSegment(3, 1, 10, true), Segments[1]);
|
|
|
|
EXPECT_EQ(CoverageSegment(3, 5, 2, false), Segments[2]);
|
|
|
|
EXPECT_EQ(CoverageSegment(5, 5, false), Segments[3]);
|
|
|
|
}
|
|
|
|
|
2016-10-13 19:22:44 +02:00
|
|
|
TEST_P(CoverageMappingTest, strip_filename_prefix) {
|
2017-07-10 05:04:59 +02:00
|
|
|
ProfileWriter.addRecord({"file1:func", 0x1234, {0}}, Err);
|
2015-05-06 01:44:48 +02:00
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
startFunction("file1:func", 0x1234);
|
2015-05-06 01:44:48 +02:00
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
|
2017-07-07 23:02:59 +02:00
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
2015-05-06 01:44:48 +02:00
|
|
|
|
|
|
|
std::vector<std::string> Names;
|
|
|
|
for (const auto &Func : LoadedCoverage->getCoveredFunctions())
|
|
|
|
Names.push_back(Func.Name);
|
|
|
|
ASSERT_EQ(1U, Names.size());
|
|
|
|
ASSERT_EQ("func", Names[0]);
|
|
|
|
}
|
|
|
|
|
2016-10-13 19:22:44 +02:00
|
|
|
TEST_P(CoverageMappingTest, strip_unknown_filename_prefix) {
|
2017-07-10 05:04:59 +02:00
|
|
|
ProfileWriter.addRecord({"<unknown>:func", 0x1234, {0}}, Err);
|
2016-03-28 03:16:12 +02:00
|
|
|
|
2016-04-15 16:46:31 +02:00
|
|
|
startFunction("<unknown>:func", 0x1234);
|
2016-03-28 03:16:12 +02:00
|
|
|
addCMR(Counter::getCounter(0), "", 1, 1, 9, 9);
|
2017-07-07 23:02:59 +02:00
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(/*EmitFilenames=*/false), Succeeded());
|
2016-03-28 03:16:12 +02:00
|
|
|
|
|
|
|
std::vector<std::string> Names;
|
|
|
|
for (const auto &Func : LoadedCoverage->getCoveredFunctions())
|
|
|
|
Names.push_back(Func.Name);
|
|
|
|
ASSERT_EQ(1U, Names.size());
|
|
|
|
ASSERT_EQ("func", Names[0]);
|
|
|
|
}
|
|
|
|
|
2016-10-13 19:22:44 +02:00
|
|
|
TEST_P(CoverageMappingTest, dont_detect_false_instantiations) {
|
2017-07-10 05:04:59 +02:00
|
|
|
ProfileWriter.addRecord({"foo", 0x1234, {10}}, Err);
|
|
|
|
ProfileWriter.addRecord({"bar", 0x2345, {20}}, Err);
|
2016-04-18 17:36:30 +02:00
|
|
|
|
|
|
|
startFunction("foo", 0x1234);
|
|
|
|
addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
|
|
|
|
addExpansionCMR("main", "expanded", 4, 1, 4, 5);
|
|
|
|
|
|
|
|
startFunction("bar", 0x2345);
|
|
|
|
addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
|
|
|
|
addExpansionCMR("main", "expanded", 9, 1, 9, 5);
|
|
|
|
|
2017-07-07 23:02:59 +02:00
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
2016-04-18 17:36:30 +02:00
|
|
|
|
2017-08-03 01:35:25 +02:00
|
|
|
std::vector<InstantiationGroup> InstantiationGroups =
|
|
|
|
LoadedCoverage->getInstantiationGroups("expanded");
|
|
|
|
for (const auto &Group : InstantiationGroups)
|
|
|
|
ASSERT_EQ(Group.size(), 1U);
|
2016-04-18 17:36:30 +02:00
|
|
|
}
|
|
|
|
|
2016-10-13 19:22:44 +02:00
|
|
|
TEST_P(CoverageMappingTest, load_coverage_for_expanded_file) {
|
2017-07-10 05:04:59 +02:00
|
|
|
ProfileWriter.addRecord({"func", 0x1234, {10}}, Err);
|
2016-04-18 17:36:30 +02:00
|
|
|
|
|
|
|
startFunction("func", 0x1234);
|
|
|
|
addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
|
|
|
|
addExpansionCMR("main", "expanded", 4, 1, 4, 5);
|
|
|
|
|
2017-07-07 23:02:59 +02:00
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
2016-04-18 17:36:30 +02:00
|
|
|
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFile("expanded");
|
|
|
|
std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
|
|
|
|
ASSERT_EQ(2U, Segments.size());
|
|
|
|
EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
|
|
|
|
EXPECT_EQ(CoverageSegment(1, 10, false), Segments[1]);
|
|
|
|
}
|
|
|
|
|
2016-10-14 19:16:53 +02:00
|
|
|
TEST_P(CoverageMappingTest, skip_duplicate_function_record) {
|
2017-07-10 05:04:59 +02:00
|
|
|
ProfileWriter.addRecord({"func", 0x1234, {1}}, Err);
|
2016-10-14 19:16:53 +02:00
|
|
|
|
2018-05-08 21:26:51 +02:00
|
|
|
// This record should be loaded.
|
2016-10-14 19:16:53 +02:00
|
|
|
startFunction("func", 0x1234);
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
|
|
|
|
|
2018-05-08 21:26:51 +02:00
|
|
|
// This record should be loaded.
|
2016-10-14 19:16:53 +02:00
|
|
|
startFunction("func", 0x1234);
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
|
2018-05-08 21:26:51 +02:00
|
|
|
addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
|
|
|
|
|
|
|
|
// This record should be skipped.
|
|
|
|
startFunction("func", 0x1234);
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
|
|
|
|
|
|
|
|
// This record should be loaded.
|
|
|
|
startFunction("func", 0x1234);
|
|
|
|
addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
|
|
|
|
|
|
|
|
// This record should be skipped.
|
|
|
|
startFunction("func", 0x1234);
|
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
|
|
|
|
addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
|
2016-10-14 19:16:53 +02:00
|
|
|
|
2017-07-07 23:02:59 +02:00
|
|
|
EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
|
2016-10-14 19:16:53 +02:00
|
|
|
|
|
|
|
auto Funcs = LoadedCoverage->getCoveredFunctions();
|
|
|
|
unsigned NumFuncs = std::distance(Funcs.begin(), Funcs.end());
|
2018-05-08 21:26:51 +02:00
|
|
|
ASSERT_EQ(3U, NumFuncs);
|
2016-10-14 19:16:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Use ::testing::Combine() when llvm updates its copy of googletest.
|
2016-10-13 19:22:44 +02:00
|
|
|
INSTANTIATE_TEST_CASE_P(ParameterizedCovMapTest, CoverageMappingTest,
|
2016-10-14 19:16:53 +02:00
|
|
|
::testing::Values(std::pair<bool, bool>({false, false}),
|
|
|
|
std::pair<bool, bool>({false, true}),
|
|
|
|
std::pair<bool, bool>({true, false}),
|
2017-06-04 07:30:26 +02:00
|
|
|
std::pair<bool, bool>({true, true})),);
|
2016-01-29 23:54:45 +01:00
|
|
|
|
2015-02-04 01:15:12 +01:00
|
|
|
} // end anonymous namespace
|