jarbin-c-coding-style 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jarbin_c_coding_style-2.0.0.dist-info/METADATA +150 -0
- jarbin_c_coding_style-2.0.0.dist-info/RECORD +186 -0
- jarbin_c_coding_style-2.0.0.dist-info/WHEEL +4 -0
- jarbin_c_coding_style-2.0.0.dist-info/entry_points.txt +2 -0
- jarbin_c_coding_style-2.0.0.dist-info/licenses/LICENSE +674 -0
- sources/__init__.py +0 -0
- sources/__main__.py +6 -0
- sources/analysis/__init__.py +0 -0
- sources/analysis/directives.py +258 -0
- sources/analysis/lexer.py +288 -0
- sources/analysis/parser.py +1754 -0
- sources/analysis/project.py +286 -0
- sources/analysis/source.py +326 -0
- sources/analysis/syntax.py +238 -0
- sources/analysis/tokens.py +116 -0
- sources/analysis/unit.py +700 -0
- sources/application.py +118 -0
- sources/build_info.json +6 -0
- sources/cli/__init__.py +0 -0
- sources/cli/arguments.py +507 -0
- sources/cli/completion.py +610 -0
- sources/cli/context.py +258 -0
- sources/cli/converters.py +204 -0
- sources/cli/help.py +162 -0
- sources/cli/parser.py +659 -0
- sources/configs/__init__.py +0 -0
- sources/configs/editor.py +207 -0
- sources/configs/ini.py +327 -0
- sources/configs/project.py +735 -0
- sources/configs/schema/__init__.py +0 -0
- sources/configs/schema/project.py +68 -0
- sources/constants.py +801 -0
- sources/diagnostics/__init__.py +0 -0
- sources/diagnostics/fingerprint.py +82 -0
- sources/diagnostics/model.py +146 -0
- sources/engine/__init__.py +0 -0
- sources/engine/api.py +355 -0
- sources/engine/examples.py +157 -0
- sources/engine/outcome.py +114 -0
- sources/engine/plan.py +440 -0
- sources/engine/runner.py +345 -0
- sources/engine/scanner.py +179 -0
- sources/enums.py +838 -0
- sources/errors/__init__.py +0 -0
- sources/errors/exceptions.py +727 -0
- sources/fixers/__init__.py +0 -0
- sources/fixers/base.py +209 -0
- sources/fixers/conflicts.py +129 -0
- sources/fixers/diff.py +73 -0
- sources/fixers/engine.py +434 -0
- sources/fixers/journal.py +290 -0
- sources/fixers/kinds.py +30 -0
- sources/fixers/layout.py +411 -0
- sources/fixers/model.py +151 -0
- sources/fixers/registry.py +54 -0
- sources/fixers/text.py +353 -0
- sources/fixers/verify.py +123 -0
- sources/fixers/writer.py +88 -0
- sources/integrations/__init__.py +0 -0
- sources/integrations/ci/__init__.py +0 -0
- sources/integrations/ci/github.py +183 -0
- sources/integrations/editors/__init__.py +0 -0
- sources/integrations/git/__init__.py +0 -0
- sources/integrations/git/repository.py +176 -0
- sources/integrations/update/__init__.py +0 -0
- sources/integrations/update/installers.py +529 -0
- sources/integrations/update/releases.py +397 -0
- sources/integrations/update/state.py +143 -0
- sources/outputs/__init__.py +0 -0
- sources/outputs/github/__init__.py +0 -0
- sources/outputs/github/renderer.py +135 -0
- sources/outputs/html/__init__.py +0 -0
- sources/outputs/html/renderer.py +694 -0
- sources/outputs/json/__init__.py +0 -0
- sources/outputs/json/renderer.py +154 -0
- sources/outputs/json/report.schema.json +121 -0
- sources/outputs/junit/__init__.py +0 -0
- sources/outputs/junit/renderer.py +151 -0
- sources/outputs/registry.py +135 -0
- sources/outputs/report.py +224 -0
- sources/outputs/sarif/__init__.py +0 -0
- sources/outputs/sarif/renderer.py +182 -0
- sources/outputs/terminal/__init__.py +0 -0
- sources/outputs/terminal/renderer.py +184 -0
- sources/outputs/terminal/style.py +159 -0
- sources/rules/__init__.py +0 -0
- sources/rules/arguments.py +362 -0
- sources/rules/catalog.py +227 -0
- sources/rules/categories/advanced/constant_pointers.ini +46 -0
- sources/rules/categories/advanced/line_break_at_end_of_file.ini +44 -0
- sources/rules/categories/advanced/static.ini +65 -0
- sources/rules/categories/advanced/typing.ini +47 -0
- sources/rules/categories/control_structures/conditional_branching.ini +89 -0
- sources/rules/categories/control_structures/goto.ini +45 -0
- sources/rules/categories/control_structures/ternary_operators.ini +57 -0
- sources/rules/categories/extensions/language_extensions.ini +71 -0
- sources/rules/categories/files_organization/contents_of_the_repository.ini +85 -0
- sources/rules/categories/files_organization/file_coherence.ini +166 -0
- sources/rules/categories/files_organization/file_extension.ini +68 -0
- sources/rules/categories/files_organization/fixtures/program +0 -0
- sources/rules/categories/files_organization/naming_files_and_folders.ini +74 -0
- sources/rules/categories/functions/coherence_of_functions.ini +45 -0
- sources/rules/categories/functions/comments_inside_a_function.ini +45 -0
- sources/rules/categories/functions/functions_without_parameters.ini +39 -0
- sources/rules/categories/functions/naming_functions.ini +79 -0
- sources/rules/categories/functions/number_of_columns.ini +61 -0
- sources/rules/categories/functions/number_of_lines.ini +70 -0
- sources/rules/categories/functions/number_of_parameters.ini +64 -0
- sources/rules/categories/functions/structures_as_parameters.ini +44 -0
- sources/rules/categories/global_scope/constant_values.ini +49 -0
- sources/rules/categories/global_scope/file_header.ini +92 -0
- sources/rules/categories/global_scope/global_variables.ini +53 -0
- sources/rules/categories/global_scope/include.ini +39 -0
- sources/rules/categories/global_scope/indentation_of_preprocessor.ini +62 -0
- sources/rules/categories/global_scope/inline_assembly.ini +43 -0
- sources/rules/categories/global_scope/leading_trailing_lines.ini +58 -0
- sources/rules/categories/global_scope/line_endings.ini +53 -0
- sources/rules/categories/global_scope/separation_of_functions.ini +61 -0
- sources/rules/categories/global_scope/trailing_spaces.ini +45 -0
- sources/rules/categories/header_files/content.ini +104 -0
- sources/rules/categories/header_files/include_guard.ini +47 -0
- sources/rules/categories/header_files/macros.ini +57 -0
- sources/rules/categories/layout_inside_a_function_scope/blank_lines.ini +71 -0
- sources/rules/categories/layout_inside_a_function_scope/code_line_content.ini +112 -0
- sources/rules/categories/layout_inside_a_function_scope/curly_brackets.ini +94 -0
- sources/rules/categories/layout_inside_a_function_scope/indentation.ini +96 -0
- sources/rules/categories/layout_inside_a_function_scope/spaces.ini +113 -0
- sources/rules/categories/layout_inside_a_function_scope/variable_declarations.ini +65 -0
- sources/rules/categories/variables_and_types/naming_identifiers.ini +74 -0
- sources/rules/categories/variables_and_types/pointers.ini +67 -0
- sources/rules/categories/variables_and_types/structures.ini +47 -0
- sources/rules/data/verbs.txt +182 -0
- sources/rules/definition.py +430 -0
- sources/rules/loader.py +925 -0
- sources/rules/paths.py +6 -0
- sources/rules/primitives/__init__.py +0 -0
- sources/rules/primitives/base.py +297 -0
- sources/rules/primitives/braces.py +211 -0
- sources/rules/primitives/directives.py +203 -0
- sources/rules/primitives/indentation.py +376 -0
- sources/rules/primitives/layout.py +199 -0
- sources/rules/primitives/naming.py +277 -0
- sources/rules/primitives/project.py +62 -0
- sources/rules/primitives/registry.py +49 -0
- sources/rules/primitives/repository.py +129 -0
- sources/rules/primitives/review.py +87 -0
- sources/rules/primitives/semantic.py +319 -0
- sources/rules/primitives/spacing.py +210 -0
- sources/rules/primitives/structure.py +586 -0
- sources/rules/primitives/syntax.py +200 -0
- sources/rules/primitives/text.py +408 -0
- sources/rules/primitives/tokens.py +84 -0
- sources/rules/template.py +138 -0
- sources/selftest/__init__.py +0 -0
- sources/selftest/cases.py +292 -0
- sources/services.py +90 -0
- sources/subcommands/__init__.py +0 -0
- sources/subcommands/base.py +45 -0
- sources/subcommands/check/__init__.py +0 -0
- sources/subcommands/check/command.py +45 -0
- sources/subcommands/check/fixing.py +224 -0
- sources/subcommands/check/handler.py +757 -0
- sources/subcommands/info/__init__.py +0 -0
- sources/subcommands/info/command.py +45 -0
- sources/subcommands/info/handler.py +527 -0
- sources/subcommands/registry.py +37 -0
- sources/subcommands/rule/__init__.py +0 -0
- sources/subcommands/rule/command.py +45 -0
- sources/subcommands/rule/handler.py +1067 -0
- sources/subcommands/rule/render.py +273 -0
- sources/subcommands/runner.py +124 -0
- sources/subcommands/test/__init__.py +0 -0
- sources/subcommands/test/command.py +45 -0
- sources/subcommands/test/handler.py +166 -0
- sources/subcommands/test/render.py +219 -0
- sources/subcommands/test/runner.py +430 -0
- sources/subcommands/update/__init__.py +0 -0
- sources/subcommands/update/command.py +45 -0
- sources/subcommands/update/handler.py +408 -0
- sources/utils/__init__.py +0 -0
- sources/utils/build_info.py +188 -0
- sources/utils/git.py +73 -0
- sources/utils/globs.py +98 -0
- sources/utils/logging.py +154 -0
- sources/utils/schema.py +77 -0
- sources/utils/versions.py +87 -0
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
from collections.abc import Iterable, Mapping
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from pathlib import PurePosixPath
|
|
4
|
+
from types import MappingProxyType
|
|
5
|
+
|
|
6
|
+
from sources.analysis.directives import Include, MacroDefinition
|
|
7
|
+
from sources.analysis.parser import collect_typedef_names
|
|
8
|
+
from sources.analysis.source import SourceFile, build_source_file
|
|
9
|
+
from sources.analysis.tokens import Token
|
|
10
|
+
from sources.analysis.unit import CallInfo, CUnit, FunctionInfo, ReferenceInfo, TypedefInfo, VariableInfo, build_unit, lex_unit
|
|
11
|
+
from sources.enums import FileKind
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _group[T](items: Iterable[tuple[str, T]]) -> MappingProxyType[str, tuple[T, ...]]:
|
|
15
|
+
"""
|
|
16
|
+
Group (name, value) pairs into a read-only mapping name -> values.
|
|
17
|
+
|
|
18
|
+
Parameters
|
|
19
|
+
----------
|
|
20
|
+
items : Iterable[tuple[str, T]]
|
|
21
|
+
The pairs, in order.
|
|
22
|
+
|
|
23
|
+
Returns
|
|
24
|
+
----------
|
|
25
|
+
MappingProxyType[str, tuple[T, ...]]
|
|
26
|
+
The grouped values, in the order of the pairs.
|
|
27
|
+
"""
|
|
28
|
+
grouped: dict[str, list[T]] = {}
|
|
29
|
+
for name, value in items:
|
|
30
|
+
grouped.setdefault(name, []).append(value)
|
|
31
|
+
return MappingProxyType({name: tuple(values) for name, values in grouped.items()})
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass(frozen=True, slots=True)
|
|
35
|
+
class ProjectContext:
|
|
36
|
+
"""
|
|
37
|
+
The whole project, loaded in memory once and shared (read-only) by every rule: every file, the analysis
|
|
38
|
+
of every C/H file, and cross-file indexes (functions, calls, references, variables, typedefs, macros,
|
|
39
|
+
include graph).
|
|
40
|
+
(dataclass)
|
|
41
|
+
|
|
42
|
+
Attributes
|
|
43
|
+
----------
|
|
44
|
+
root : str
|
|
45
|
+
Display name of the project root (absolute path, or "." for in-memory projects).
|
|
46
|
+
files : Mapping[str, SourceFile]
|
|
47
|
+
Every loaded file, by relative POSIX path, sorted by path.
|
|
48
|
+
units : Mapping[str, CUnit]
|
|
49
|
+
Analysis of every C/H file, by relative path.
|
|
50
|
+
type_names : frozenset[str]
|
|
51
|
+
Typedef names declared anywhere in the project.
|
|
52
|
+
function_definitions : Mapping[str, tuple[FunctionInfo, ...]]
|
|
53
|
+
Function definitions by name.
|
|
54
|
+
function_prototypes : Mapping[str, tuple[FunctionInfo, ...]]
|
|
55
|
+
Function prototypes by name.
|
|
56
|
+
calls : Mapping[str, tuple[CallInfo, ...]]
|
|
57
|
+
Calls by called name.
|
|
58
|
+
references : Mapping[str, tuple[ReferenceInfo, ...]]
|
|
59
|
+
Uses of names in expressions (calls included), by name.
|
|
60
|
+
variables : Mapping[str, tuple[VariableInfo, ...]]
|
|
61
|
+
File-scope variables and static locals by name.
|
|
62
|
+
typedefs : Mapping[str, tuple[TypedefInfo, ...]]
|
|
63
|
+
Typedef declarations by name.
|
|
64
|
+
macros : Mapping[str, tuple[MacroDefinition, ...]]
|
|
65
|
+
Macro definitions by name.
|
|
66
|
+
includes : Mapping[str, tuple[Include, ...]]
|
|
67
|
+
`#include` directives by including file.
|
|
68
|
+
"""
|
|
69
|
+
|
|
70
|
+
root: str
|
|
71
|
+
files: Mapping[str, SourceFile]
|
|
72
|
+
units: Mapping[str, CUnit]
|
|
73
|
+
type_names: frozenset[str]
|
|
74
|
+
function_definitions: Mapping[str, tuple[FunctionInfo, ...]]
|
|
75
|
+
function_prototypes: Mapping[str, tuple[FunctionInfo, ...]]
|
|
76
|
+
calls: Mapping[str, tuple[CallInfo, ...]]
|
|
77
|
+
references: Mapping[str, tuple[ReferenceInfo, ...]]
|
|
78
|
+
variables: Mapping[str, tuple[VariableInfo, ...]]
|
|
79
|
+
typedefs: Mapping[str, tuple[TypedefInfo, ...]]
|
|
80
|
+
macros: Mapping[str, tuple[MacroDefinition, ...]]
|
|
81
|
+
includes: Mapping[str, tuple[Include, ...]]
|
|
82
|
+
|
|
83
|
+
def file(self, path: str) -> SourceFile:
|
|
84
|
+
"""
|
|
85
|
+
Get a file by its relative path.
|
|
86
|
+
|
|
87
|
+
Parameters
|
|
88
|
+
----------
|
|
89
|
+
path : str
|
|
90
|
+
Relative POSIX path.
|
|
91
|
+
|
|
92
|
+
Returns
|
|
93
|
+
----------
|
|
94
|
+
SourceFile
|
|
95
|
+
The file.
|
|
96
|
+
|
|
97
|
+
Raises
|
|
98
|
+
----------
|
|
99
|
+
KeyError
|
|
100
|
+
The file is not part of the project.
|
|
101
|
+
"""
|
|
102
|
+
return self.files[path]
|
|
103
|
+
|
|
104
|
+
def unit(self, path: str) -> CUnit | None:
|
|
105
|
+
"""
|
|
106
|
+
Get the analysis of a C/H file.
|
|
107
|
+
|
|
108
|
+
Parameters
|
|
109
|
+
----------
|
|
110
|
+
path : str
|
|
111
|
+
Relative POSIX path.
|
|
112
|
+
|
|
113
|
+
Returns
|
|
114
|
+
----------
|
|
115
|
+
CUnit | None
|
|
116
|
+
The unit, or None when the file is not a C/H file of the project.
|
|
117
|
+
"""
|
|
118
|
+
return self.units.get(path)
|
|
119
|
+
|
|
120
|
+
def files_of_kind(self, *kinds: FileKind) -> tuple[SourceFile, ...]:
|
|
121
|
+
"""
|
|
122
|
+
Files of the given kinds, sorted by path.
|
|
123
|
+
|
|
124
|
+
Parameters
|
|
125
|
+
----------
|
|
126
|
+
*kinds : FileKind
|
|
127
|
+
Accepted kinds.
|
|
128
|
+
|
|
129
|
+
Returns
|
|
130
|
+
----------
|
|
131
|
+
tuple[SourceFile, ...]
|
|
132
|
+
The files.
|
|
133
|
+
"""
|
|
134
|
+
return tuple(source for source in self.files.values() if source.kind in kinds)
|
|
135
|
+
|
|
136
|
+
def function_exists(self, name: str) -> bool:
|
|
137
|
+
"""
|
|
138
|
+
Whether a function with this name is defined somewhere in the project.
|
|
139
|
+
|
|
140
|
+
Parameters
|
|
141
|
+
----------
|
|
142
|
+
name : str
|
|
143
|
+
Function name.
|
|
144
|
+
|
|
145
|
+
Returns
|
|
146
|
+
----------
|
|
147
|
+
bool
|
|
148
|
+
True when a definition exists.
|
|
149
|
+
"""
|
|
150
|
+
return name in self.function_definitions
|
|
151
|
+
|
|
152
|
+
def is_called(self, name: str) -> bool:
|
|
153
|
+
"""
|
|
154
|
+
Whether a function with this name is called somewhere in the project.
|
|
155
|
+
|
|
156
|
+
Parameters
|
|
157
|
+
----------
|
|
158
|
+
name : str
|
|
159
|
+
Function name.
|
|
160
|
+
|
|
161
|
+
Returns
|
|
162
|
+
----------
|
|
163
|
+
bool
|
|
164
|
+
True when at least one call exists.
|
|
165
|
+
"""
|
|
166
|
+
return name in self.calls
|
|
167
|
+
|
|
168
|
+
def callers(self, name: str) -> tuple[CallInfo, ...]:
|
|
169
|
+
"""
|
|
170
|
+
Every call to a function.
|
|
171
|
+
|
|
172
|
+
Parameters
|
|
173
|
+
----------
|
|
174
|
+
name : str
|
|
175
|
+
Function name.
|
|
176
|
+
|
|
177
|
+
Returns
|
|
178
|
+
----------
|
|
179
|
+
tuple[CallInfo, ...]
|
|
180
|
+
The calls.
|
|
181
|
+
"""
|
|
182
|
+
return self.calls.get(name, ())
|
|
183
|
+
|
|
184
|
+
def is_used_outside(self, name: str, path: str) -> bool:
|
|
185
|
+
"""
|
|
186
|
+
Whether a name is used (called or referenced) in another C file than `path`. Uses inside headers
|
|
187
|
+
count for every C file, since a header is part of each file that includes it.
|
|
188
|
+
|
|
189
|
+
Parameters
|
|
190
|
+
----------
|
|
191
|
+
name : str
|
|
192
|
+
Function or variable name.
|
|
193
|
+
path : str
|
|
194
|
+
File that defines the name.
|
|
195
|
+
|
|
196
|
+
Returns
|
|
197
|
+
----------
|
|
198
|
+
bool
|
|
199
|
+
True when another translation unit uses the name.
|
|
200
|
+
"""
|
|
201
|
+
return any(reference.path != path for reference in self.references.get(name, ()))
|
|
202
|
+
|
|
203
|
+
def resolve_include(self, include: Include, from_path: str) -> str | None:
|
|
204
|
+
"""
|
|
205
|
+
Find the project file an `#include "..."` refers to: relative to the including file first, then
|
|
206
|
+
to the project root, then by unique file name.
|
|
207
|
+
|
|
208
|
+
Parameters
|
|
209
|
+
----------
|
|
210
|
+
include : Include
|
|
211
|
+
The include directive.
|
|
212
|
+
from_path : str
|
|
213
|
+
File that contains the directive.
|
|
214
|
+
|
|
215
|
+
Returns
|
|
216
|
+
----------
|
|
217
|
+
str | None
|
|
218
|
+
Relative path of the included project file, or None (system or unknown header).
|
|
219
|
+
"""
|
|
220
|
+
if not include.target:
|
|
221
|
+
return None
|
|
222
|
+
candidates: list[str] = [
|
|
223
|
+
str(PurePosixPath(from_path).parent / include.target),
|
|
224
|
+
include.target,
|
|
225
|
+
]
|
|
226
|
+
for candidate in candidates:
|
|
227
|
+
normalised: str = str(PurePosixPath(*[part for part in PurePosixPath(candidate).parts if part != "."]))
|
|
228
|
+
if normalised in self.files:
|
|
229
|
+
return normalised
|
|
230
|
+
name: str = PurePosixPath(include.target).name
|
|
231
|
+
matches: list[str] = [path for path in self.files if PurePosixPath(path).name == name]
|
|
232
|
+
return matches[0] if len(matches) == 1 else None
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def build_project_context(
|
|
236
|
+
contents: Mapping[str, bytes],
|
|
237
|
+
*,
|
|
238
|
+
root: str = ".",
|
|
239
|
+
skipped: Mapping[str, str] | None = None,
|
|
240
|
+
extra_type_names: Iterable[str] = (),
|
|
241
|
+
) -> ProjectContext:
|
|
242
|
+
"""
|
|
243
|
+
Load a whole project in memory: decode every file, then lex all C/H files, collect the typedef names
|
|
244
|
+
of the whole project, and parse every C/H file with them (so a file can use a type declared in
|
|
245
|
+
another one).
|
|
246
|
+
|
|
247
|
+
Parameters
|
|
248
|
+
----------
|
|
249
|
+
contents : Mapping[str, bytes]
|
|
250
|
+
Content of every file, by relative POSIX path.
|
|
251
|
+
root : str
|
|
252
|
+
Display name of the project root.
|
|
253
|
+
skipped : Mapping[str, str] | None
|
|
254
|
+
Files listed without content, with the reason (e.g. too large).
|
|
255
|
+
extra_type_names : Iterable[str]
|
|
256
|
+
Typedef names declared outside these files (re-analysis of one file of a larger project).
|
|
257
|
+
|
|
258
|
+
Returns
|
|
259
|
+
----------
|
|
260
|
+
ProjectContext
|
|
261
|
+
The project context.
|
|
262
|
+
"""
|
|
263
|
+
files: dict[str, SourceFile] = {}
|
|
264
|
+
for path in sorted(set(contents) | set(skipped or {})):
|
|
265
|
+
reason: str | None = (skipped or {}).get(path)
|
|
266
|
+
files[path] = build_source_file(path, b"" if reason else contents[path], skipped=reason)
|
|
267
|
+
tokens: dict[str, tuple[Token, ...]] = {path: lex_unit(source) for path, source in files.items() if source.is_c_like}
|
|
268
|
+
type_names: set[str] = set(extra_type_names)
|
|
269
|
+
for stream in tokens.values():
|
|
270
|
+
type_names.update(collect_typedef_names(stream))
|
|
271
|
+
units: dict[str, CUnit] = {path: build_unit(files[path], type_names, stream) for path, stream in tokens.items()}
|
|
272
|
+
every_unit: list[CUnit] = list(units.values())
|
|
273
|
+
return ProjectContext(
|
|
274
|
+
root=root,
|
|
275
|
+
files=MappingProxyType(files),
|
|
276
|
+
units=MappingProxyType(units),
|
|
277
|
+
type_names=frozenset(type_names),
|
|
278
|
+
function_definitions=_group((function.name, function) for unit in every_unit for function in unit.functions),
|
|
279
|
+
function_prototypes=_group((function.name, function) for unit in every_unit for function in unit.prototypes),
|
|
280
|
+
calls=_group((call.name, call) for unit in every_unit for call in unit.calls),
|
|
281
|
+
references=_group((reference.name, reference) for unit in every_unit for reference in unit.references),
|
|
282
|
+
variables=_group((variable.name, variable) for unit in every_unit for variable in unit.variables),
|
|
283
|
+
typedefs=_group((typedef.name, typedef) for unit in every_unit for typedef in unit.typedefs),
|
|
284
|
+
macros=_group((macro.name, macro) for unit in every_unit for macro in unit.macros),
|
|
285
|
+
includes=MappingProxyType({unit.path: unit.includes for unit in every_unit}),
|
|
286
|
+
)
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from bisect import bisect_right
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
from pathlib import PurePosixPath
|
|
5
|
+
|
|
6
|
+
from sources.enums import FileKind
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# End of a physical line: CRLF, LF or a lone CR (never `str.splitlines`, which also splits on form feeds).
|
|
10
|
+
EOL_PATTERN: re.Pattern[str] = re.compile(r"\r\n|\n|\r")
|
|
11
|
+
|
|
12
|
+
# UTF-8 byte order mark.
|
|
13
|
+
UTF8_BOM: bytes = b"\xef\xbb\xbf"
|
|
14
|
+
|
|
15
|
+
# File names recognised as Makefiles (besides the `.mk` extension).
|
|
16
|
+
MAKEFILE_NAMES: frozenset[str] = frozenset({"Makefile", "makefile", "GNUmakefile"})
|
|
17
|
+
|
|
18
|
+
# Extensions mapped to a file kind (lower-case, with the dot).
|
|
19
|
+
EXTENSION_KINDS: dict[str, FileKind] = {
|
|
20
|
+
".c": FileKind.C,
|
|
21
|
+
".h": FileKind.H,
|
|
22
|
+
".mk": FileKind.MAKE,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def detect_file_kind(path: str) -> FileKind:
|
|
27
|
+
"""
|
|
28
|
+
Find the kind of a file from its name.
|
|
29
|
+
|
|
30
|
+
Parameters
|
|
31
|
+
----------
|
|
32
|
+
path : str
|
|
33
|
+
Path of the file (only the last component is used).
|
|
34
|
+
|
|
35
|
+
Returns
|
|
36
|
+
----------
|
|
37
|
+
FileKind
|
|
38
|
+
The kind of the file.
|
|
39
|
+
"""
|
|
40
|
+
name: PurePosixPath = PurePosixPath(path)
|
|
41
|
+
if name.name in MAKEFILE_NAMES:
|
|
42
|
+
return FileKind.MAKE
|
|
43
|
+
return EXTENSION_KINDS.get(name.suffix.lower(), FileKind.OTHER)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def visual_width(text: str, tab_width: int) -> int:
|
|
47
|
+
"""
|
|
48
|
+
Width of a text on screen, a tab moving to the next multiple of `tab_width`.
|
|
49
|
+
|
|
50
|
+
Parameters
|
|
51
|
+
----------
|
|
52
|
+
text : str
|
|
53
|
+
Text of one physical line (without its end of line).
|
|
54
|
+
tab_width : int
|
|
55
|
+
Number of columns between two tab stops.
|
|
56
|
+
|
|
57
|
+
Returns
|
|
58
|
+
----------
|
|
59
|
+
int
|
|
60
|
+
Number of screen columns used by the text.
|
|
61
|
+
"""
|
|
62
|
+
width: int = 0
|
|
63
|
+
for char in text:
|
|
64
|
+
if char == "\t":
|
|
65
|
+
width += tab_width - width % tab_width
|
|
66
|
+
else:
|
|
67
|
+
width += 1
|
|
68
|
+
return width
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def visual_overflow_index(text: str, tab_width: int, limit: int) -> int | None:
|
|
72
|
+
"""
|
|
73
|
+
Index of the first character that ends past a given screen column.
|
|
74
|
+
|
|
75
|
+
Parameters
|
|
76
|
+
----------
|
|
77
|
+
text : str
|
|
78
|
+
Text of one physical line (without its end of line).
|
|
79
|
+
tab_width : int
|
|
80
|
+
Number of columns between two tab stops.
|
|
81
|
+
limit : int
|
|
82
|
+
Last allowed screen column.
|
|
83
|
+
|
|
84
|
+
Returns
|
|
85
|
+
----------
|
|
86
|
+
int | None
|
|
87
|
+
Index of the first character ending after `limit`, or None when the text fits.
|
|
88
|
+
"""
|
|
89
|
+
width: int = 0
|
|
90
|
+
for index, char in enumerate(text):
|
|
91
|
+
width += tab_width - width % tab_width if char == "\t" else 1
|
|
92
|
+
if width > limit:
|
|
93
|
+
return index
|
|
94
|
+
return None
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@dataclass(frozen=True, slots=True)
|
|
98
|
+
class Line:
|
|
99
|
+
"""
|
|
100
|
+
One physical line of a text file.
|
|
101
|
+
(dataclass)
|
|
102
|
+
|
|
103
|
+
Attributes
|
|
104
|
+
----------
|
|
105
|
+
number : int
|
|
106
|
+
1-based line number.
|
|
107
|
+
start : int
|
|
108
|
+
Offset of the first character of the line in the decoded text.
|
|
109
|
+
text : str
|
|
110
|
+
Content of the line, without its end of line.
|
|
111
|
+
eol : str
|
|
112
|
+
End of line of the line ("\\n", "\\r\\n", "\\r"), or "" for a last line without one.
|
|
113
|
+
"""
|
|
114
|
+
|
|
115
|
+
number: int
|
|
116
|
+
start: int
|
|
117
|
+
text: str
|
|
118
|
+
eol: str
|
|
119
|
+
|
|
120
|
+
@property
|
|
121
|
+
def end(self) -> int:
|
|
122
|
+
"""
|
|
123
|
+
Offset just after the last character of the line content (before the end of line).
|
|
124
|
+
|
|
125
|
+
Returns
|
|
126
|
+
----------
|
|
127
|
+
int
|
|
128
|
+
The end offset.
|
|
129
|
+
"""
|
|
130
|
+
return self.start + len(self.text)
|
|
131
|
+
|
|
132
|
+
@property
|
|
133
|
+
def is_blank(self) -> bool:
|
|
134
|
+
"""
|
|
135
|
+
Whether the line holds only spaces and tabs (or nothing).
|
|
136
|
+
|
|
137
|
+
Returns
|
|
138
|
+
----------
|
|
139
|
+
bool
|
|
140
|
+
True for a blank line.
|
|
141
|
+
"""
|
|
142
|
+
return not self.text.strip(" \t\f\v")
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def split_lines(text: str) -> tuple[Line, ...]:
|
|
146
|
+
"""
|
|
147
|
+
Split a decoded text into physical lines. A last line without end of line exists only when it is
|
|
148
|
+
not empty, so "a\\n" has one line and "a\\n\\n" has two.
|
|
149
|
+
|
|
150
|
+
Parameters
|
|
151
|
+
----------
|
|
152
|
+
text : str
|
|
153
|
+
Decoded content of a file.
|
|
154
|
+
|
|
155
|
+
Returns
|
|
156
|
+
----------
|
|
157
|
+
tuple[Line, ...]
|
|
158
|
+
The lines, in order.
|
|
159
|
+
"""
|
|
160
|
+
lines: list[Line] = []
|
|
161
|
+
start: int = 0
|
|
162
|
+
for match in EOL_PATTERN.finditer(text):
|
|
163
|
+
lines.append(Line(len(lines) + 1, start, text[start:match.start()], match.group()))
|
|
164
|
+
start = match.end()
|
|
165
|
+
if start < len(text):
|
|
166
|
+
lines.append(Line(len(lines) + 1, start, text[start:], ""))
|
|
167
|
+
return tuple(lines)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
@dataclass(frozen=True, slots=True)
|
|
171
|
+
class SourceFile:
|
|
172
|
+
"""
|
|
173
|
+
One file of the project, loaded in memory. C and H files are additionally analysed into a CUnit.
|
|
174
|
+
(dataclass)
|
|
175
|
+
|
|
176
|
+
Attributes
|
|
177
|
+
----------
|
|
178
|
+
path : str
|
|
179
|
+
Path relative to the project root, with POSIX separators.
|
|
180
|
+
raw : bytes
|
|
181
|
+
Exact content of the file (empty when the file was skipped).
|
|
182
|
+
kind : FileKind
|
|
183
|
+
Kind of the file, from its name.
|
|
184
|
+
text : str
|
|
185
|
+
Content decoded as UTF-8 (invalid bytes kept with `surrogateescape`, BOM removed).
|
|
186
|
+
has_bom : bool
|
|
187
|
+
Whether the file starts with a UTF-8 byte order mark.
|
|
188
|
+
decode_error : bool
|
|
189
|
+
Whether some bytes are not valid UTF-8.
|
|
190
|
+
binary : bool
|
|
191
|
+
Whether the file contains NUL bytes (it then has no text and no lines).
|
|
192
|
+
skipped : str | None
|
|
193
|
+
Reason why the content was not loaded (e.g. size cap), or None.
|
|
194
|
+
lines : tuple[Line, ...]
|
|
195
|
+
Physical lines of the text.
|
|
196
|
+
"""
|
|
197
|
+
|
|
198
|
+
path: str
|
|
199
|
+
raw: bytes
|
|
200
|
+
kind: FileKind
|
|
201
|
+
text: str
|
|
202
|
+
has_bom: bool
|
|
203
|
+
decode_error: bool
|
|
204
|
+
binary: bool
|
|
205
|
+
skipped: str | None
|
|
206
|
+
lines: tuple[Line, ...]
|
|
207
|
+
_line_starts: tuple[int, ...] = field(repr=False, compare=False)
|
|
208
|
+
|
|
209
|
+
@property
|
|
210
|
+
def is_c_like(self) -> bool:
|
|
211
|
+
"""
|
|
212
|
+
Whether the file is analysed by the C lexer and parser.
|
|
213
|
+
|
|
214
|
+
Returns
|
|
215
|
+
----------
|
|
216
|
+
bool
|
|
217
|
+
True for `.c` and `.h` files that have text.
|
|
218
|
+
"""
|
|
219
|
+
return self.kind in (FileKind.C, FileKind.H) and not self.binary and self.skipped is None
|
|
220
|
+
|
|
221
|
+
def position(self, offset: int) -> tuple[int, int]:
|
|
222
|
+
"""
|
|
223
|
+
Line and column of a character offset of the decoded text.
|
|
224
|
+
|
|
225
|
+
Parameters
|
|
226
|
+
----------
|
|
227
|
+
offset : int
|
|
228
|
+
0-based character offset.
|
|
229
|
+
|
|
230
|
+
Returns
|
|
231
|
+
----------
|
|
232
|
+
tuple[int, int]
|
|
233
|
+
1-based line and 1-based column (in code points).
|
|
234
|
+
"""
|
|
235
|
+
if not self._line_starts:
|
|
236
|
+
return 1, 1
|
|
237
|
+
index: int = max(bisect_right(self._line_starts, offset) - 1, 0)
|
|
238
|
+
return index + 1, offset - self._line_starts[index] + 1
|
|
239
|
+
|
|
240
|
+
def char_offset_of_byte(self, byte_offset: int) -> int:
|
|
241
|
+
"""
|
|
242
|
+
Convert an offset in the raw bytes into an offset in the decoded text.
|
|
243
|
+
|
|
244
|
+
Parameters
|
|
245
|
+
----------
|
|
246
|
+
byte_offset : int
|
|
247
|
+
0-based offset in `raw`.
|
|
248
|
+
|
|
249
|
+
Returns
|
|
250
|
+
----------
|
|
251
|
+
int
|
|
252
|
+
0-based offset in `text`.
|
|
253
|
+
"""
|
|
254
|
+
skip: int = len(UTF8_BOM) if self.has_bom else 0
|
|
255
|
+
if byte_offset <= skip:
|
|
256
|
+
return 0
|
|
257
|
+
return len(self.raw[skip:byte_offset].decode("utf-8", "surrogateescape"))
|
|
258
|
+
|
|
259
|
+
def line(self, number: int) -> Line:
|
|
260
|
+
"""
|
|
261
|
+
Get a line by its number.
|
|
262
|
+
|
|
263
|
+
Parameters
|
|
264
|
+
----------
|
|
265
|
+
number : int
|
|
266
|
+
1-based line number.
|
|
267
|
+
|
|
268
|
+
Returns
|
|
269
|
+
----------
|
|
270
|
+
Line
|
|
271
|
+
The line.
|
|
272
|
+
|
|
273
|
+
Raises
|
|
274
|
+
----------
|
|
275
|
+
IndexError
|
|
276
|
+
The file has no such line.
|
|
277
|
+
"""
|
|
278
|
+
if number < 1:
|
|
279
|
+
raise IndexError(number)
|
|
280
|
+
return self.lines[number - 1]
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def build_source_file(path: str, raw: bytes, *, skipped: str | None = None) -> SourceFile:
|
|
284
|
+
"""
|
|
285
|
+
Decode a file and split it into lines. Never fails: invalid UTF-8 is kept with `surrogateescape`
|
|
286
|
+
and flagged, binary files get no text.
|
|
287
|
+
|
|
288
|
+
Parameters
|
|
289
|
+
----------
|
|
290
|
+
path : str
|
|
291
|
+
Path relative to the project root, with POSIX separators.
|
|
292
|
+
raw : bytes
|
|
293
|
+
Exact content of the file.
|
|
294
|
+
skipped : str | None
|
|
295
|
+
Reason why the content was not loaded, or None.
|
|
296
|
+
|
|
297
|
+
Returns
|
|
298
|
+
----------
|
|
299
|
+
SourceFile
|
|
300
|
+
The loaded file.
|
|
301
|
+
"""
|
|
302
|
+
kind: FileKind = detect_file_kind(path)
|
|
303
|
+
binary: bool = b"\0" in raw
|
|
304
|
+
has_bom: bool = raw.startswith(UTF8_BOM)
|
|
305
|
+
text: str = ""
|
|
306
|
+
decode_error: bool = False
|
|
307
|
+
if not binary and skipped is None:
|
|
308
|
+
body: bytes = raw[len(UTF8_BOM):] if has_bom else raw
|
|
309
|
+
try:
|
|
310
|
+
text = body.decode("utf-8")
|
|
311
|
+
except UnicodeDecodeError:
|
|
312
|
+
text = body.decode("utf-8", "surrogateescape")
|
|
313
|
+
decode_error = True
|
|
314
|
+
lines: tuple[Line, ...] = split_lines(text)
|
|
315
|
+
return SourceFile(
|
|
316
|
+
path=path,
|
|
317
|
+
raw=raw,
|
|
318
|
+
kind=kind,
|
|
319
|
+
text=text,
|
|
320
|
+
has_bom=has_bom,
|
|
321
|
+
decode_error=decode_error,
|
|
322
|
+
binary=binary,
|
|
323
|
+
skipped=skipped,
|
|
324
|
+
lines=lines,
|
|
325
|
+
_line_starts=tuple(line.start for line in lines),
|
|
326
|
+
)
|