-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource_checker.py
More file actions
366 lines (348 loc) · 14.4 KB
/
Copy pathsource_checker.py
File metadata and controls
366 lines (348 loc) · 14.4 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
from enum import Enum
from pathlib import Path
import re
class CheckResult(Enum):
FAIL = False
SUCCESS = True
def check_guards(path: Path, content: str):
assert(path.suffix == '.h')
content = content.strip()
content_lines = [line for line in content.splitlines(keepends=False) if line]
base_path = Path(__file__).resolve().parent
path = path.relative_to(base_path)
guard = '_'.join(re.sub(r'[^A-Z0-9]', '_', part.upper()) for part in path.parts)
if content_lines[0] != f'#ifndef {guard}' or content_lines[1] != f'#define {guard}' or content_lines[-1] != f'#endif //{guard}':
print(f'{path}: Bad or missing guard. Expected {guard}.')
return CheckResult.FAIL
return CheckResult.SUCCESS
def check_test_group_name(path: Path, content: str):
assert(path.name.endswith('_test.cpp'))
content = content.strip()
content_lines = [line for line in content.splitlines(keepends=False) if line]
base_path = Path(__file__).resolve().parent
path = path.relative_to(base_path)
def snake_to_pascal(text):
return ''.join(word.capitalize() for word in text.split('_'))
group_parts = []
for part in path.parts[:-1]:
group_parts.append(re.sub(r'[^a-zA-Z0-9]', '_', part)[0])
group_parts.append(re.sub(r'[^a-zA-Z0-9]', '_', path.stem))
group = '_'.join(group_parts)
group = snake_to_pascal(group)
group_found_set = set()
result = CheckResult.SUCCESS
for line in content_lines:
for group_found, _ in re.findall(r'TEST\((\w*?), (\w*?)\)', line):
if group_found != group and group_found not in group_found_set:
group_found_set.add(group_found)
print(f'{path}: Bad or missing test group. Expected {group}.')
result = CheckResult.FAIL
return result
SYSTEM_TYPE_TO_HEADER_MAPPING = {
'std::ranges::range': 'ranges',
'std::ranges::forward_range': 'ranges',
'std::ranges::bidirectional_range': 'ranges',
'std::ranges::random_access_range': 'ranges',
'std::ranges::range_reference_t': 'ranges',
'std::ranges::range_value_t': 'ranges',
'std::ranges::range_difference_t': 'ranges',
'std::ranges::range_size_t': 'ranges',
'std::ranges::sized_range': 'ranges',
'std::ranges::size': 'ranges',
'std::ranges::distance': 'ranges',
'std::ranges::sort': 'algorithm',
'std::ranges::max_element': 'ranges',
'std::ranges::begin': 'ranges',
'std::ranges::end': 'ranges',
'std::ranges::copy': 'ranges',
'std::ranges::single_view': 'ranges',
'std::ranges::common_view': 'ranges',
'std::ranges::common_range': 'ranges',
'std::ranges::view_interface': 'ranges',
'std::views::empty': 'ranges',
'std::views::single': 'ranges',
'std::views::transform': 'ranges',
'std::views::filter': 'ranges',
'std::views::iota': 'ranges',
'std::views::cartesian_product': 'ranges',
'std::views::take': 'ranges',
'std::views::drop': 'ranges',
'std::views::drop_while': 'ranges',
'std::views::split': 'ranges',
'std::views::common': 'ranges',
'std::views::reverse': 'ranges',
'std::views::concat': 'ranges',
'std::views::enumerate': 'ranges',
'std::views::all': 'ranges',
'std::views::all_t': 'ranges',
'std::views::join': 'ranges',
'std::ranges::sentinel_t': 'ranges',
'std::ranges::iterator_t': 'ranges',
'std::same_as': 'concepts',
'std::floating_point': 'concepts',
'std::integral': 'concepts',
'std::unsigned_integral': 'concepts',
'std::regular': 'concepts',
'std::semiregular': 'concepts',
'std::convertible_to': 'concepts',
'std::copyable': 'concepts',
'std::movable': 'concepts',
'std::equality_comparable': 'concepts',
'std::default_initializable': 'concepts',
'std::is_void_v': 'type_traits',
'std::is_same_v': 'type_traits',
'std::is_object_v': 'type_traits',
'std::is_convertible_v': 'type_traits',
'std::is_rvalue_reference_v': 'type_traits',
'std::is_lvalue_reference_v': 'type_traits',
'std::is_reference_v': 'type_traits',
'std::is_signed_v': 'type_traits',
'std::is_default_constructible_v': 'type_traits',
'std::is_copy_constructible_v': 'type_traits',
'std::is_copy_assignable_v': 'type_traits',
'std::is_move_constructible_v': 'type_traits',
'std::is_move_assignable_v': 'type_traits',
'std::common_type_t': 'type_traits',
'std::common_reference_t': 'type_traits',
'std::integral_constant': 'type_traits',
'std::remove_reference_t': 'type_traits',
'std::remove_cvref_t': 'type_traits',
'std::remove_const_t': 'type_traits',
'std::decay_t': 'type_traits',
'std::invoke_result_t': 'type_traits',
'std::conditional_t': 'type_traits',
'std::numeric_limits': 'limits',
'std::begin': 'iterator',
'std::end': 'iterator',
'std::advance': 'iterator',
'std::input_or_output_iterator': 'iterator',
'std::input_iterator': 'iterator',
'std::input_iterator_tag': 'iterator',
'std::forward_iterator': 'iterator',
'std::forward_iterator_tag': 'iterator',
'std::bidirectional_iterator': 'iterator',
'std::bidirectional_iterator_tag': 'iterator',
'std::random_access_iterator': 'iterator',
'std::random_access_iterator_tag': 'iterator',
'std::sentinel_for': 'iterator',
'std::back_inserter': 'iterator',
'std::sized_sentinel_for': 'iterator',
'std::iter_reference_t': 'iterator',
'std::iter_value_t': 'iterator',
'std::iter_difference_t': 'iterator',
'std::iterator_traits': 'iterator',
'std::lower_bound': 'algorithm',
'std::reverse': 'algorithm',
'std::sort': 'algorithm',
'std::max': 'algorithm',
'std::min': 'algorithm',
'std::mt19937_64': 'random',
'std::uniform_int_distribution': 'random',
'std::uniform_real_distribution': 'random',
'std::abs': 'cmath',
'std::hypot': 'cmath',
'std::isfinite': 'cmath',
'std::size_t': 'cstddef',
'std::int8_t': 'cstdint',
'std::int16_t': 'cstdint',
'std::int32_t': 'cstdint',
'std::int64_t': 'cstdint',
'std::uint8_t': 'cstdint',
'std::uint16_t': 'cstdint',
'std::uint32_t': 'cstdint',
'std::uint64_t': 'cstdint',
'std::ptrdiff_t': 'cstdint',
'std::uintmax_t': 'cstdint',
'std::float16_t': 'stdfloat',
'std::float32_t': 'stdfloat',
'std::float64_t': 'stdfloat',
'std::string': 'string',
'std::to_string': 'string',
'std::string_view': 'string_view',
'std::chrono_literals': 'chrono',
'std::isalnum': 'cctype',
'std::formatter': 'format',
'std::format': 'format',
'std::format_to': 'format',
'std::format_context': 'format',
'std::format_parse_context': 'format',
'std::format_error': 'format',
'std::optional': 'optional',
'std::nullopt': 'optional',
'std::nullopt_t': 'optional',
'std::reference_wrapper': 'functional',
'std::ref': 'functional',
'std::function': 'functional',
'std::less': 'functional',
'std::hash': 'utility',
'std::make_pair': 'utility',
'std::pair': 'utility',
'std::declval': 'utility',
'std::forward': 'utility',
'std::move': 'utility',
'std::unreachable': 'utility',
'std::in_place': 'utility',
'std::in_place_t': 'utility',
'std::tuple': 'tuple',
'std::tuple_element_t': 'tuple',
'std::tuple_size': 'tuple',
'std::tuple_size_v': 'tuple',
'std::tuple_cat': 'tuple',
'std::make_tuple': 'tuple',
'std::tie': 'tuple',
'std::get': 'tuple',
'std::apply': 'tuple',
'std::variant': 'variant',
'std::get_if': 'variant',
'std::holds_alternative': 'variant',
'std::visit': 'variant',
'std::strong_ordering': 'compare',
'std::vector': 'vector',
'std::deque': 'deque',
'std::array': 'array',
'std::map': 'map',
'std::unordered_map': 'unordered_map',
'std::set': 'set',
'std::multiset': 'set',
'std::runtime_error': 'stdexcept',
'std::exception': 'exception',
'std::cout': 'iostream',
'std::ofstream': 'fstream',
'std::ostream': 'ostream',
'std::endl': 'ostream',
'std::istringstream': 'sstream',
'std::ios::binary': 'ios',
'std::shared_mutex': 'shared_mutex',
'std::mutex': 'mutex',
'std::unique_lock': 'mutex',
'std::condition_variable': 'condition_variable',
'std::packaged_task': 'future',
'std::future': 'future',
'std::future_status': 'future',
'std::future_status::ready': 'future',
'std::future_status::timeout': 'future',
'std::future_status::deferred': 'future',
'std::promise': 'future',
'std::jthread': 'thread',
'std::thread': 'thread',
'std::thread::hardware_concurrency': 'thread',
'std::thread::id': 'thread',
'std::this_thread': 'thread',
'std::this_thread::get_id': 'thread',
'std::memory_order_acquire': 'atomic',
'std::memory_order_release': 'atomic',
'std::atomic_flag': 'atomic',
'std::atomic_int64_t': 'atomic',
'std::any': 'any',
'std::any_cast': 'any',
'std::unique_ptr': 'memory',
'std::make_unique': 'memory',
'std::addressof': 'memory',
'boost::container::small_vector': 'boost/container/small_vector.hpp',
'boost::container::static_vector': 'boost/container/static_vector.hpp',
'boost::container::static_vector_options': 'boost/container/static_vector.hpp',
'boost::container::throw_on_overflow': 'boost/container/options.hpp',
'boost::container::inplace_alignment': 'boost/container/options.hpp',
'boost::filesystem::path': 'boost/filesystem.hpp',
'boost::filesystem::temp_directory_path': 'boost/filesystem.hpp',
'boost::filesystem::unique_path': 'boost/filesystem.hpp',
'boost::iostreams::mapped_file_source': 'boost/iostreams/device/mapped_file.hpp',
'boost::iostreams::mapped_file_base::readonly': 'boost/iostreams/device/mapped_file.hpp',
'boost::safe_numerics': 'boost/safe_numerics/safe_integer.hpp',
'boost::safe_numerics::checked::multiply': 'boost/safe_numerics/checked_default.hpp',
'boost::safe_numerics::checked::add': 'boost/safe_numerics/checked_default.hpp',
'boost::safe_numerics::safe_numerics_error': 'boost/safe_numerics/safe_integer.hpp',
'boost::callable_traits::args_t': 'boost/callable_traits/args.hpp',
}
def check_system_includes(path: Path, content: str):
assert(path.suffix in {'.cpp', '.h'})
content = content.strip()
content_lines = [line for line in content.splitlines(keepends=False) if line]
base_path = Path(__file__).resolve().parent
path = path.relative_to(base_path)
result = CheckResult.SUCCESS
found_includes = set()
expected_includes = set()
usages = set()
for line in content_lines:
for include in re.findall(r'#include <([\w/\.]+)>', line):
found_includes.add(include)
for usage in re.findall(r'((?:std|boost)::[\w:]+)', line):
if usage in usages:
continue
usages.add(usage)
expected_include = SYSTEM_TYPE_TO_HEADER_MAPPING.get(usage, None)
expected_includes.add(expected_include)
if expected_include is None:
print(f'{path}: No include mapping for {usage}')
result = CheckResult.FAIL
elif expected_include not in found_includes:
print(f'{path}: {usage} requires include {expected_include}')
result = CheckResult.FAIL
excess_includes = found_includes - expected_includes
if excess_includes:
for excess_include in excess_includes:
print(f'{path}: {excess_include} included but not required')
result = CheckResult.FAIL
return result
def check_line_length(path: Path, content: str):
assert(path.suffix in {'.cpp', '.h'})
content = content.strip()
content_lines = [line for line in content.splitlines(keepends=False) if line]
result = CheckResult.SUCCESS
for line in content_lines:
if line.startswith('#include ') or line.startswith('#ifdef ') or line.startswith('#ifndef ') or line.startswith('#endif ') \
or line.startswith('#define '):
continue
if len(line) > 140:
if re.match(r'^(/\*\*|\*)\s+@', line.strip()): # Ignore "/** @..." or "* @...", because @ref, @link, @copydoc, ... can be long
continue
print(f'{path}: line exceeds 140 chars\n > {line}')
result = CheckResult.FAIL
return result
def check_doxygen_refs(path: Path, content: str):
assert(path.name.endswith('.h'))
content = content.strip()
content_lines = [line for line in content.splitlines(keepends=False) if line]
base_path = Path(__file__).resolve().parent
path = path.relative_to(base_path)
result = CheckResult.SUCCESS
for line in content_lines:
for ref in re.findall(r'@ref\s+((?:[a-z0-9_]+::)+[a-z0-9_]+)', line):
search_path = ref.split('::')
extras = []
while search_path:
ref_path = base_path / Path('/'.join(search_path) + '.h')
if ref_path.is_file():
text = ref_path.read_text()
for extra in extras:
if extra not in text:
print(f'{path}: Bad ref tag {ref} (searched {ref_path})')
result = CheckResult.FAIL
break
extras += [search_path.pop()]
if not search_path:
print(f'{path}: Bad ref tag {ref} (not found)')
result = CheckResult.FAIL
return result
def main():
result = CheckResult.SUCCESS
base_path = Path(__file__).resolve().parent / 'offbynull'
for source_path in base_path.rglob('*'):
if source_path.suffix == '.h':
if check_guards(source_path, source_path.read_text()) == CheckResult.FAIL:
result = CheckResult.FAIL
if check_doxygen_refs(source_path, source_path.read_text()) == CheckResult.FAIL:
result = CheckResult.FAIL
if source_path.name.endswith('_test.cpp'):
if check_test_group_name(source_path, source_path.read_text()) == CheckResult.FAIL:
result = CheckResult.FAIL
if source_path.suffix in {'.cpp', '.h'}:
if check_system_includes(source_path, source_path.read_text()) == CheckResult.FAIL:
result = CheckResult.FAIL
if check_line_length(source_path, source_path.read_text()) == CheckResult.FAIL:
result = CheckResult.FAIL
return result
if __name__ == '__main__':
result = main()
exit(0 if result == CheckResult.SUCCESS else 1)