2015-02-04 08:15:12 +08:00
|
|
|
//===- unittest/ProfileData/CoverageMappingTest.cpp -------------------------=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-04-30 02:53:05 +08:00
|
|
|
#include "llvm/ProfileData/Coverage/CoverageMapping.h"
|
|
|
|
#include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
|
|
|
|
#include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
|
2015-02-19 02:01:14 +08:00
|
|
|
#include "llvm/ProfileData/InstrProfReader.h"
|
|
|
|
#include "llvm/ProfileData/InstrProfWriter.h"
|
2015-02-04 08:15:12 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2016-04-18 17:17:29 +08:00
|
|
|
#include <ostream>
|
2015-02-04 19:19:16 +08:00
|
|
|
|
2015-02-04 08:15:12 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace coverage;
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
static ::testing::AssertionResult NoError(Error E) {
|
|
|
|
if (!E)
|
2015-02-18 05:33:43 +08:00
|
|
|
return ::testing::AssertionSuccess();
|
2016-05-19 11:54:45 +08:00
|
|
|
return ::testing::AssertionFailure() << "error: " << toString(std::move(E))
|
|
|
|
<< "\n";
|
2015-02-18 05:33:43 +08:00
|
|
|
}
|
|
|
|
|
2015-02-04 08:15:12 +08: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-19 02:01:14 +08: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 08:15:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-04-15 22:46:31 +08:00
|
|
|
struct OutputFunctionCoverageData {
|
2015-02-19 02:01:14 +08:00
|
|
|
StringRef Name;
|
|
|
|
uint64_t Hash;
|
|
|
|
std::vector<StringRef> Filenames;
|
2016-04-15 22:46:31 +08:00
|
|
|
std::vector<CounterMappingRegion> Regions;
|
2015-02-19 02:01:14 +08:00
|
|
|
|
2016-04-15 22:46:31 +08:00
|
|
|
void fillCoverageMappingRecord(CoverageMappingRecord &Record) const {
|
2015-02-19 02:01:14 +08:00
|
|
|
Record.FunctionName = Name;
|
|
|
|
Record.FunctionHash = Hash;
|
|
|
|
Record.Filenames = Filenames;
|
|
|
|
Record.Expressions = {};
|
|
|
|
Record.MappingRegions = Regions;
|
2016-04-15 22:46:31 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CoverageMappingReaderMock : CoverageMappingReader {
|
|
|
|
ArrayRef<OutputFunctionCoverageData> Functions;
|
|
|
|
|
|
|
|
CoverageMappingReaderMock(ArrayRef<OutputFunctionCoverageData> Functions)
|
|
|
|
: Functions(Functions) {}
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Error readNextRecord(CoverageMappingRecord &Record) override {
|
2016-04-15 22:46:31 +08:00
|
|
|
if (Functions.empty())
|
2016-05-19 11:54:45 +08:00
|
|
|
return make_error<CoverageMapError>(coveragemap_error::eof);
|
2016-04-15 22:46:31 +08:00
|
|
|
|
|
|
|
Functions.front().fillCoverageMappingRecord(Record);
|
|
|
|
Functions = Functions.slice(1);
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
return Error::success();
|
2015-02-19 02:01:14 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-15 22:46:31 +08: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) {}
|
|
|
|
};
|
|
|
|
|
2015-02-18 05:33:43 +08:00
|
|
|
struct CoverageMappingTest : ::testing::Test {
|
|
|
|
StringMap<unsigned> Files;
|
2016-04-15 22:46:31 +08:00
|
|
|
std::vector<InputFunctionCoverageData> InputFunctions;
|
|
|
|
std::vector<OutputFunctionCoverageData> OutputFunctions;
|
2015-02-17 21:18:43 +08:00
|
|
|
|
2015-02-19 02:01:14 +08:00
|
|
|
InstrProfWriter ProfileWriter;
|
|
|
|
std::unique_ptr<IndexedInstrProfReader> ProfileReader;
|
|
|
|
|
|
|
|
std::unique_ptr<CoverageMapping> LoadedCoverage;
|
|
|
|
|
2015-02-18 05:33:43 +08:00
|
|
|
void SetUp() override {
|
2016-01-30 06:54:45 +08:00
|
|
|
ProfileWriter.setOutputSparse(false);
|
2015-02-18 05:33:43 +08:00
|
|
|
}
|
2015-02-04 08:15:12 +08:00
|
|
|
|
2016-04-15 22:46:31 +08:00
|
|
|
unsigned getGlobalFileIndex(StringRef Name) {
|
2015-02-18 05:33:43 +08:00
|
|
|
auto R = Files.find(Name);
|
|
|
|
if (R != Files.end())
|
|
|
|
return R->second;
|
2016-04-14 18:43:37 +08:00
|
|
|
unsigned Index = Files.size();
|
|
|
|
Files.emplace_second(Name, Index);
|
|
|
|
return Index;
|
2015-02-17 14:56:49 +08:00
|
|
|
}
|
|
|
|
|
2016-04-15 22:46:31 +08:00
|
|
|
// Return the file index of file 'Name' for the current function.
|
|
|
|
// Add the file into the global map if necesary.
|
|
|
|
// 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-18 05:33:43 +08:00
|
|
|
void addCMR(Counter C, StringRef File, unsigned LS, unsigned CS, unsigned LE,
|
|
|
|
unsigned CE) {
|
2016-04-15 22:46:31 +08:00
|
|
|
InputFunctions.back().Regions.push_back(CounterMappingRegion::makeRegion(
|
|
|
|
C, getFileIndexForFunction(File), LS, CS, LE, CE));
|
2015-02-18 05:33:43 +08:00
|
|
|
}
|
2015-02-17 14:56:49 +08:00
|
|
|
|
2015-02-18 05:33:43 +08:00
|
|
|
void addExpansionCMR(StringRef File, StringRef ExpandedFile, unsigned LS,
|
|
|
|
unsigned CS, unsigned LE, unsigned CE) {
|
2016-04-15 22:46:31 +08:00
|
|
|
InputFunctions.back().Regions.push_back(CounterMappingRegion::makeExpansion(
|
|
|
|
getFileIndexForFunction(File), getFileIndexForFunction(ExpandedFile),
|
|
|
|
LS, CS, LE, CE));
|
2015-02-18 05:33:43 +08:00
|
|
|
}
|
|
|
|
|
2016-04-15 22:46:31 +08: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-18 05:33:43 +08:00
|
|
|
std::string Coverage;
|
|
|
|
llvm::raw_string_ostream OS(Coverage);
|
2016-04-15 22:46:31 +08:00
|
|
|
CoverageMappingWriter(FileIDs, None, Data.Regions).write(OS);
|
2015-02-18 05:33:43 +08:00
|
|
|
return OS.str();
|
|
|
|
}
|
|
|
|
|
2016-04-15 22:46:31 +08:00
|
|
|
void readCoverageRegions(std::string Coverage,
|
|
|
|
OutputFunctionCoverageData &Data) {
|
2016-04-14 18:43:37 +08:00
|
|
|
SmallVector<StringRef, 8> Filenames(Files.size());
|
2015-02-18 05:33:43 +08:00
|
|
|
for (const auto &E : Files)
|
2016-04-14 18:43:37 +08:00
|
|
|
Filenames[E.getValue()] = E.getKey();
|
2016-04-15 22:46:31 +08:00
|
|
|
std::vector<CounterExpression> Expressions;
|
|
|
|
RawCoverageMappingReader Reader(Coverage, Filenames, Data.Filenames,
|
|
|
|
Expressions, Data.Regions);
|
2015-02-18 05:33:43 +08:00
|
|
|
ASSERT_TRUE(NoError(Reader.read()));
|
|
|
|
}
|
2015-02-19 02:01:14 +08:00
|
|
|
|
2016-04-15 22:46:31 +08: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-19 02:01:14 +08:00
|
|
|
void readProfCounts() {
|
|
|
|
auto Profile = ProfileWriter.writeBuffer();
|
|
|
|
auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
|
2016-05-19 11:54:45 +08:00
|
|
|
ASSERT_TRUE(NoError(ReaderOrErr.takeError()));
|
2015-02-19 02:01:14 +08:00
|
|
|
ProfileReader = std::move(ReaderOrErr.get());
|
|
|
|
}
|
|
|
|
|
2016-04-15 22:46:31 +08:00
|
|
|
void loadCoverageMapping(bool EmitFilenames = true) {
|
|
|
|
readProfCounts();
|
|
|
|
writeAndReadCoverageRegions(EmitFilenames);
|
2015-02-19 02:01:14 +08:00
|
|
|
|
2016-04-15 22:46:31 +08:00
|
|
|
CoverageMappingReaderMock CovReader(OutputFunctions);
|
2015-02-19 02:01:14 +08:00
|
|
|
auto CoverageOrErr = CoverageMapping::load(CovReader, *ProfileReader);
|
2016-05-19 11:54:45 +08:00
|
|
|
ASSERT_TRUE(NoError(CoverageOrErr.takeError()));
|
2015-02-19 02:01:14 +08:00
|
|
|
LoadedCoverage = std::move(CoverageOrErr.get());
|
|
|
|
}
|
2015-02-18 05:33:43 +08:00
|
|
|
};
|
|
|
|
|
2016-01-30 06:54:45 +08:00
|
|
|
struct MaybeSparseCoverageMappingTest
|
|
|
|
: public CoverageMappingTest,
|
|
|
|
public ::testing::WithParamInterface<bool> {
|
|
|
|
void SetUp() {
|
|
|
|
CoverageMappingTest::SetUp();
|
|
|
|
ProfileWriter.setOutputSparse(GetParam());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_P(MaybeSparseCoverageMappingTest, basic_write_read) {
|
2016-04-15 22:46:31 +08:00
|
|
|
startFunction("func", 0x1234);
|
2015-02-18 05:33:43 +08: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 22:46:31 +08: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 08:15:12 +08:00
|
|
|
for (size_t I = 0; I < N; ++I) {
|
2016-04-15 22:46:31 +08: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 08:15:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 18:43:37 +08:00
|
|
|
TEST_P(MaybeSparseCoverageMappingTest,
|
|
|
|
correct_deserialize_for_more_than_two_files) {
|
|
|
|
const char *FileNames[] = {"bar", "baz", "foo"};
|
|
|
|
static const unsigned N = array_lengthof(FileNames);
|
|
|
|
|
2016-04-15 22:46:31 +08:00
|
|
|
startFunction("func", 0x1234);
|
2016-04-14 18:43:37 +08: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 22:46:31 +08:00
|
|
|
writeAndReadCoverageRegions();
|
|
|
|
ASSERT_EQ(1u, OutputFunctions.size());
|
|
|
|
OutputFunctionCoverageData &Output = OutputFunctions.back();
|
2016-04-14 18:43:37 +08:00
|
|
|
|
2016-04-15 22:46:31 +08:00
|
|
|
ASSERT_EQ(N, Output.Regions.size());
|
|
|
|
ASSERT_EQ(N, Output.Filenames.size());
|
2016-04-14 18:43:37 +08:00
|
|
|
|
|
|
|
for (unsigned I = 0; I < N; ++I) {
|
2016-04-15 22:46:31 +08: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 18:43:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(MaybeSparseCoverageMappingTest, load_coverage_for_more_than_two_files) {
|
|
|
|
InstrProfRecord Record("func", 0x1234, {0});
|
2016-05-04 02:49:41 +08:00
|
|
|
NoError(ProfileWriter.addRecord(std::move(Record)));
|
2016-04-14 18:43:37 +08:00
|
|
|
|
|
|
|
const char *FileNames[] = {"bar", "baz", "foo"};
|
|
|
|
static const unsigned N = array_lengthof(FileNames);
|
|
|
|
|
2016-04-15 22:46:31 +08:00
|
|
|
startFunction("func", 0x1234);
|
2016-04-14 18:43:37 +08: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 22:46:31 +08:00
|
|
|
loadCoverageMapping();
|
2016-04-14 18:43:37 +08:00
|
|
|
|
|
|
|
for (unsigned I = 0; I < N; ++I) {
|
|
|
|
CoverageData Data = LoadedCoverage->getCoverageForFile(FileNames[I]);
|
|
|
|
ASSERT_TRUE(!Data.empty());
|
|
|
|
EXPECT_EQ(I, Data.begin()->Line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-15 22:46:31 +08:00
|
|
|
TEST_P(MaybeSparseCoverageMappingTest, load_coverage_for_several_functions) {
|
|
|
|
InstrProfRecord RecordFunc1("func1", 0x1234, {10});
|
2016-05-04 02:49:41 +08:00
|
|
|
NoError(ProfileWriter.addRecord(std::move(RecordFunc1)));
|
2016-04-15 22:46:31 +08:00
|
|
|
InstrProfRecord RecordFunc2("func2", 0x2345, {20});
|
2016-05-04 02:49:41 +08:00
|
|
|
NoError(ProfileWriter.addRecord(std::move(RecordFunc2)));
|
2016-04-15 22:46:31 +08: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);
|
|
|
|
|
|
|
|
loadCoverageMapping();
|
|
|
|
|
|
|
|
const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
|
|
|
|
EXPECT_EQ(2U, std::distance(FunctionRecords.begin(), FunctionRecords.end()));
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseCoverageMappingTest, expansion_gets_first_counter) {
|
2016-04-15 22:46:31 +08:00
|
|
|
startFunction("func", 0x1234);
|
2015-02-18 05:33:43 +08: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 22:46:31 +08: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 08:15:12 +08:00
|
|
|
}
|
|
|
|
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseCoverageMappingTest, basic_coverage_iteration) {
|
2015-09-30 06:13:58 +08:00
|
|
|
InstrProfRecord Record("func", 0x1234, {30, 20, 10, 0});
|
2016-05-04 02:49:41 +08:00
|
|
|
NoError(ProfileWriter.addRecord(std::move(Record)));
|
2015-02-19 02:01:14 +08:00
|
|
|
|
2016-04-15 22:46:31 +08:00
|
|
|
startFunction("func", 0x1234);
|
2015-02-19 02:01:14 +08: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);
|
2016-04-15 22:46:31 +08:00
|
|
|
loadCoverageMapping();
|
2015-02-19 02:01:14 +08: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 08:15:12 +08:00
|
|
|
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseCoverageMappingTest, uncovered_function) {
|
2016-04-15 22:46:31 +08:00
|
|
|
startFunction("func", 0x1234);
|
2015-02-19 02:40:46 +08:00
|
|
|
addCMR(Counter::getZero(), "file1", 1, 2, 3, 4);
|
2016-04-15 22:46:31 +08:00
|
|
|
loadCoverageMapping();
|
2015-02-19 02:40:46 +08: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-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseCoverageMappingTest, uncovered_function_with_mapping) {
|
2016-04-15 22:46:31 +08:00
|
|
|
startFunction("func", 0x1234);
|
2015-05-14 06:03:04 +08:00
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
|
|
|
|
addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
|
2016-04-15 22:46:31 +08:00
|
|
|
loadCoverageMapping();
|
2015-05-14 06:03:04 +08: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-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseCoverageMappingTest, combine_regions) {
|
2015-09-30 06:13:58 +08:00
|
|
|
InstrProfRecord Record("func", 0x1234, {10, 20, 30});
|
2016-05-04 02:49:41 +08:00
|
|
|
NoError(ProfileWriter.addRecord(std::move(Record)));
|
2015-02-19 03:01:06 +08:00
|
|
|
|
2016-04-15 22:46:31 +08:00
|
|
|
startFunction("func", 0x1234);
|
2015-02-19 03:01:06 +08: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);
|
2016-04-15 22:46:31 +08:00
|
|
|
loadCoverageMapping();
|
2015-02-19 03:01:06 +08: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-04-25 17:43:37 +08:00
|
|
|
TEST_P(MaybeSparseCoverageMappingTest,
|
|
|
|
restore_combined_counter_after_nested_region) {
|
|
|
|
InstrProfRecord Record("func", 0x1234, {10, 20, 40});
|
2016-05-04 02:49:41 +08:00
|
|
|
NoError(ProfileWriter.addRecord(std::move(Record)));
|
2016-04-25 17:43:37 +08: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);
|
|
|
|
loadCoverageMapping();
|
|
|
|
|
|
|
|
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 17:39:45 +08:00
|
|
|
// If CodeRegions and ExpansionRegions cover the same area,
|
|
|
|
// only counts of CodeRegions should be used.
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseCoverageMappingTest, dont_combine_expansions) {
|
|
|
|
InstrProfRecord Record1("func", 0x1234, {10, 20});
|
|
|
|
InstrProfRecord Record2("func", 0x1234, {0, 0});
|
2016-05-04 02:49:41 +08:00
|
|
|
NoError(ProfileWriter.addRecord(std::move(Record1)));
|
|
|
|
NoError(ProfileWriter.addRecord(std::move(Record2)));
|
2015-02-19 03:01:06 +08:00
|
|
|
|
2016-04-15 22:46:31 +08:00
|
|
|
startFunction("func", 0x1234);
|
2015-02-19 03:01:06 +08: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);
|
2016-04-15 22:46:31 +08:00
|
|
|
loadCoverageMapping();
|
2015-02-19 03:01:06 +08: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 17:39:45 +08:00
|
|
|
// If an area is covered only by ExpansionRegions, they should be combinated.
|
|
|
|
TEST_P(MaybeSparseCoverageMappingTest, combine_expansions) {
|
|
|
|
InstrProfRecord Record("func", 0x1234, {2, 3, 7});
|
|
|
|
NoError(ProfileWriter.addRecord(std::move(Record)));
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
loadCoverageMapping();
|
|
|
|
|
|
|
|
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-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseCoverageMappingTest, strip_filename_prefix) {
|
|
|
|
InstrProfRecord Record("file1:func", 0x1234, {0});
|
2016-05-04 02:49:41 +08:00
|
|
|
NoError(ProfileWriter.addRecord(std::move(Record)));
|
2015-05-06 07:44:48 +08:00
|
|
|
|
2016-04-15 22:46:31 +08:00
|
|
|
startFunction("file1:func", 0x1234);
|
2015-05-06 07:44:48 +08:00
|
|
|
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
|
2016-04-15 22:46:31 +08:00
|
|
|
loadCoverageMapping();
|
2015-05-06 07:44:48 +08: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-03-28 09:16:12 +08:00
|
|
|
TEST_P(MaybeSparseCoverageMappingTest, strip_unknown_filename_prefix) {
|
|
|
|
InstrProfRecord Record("<unknown>:func", 0x1234, {0});
|
2016-05-04 02:49:41 +08:00
|
|
|
NoError(ProfileWriter.addRecord(std::move(Record)));
|
2016-03-28 09:16:12 +08:00
|
|
|
|
2016-04-15 22:46:31 +08:00
|
|
|
startFunction("<unknown>:func", 0x1234);
|
2016-03-28 09:16:12 +08:00
|
|
|
addCMR(Counter::getCounter(0), "", 1, 1, 9, 9);
|
2016-04-15 22:46:31 +08:00
|
|
|
loadCoverageMapping(/*EmitFilenames=*/false);
|
2016-03-28 09:16:12 +08: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-04-18 23:36:30 +08:00
|
|
|
TEST_P(MaybeSparseCoverageMappingTest, dont_detect_false_instantiations) {
|
|
|
|
InstrProfRecord Record1("foo", 0x1234, {10});
|
|
|
|
InstrProfRecord Record2("bar", 0x2345, {20});
|
2016-05-04 02:49:41 +08:00
|
|
|
NoError(ProfileWriter.addRecord(std::move(Record1)));
|
|
|
|
NoError(ProfileWriter.addRecord(std::move(Record2)));
|
2016-04-18 23:36:30 +08: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);
|
|
|
|
|
|
|
|
loadCoverageMapping();
|
|
|
|
|
|
|
|
std::vector<const FunctionRecord *> Instantiations =
|
|
|
|
LoadedCoverage->getInstantiations("expanded");
|
|
|
|
ASSERT_TRUE(Instantiations.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(MaybeSparseCoverageMappingTest, load_coverage_for_expanded_file) {
|
|
|
|
InstrProfRecord Record("func", 0x1234, {10});
|
2016-05-04 02:49:41 +08:00
|
|
|
NoError(ProfileWriter.addRecord(std::move(Record)));
|
2016-04-18 23:36:30 +08:00
|
|
|
|
|
|
|
startFunction("func", 0x1234);
|
|
|
|
addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
|
|
|
|
addExpansionCMR("main", "expanded", 4, 1, 4, 5);
|
|
|
|
|
|
|
|
loadCoverageMapping();
|
|
|
|
|
|
|
|
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-01-30 06:54:45 +08:00
|
|
|
INSTANTIATE_TEST_CASE_P(MaybeSparse, MaybeSparseCoverageMappingTest,
|
|
|
|
::testing::Bool());
|
|
|
|
|
2015-02-04 08:15:12 +08:00
|
|
|
} // end anonymous namespace
|