forked from OpenCppCoverage/OpenCppCoverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoverageDataMerger.cpp
176 lines (149 loc) · 5.68 KB
/
CoverageDataMerger.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// OpenCppCoverage is an open source code coverage for C++.
// Copyright (C) 2014 OpenCppCoverage
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "stdafx.h"
#include "CoverageDataMerger.hpp"
#include <functional>
#include "Plugin/Exporter/CoverageData.hpp"
#include "Plugin/Exporter/ModuleCoverage.hpp"
#include "Plugin/Exporter/FileCoverage.hpp"
#include "Plugin/Exporter/LineCoverage.hpp"
namespace fs = std::filesystem;
namespace CppCoverage
{
namespace
{
//---------------------------------------------------------------------
Plugin::CoverageData CreateCoverageData(const std::vector<Plugin::CoverageData>& coverageDataCollection)
{
std::wstring name;
int lastNotZeroExitCode = 0;
for (const auto& coverageData : coverageDataCollection)
{
name = coverageData.GetName();
auto exitCode = coverageData.GetExitCode();
if (exitCode)
lastNotZeroExitCode = exitCode;
}
return Plugin::CoverageData{ name, lastNotZeroExitCode };
}
//---------------------------------------------------------------------
template <typename Object, typename Key, typename Child>
std::map<Key, std::vector<Child*>> GroupChildrenByKey(
const std::vector<Object>& collection,
const std::function<const std::vector<std::unique_ptr<Child>>& (const Object&)>& getChildren,
const std::function<const Key& (const Child&)>& getKey)
{
std::map<Key, std::vector<Child*>> childrenByKey;
for (const auto& object : collection)
{
for (const auto& child : getChildren(object))
{
const auto& key = getKey(*child);
childrenByKey[key].push_back(child.get());
}
}
return childrenByKey;
}
//---------------------------------------------------------------------
void AddFileCoverageTo(
const Plugin::FileCoverage* sourceFile,
Plugin::FileCoverage* destinationFile)
{
if (sourceFile && destinationFile)
{
for (const auto& line : sourceFile->GetLines())
{
auto lineNumber = line.GetLineNumber();
auto hasBeenExecuted = line.HasBeenExecuted();
if (!(*destinationFile)[lineNumber])
destinationFile->AddLine(lineNumber, hasBeenExecuted);
else if (hasBeenExecuted)
destinationFile->UpdateLine(lineNumber, true);
}
}
}
//---------------------------------------------------------------------
void FillFiles(
Plugin::FileCoverage& file,
const std::vector<Plugin::FileCoverage*>& files)
{
for (const auto& f : files)
AddFileCoverageTo(f, &file);
}
//---------------------------------------------------------------------
void FillModule(
Plugin::ModuleCoverage& module,
const std::vector<Plugin::ModuleCoverage*>& modules)
{
std::map<fs::path, std::vector<Plugin::FileCoverage*>> filesByPath =
GroupChildrenByKey<Plugin::ModuleCoverage*, fs::path, Plugin::FileCoverage>(
modules,
[](const Plugin::ModuleCoverage* m) -> const Plugin::ModuleCoverage::T_FileCoverageCollection&{ return m->GetFiles(); },
[](const Plugin::FileCoverage& file) -> const fs::path&{ return file.GetPath(); });
for (const auto& pair : filesByPath)
{
auto& file = module.AddFile(pair.first);
FillFiles(file, pair.second);
}
}
//-------------------------------------------------------------------------
void MergeFileCoverages(const std::vector<Plugin::FileCoverage*>& fileCoverages)
{
if (fileCoverages.size() > 1)
{
auto mutableFileCoverages = fileCoverages;
auto& fileCoverageSum = mutableFileCoverages.back();
mutableFileCoverages.pop_back();
for (const auto* fileCoverage : mutableFileCoverages)
AddFileCoverageTo(fileCoverage, fileCoverageSum);
for (auto* fileCoverage : mutableFileCoverages)
*fileCoverage = *fileCoverageSum;
}
}
}
//-------------------------------------------------------------------------
Plugin::CoverageData CoverageDataMerger::Merge(
const std::vector<Plugin::CoverageData>& coverageDataCollection) const
{
auto coverageData = CreateCoverageData(coverageDataCollection);
std::map<fs::path, std::vector<Plugin::ModuleCoverage*>> modulesByPath =
GroupChildrenByKey<Plugin::CoverageData, fs::path, Plugin::ModuleCoverage>(
coverageDataCollection,
[](const Plugin::CoverageData& data) -> const Plugin::CoverageData::T_ModuleCoverageCollection& { return data.GetModules(); },
[](const Plugin::ModuleCoverage& module) -> const fs::path& { return module.GetPath(); });
for (const auto& pair : modulesByPath)
{
auto& module = coverageData.AddModule(pair.first);
FillModule(module, pair.second);
}
return coverageData;
}
//-------------------------------------------------------------------------
void CoverageDataMerger::MergeFileCoverage(Plugin::CoverageData& coverageData) const
{
std::map<std::filesystem::path, std::vector<Plugin::FileCoverage*>> fileCoveragesByPath;
for (const auto& module : coverageData.GetModules())
{
for (const auto& file : module->GetFiles())
fileCoveragesByPath[file->GetPath()].push_back(file.get());
}
for (const auto& fileCoverageByPath : fileCoveragesByPath)
{
const auto& fileCoverages = fileCoverageByPath.second;
MergeFileCoverages(fileCoverages);
}
}
}