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,210 @@
|
|
|
1
|
+
from collections.abc import Iterator
|
|
2
|
+
|
|
3
|
+
from sources.analysis.syntax import Node
|
|
4
|
+
from sources.analysis.tokens import Token
|
|
5
|
+
from sources.analysis.unit import CUnit
|
|
6
|
+
from sources.enums import ArgumentType, Layer, NodeKind, TokenKind
|
|
7
|
+
from sources.rules.arguments import ArgumentSpec
|
|
8
|
+
from sources.rules.primitives.base import CheckRun, Finding, Primitive
|
|
9
|
+
from sources.rules.primitives.layout import BEFORE_COMMENT, LINE_BREAK_KINDS, LINE_END, LINE_START, gap_after, gap_before, next_code
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# Token classes checked by `spacing` (plan 03, section 6.4).
|
|
13
|
+
SPACING_CLASSES: tuple[str, ...] = (
|
|
14
|
+
"comma", "semicolon", "for_semicolon", "keyword", "else", "call_paren", "unary_operator",
|
|
15
|
+
"binary_operator", "ternary_operator", "separator", "tab_separator", "pointer_star",
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
# Keywords that take an argument and must be followed by a space when they have one.
|
|
19
|
+
ARGUMENT_KEYWORDS: frozenset[str] = frozenset({"if", "while", "for", "switch", "case", "return", "goto"})
|
|
20
|
+
|
|
21
|
+
# Unary operators whose operand must be attached (the pointer `*` belongs to the pointer_star class).
|
|
22
|
+
ATTACHED_UNARY_OPERATORS: frozenset[str] = frozenset({"!", "~", "-", "+", "&"})
|
|
23
|
+
|
|
24
|
+
# Accepted whitespace on each side of a binary or ternary operator.
|
|
25
|
+
OPERATOR_BEFORE: frozenset[str] = frozenset({" ", LINE_START})
|
|
26
|
+
|
|
27
|
+
# Accepted whitespace after a binary or ternary operator, or a comma.
|
|
28
|
+
OPERATOR_AFTER: frozenset[str] = frozenset({" ", LINE_END, BEFORE_COMMENT})
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _finding(unit: CUnit, token: Token, kind: str) -> Finding:
|
|
32
|
+
"""
|
|
33
|
+
Finding located on a token, for one spacing class.
|
|
34
|
+
|
|
35
|
+
Parameters
|
|
36
|
+
----------
|
|
37
|
+
unit : CUnit
|
|
38
|
+
File of the token.
|
|
39
|
+
token : Token
|
|
40
|
+
The token.
|
|
41
|
+
kind : str
|
|
42
|
+
Spacing class that is broken.
|
|
43
|
+
|
|
44
|
+
Returns
|
|
45
|
+
----------
|
|
46
|
+
Finding
|
|
47
|
+
The finding.
|
|
48
|
+
"""
|
|
49
|
+
return Finding(unit.path, token.line, token.column, token.end_line, token.end_column, name=token.text, value=kind, node_key=(token.index, kind))
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class Spacing(Primitive):
|
|
53
|
+
"""
|
|
54
|
+
Whitespace around tokens, by token class: commas, semicolons (and the `for` header rule), keywords with
|
|
55
|
+
arguments, `else`, function call/declaration parentheses, unary, binary and ternary operators,
|
|
56
|
+
separators (one space, never a tab), and pointer asterisks (attached to their right, preceded by a space
|
|
57
|
+
unless after `(` or `*`).
|
|
58
|
+
(ABC -> Primitive)
|
|
59
|
+
|
|
60
|
+
Attributes
|
|
61
|
+
----------
|
|
62
|
+
kind : str
|
|
63
|
+
"spacing".
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
kind = "spacing"
|
|
67
|
+
layer = Layer.SYNTAX
|
|
68
|
+
parameters = (
|
|
69
|
+
ArgumentSpec("classes", ArgumentType.LIST, required=True, item_type=ArgumentType.ENUM, choices=SPACING_CLASSES, description="Token classes checked."),
|
|
70
|
+
)
|
|
71
|
+
default_message = "{code}: bad spacing around '{name}' ({value})"
|
|
72
|
+
|
|
73
|
+
def _token_classes(self, unit: CUnit, classes: frozenset[str]) -> Iterator[Finding]:
|
|
74
|
+
"""
|
|
75
|
+
Checks that only need tokens: commas, semicolons, keywords, `else` and separators.
|
|
76
|
+
|
|
77
|
+
Parameters
|
|
78
|
+
----------
|
|
79
|
+
unit : CUnit
|
|
80
|
+
The file.
|
|
81
|
+
classes : frozenset[str]
|
|
82
|
+
Enabled classes.
|
|
83
|
+
|
|
84
|
+
Returns
|
|
85
|
+
----------
|
|
86
|
+
Iterator[Finding]
|
|
87
|
+
The problems found.
|
|
88
|
+
"""
|
|
89
|
+
tokens: tuple[Token, ...] = unit.tokens
|
|
90
|
+
for token in tokens:
|
|
91
|
+
if token.kind is TokenKind.WHITESPACE:
|
|
92
|
+
index: int = token.index
|
|
93
|
+
leading: bool = index == 0 or tokens[index - 1].kind in LINE_BREAK_KINDS
|
|
94
|
+
trailing: bool = index + 1 >= len(tokens) or tokens[index + 1].kind in LINE_BREAK_KINDS
|
|
95
|
+
if leading or trailing:
|
|
96
|
+
continue
|
|
97
|
+
if "\t" in token.text:
|
|
98
|
+
if "tab_separator" in classes:
|
|
99
|
+
yield _finding(unit, token, "tab_separator")
|
|
100
|
+
elif len(token.text) > 1 and "separator" in classes and not token.in_directive and tokens[index + 1].kind is not TokenKind.COMMENT:
|
|
101
|
+
yield _finding(unit, token, "separator")
|
|
102
|
+
continue
|
|
103
|
+
if token.in_directive or token.is_trivia:
|
|
104
|
+
continue
|
|
105
|
+
if token.kind is TokenKind.PUNCTUATOR and token.text == "," and "comma" in classes:
|
|
106
|
+
if gap_before(unit, token) not in ("", LINE_START) or gap_after(unit, token) not in OPERATOR_AFTER:
|
|
107
|
+
yield _finding(unit, token, "comma")
|
|
108
|
+
elif token.kind is TokenKind.PUNCTUATOR and token.text == ";" and "semicolon" in classes:
|
|
109
|
+
if gap_before(unit, token) not in ("", LINE_START):
|
|
110
|
+
yield _finding(unit, token, "semicolon")
|
|
111
|
+
elif token.kind is TokenKind.KEYWORD and token.text in ARGUMENT_KEYWORDS and "keyword" in classes:
|
|
112
|
+
following: Token | None = next_code(unit, token)
|
|
113
|
+
if gap_after(unit, token) == "" and following is not None and following.text not in (";", ":"):
|
|
114
|
+
yield _finding(unit, token, "keyword")
|
|
115
|
+
elif token.kind is TokenKind.KEYWORD and token.text == "else" and "else" in classes:
|
|
116
|
+
if gap_before(unit, token) not in OPERATOR_BEFORE or gap_after(unit, token) not in OPERATOR_AFTER:
|
|
117
|
+
yield _finding(unit, token, "else")
|
|
118
|
+
|
|
119
|
+
def _syntax_classes(self, unit: CUnit, classes: frozenset[str]) -> Iterator[Finding]:
|
|
120
|
+
"""
|
|
121
|
+
Checks that need the syntax tree: `for` semicolons, call/declaration parentheses, operators and
|
|
122
|
+
pointer asterisks.
|
|
123
|
+
|
|
124
|
+
Parameters
|
|
125
|
+
----------
|
|
126
|
+
unit : CUnit
|
|
127
|
+
The file.
|
|
128
|
+
classes : frozenset[str]
|
|
129
|
+
Enabled classes.
|
|
130
|
+
|
|
131
|
+
Returns
|
|
132
|
+
----------
|
|
133
|
+
Iterator[Finding]
|
|
134
|
+
The problems found.
|
|
135
|
+
"""
|
|
136
|
+
if "for_semicolon" in classes:
|
|
137
|
+
for loop in unit.nodes(NodeKind.FOR_STATEMENT):
|
|
138
|
+
opening: Token = loop.fields["open_paren"]
|
|
139
|
+
closing: Token = loop.fields["close_paren"]
|
|
140
|
+
for token in unit.tokens[opening.index + 1:closing.index]:
|
|
141
|
+
if token.text == ";" and token.kind is TokenKind.PUNCTUATOR:
|
|
142
|
+
following: Token = unit.tokens[token.index + 1]
|
|
143
|
+
if following.text != ";" and gap_after(unit, token) != " ":
|
|
144
|
+
yield _finding(unit, token, "for_semicolon")
|
|
145
|
+
if "call_paren" in classes:
|
|
146
|
+
for call in unit.nodes(NodeKind.CALL_EXPRESSION):
|
|
147
|
+
opening = call.fields.get("open_paren")
|
|
148
|
+
if opening is not None and not call.fields.get("opaque") and gap_before(unit, opening) != "":
|
|
149
|
+
yield _finding(unit, opening, "call_paren")
|
|
150
|
+
for declarator in unit.nodes(NodeKind.FUNCTION_DECLARATOR):
|
|
151
|
+
parameters: Node | None = declarator.fields.get("own_parameters")
|
|
152
|
+
name: Token | None = declarator.fields.get("name_token")
|
|
153
|
+
if parameters is None or name is None or declarator.fields.get("nested") is not None:
|
|
154
|
+
continue
|
|
155
|
+
if gap_before(unit, parameters.first) != "": # type: ignore[arg-type]
|
|
156
|
+
yield _finding(unit, parameters.first, "call_paren") # type: ignore[arg-type]
|
|
157
|
+
if "unary_operator" in classes:
|
|
158
|
+
for node in unit.nodes(NodeKind.UNARY_EXPRESSION, NodeKind.UPDATE_EXPRESSION, NodeKind.SIZEOF_EXPRESSION):
|
|
159
|
+
token = node.fields.get("operator_token") or node.first
|
|
160
|
+
if node.kind is NodeKind.UNARY_EXPRESSION and token.text not in ATTACHED_UNARY_OPERATORS:
|
|
161
|
+
continue
|
|
162
|
+
if node.kind is NodeKind.UPDATE_EXPRESSION and not node.fields["prefix"]:
|
|
163
|
+
if gap_before(unit, token) != "":
|
|
164
|
+
yield _finding(unit, token, "unary_operator")
|
|
165
|
+
continue
|
|
166
|
+
if node.kind is NodeKind.SIZEOF_EXPRESSION:
|
|
167
|
+
following = next_code(unit, token)
|
|
168
|
+
if following is not None and following.text == "(" and gap_after(unit, token) != "":
|
|
169
|
+
yield _finding(unit, token, "unary_operator")
|
|
170
|
+
continue
|
|
171
|
+
if gap_after(unit, token) != "":
|
|
172
|
+
yield _finding(unit, token, "unary_operator")
|
|
173
|
+
if "binary_operator" in classes:
|
|
174
|
+
for node in unit.nodes(NodeKind.BINARY_EXPRESSION, NodeKind.LOGICAL_EXPRESSION, NodeKind.ASSIGNMENT_EXPRESSION):
|
|
175
|
+
token = node.fields["operator_token"]
|
|
176
|
+
if gap_before(unit, token) not in OPERATOR_BEFORE or gap_after(unit, token) not in OPERATOR_AFTER:
|
|
177
|
+
yield _finding(unit, token, "binary_operator")
|
|
178
|
+
if "ternary_operator" in classes:
|
|
179
|
+
for node in unit.nodes(NodeKind.CONDITIONAL_EXPRESSION):
|
|
180
|
+
for token in (node.fields["question_token"], node.fields["colon_token"]):
|
|
181
|
+
if gap_before(unit, token) not in OPERATOR_BEFORE or gap_after(unit, token) not in OPERATOR_AFTER:
|
|
182
|
+
yield _finding(unit, token, "ternary_operator")
|
|
183
|
+
if "pointer_star" in classes:
|
|
184
|
+
stars: list[Token] = [star for node in unit.nodes(NodeKind.DECLARATOR, NodeKind.FUNCTION_DECLARATOR) for star in node.fields.get("star_tokens", ())]
|
|
185
|
+
stars.extend(node.fields["operator_token"] for node in unit.nodes(NodeKind.UNARY_EXPRESSION) if node.fields["operator"] == "*")
|
|
186
|
+
for star in sorted(stars, key=lambda item: item.index):
|
|
187
|
+
previous: Token | None = unit.tokens[star.index - 1] if star.index > 0 else None
|
|
188
|
+
before_ok: bool = (previous is not None and previous.text in ("(", "*")) or gap_before(unit, star) in OPERATOR_BEFORE
|
|
189
|
+
if gap_after(unit, star) != "" or not before_ok:
|
|
190
|
+
yield _finding(unit, star, "pointer_star")
|
|
191
|
+
|
|
192
|
+
def run(self, run: CheckRun) -> Iterator[Finding]:
|
|
193
|
+
"""
|
|
194
|
+
Run the check (see class description).
|
|
195
|
+
|
|
196
|
+
Parameters
|
|
197
|
+
----------
|
|
198
|
+
run : CheckRun
|
|
199
|
+
Project context, files and parameters.
|
|
200
|
+
|
|
201
|
+
Returns
|
|
202
|
+
----------
|
|
203
|
+
Iterator[Finding]
|
|
204
|
+
The problems found.
|
|
205
|
+
"""
|
|
206
|
+
classes: frozenset[str] = frozenset(run.parameters["classes"])
|
|
207
|
+
for unit in run.units():
|
|
208
|
+
found: list[Finding] = list(self._token_classes(unit, classes))
|
|
209
|
+
found.extend(self._syntax_classes(unit, classes))
|
|
210
|
+
yield from sorted(found, key=lambda finding: (finding.line, finding.column))
|