-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrp_unresolved_reporter.cpp
More file actions
289 lines (232 loc) · 7.22 KB
/
Copy pathrp_unresolved_reporter.cpp
File metadata and controls
289 lines (232 loc) · 7.22 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
#include "reporter/impl/rp_unresolved_reporter.hpp"
#include "reporter/resources/rp_unresolved_reporter_resources.hpp"
#include "reporter/resources/rp_report_resources.hpp"
#include "reporter/api/enums/rp_reporter_kind.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_include_status.hpp"
#include "exception/ih/exc_internal_error.hpp"
#include <fmt/format.h>
#include <set>
#include <functional>
#include <std_fs>
#include <unordered_map>
#include <vector>
//------------------------------------------------------------------------------
namespace reporter {
//------------------------------------------------------------------------------
bool UnresolvedReporter::SourceOrder::operator()(
const model_includes::Include * _r,
const model_includes::Include * _l
) const
{
INTERNAL_CHECK_ERROR( _r );
INTERNAL_CHECK_ERROR( _l );
const model_includes::File & sourceRight = _r->getSourceFile();
const model_includes::File & sourceLeft = _l->getSourceFile();
return sourceRight.getPath() < sourceLeft.getPath();
}
//------------------------------------------------------------------------------
bool UnresolvedReporter::DestinationFileInfoorder::operator()(
const DestinationFileInfo & _r,
const DestinationFileInfo & _l
) const
{
if( _r.m_count != _l.m_count )
{
return _r.m_count > _l.m_count;
}
else
{
INTERNAL_CHECK_ERROR( _r.m_file );
INTERNAL_CHECK_ERROR( _l.m_file );
return _r.m_file->getPath() < _l.m_file->getPath();
}
}
//------------------------------------------------------------------------------
void UnresolvedReporter::report(
const model_includes::Model & _model,
std::ostream & _stream
)
{
UnorderedIncludesByDestination unorderedIncludes;
collectIncludes( _model, unorderedIncludes );
DestinationFileByCount destinationFilesByCount =
orderDestinationByCount( unorderedIncludes );
report(
unorderedIncludes,
destinationFilesByCount,
_model.getProjectDir(),
_stream
);
}
//------------------------------------------------------------------------------
ReporterKind UnresolvedReporter::getKind() const
{
return ReporterKind::Unresolved;
}
//------------------------------------------------------------------------------
void UnresolvedReporter::collectIncludes(
const model_includes::Model & _model,
UnorderedIncludesByDestination & _unorderedIncludes
) const
{
using namespace model_includes;
_model.forEachInclude(
[&]( const Include & _include )
{
if( isUnresolvedInclude( _include ) )
{
const File & destinationFile = _include.getDestinationFile();
_unorderedIncludes[ &destinationFile ].push_back( &_include );
}
return true;
}
);
}
//------------------------------------------------------------------------------
UnresolvedReporter::DestinationFileByCount UnresolvedReporter::orderDestinationByCount(
const UnorderedIncludesByDestination & _unorderedIncludes
)
{
DestinationFileByCount result;
for( const auto & pair : _unorderedIncludes )
{
const model_includes::File * destinationFile = pair.first;
const auto & includes = pair.second;
auto pairInsert = result.insert( { destinationFile, includes.size() } );
if( !pairInsert.second )
{
INTERNAL_CHECK_WARRING( false );
}
}
return result;
}
//------------------------------------------------------------------------------
void UnresolvedReporter::report(
const UnorderedIncludesByDestination & _unorderedIncludesByDestination,
const DestinationFileByCount & _destinationFileByCount,
const Path & _projectDir,
std::ostream & _stream
) const
{
using namespace model_includes;
if( _unorderedIncludesByDestination.empty() )
return;
_stream << resources::unresolved_reporter::Header;
size_t number = 1;
const size_t limit = getMaxFilesCount();
for( const DestinationFileInfo & info : _destinationFileByCount )
{
if( limit && number > limit )
{
const size_t detailsCount = _destinationFileByCount.size();
_stream << fmt::format( resources::LimitLineFmt, limit, detailsCount );
break;
}
const File * destinationFilePtr = info.m_file;
INTERNAL_CHECK_WARRING( destinationFilePtr );
if( !destinationFilePtr )
continue;
const File & destinationFile = *destinationFilePtr;
reportDestinationFile( destinationFile, number, _projectDir, _stream );
if( !_unorderedIncludesByDestination.count( destinationFilePtr ) )
{
INTERNAL_CHECK_WARRING( false );
continue;
}
const UnorderedIncludes & includes =
_unorderedIncludesByDestination.at( destinationFilePtr );
reportSourceFiles( includes, _projectDir, _stream );
++number;
}
}
//------------------------------------------------------------------------------
void UnresolvedReporter::reportDestinationFile(
const model_includes::File & _file,
size_t _number,
const Path & _projectDir,
std::ostream & _stream
) const
{
const std::string destinationPath =
getPathWithoutProject( _file.getPath(), _projectDir );
INTERNAL_CHECK_WARRING( !destinationPath.empty() );
_stream << fmt::format(
resources::unresolved_reporter::UnresolvedDestinationFmt,
_number,
destinationPath
);
}
//------------------------------------------------------------------------------
void UnresolvedReporter::reportSourceFiles(
const UnorderedIncludes & _includes,
const Path & _projectDir,
std::ostream & _stream
) const
{
OrderedIncludes orderedIncludes{ _includes.begin(), _includes.end() };
size_t number = 1;
const size_t limit = getMaxDetailsCount();
for( const model_includes::Include * includePtr : orderedIncludes )
{
if( limit && number > limit )
{
_stream << fmt::format(
resources::LimitDetailLineFmt,
limit,
orderedIncludes.size()
);
break;
}
INTERNAL_CHECK_WARRING( includePtr );
if( !includePtr )
continue;
reportSourceFile( *includePtr, number, _projectDir, _stream );
++number;
}
}
//------------------------------------------------------------------------------
void UnresolvedReporter::reportSourceFile(
const model_includes::Include & _include,
size_t _number,
const Path & _projectDir,
std::ostream & _stream
) const
{
using namespace model_includes;
_stream << resources::unresolved_reporter::Intend;
const File & sourceFile = _include.getSourceFile();
const IncludeLocation & location = _include.getLocation();
const IncludeLocation::LineNumber line = location.getLineNumber();
const std::string sourcePath =
getPathWithoutProject( sourceFile.getPath(), _projectDir );
INTERNAL_CHECK_WARRING( !sourcePath.empty() );
_stream << fmt::format(
resources::unresolved_reporter::UnresolvedSourceFmt,
_number,
sourcePath,
line
);
}
//------------------------------------------------------------------------------
bool UnresolvedReporter::isUnresolvedInclude(
const model_includes::Include & _include
) const
{
using namespace model_includes;
const IncludeStatus status = _include.getStatus();
static_assert( static_cast< int >( IncludeStatus::Count ) == 2 );
switch( status )
{
case IncludeStatus::Resolved : return false;
case IncludeStatus::Unresolved : return true;
default:
INTERNAL_CHECK_WARRING( false );
return false;
}
}
//------------------------------------------------------------------------------
}