-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrp_most_impact_reporter.cpp
More file actions
290 lines (230 loc) · 6.68 KB
/
rp_most_impact_reporter.cpp
File metadata and controls
290 lines (230 loc) · 6.68 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "reporter/impl/rp_most_impact_reporter.hpp"
#include "reporter/api/enums/rp_reporter_kind.hpp"
#include "reporter/impl/impact_reporter/rp_file_info.hpp"
#include "reporter/impl/impact_reporter/rp_most_impact_reporter_detail.hpp"
#include "reporter/resources/rp_most_impact_reporter_resources.hpp"
#include "reporter/resources/rp_report_resources.hpp"
#include "model_includes/api/mi_model.hpp"
#include "model_includes/api/mi_file.hpp"
#include "model_includes/api/mi_include.hpp"
#include "model_includes/api/mi_include_location.hpp"
#include "model_includes/api/enums/mi_file_type.hpp"
#include "exception/ih/exc_internal_error.hpp"
#include <std_fs>
#include <functional>
#include <fmt/format.h>
//------------------------------------------------------------------------------
namespace reporter {
//------------------------------------------------------------------------------
bool MostImpcatReporter::FileWithCountSorter::operator()(
const FileInfo & _r,
const FileInfo & _l
) const
{
if( _r.getIncludedByCount() != _l.getIncludedByCount() )
{
// max is first
return _r.getIncludedByCount() > _l.getIncludedByCount();
}
else
{
return _r.getFile().getPath() < _l.getFile().getPath();
}
}
//------------------------------------------------------------------------------
bool MostImpcatReporter::DetailsSorter::operator()(
const MostImpactReporterDetail & _r,
const MostImpactReporterDetail & _l
) const
{
return FileWithCountSorter()( _r.getFileInfo(), _l.getFileInfo() );
}
//------------------------------------------------------------------------------
void MostImpcatReporter::report(
const model_includes::Model & _model,
std::ostream & _stream
)
{
FilesContainer files;
collectFiles( _model, files );
const size_t originSize = files.size();
resizeByLimit( files );
printIncludesByFiles( files, originSize, _model.getProjectDir(), _stream );
}
//------------------------------------------------------------------------------
ReporterKind MostImpcatReporter::getKind() const
{
return ReporterKind::MostImpact;
}
//------------------------------------------------------------------------------
void MostImpcatReporter::collectFiles(
const model_includes::Model & _model,
FilesContainer & _files
) const
{
using namespace model_includes;
_model.forEachFile(
[&]( const File & _file )
{
if( !isCollectFile( _file ) )
return true;
const auto count = _file.getIncludedByFilesCountRecursive();
if( count > 0 )
{
_files.insert( { _file, count } );
}
return true;
}
);
}
//------------------------------------------------------------------------------
void MostImpcatReporter::resizeByLimit( FilesContainer & _files ) const
{
const size_t limit = getMaxFilesCount();
if( limit && _files.size() >= limit )
{
auto it = _files.begin();
for( size_t i = 0; i < limit; ++i )
++it;
_files.erase( it, _files.end() );
}
}
//------------------------------------------------------------------------------
void MostImpcatReporter::printIncludesByFiles(
const FilesContainer & _files,
size_t _originSize,
const Path & _projectDir,
std::ostream & _stream
) const
{
using namespace model_includes;
if( _files.empty() )
return;
_stream << resources::most_impact_reporter::Header;
size_t currentNumber = 1;
for( const FileInfo & fileInfo : _files )
{
const File & includedByFile = fileInfo.getFile();
const std::string path =
getPathWithoutProject( includedByFile.getPath(), _projectDir );
const File::IncludeIndex count = fileInfo.getIncludedByCount();
_stream << fmt::format(
resources::most_impact_reporter::LineForFileFmt,
currentNumber,
path,
count
);
printDetails( includedByFile, _projectDir, _stream );
++currentNumber;
}
const size_t limit = getMaxFilesCount();
if(
limit &&
currentNumber >= limit &&
currentNumber - 1 != _originSize
)
{
_stream << fmt::format( resources::LimitLineFmt, limit, _originSize );
}
}
//------------------------------------------------------------------------------
void MostImpcatReporter::printDetails(
const model_includes::File & _includedByFile,
const Path & _projectDir,
std::ostream & _stream
) const
{
using namespace model_includes;
DetailsContainer details;
collectDetails( _includedByFile, details );
if( details.empty() )
return;
_stream << resources::most_impact_reporter::HeaderForDetails;
size_t currentNumber = 1;
const size_t limit = getMaxDetailsCount();
for( const MostImpactReporterDetail & detail : details )
{
if( limit && currentNumber > limit )
{
const size_t detailsCount = details.size();
_stream << fmt::format(
resources::LimitDetailLineFmt,
limit,
detailsCount
);
break;
}
printDetail( detail, _projectDir, currentNumber, _stream );
++currentNumber;
}
}
//------------------------------------------------------------------------------
void MostImpcatReporter::printDetail(
const MostImpactReporterDetail & _detail,
const Path & _projectDir,
size_t currentNumber,
std::ostream & _stream
) const
{
using namespace model_includes;
const File & file = _detail.getFile();
const std::string path =
getPathWithoutProject( file.getPath(), _projectDir );
const File::IncludeIndex count = _detail.getIncludedByCount();
const auto line = _detail.getIncludeLocation().getLineNumber();
if( count )
{
_stream << fmt::format(
resources::most_impact_reporter::LineForDetailFmt,
currentNumber,
path,
line,
count
);
}
else
{
_stream << fmt::format(
resources::most_impact_reporter::LineForNotImpactDetailFmt,
currentNumber,
path,
line
);
}
}
//------------------------------------------------------------------------------
void MostImpcatReporter::collectDetails(
const model_includes::File & _includedByFile,
DetailsContainer & _details
) const
{
using namespace model_includes;
const File::IncludeIndex count = _includedByFile.getIncludedByCount();
for( File::IncludeIndex i = 0; i < count; ++i )
{
const Include & include = _includedByFile.getIncludedBy( i );
const File & file = include.getSourceFile();
_details.insert({
file,
file.getIncludedByFilesCountRecursive(),
include.getLocation()
});
}
}
//------------------------------------------------------------------------------
bool MostImpcatReporter::isCollectFile( const model_includes::File & _file ) const
{
using namespace model_includes;
const FileType type = _file.getType();
static_assert( static_cast< int >( FileType::Count ) == 2 );
switch( type )
{
case FileType::ProjectFile : return true;
case FileType::StdLibraryFile : return getShowStdFiles();
default:
INTERNAL_CHECK_WARRING( false );
return false;
}
}
//------------------------------------------------------------------------------
}