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
sources/analysis/unit.py
ADDED
|
@@ -0,0 +1,700 @@
|
|
|
1
|
+
from bisect import bisect_right
|
|
2
|
+
from collections.abc import Iterable
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
|
|
5
|
+
from sources.analysis.directives import Directive, Include, MacroDefinition, collect_directives, collect_includes, collect_macros
|
|
6
|
+
from sources.analysis.lexer import tokenize
|
|
7
|
+
from sources.analysis.parser import parse_tokens
|
|
8
|
+
from sources.analysis.source import SourceFile
|
|
9
|
+
from sources.analysis.syntax import Node
|
|
10
|
+
from sources.analysis.tokens import Token
|
|
11
|
+
from sources.enums import CodeContext, NodeKind, TokenKind
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# Node kinds whose header is a parenthesised controlling expression.
|
|
15
|
+
CONTROL_STATEMENTS: frozenset[NodeKind] = frozenset({
|
|
16
|
+
NodeKind.IF_STATEMENT, NodeKind.WHILE_STATEMENT, NodeKind.DO_STATEMENT, NodeKind.SWITCH_STATEMENT, NodeKind.FOR_STATEMENT,
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True, slots=True)
|
|
21
|
+
class ParameterInfo:
|
|
22
|
+
"""
|
|
23
|
+
One parameter of a function.
|
|
24
|
+
(dataclass)
|
|
25
|
+
|
|
26
|
+
Attributes
|
|
27
|
+
----------
|
|
28
|
+
name : str | None
|
|
29
|
+
Parameter name (None in prototypes without names).
|
|
30
|
+
type_text : str
|
|
31
|
+
Declared type, as written (tokens separated by spaces).
|
|
32
|
+
pointer_depth : int
|
|
33
|
+
Number of `*` in the declarator.
|
|
34
|
+
record_kind : str | None
|
|
35
|
+
"struct" or "union" when the parameter type is a struct/union written directly, else None.
|
|
36
|
+
node : Node
|
|
37
|
+
The PARAMETER node.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
name: str | None
|
|
41
|
+
type_text: str
|
|
42
|
+
pointer_depth: int
|
|
43
|
+
record_kind: str | None
|
|
44
|
+
node: Node
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass(frozen=True, slots=True)
|
|
48
|
+
class FunctionInfo:
|
|
49
|
+
"""
|
|
50
|
+
A function definition or prototype.
|
|
51
|
+
(dataclass)
|
|
52
|
+
|
|
53
|
+
Attributes
|
|
54
|
+
----------
|
|
55
|
+
name : str
|
|
56
|
+
Function name.
|
|
57
|
+
path : str
|
|
58
|
+
File of the function.
|
|
59
|
+
node : Node
|
|
60
|
+
FUNCTION_DEFINITION node, or the DECLARATION node of a prototype.
|
|
61
|
+
declarator : Node
|
|
62
|
+
The function declarator.
|
|
63
|
+
storage : frozenset[str]
|
|
64
|
+
Storage class and function specifiers (`static`, `inline`...).
|
|
65
|
+
parameters : tuple[ParameterInfo, ...]
|
|
66
|
+
Parameters (empty for `(void)` and `()`).
|
|
67
|
+
variadic : bool
|
|
68
|
+
Whether the parameter list ends with `...`.
|
|
69
|
+
empty_parameter_list : bool
|
|
70
|
+
Whether the parameter list is `()` (neither parameters nor `void`).
|
|
71
|
+
is_definition : bool
|
|
72
|
+
True for a definition (with body), False for a prototype.
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
name: str
|
|
76
|
+
path: str
|
|
77
|
+
node: Node
|
|
78
|
+
declarator: Node
|
|
79
|
+
storage: frozenset[str]
|
|
80
|
+
parameters: tuple[ParameterInfo, ...]
|
|
81
|
+
variadic: bool
|
|
82
|
+
empty_parameter_list: bool
|
|
83
|
+
is_definition: bool
|
|
84
|
+
|
|
85
|
+
@property
|
|
86
|
+
def is_static(self) -> bool:
|
|
87
|
+
"""
|
|
88
|
+
Whether the function has internal linkage.
|
|
89
|
+
|
|
90
|
+
Returns
|
|
91
|
+
----------
|
|
92
|
+
bool
|
|
93
|
+
True for `static` functions.
|
|
94
|
+
"""
|
|
95
|
+
return "static" in self.storage
|
|
96
|
+
|
|
97
|
+
@property
|
|
98
|
+
def body(self) -> Node | None:
|
|
99
|
+
"""
|
|
100
|
+
Body of a definition.
|
|
101
|
+
|
|
102
|
+
Returns
|
|
103
|
+
----------
|
|
104
|
+
Node | None
|
|
105
|
+
The COMPOUND_STATEMENT node, or None for a prototype.
|
|
106
|
+
"""
|
|
107
|
+
return self.node.fields.get("body") if self.is_definition else None
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@dataclass(frozen=True, slots=True)
|
|
111
|
+
class VariableInfo:
|
|
112
|
+
"""
|
|
113
|
+
A variable declared at file scope, or a `static` local variable.
|
|
114
|
+
(dataclass)
|
|
115
|
+
|
|
116
|
+
Attributes
|
|
117
|
+
----------
|
|
118
|
+
name : str
|
|
119
|
+
Variable name.
|
|
120
|
+
path : str
|
|
121
|
+
File of the declaration.
|
|
122
|
+
node : Node
|
|
123
|
+
The declarator (or INIT_DECLARATOR) node.
|
|
124
|
+
declaration : Node
|
|
125
|
+
The enclosing DECLARATION node.
|
|
126
|
+
storage : frozenset[str]
|
|
127
|
+
Storage class specifiers.
|
|
128
|
+
is_definition : bool
|
|
129
|
+
False for `extern` declarations without initialiser.
|
|
130
|
+
is_constant : bool
|
|
131
|
+
Whether the variable itself is constant (its own top-level declarator level is `const`).
|
|
132
|
+
local : bool
|
|
133
|
+
True for a `static` variable declared inside a function.
|
|
134
|
+
"""
|
|
135
|
+
|
|
136
|
+
name: str
|
|
137
|
+
path: str
|
|
138
|
+
node: Node
|
|
139
|
+
declaration: Node
|
|
140
|
+
storage: frozenset[str]
|
|
141
|
+
is_definition: bool
|
|
142
|
+
is_constant: bool
|
|
143
|
+
local: bool
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@dataclass(frozen=True, slots=True)
|
|
147
|
+
class TypedefInfo:
|
|
148
|
+
"""
|
|
149
|
+
A name declared by `typedef`.
|
|
150
|
+
(dataclass)
|
|
151
|
+
|
|
152
|
+
Attributes
|
|
153
|
+
----------
|
|
154
|
+
name : str
|
|
155
|
+
Type name.
|
|
156
|
+
path : str
|
|
157
|
+
File of the declaration.
|
|
158
|
+
node : Node
|
|
159
|
+
The declarator node.
|
|
160
|
+
declaration : Node
|
|
161
|
+
The TYPE_DEFINITION node.
|
|
162
|
+
record_kind : str | None
|
|
163
|
+
"struct", "union" or "enum" when the type is one of them without pointer, else None.
|
|
164
|
+
"""
|
|
165
|
+
|
|
166
|
+
name: str
|
|
167
|
+
path: str
|
|
168
|
+
node: Node
|
|
169
|
+
declaration: Node
|
|
170
|
+
record_kind: str | None
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
@dataclass(frozen=True, slots=True)
|
|
174
|
+
class CallInfo:
|
|
175
|
+
"""
|
|
176
|
+
A call to a named function.
|
|
177
|
+
(dataclass)
|
|
178
|
+
|
|
179
|
+
Attributes
|
|
180
|
+
----------
|
|
181
|
+
name : str
|
|
182
|
+
Called name.
|
|
183
|
+
path : str
|
|
184
|
+
File of the call.
|
|
185
|
+
node : Node
|
|
186
|
+
The CALL_EXPRESSION node.
|
|
187
|
+
caller : str | None
|
|
188
|
+
Name of the function containing the call (None outside functions).
|
|
189
|
+
"""
|
|
190
|
+
|
|
191
|
+
name: str
|
|
192
|
+
path: str
|
|
193
|
+
node: Node
|
|
194
|
+
caller: str | None
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
@dataclass(frozen=True, slots=True)
|
|
198
|
+
class ReferenceInfo:
|
|
199
|
+
"""
|
|
200
|
+
A use of a name in an expression (calls included).
|
|
201
|
+
(dataclass)
|
|
202
|
+
|
|
203
|
+
Attributes
|
|
204
|
+
----------
|
|
205
|
+
name : str
|
|
206
|
+
Used name.
|
|
207
|
+
path : str
|
|
208
|
+
File of the use.
|
|
209
|
+
node : Node
|
|
210
|
+
The IDENTIFIER node.
|
|
211
|
+
function : str | None
|
|
212
|
+
Name of the function containing the use (None outside functions).
|
|
213
|
+
"""
|
|
214
|
+
|
|
215
|
+
name: str
|
|
216
|
+
path: str
|
|
217
|
+
node: Node
|
|
218
|
+
function: str | None
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _record_kind(specifiers_type_node: Node | None, pointer_depth: int) -> str | None:
|
|
222
|
+
"""
|
|
223
|
+
"struct"/"union"/"enum" when a type is written as one of them without pointer.
|
|
224
|
+
|
|
225
|
+
Parameters
|
|
226
|
+
----------
|
|
227
|
+
specifiers_type_node : Node | None
|
|
228
|
+
Type node from the declaration specifiers.
|
|
229
|
+
pointer_depth : int
|
|
230
|
+
Number of `*` in the declarator.
|
|
231
|
+
|
|
232
|
+
Returns
|
|
233
|
+
----------
|
|
234
|
+
str | None
|
|
235
|
+
The record kind, or None.
|
|
236
|
+
"""
|
|
237
|
+
if specifiers_type_node is None or pointer_depth:
|
|
238
|
+
return None
|
|
239
|
+
return {
|
|
240
|
+
NodeKind.STRUCT_SPECIFIER: "struct",
|
|
241
|
+
NodeKind.UNION_SPECIFIER: "union",
|
|
242
|
+
NodeKind.ENUM_SPECIFIER: "enum",
|
|
243
|
+
}.get(specifiers_type_node.kind)
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def _declarator_of(node: Node) -> Node:
|
|
247
|
+
"""
|
|
248
|
+
The declarator inside an INIT_DECLARATOR (or the node itself).
|
|
249
|
+
|
|
250
|
+
Parameters
|
|
251
|
+
----------
|
|
252
|
+
node : Node
|
|
253
|
+
A declarator or INIT_DECLARATOR node.
|
|
254
|
+
|
|
255
|
+
Returns
|
|
256
|
+
----------
|
|
257
|
+
Node
|
|
258
|
+
The declarator.
|
|
259
|
+
"""
|
|
260
|
+
return node.fields["declarator"] if node.kind is NodeKind.INIT_DECLARATOR else node
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def _parameters_of(declarator: Node) -> tuple[tuple[ParameterInfo, ...], bool, bool]:
|
|
264
|
+
"""
|
|
265
|
+
Parameters of a function declarator.
|
|
266
|
+
|
|
267
|
+
Parameters
|
|
268
|
+
----------
|
|
269
|
+
declarator : Node
|
|
270
|
+
A declarator whose `declares_function` field is True.
|
|
271
|
+
|
|
272
|
+
Returns
|
|
273
|
+
----------
|
|
274
|
+
tuple[tuple[ParameterInfo, ...], bool, bool]
|
|
275
|
+
The parameters, whether the list is variadic, and whether it is empty (`()`).
|
|
276
|
+
"""
|
|
277
|
+
parameter_list: Node | None = declarator.fields.get("parameters")
|
|
278
|
+
if parameter_list is None:
|
|
279
|
+
return (), False, True
|
|
280
|
+
parameters: list[ParameterInfo] = []
|
|
281
|
+
for parameter in parameter_list.fields["parameters"]:
|
|
282
|
+
specifiers = parameter.fields["specifiers"]
|
|
283
|
+
parameters.append(ParameterInfo(
|
|
284
|
+
name=parameter.name,
|
|
285
|
+
type_text=parameter.fields["type_text"],
|
|
286
|
+
pointer_depth=parameter.fields["pointer_depth"],
|
|
287
|
+
record_kind=_record_kind(specifiers.type_node, parameter.fields["pointer_depth"]),
|
|
288
|
+
node=parameter,
|
|
289
|
+
))
|
|
290
|
+
return tuple(parameters), parameter_list.fields["variadic"], parameter_list.fields["empty"]
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def _is_constant(declarator: Node, qualifiers: frozenset[str]) -> bool:
|
|
294
|
+
"""
|
|
295
|
+
Whether the declared object itself is constant: `const int x` and `char *const p` are,
|
|
296
|
+
`const char *p` is not.
|
|
297
|
+
|
|
298
|
+
Parameters
|
|
299
|
+
----------
|
|
300
|
+
declarator : Node
|
|
301
|
+
The declarator.
|
|
302
|
+
qualifiers : frozenset[str]
|
|
303
|
+
Qualifiers of the declaration specifiers.
|
|
304
|
+
|
|
305
|
+
Returns
|
|
306
|
+
----------
|
|
307
|
+
bool
|
|
308
|
+
True when the object cannot be modified.
|
|
309
|
+
"""
|
|
310
|
+
pointers: tuple[tuple[str, ...], ...] = declarator.fields.get("pointers", ())
|
|
311
|
+
nested: Node | None = declarator.fields.get("nested")
|
|
312
|
+
if nested is not None and nested.fields.get("pointers"):
|
|
313
|
+
return "const" in nested.fields["pointers"][-1]
|
|
314
|
+
if pointers:
|
|
315
|
+
return "const" in pointers[-1]
|
|
316
|
+
return "const" in qualifiers
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
@dataclass(slots=True)
|
|
320
|
+
class CUnit:
|
|
321
|
+
"""
|
|
322
|
+
Everything known about one C or H file: tokens, directives, syntax tree and the facts extracted
|
|
323
|
+
from it. Built once when the project is loaded and shared by every rule.
|
|
324
|
+
(dataclass)
|
|
325
|
+
|
|
326
|
+
Attributes
|
|
327
|
+
----------
|
|
328
|
+
source : SourceFile
|
|
329
|
+
The file.
|
|
330
|
+
tokens : tuple[Token, ...]
|
|
331
|
+
Every token (lossless).
|
|
332
|
+
directives : tuple[Directive, ...]
|
|
333
|
+
Preprocessor directives.
|
|
334
|
+
macros : tuple[MacroDefinition, ...]
|
|
335
|
+
`#define` directives.
|
|
336
|
+
includes : tuple[Include, ...]
|
|
337
|
+
`#include` directives.
|
|
338
|
+
comments : tuple[Token, ...]
|
|
339
|
+
Comment tokens.
|
|
340
|
+
tree : Node
|
|
341
|
+
TRANSLATION_UNIT node.
|
|
342
|
+
errors : tuple[Node, ...]
|
|
343
|
+
ERROR nodes (regions the parser could not understand).
|
|
344
|
+
functions : tuple[FunctionInfo, ...]
|
|
345
|
+
Function definitions.
|
|
346
|
+
prototypes : tuple[FunctionInfo, ...]
|
|
347
|
+
Function prototypes (declarations without body).
|
|
348
|
+
variables : tuple[VariableInfo, ...]
|
|
349
|
+
File-scope variables and `static` locals.
|
|
350
|
+
typedefs : tuple[TypedefInfo, ...]
|
|
351
|
+
Typedef names.
|
|
352
|
+
calls : tuple[CallInfo, ...]
|
|
353
|
+
Calls to named functions.
|
|
354
|
+
references : tuple[ReferenceInfo, ...]
|
|
355
|
+
Names used in expressions.
|
|
356
|
+
"""
|
|
357
|
+
|
|
358
|
+
source: SourceFile
|
|
359
|
+
tokens: tuple[Token, ...]
|
|
360
|
+
directives: tuple[Directive, ...]
|
|
361
|
+
macros: tuple[MacroDefinition, ...]
|
|
362
|
+
includes: tuple[Include, ...]
|
|
363
|
+
comments: tuple[Token, ...]
|
|
364
|
+
tree: Node
|
|
365
|
+
errors: tuple[Node, ...]
|
|
366
|
+
functions: tuple[FunctionInfo, ...] = ()
|
|
367
|
+
prototypes: tuple[FunctionInfo, ...] = ()
|
|
368
|
+
variables: tuple[VariableInfo, ...] = ()
|
|
369
|
+
typedefs: tuple[TypedefInfo, ...] = ()
|
|
370
|
+
calls: tuple[CallInfo, ...] = ()
|
|
371
|
+
references: tuple[ReferenceInfo, ...] = ()
|
|
372
|
+
_nodes_by_kind: dict[NodeKind, list[Node]] = field(default_factory=dict, repr=False)
|
|
373
|
+
_body_spans: list[tuple[int, int]] = field(default_factory=list, repr=False)
|
|
374
|
+
_header_spans: list[tuple[int, int]] = field(default_factory=list, repr=False)
|
|
375
|
+
_control_spans: list[tuple[int, int]] = field(default_factory=list, repr=False)
|
|
376
|
+
_for_spans: list[tuple[int, int]] = field(default_factory=list, repr=False)
|
|
377
|
+
_condition_spans: list[tuple[int, int]] = field(default_factory=list, repr=False)
|
|
378
|
+
_function_starts: list[int] = field(default_factory=list, repr=False)
|
|
379
|
+
_line_tokens: dict[int, list[Token]] = field(default_factory=dict, repr=False)
|
|
380
|
+
|
|
381
|
+
@property
|
|
382
|
+
def path(self) -> str:
|
|
383
|
+
"""
|
|
384
|
+
Path of the file relative to the project root.
|
|
385
|
+
|
|
386
|
+
Returns
|
|
387
|
+
----------
|
|
388
|
+
str
|
|
389
|
+
The path.
|
|
390
|
+
"""
|
|
391
|
+
return self.source.path
|
|
392
|
+
|
|
393
|
+
@property
|
|
394
|
+
def confidence(self) -> str:
|
|
395
|
+
"""
|
|
396
|
+
"full" when the whole file was understood, "partial" when it has error regions.
|
|
397
|
+
|
|
398
|
+
Returns
|
|
399
|
+
----------
|
|
400
|
+
str
|
|
401
|
+
The confidence.
|
|
402
|
+
"""
|
|
403
|
+
return "partial" if self.errors else "full"
|
|
404
|
+
|
|
405
|
+
def nodes(self, *kinds: NodeKind) -> list[Node]:
|
|
406
|
+
"""
|
|
407
|
+
Every node of the given kinds, in source order (index built once).
|
|
408
|
+
|
|
409
|
+
Parameters
|
|
410
|
+
----------
|
|
411
|
+
*kinds : NodeKind
|
|
412
|
+
Accepted kinds.
|
|
413
|
+
|
|
414
|
+
Returns
|
|
415
|
+
----------
|
|
416
|
+
list[Node]
|
|
417
|
+
The nodes.
|
|
418
|
+
"""
|
|
419
|
+
if len(kinds) == 1:
|
|
420
|
+
return list(self._nodes_by_kind.get(kinds[0], ()))
|
|
421
|
+
found: list[Node] = [node for kind in kinds for node in self._nodes_by_kind.get(kind, ())]
|
|
422
|
+
found.sort(key=lambda node: node.start_offset)
|
|
423
|
+
return found
|
|
424
|
+
|
|
425
|
+
def tokens_on_line(self, line: int) -> list[Token]:
|
|
426
|
+
"""
|
|
427
|
+
Tokens that start on a line, in order (index built on first use).
|
|
428
|
+
|
|
429
|
+
Parameters
|
|
430
|
+
----------
|
|
431
|
+
line : int
|
|
432
|
+
1-based line.
|
|
433
|
+
|
|
434
|
+
Returns
|
|
435
|
+
----------
|
|
436
|
+
list[Token]
|
|
437
|
+
The tokens.
|
|
438
|
+
"""
|
|
439
|
+
if not self._line_tokens and self.tokens:
|
|
440
|
+
for token in self.tokens:
|
|
441
|
+
self._line_tokens.setdefault(token.line, []).append(token)
|
|
442
|
+
return self._line_tokens.get(line, [])
|
|
443
|
+
|
|
444
|
+
def in_error(self, offset: int) -> bool:
|
|
445
|
+
"""
|
|
446
|
+
Whether an offset lies in an error region.
|
|
447
|
+
|
|
448
|
+
Parameters
|
|
449
|
+
----------
|
|
450
|
+
offset : int
|
|
451
|
+
0-based character offset.
|
|
452
|
+
|
|
453
|
+
Returns
|
|
454
|
+
----------
|
|
455
|
+
bool
|
|
456
|
+
True inside an ERROR node.
|
|
457
|
+
"""
|
|
458
|
+
return any(error.contains_offset(offset) for error in self.errors)
|
|
459
|
+
|
|
460
|
+
def function_at(self, offset: int) -> FunctionInfo | None:
|
|
461
|
+
"""
|
|
462
|
+
Function definition containing an offset.
|
|
463
|
+
|
|
464
|
+
Parameters
|
|
465
|
+
----------
|
|
466
|
+
offset : int
|
|
467
|
+
0-based character offset.
|
|
468
|
+
|
|
469
|
+
Returns
|
|
470
|
+
----------
|
|
471
|
+
FunctionInfo | None
|
|
472
|
+
The function, or None outside any definition.
|
|
473
|
+
"""
|
|
474
|
+
index: int = bisect_right(self._function_starts, offset) - 1
|
|
475
|
+
if index >= 0 and self.functions[index].node.contains_offset(offset):
|
|
476
|
+
return self.functions[index]
|
|
477
|
+
return None
|
|
478
|
+
|
|
479
|
+
def token_contexts(self, token: Token) -> frozenset[CodeContext]:
|
|
480
|
+
"""
|
|
481
|
+
Contexts a token is in (comment, string, preprocessor, function body...).
|
|
482
|
+
|
|
483
|
+
Parameters
|
|
484
|
+
----------
|
|
485
|
+
token : Token
|
|
486
|
+
A token of this file.
|
|
487
|
+
|
|
488
|
+
Returns
|
|
489
|
+
----------
|
|
490
|
+
frozenset[CodeContext]
|
|
491
|
+
The contexts.
|
|
492
|
+
"""
|
|
493
|
+
contexts: set[CodeContext] = set()
|
|
494
|
+
if token.kind is TokenKind.COMMENT:
|
|
495
|
+
contexts.add(CodeContext.COMMENT)
|
|
496
|
+
if token.kind in (TokenKind.STRING, TokenKind.CHAR, TokenKind.HEADER_NAME):
|
|
497
|
+
contexts.add(CodeContext.STRING)
|
|
498
|
+
if token.in_directive:
|
|
499
|
+
contexts.add(CodeContext.PREPROCESSOR)
|
|
500
|
+
if token.in_macro_body:
|
|
501
|
+
contexts.add(CodeContext.MACRO_BODY)
|
|
502
|
+
contexts.update(self.offset_contexts(token.offset))
|
|
503
|
+
return frozenset(contexts)
|
|
504
|
+
|
|
505
|
+
def offset_contexts(self, offset: int) -> frozenset[CodeContext]:
|
|
506
|
+
"""
|
|
507
|
+
Structural contexts of an offset (function body or header, control headers, conditions, file scope).
|
|
508
|
+
|
|
509
|
+
Parameters
|
|
510
|
+
----------
|
|
511
|
+
offset : int
|
|
512
|
+
0-based character offset.
|
|
513
|
+
|
|
514
|
+
Returns
|
|
515
|
+
----------
|
|
516
|
+
frozenset[CodeContext]
|
|
517
|
+
The contexts.
|
|
518
|
+
"""
|
|
519
|
+
contexts: set[CodeContext] = set()
|
|
520
|
+
if any(start <= offset < end for start, end in self._body_spans):
|
|
521
|
+
contexts.add(CodeContext.FUNCTION_BODY)
|
|
522
|
+
elif any(start <= offset < end for start, end in self._header_spans):
|
|
523
|
+
contexts.add(CodeContext.FUNCTION_HEADER)
|
|
524
|
+
else:
|
|
525
|
+
contexts.add(CodeContext.FILE_SCOPE)
|
|
526
|
+
if any(start <= offset < end for start, end in self._control_spans):
|
|
527
|
+
contexts.add(CodeContext.CONTROL_HEADER)
|
|
528
|
+
if any(start <= offset < end for start, end in self._for_spans):
|
|
529
|
+
contexts.add(CodeContext.FOR_HEADER)
|
|
530
|
+
if any(start <= offset < end for start, end in self._condition_spans):
|
|
531
|
+
contexts.add(CodeContext.CONDITION)
|
|
532
|
+
return frozenset(contexts)
|
|
533
|
+
|
|
534
|
+
def node_contexts(self, node: Node) -> frozenset[CodeContext]:
|
|
535
|
+
"""
|
|
536
|
+
Structural contexts of a node (taken at its first token).
|
|
537
|
+
|
|
538
|
+
Parameters
|
|
539
|
+
----------
|
|
540
|
+
node : Node
|
|
541
|
+
A node of this file.
|
|
542
|
+
|
|
543
|
+
Returns
|
|
544
|
+
----------
|
|
545
|
+
frozenset[CodeContext]
|
|
546
|
+
The contexts.
|
|
547
|
+
"""
|
|
548
|
+
return self.offset_contexts(node.start_offset)
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
def _index_tree(unit: CUnit) -> None:
|
|
552
|
+
"""
|
|
553
|
+
Fill the node index and the context spans of a unit.
|
|
554
|
+
|
|
555
|
+
Parameters
|
|
556
|
+
----------
|
|
557
|
+
unit : CUnit
|
|
558
|
+
The unit to index (modified in place).
|
|
559
|
+
|
|
560
|
+
Returns
|
|
561
|
+
----------
|
|
562
|
+
None
|
|
563
|
+
Nothing.
|
|
564
|
+
"""
|
|
565
|
+
for node in unit.tree.walk():
|
|
566
|
+
unit._nodes_by_kind.setdefault(node.kind, []).append(node)
|
|
567
|
+
if node.kind is NodeKind.FUNCTION_DEFINITION:
|
|
568
|
+
body: Node = node.fields["body"]
|
|
569
|
+
unit._body_spans.append((body.start_offset, body.end_offset))
|
|
570
|
+
unit._header_spans.append((node.start_offset, body.start_offset))
|
|
571
|
+
elif node.kind in CONTROL_STATEMENTS:
|
|
572
|
+
opening: Token | None = node.fields.get("open_paren") or (node.fields["condition"].fields.get("open_paren") if node.fields.get("condition") else None)
|
|
573
|
+
closing: Token | None = node.fields.get("close_paren") or (node.fields["condition"].fields.get("close_paren") if node.fields.get("condition") else None)
|
|
574
|
+
if opening is not None and closing is not None:
|
|
575
|
+
unit._control_spans.append((opening.offset, closing.end_offset))
|
|
576
|
+
if node.kind is NodeKind.FOR_STATEMENT:
|
|
577
|
+
unit._for_spans.append((opening.offset, closing.end_offset))
|
|
578
|
+
elif node.kind is NodeKind.CONDITION:
|
|
579
|
+
unit._condition_spans.append((node.start_offset, node.end_offset))
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
def _extract_facts(unit: CUnit) -> None:
|
|
583
|
+
"""
|
|
584
|
+
Extract functions, prototypes, variables, typedefs, calls and references from a parsed unit.
|
|
585
|
+
|
|
586
|
+
Parameters
|
|
587
|
+
----------
|
|
588
|
+
unit : CUnit
|
|
589
|
+
The unit (modified in place).
|
|
590
|
+
|
|
591
|
+
Returns
|
|
592
|
+
----------
|
|
593
|
+
None
|
|
594
|
+
Nothing.
|
|
595
|
+
"""
|
|
596
|
+
path: str = unit.path
|
|
597
|
+
functions: list[FunctionInfo] = []
|
|
598
|
+
prototypes: list[FunctionInfo] = []
|
|
599
|
+
variables: list[VariableInfo] = []
|
|
600
|
+
typedefs: list[TypedefInfo] = []
|
|
601
|
+
for definition in unit.nodes(NodeKind.FUNCTION_DEFINITION):
|
|
602
|
+
declarator: Node = definition.fields["declarator"]
|
|
603
|
+
parameters, variadic, empty = _parameters_of(declarator)
|
|
604
|
+
functions.append(FunctionInfo(declarator.name or "", path, definition, declarator, definition.fields["storage"], parameters, variadic, empty, True))
|
|
605
|
+
for declaration in unit.nodes(NodeKind.DECLARATION, NodeKind.TYPE_DEFINITION):
|
|
606
|
+
storage: frozenset[str] = declaration.fields["storage"]
|
|
607
|
+
local: bool = declaration.nearest(NodeKind.FUNCTION_DEFINITION) is not None
|
|
608
|
+
for item in declaration.fields["declarators"]:
|
|
609
|
+
declarator = _declarator_of(item)
|
|
610
|
+
if not declarator.name:
|
|
611
|
+
continue
|
|
612
|
+
if declaration.kind is NodeKind.TYPE_DEFINITION:
|
|
613
|
+
type_node: Node | None = declaration.fields["type_node"]
|
|
614
|
+
typedefs.append(TypedefInfo(declarator.name, path, declarator, declaration, _record_kind(type_node, declarator.fields["pointer_depth"])))
|
|
615
|
+
elif declarator.fields["declares_function"]:
|
|
616
|
+
parameters, variadic, empty = _parameters_of(declarator)
|
|
617
|
+
prototypes.append(FunctionInfo(declarator.name, path, declaration, declarator, storage, parameters, variadic, empty, False))
|
|
618
|
+
elif not local or "static" in storage:
|
|
619
|
+
variables.append(VariableInfo(
|
|
620
|
+
name=declarator.name,
|
|
621
|
+
path=path,
|
|
622
|
+
node=item,
|
|
623
|
+
declaration=declaration,
|
|
624
|
+
storage=storage,
|
|
625
|
+
is_definition="extern" not in storage or item.kind is NodeKind.INIT_DECLARATOR,
|
|
626
|
+
is_constant=_is_constant(declarator, declaration.fields["qualifiers"]),
|
|
627
|
+
local=local,
|
|
628
|
+
))
|
|
629
|
+
unit.functions = tuple(functions)
|
|
630
|
+
unit._function_starts = [function.node.start_offset for function in functions]
|
|
631
|
+
calls: list[CallInfo] = []
|
|
632
|
+
references: list[ReferenceInfo] = []
|
|
633
|
+
for call in unit.nodes(NodeKind.CALL_EXPRESSION):
|
|
634
|
+
if call.name:
|
|
635
|
+
owner: FunctionInfo | None = unit.function_at(call.start_offset)
|
|
636
|
+
calls.append(CallInfo(call.name, path, call, owner.name if owner else None))
|
|
637
|
+
for identifier in unit.nodes(NodeKind.IDENTIFIER):
|
|
638
|
+
if identifier.fields.get("constant"):
|
|
639
|
+
continue
|
|
640
|
+
owner = unit.function_at(identifier.start_offset)
|
|
641
|
+
references.append(ReferenceInfo(identifier.name or "", path, identifier, owner.name if owner else None))
|
|
642
|
+
unit.prototypes = tuple(prototypes)
|
|
643
|
+
unit.variables = tuple(variables)
|
|
644
|
+
unit.typedefs = tuple(typedefs)
|
|
645
|
+
unit.calls = tuple(calls)
|
|
646
|
+
unit.references = tuple(references)
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
def lex_unit(source: SourceFile) -> tuple[Token, ...]:
|
|
650
|
+
"""
|
|
651
|
+
Tokenize a C or H file (first step of building a unit, used to collect project typedef names).
|
|
652
|
+
|
|
653
|
+
Parameters
|
|
654
|
+
----------
|
|
655
|
+
source : SourceFile
|
|
656
|
+
The file.
|
|
657
|
+
|
|
658
|
+
Returns
|
|
659
|
+
----------
|
|
660
|
+
tuple[Token, ...]
|
|
661
|
+
Its tokens.
|
|
662
|
+
"""
|
|
663
|
+
return tokenize(source.text)
|
|
664
|
+
|
|
665
|
+
|
|
666
|
+
def build_unit(source: SourceFile, type_names: Iterable[str] = (), tokens: tuple[Token, ...] | None = None) -> CUnit:
|
|
667
|
+
"""
|
|
668
|
+
Lex, parse and analyse a C or H file.
|
|
669
|
+
|
|
670
|
+
Parameters
|
|
671
|
+
----------
|
|
672
|
+
source : SourceFile
|
|
673
|
+
The file.
|
|
674
|
+
type_names : Iterable[str]
|
|
675
|
+
Typedef names known in the project.
|
|
676
|
+
tokens : tuple[Token, ...] | None
|
|
677
|
+
Tokens already produced by `lex_unit`, to avoid lexing twice.
|
|
678
|
+
|
|
679
|
+
Returns
|
|
680
|
+
----------
|
|
681
|
+
CUnit
|
|
682
|
+
The analysed unit.
|
|
683
|
+
"""
|
|
684
|
+
if tokens is None:
|
|
685
|
+
tokens = lex_unit(source)
|
|
686
|
+
directives: tuple[Directive, ...] = collect_directives(tokens)
|
|
687
|
+
tree, errors = parse_tokens(tokens, type_names)
|
|
688
|
+
unit: CUnit = CUnit(
|
|
689
|
+
source=source,
|
|
690
|
+
tokens=tokens,
|
|
691
|
+
directives=directives,
|
|
692
|
+
macros=collect_macros(directives),
|
|
693
|
+
includes=collect_includes(directives),
|
|
694
|
+
comments=tuple(token for token in tokens if token.kind is TokenKind.COMMENT),
|
|
695
|
+
tree=tree,
|
|
696
|
+
errors=errors,
|
|
697
|
+
)
|
|
698
|
+
_index_tree(unit)
|
|
699
|
+
_extract_facts(unit)
|
|
700
|
+
return unit
|