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/rules/catalog.py
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import difflib
|
|
2
|
+
from collections.abc import Iterable, Mapping
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from types import MappingProxyType
|
|
6
|
+
|
|
7
|
+
from sources.errors.exceptions import RuleDefinitionError, UnknownSelectorError
|
|
8
|
+
from sources.rules.definition import RuleDefinition
|
|
9
|
+
from sources.rules.loader import load_rule_file
|
|
10
|
+
from sources.rules.paths import BUILTIN_RULES_DIRECTORY
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# Selector matching every rule.
|
|
14
|
+
ALL_RULES_SELECTOR: str = "*"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass(frozen=True, slots=True)
|
|
18
|
+
class SelectedRule:
|
|
19
|
+
"""
|
|
20
|
+
Result of resolving a selector: a rule, and optionally one of its checks.
|
|
21
|
+
(dataclass)
|
|
22
|
+
|
|
23
|
+
Attributes
|
|
24
|
+
----------
|
|
25
|
+
rule_id : str
|
|
26
|
+
Selected rule.
|
|
27
|
+
check_id : str | None
|
|
28
|
+
Selected check, or None for the whole rule.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
rule_id: str
|
|
32
|
+
check_id: str | None = None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _rule_files(directory: Path) -> list[Path]:
|
|
36
|
+
"""
|
|
37
|
+
Rule files of a categories directory, sorted.
|
|
38
|
+
|
|
39
|
+
Parameters
|
|
40
|
+
----------
|
|
41
|
+
directory : Path
|
|
42
|
+
Directory holding `<category>/<name>.ini` files.
|
|
43
|
+
|
|
44
|
+
Returns
|
|
45
|
+
----------
|
|
46
|
+
list[Path]
|
|
47
|
+
The files (none when the directory does not exist).
|
|
48
|
+
"""
|
|
49
|
+
if not directory.is_dir():
|
|
50
|
+
return []
|
|
51
|
+
return sorted(path for path in directory.glob("*/*.ini") if path.is_file())
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class RuleCatalog:
|
|
55
|
+
"""
|
|
56
|
+
Every known rule (built-in and custom), with selector resolution (plan 03, section 12). Metadata only:
|
|
57
|
+
nothing here depends on the project configuration.
|
|
58
|
+
|
|
59
|
+
Attributes
|
|
60
|
+
----------
|
|
61
|
+
rules : Mapping[str, RuleDefinition]
|
|
62
|
+
Rules by ID, sorted by ID.
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
def __init__(self, rules: Iterable[RuleDefinition]) -> None:
|
|
66
|
+
"""
|
|
67
|
+
Index the rules.
|
|
68
|
+
|
|
69
|
+
Parameters
|
|
70
|
+
----------
|
|
71
|
+
rules : Iterable[RuleDefinition]
|
|
72
|
+
The rules.
|
|
73
|
+
|
|
74
|
+
Returns
|
|
75
|
+
----------
|
|
76
|
+
None
|
|
77
|
+
Nothing.
|
|
78
|
+
|
|
79
|
+
Raises
|
|
80
|
+
----------
|
|
81
|
+
RuleDefinitionError
|
|
82
|
+
Two rules share an ID or a selector, or a custom rule reuses a built-in rule name.
|
|
83
|
+
"""
|
|
84
|
+
indexed: dict[str, RuleDefinition] = {}
|
|
85
|
+
builtin_names: set[str] = set()
|
|
86
|
+
for rule in rules:
|
|
87
|
+
if rule.id in indexed:
|
|
88
|
+
raise RuleDefinitionError(f"Rule '{rule.id}' is defined twice.", path=rule.source)
|
|
89
|
+
indexed[rule.id] = rule
|
|
90
|
+
if rule.builtin:
|
|
91
|
+
builtin_names.add(rule.name)
|
|
92
|
+
for rule in indexed.values():
|
|
93
|
+
if not rule.builtin and rule.name in builtin_names:
|
|
94
|
+
raise RuleDefinitionError(f"Custom rule '{rule.id}' reuses the name of a built-in rule.", "Choose another rule name.", path=rule.source)
|
|
95
|
+
self.rules: Mapping[str, RuleDefinition] = MappingProxyType(dict(sorted(indexed.items())))
|
|
96
|
+
aliases: dict[str, str] = {}
|
|
97
|
+
for rule in self.rules.values():
|
|
98
|
+
for selector in rule.selectors[1:]:
|
|
99
|
+
if selector in aliases or selector in self.rules:
|
|
100
|
+
raise RuleDefinitionError(f"Selector '{selector}' of '{rule.id}' is already used.", path=rule.source)
|
|
101
|
+
aliases[selector] = rule.id
|
|
102
|
+
self._aliases: Mapping[str, str] = MappingProxyType(aliases)
|
|
103
|
+
|
|
104
|
+
@classmethod
|
|
105
|
+
def load(cls, custom_directories: Iterable[Path] = (), *, include_builtin: bool = True) -> "RuleCatalog":
|
|
106
|
+
"""
|
|
107
|
+
Load the built-in rules and the custom rules of some directories.
|
|
108
|
+
|
|
109
|
+
Parameters
|
|
110
|
+
----------
|
|
111
|
+
custom_directories : Iterable[Path]
|
|
112
|
+
Directories holding custom `<category>/<name>.ini` files.
|
|
113
|
+
include_builtin : bool
|
|
114
|
+
Whether the built-in rules are loaded.
|
|
115
|
+
|
|
116
|
+
Returns
|
|
117
|
+
----------
|
|
118
|
+
RuleCatalog
|
|
119
|
+
The catalog.
|
|
120
|
+
|
|
121
|
+
Raises
|
|
122
|
+
----------
|
|
123
|
+
RuleDefinitionError
|
|
124
|
+
A rule file is invalid.
|
|
125
|
+
IniSyntaxError
|
|
126
|
+
A rule file breaks the INI dialect.
|
|
127
|
+
"""
|
|
128
|
+
rules: list[RuleDefinition] = []
|
|
129
|
+
if include_builtin:
|
|
130
|
+
rules.extend(load_rule_file(path, BUILTIN_RULES_DIRECTORY, builtin=True) for path in _rule_files(BUILTIN_RULES_DIRECTORY))
|
|
131
|
+
for directory in custom_directories:
|
|
132
|
+
rules.extend(load_rule_file(path, directory, builtin=False) for path in _rule_files(directory))
|
|
133
|
+
return cls(rules)
|
|
134
|
+
|
|
135
|
+
@property
|
|
136
|
+
def categories(self) -> tuple[str, ...]:
|
|
137
|
+
"""
|
|
138
|
+
Categories that hold at least one rule.
|
|
139
|
+
|
|
140
|
+
Returns
|
|
141
|
+
----------
|
|
142
|
+
tuple[str, ...]
|
|
143
|
+
The categories, sorted.
|
|
144
|
+
"""
|
|
145
|
+
return tuple(sorted({rule.category for rule in self.rules.values()}))
|
|
146
|
+
|
|
147
|
+
def get(self, rule_id: str, *, source: str | None = None) -> RuleDefinition:
|
|
148
|
+
"""
|
|
149
|
+
Get a rule by ID or alias.
|
|
150
|
+
|
|
151
|
+
Parameters
|
|
152
|
+
----------
|
|
153
|
+
rule_id : str
|
|
154
|
+
Rule ID or alias.
|
|
155
|
+
source : str | None
|
|
156
|
+
File the name comes from, for error messages.
|
|
157
|
+
|
|
158
|
+
Returns
|
|
159
|
+
----------
|
|
160
|
+
RuleDefinition
|
|
161
|
+
The rule.
|
|
162
|
+
|
|
163
|
+
Raises
|
|
164
|
+
----------
|
|
165
|
+
UnknownSelectorError
|
|
166
|
+
No rule has this ID or alias.
|
|
167
|
+
"""
|
|
168
|
+
resolved: str = self._aliases.get(rule_id, rule_id)
|
|
169
|
+
if resolved not in self.rules:
|
|
170
|
+
raise UnknownSelectorError(rule_id, self._suggestions(rule_id), path=source)
|
|
171
|
+
return self.rules[resolved]
|
|
172
|
+
|
|
173
|
+
def _suggestions(self, selector: str) -> list[str]:
|
|
174
|
+
"""
|
|
175
|
+
Close matches of an unknown selector.
|
|
176
|
+
|
|
177
|
+
Parameters
|
|
178
|
+
----------
|
|
179
|
+
selector : str
|
|
180
|
+
The unknown selector.
|
|
181
|
+
|
|
182
|
+
Returns
|
|
183
|
+
----------
|
|
184
|
+
list[str]
|
|
185
|
+
Up to two suggestions.
|
|
186
|
+
"""
|
|
187
|
+
known: list[str] = [*self.rules, *self._aliases, *self.categories]
|
|
188
|
+
return difflib.get_close_matches(selector, known, n=2)
|
|
189
|
+
|
|
190
|
+
def resolve(self, selector: str, *, source: str | None = None) -> tuple[SelectedRule, ...]:
|
|
191
|
+
"""
|
|
192
|
+
Resolve a selector: `*`, `<category>`, `<category>/*`, `<category>/<name>`, an alias (`C-L1`), each
|
|
193
|
+
optionally followed by `:<check_id>` when it names one rule.
|
|
194
|
+
|
|
195
|
+
Parameters
|
|
196
|
+
----------
|
|
197
|
+
selector : str
|
|
198
|
+
The selector.
|
|
199
|
+
source : str | None
|
|
200
|
+
File the selector comes from, for error messages.
|
|
201
|
+
|
|
202
|
+
Returns
|
|
203
|
+
----------
|
|
204
|
+
tuple[SelectedRule, ...]
|
|
205
|
+
The selected rules (and check).
|
|
206
|
+
|
|
207
|
+
Raises
|
|
208
|
+
----------
|
|
209
|
+
UnknownSelectorError
|
|
210
|
+
The selector matches nothing.
|
|
211
|
+
"""
|
|
212
|
+
text: str = selector.strip()
|
|
213
|
+
if text == ALL_RULES_SELECTOR:
|
|
214
|
+
return tuple(SelectedRule(rule_id) for rule_id in self.rules)
|
|
215
|
+
if text in self.categories:
|
|
216
|
+
return tuple(SelectedRule(rule.id) for rule in self.rules.values() if rule.category == text)
|
|
217
|
+
if text.endswith("/*") and text[:-2] in self.categories:
|
|
218
|
+
return tuple(SelectedRule(rule.id) for rule in self.rules.values() if rule.category == text[:-2])
|
|
219
|
+
name, _, check = text.partition(":")
|
|
220
|
+
rule_id: str = self._aliases.get(name, name)
|
|
221
|
+
if rule_id not in self.rules:
|
|
222
|
+
raise UnknownSelectorError(selector, self._suggestions(name), path=source)
|
|
223
|
+
if not check:
|
|
224
|
+
return (SelectedRule(rule_id),)
|
|
225
|
+
if check not in {item.id for item in self.rules[rule_id].checks}:
|
|
226
|
+
raise UnknownSelectorError(selector, [f"{name}:{item.id}" for item in self.rules[rule_id].checks][:2], path=source)
|
|
227
|
+
return (SelectedRule(rule_id, check),)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
[rule]
|
|
2
|
+
schema = 2
|
|
3
|
+
id = advanced/constant_pointers
|
|
4
|
+
title = Constant pointers
|
|
5
|
+
summary = When creating a pointer, if the pointed data is not (or should not be) modified by the function, it is marked as constant.
|
|
6
|
+
applies_to = c, h
|
|
7
|
+
severity = info
|
|
8
|
+
detection = heuristic
|
|
9
|
+
status = stable
|
|
10
|
+
|
|
11
|
+
[epitech]
|
|
12
|
+
ref = C-A1
|
|
13
|
+
version = 8
|
|
14
|
+
banana = none
|
|
15
|
+
manual_part = Pointers whose data should not be modified, and local pointers.
|
|
16
|
+
|
|
17
|
+
[check:const_candidate]
|
|
18
|
+
kind = const_correctness
|
|
19
|
+
severity = info
|
|
20
|
+
message = {code}: the data pointed by '{name}' is not modified; mark it const
|
|
21
|
+
|
|
22
|
+
[example:good_1]
|
|
23
|
+
kind = good
|
|
24
|
+
|
|
25
|
+
[example:good_1:file:ok.c]
|
|
26
|
+
code =
|
|
27
|
+
|int get_first(char const *text)
|
|
28
|
+
|{
|
|
29
|
+
| return text[0];
|
|
30
|
+
|}
|
|
31
|
+
|
|
|
32
|
+
|void set_first(char *text)
|
|
33
|
+
|{
|
|
34
|
+
| text[0] = 'a';
|
|
35
|
+
|}
|
|
36
|
+
|
|
37
|
+
[example:bad_1]
|
|
38
|
+
kind = bad
|
|
39
|
+
expect = 1:21:const_candidate
|
|
40
|
+
|
|
41
|
+
[example:bad_1:file:bad.c]
|
|
42
|
+
code =
|
|
43
|
+
|int get_first(char *text)
|
|
44
|
+
|{
|
|
45
|
+
| return text[0];
|
|
46
|
+
|}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
[rule]
|
|
2
|
+
schema = 2
|
|
3
|
+
id = advanced/line_break_at_end_of_file
|
|
4
|
+
title = Line break at the end of file
|
|
5
|
+
summary = Files end with a line break.
|
|
6
|
+
applies_to = c, h
|
|
7
|
+
severity = info
|
|
8
|
+
detection = automatic
|
|
9
|
+
status = stable
|
|
10
|
+
|
|
11
|
+
[epitech]
|
|
12
|
+
ref = C-A3
|
|
13
|
+
version = 8
|
|
14
|
+
banana = full
|
|
15
|
+
|
|
16
|
+
[check:final_newline]
|
|
17
|
+
kind = text_regex
|
|
18
|
+
on = bytes
|
|
19
|
+
pattern = \n\Z
|
|
20
|
+
mode = require
|
|
21
|
+
anchor = file_end
|
|
22
|
+
message = {code}: the file does not end with a line break
|
|
23
|
+
fix = safe
|
|
24
|
+
fix.kind = ensure_final_newline
|
|
25
|
+
|
|
26
|
+
[example:good_1]
|
|
27
|
+
kind = good
|
|
28
|
+
|
|
29
|
+
[example:good_1:file:correct.c]
|
|
30
|
+
code =
|
|
31
|
+
|int main(void) {
|
|
32
|
+
| return 0;
|
|
33
|
+
|}
|
|
34
|
+
|
|
35
|
+
[example:bad_1]
|
|
36
|
+
kind = bad
|
|
37
|
+
expect = 3:2:final_newline
|
|
38
|
+
|
|
39
|
+
[example:bad_1:file:incorrect.c]
|
|
40
|
+
final_newline = false
|
|
41
|
+
code =
|
|
42
|
+
|int main(void) {
|
|
43
|
+
| return 0;
|
|
44
|
+
|}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
[rule]
|
|
2
|
+
schema = 2
|
|
3
|
+
id = advanced/static
|
|
4
|
+
title = static
|
|
5
|
+
summary = Global variables and functions that are not used outside the compilation unit to which they belong are marked with the static keyword.
|
|
6
|
+
applies_to = c
|
|
7
|
+
severity = info
|
|
8
|
+
detection = heuristic
|
|
9
|
+
status = stable
|
|
10
|
+
|
|
11
|
+
[epitech]
|
|
12
|
+
ref = C-A4
|
|
13
|
+
version = 8
|
|
14
|
+
banana = none
|
|
15
|
+
|
|
16
|
+
[argument:exempt_names]
|
|
17
|
+
type = list
|
|
18
|
+
default = main
|
|
19
|
+
description = Symbols that never need to be static.
|
|
20
|
+
|
|
21
|
+
[check:should_be_static]
|
|
22
|
+
kind = symbol_linkage
|
|
23
|
+
symbol_kinds = function, variable
|
|
24
|
+
exempt = @exempt_names
|
|
25
|
+
severity = info
|
|
26
|
+
message = {code}: '{name}' is not used outside {path}; mark it static
|
|
27
|
+
|
|
28
|
+
[example:good_1]
|
|
29
|
+
kind = good
|
|
30
|
+
|
|
31
|
+
[example:good_1:file:src/tools.c]
|
|
32
|
+
code =
|
|
33
|
+
|int add_numbers(int a, int b)
|
|
34
|
+
|{
|
|
35
|
+
| return a + b;
|
|
36
|
+
|}
|
|
37
|
+
|
|
38
|
+
[example:good_1:file:src/main.c]
|
|
39
|
+
code =
|
|
40
|
+
|int main(void)
|
|
41
|
+
|{
|
|
42
|
+
| return add_numbers(1, 2);
|
|
43
|
+
|}
|
|
44
|
+
|
|
45
|
+
[example:bad_1]
|
|
46
|
+
kind = bad
|
|
47
|
+
expect =
|
|
48
|
+
src/tools.c:1:5:should_be_static
|
|
49
|
+
src/tools.c:6:5:should_be_static
|
|
50
|
+
|
|
51
|
+
[example:bad_1:file:src/tools.c]
|
|
52
|
+
code =
|
|
53
|
+
|int add_numbers(int a, int b)
|
|
54
|
+
|{
|
|
55
|
+
| return a + b;
|
|
56
|
+
|}
|
|
57
|
+
|
|
|
58
|
+
|int counter = 0;
|
|
59
|
+
|
|
60
|
+
[example:bad_1:file:src/main.c]
|
|
61
|
+
code =
|
|
62
|
+
|int main(void)
|
|
63
|
+
|{
|
|
64
|
+
| return 0;
|
|
65
|
+
|}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
[rule]
|
|
2
|
+
schema = 2
|
|
3
|
+
id = advanced/typing
|
|
4
|
+
title = Typing
|
|
5
|
+
summary = The most accurate types possible are preferred according to the use of the data (size_t, ptrdiff_t, uint8_t, int32_t...).
|
|
6
|
+
applies_to = c, h
|
|
7
|
+
severity = info
|
|
8
|
+
detection = heuristic
|
|
9
|
+
status = stable
|
|
10
|
+
|
|
11
|
+
[epitech]
|
|
12
|
+
ref = C-A2
|
|
13
|
+
version = 8
|
|
14
|
+
banana = none
|
|
15
|
+
manual_part = Every other inaccurate type.
|
|
16
|
+
|
|
17
|
+
[check:size_type]
|
|
18
|
+
kind = type_accuracy
|
|
19
|
+
severity = info
|
|
20
|
+
message = {code}: '{name}' is declared as {type}; a more accurate type such as size_t fits
|
|
21
|
+
|
|
22
|
+
[example:good_1]
|
|
23
|
+
kind = good
|
|
24
|
+
|
|
25
|
+
[example:good_1:file:ok.c]
|
|
26
|
+
code =
|
|
27
|
+
|size_t get_obj_size(void const *object)
|
|
28
|
+
|{
|
|
29
|
+
| size_t length = strlen(object);
|
|
30
|
+
|
|
|
31
|
+
| return length;
|
|
32
|
+
|}
|
|
33
|
+
|
|
34
|
+
[example:bad_1]
|
|
35
|
+
kind = bad
|
|
36
|
+
expect =
|
|
37
|
+
1:14:size_type
|
|
38
|
+
3:9:size_type
|
|
39
|
+
|
|
40
|
+
[example:bad_1:file:bad.c]
|
|
41
|
+
code =
|
|
42
|
+
|unsigned int get_obj_size(void const *object)
|
|
43
|
+
|{
|
|
44
|
+
| int length = strlen(object);
|
|
45
|
+
|
|
|
46
|
+
| return length;
|
|
47
|
+
|}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
[rule]
|
|
2
|
+
schema = 2
|
|
3
|
+
id = control_structures/conditional_branching
|
|
4
|
+
title = Conditional branching
|
|
5
|
+
summary = Nested conditional branches with a depth of 3 or more are avoided; an else if counts as an if inside an else.
|
|
6
|
+
applies_to = c, h
|
|
7
|
+
severity = major
|
|
8
|
+
detection = automatic
|
|
9
|
+
status = stable
|
|
10
|
+
|
|
11
|
+
[epitech]
|
|
12
|
+
ref = C-C1
|
|
13
|
+
version = 8
|
|
14
|
+
banana = full
|
|
15
|
+
|
|
16
|
+
[argument:max_depth]
|
|
17
|
+
type = int
|
|
18
|
+
default = 2
|
|
19
|
+
min = 1
|
|
20
|
+
description = Deepest allowed nesting of conditional branches (depth 3 or more is forbidden).
|
|
21
|
+
|
|
22
|
+
[check:depth]
|
|
23
|
+
kind = nesting_depth
|
|
24
|
+
counted = if_statement, for_statement, while_statement, do_statement, switch_statement
|
|
25
|
+
max = @max_depth
|
|
26
|
+
message = {code}: nested conditional branches with a depth of {value}
|
|
27
|
+
|
|
28
|
+
[example:good_1]
|
|
29
|
+
kind = good
|
|
30
|
+
|
|
31
|
+
[example:good_1:file:ok.c]
|
|
32
|
+
code =
|
|
33
|
+
|void pick(int a)
|
|
34
|
+
|{
|
|
35
|
+
| if (a == 1) {
|
|
36
|
+
| do_something();
|
|
37
|
+
| } else if (a == 2) {
|
|
38
|
+
| do_something_else();
|
|
39
|
+
| } else {
|
|
40
|
+
| do_something_more();
|
|
41
|
+
| }
|
|
42
|
+
| while (a) {
|
|
43
|
+
| if (a) {
|
|
44
|
+
| do_something();
|
|
45
|
+
| }
|
|
46
|
+
| }
|
|
47
|
+
|}
|
|
48
|
+
|
|
49
|
+
[example:bad_1]
|
|
50
|
+
kind = bad
|
|
51
|
+
expect =
|
|
52
|
+
7:12:depth
|
|
53
|
+
16:13:depth
|
|
54
|
+
28:9:depth
|
|
55
|
+
|
|
56
|
+
[example:bad_1:file:bad.c]
|
|
57
|
+
code =
|
|
58
|
+
|void pick(int a)
|
|
59
|
+
|{
|
|
60
|
+
| if (a == 1) {
|
|
61
|
+
| do_something();
|
|
62
|
+
| } else if (a == 2) {
|
|
63
|
+
| do_something_else();
|
|
64
|
+
| } else if (a == 3) {
|
|
65
|
+
| do_one_last_thing();
|
|
66
|
+
| }
|
|
67
|
+
|}
|
|
68
|
+
|
|
|
69
|
+
|void loop(int a)
|
|
70
|
+
|{
|
|
71
|
+
| while (a) {
|
|
72
|
+
| for (;;) {
|
|
73
|
+
| if (a) {
|
|
74
|
+
| do_something();
|
|
75
|
+
| }
|
|
76
|
+
| }
|
|
77
|
+
| }
|
|
78
|
+
|}
|
|
79
|
+
|
|
|
80
|
+
|void branch(int a)
|
|
81
|
+
|{
|
|
82
|
+
| if (a) {
|
|
83
|
+
| do_something();
|
|
84
|
+
| } else if (a > 2) {
|
|
85
|
+
| while (a) {
|
|
86
|
+
| do_something_else();
|
|
87
|
+
| }
|
|
88
|
+
| }
|
|
89
|
+
|}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
[rule]
|
|
2
|
+
schema = 2
|
|
3
|
+
id = control_structures/goto
|
|
4
|
+
title = goto
|
|
5
|
+
summary = Using the goto keyword is forbidden.
|
|
6
|
+
applies_to = c, h
|
|
7
|
+
severity = major
|
|
8
|
+
detection = automatic
|
|
9
|
+
status = stable
|
|
10
|
+
|
|
11
|
+
[epitech]
|
|
12
|
+
ref = C-C3
|
|
13
|
+
version = 8
|
|
14
|
+
banana = full
|
|
15
|
+
|
|
16
|
+
[check:goto]
|
|
17
|
+
kind = token_forbid
|
|
18
|
+
kinds = keyword
|
|
19
|
+
values = goto
|
|
20
|
+
message = {code}: the goto keyword is forbidden
|
|
21
|
+
|
|
22
|
+
[example:good_1]
|
|
23
|
+
kind = good
|
|
24
|
+
|
|
25
|
+
[example:good_1:file:ok.c]
|
|
26
|
+
code =
|
|
27
|
+
|int main(void)
|
|
28
|
+
|{
|
|
29
|
+
| char const *text = "goto is fine in a string";
|
|
30
|
+
|
|
|
31
|
+
| return text[0];
|
|
32
|
+
|}
|
|
33
|
+
|
|
34
|
+
[example:bad_1]
|
|
35
|
+
kind = bad
|
|
36
|
+
expect = 3:5:goto
|
|
37
|
+
|
|
38
|
+
[example:bad_1:file:bad.c]
|
|
39
|
+
code =
|
|
40
|
+
|int main(void)
|
|
41
|
+
|{
|
|
42
|
+
| goto end;
|
|
43
|
+
|end:
|
|
44
|
+
| return 0;
|
|
45
|
+
|}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
[rule]
|
|
2
|
+
schema = 2
|
|
3
|
+
id = control_structures/ternary_operators
|
|
4
|
+
title = Ternary operators
|
|
5
|
+
summary = Ternary operators are allowed when kept simple and readable: they are never nested or chained, and their value is always used.
|
|
6
|
+
applies_to = c, h
|
|
7
|
+
severity = major
|
|
8
|
+
detection = automatic
|
|
9
|
+
status = stable
|
|
10
|
+
|
|
11
|
+
[epitech]
|
|
12
|
+
ref = C-C2
|
|
13
|
+
version = 8
|
|
14
|
+
banana = partial
|
|
15
|
+
manual_part = Whether a ternary is simple, readable and does not obfuscate code.
|
|
16
|
+
|
|
17
|
+
[check:nested]
|
|
18
|
+
kind = node_forbid
|
|
19
|
+
node = conditional_expression
|
|
20
|
+
pattern.ancestor = conditional_expression
|
|
21
|
+
report = once_per_line
|
|
22
|
+
message = {code}: nested or chained ternary operators
|
|
23
|
+
|
|
24
|
+
[check:unused_value]
|
|
25
|
+
kind = node_forbid
|
|
26
|
+
node = conditional_expression
|
|
27
|
+
pattern.fields = value_used=false
|
|
28
|
+
message = {code}: the value produced by the ternary operator is not used
|
|
29
|
+
|
|
30
|
+
[example:good_1]
|
|
31
|
+
kind = good
|
|
32
|
+
|
|
33
|
+
[example:good_1:file:ok.c]
|
|
34
|
+
code =
|
|
35
|
+
|int pick(int year, int a, int b)
|
|
36
|
+
|{
|
|
37
|
+
| parity_t year_parity = (year % 2 == 0) ? EVEN : ODD;
|
|
38
|
+
| unsigned int safe_sum = is_sum_overflow(a, b) ? 0 : a + b;
|
|
39
|
+
|
|
|
40
|
+
| return (a > 0 ? a : 0);
|
|
41
|
+
|}
|
|
42
|
+
|
|
43
|
+
[example:bad_1]
|
|
44
|
+
kind = bad
|
|
45
|
+
expect =
|
|
46
|
+
3:22:nested
|
|
47
|
+
4:5:unused_value
|
|
48
|
+
5:5:unused_value
|
|
49
|
+
|
|
50
|
+
[example:bad_1:file:bad.c]
|
|
51
|
+
code =
|
|
52
|
+
|void pick(int b, int c, int e)
|
|
53
|
+
|{
|
|
54
|
+
| int a = b > 10 ? c < 20 ? 50 : 80 : e == 2 ? 4 : 8;
|
|
55
|
+
| already_checked ? go_there() : check();
|
|
56
|
+
| first() ? second() : 0;
|
|
57
|
+
|}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
[rule]
|
|
2
|
+
schema = 2
|
|
3
|
+
id = extensions/language_extensions
|
|
4
|
+
title = Language extensions
|
|
5
|
+
summary = Language extensions are not supported (e.g. __attribute__((noreturn)) instead of [[noreturn]], #pragma once instead of #ifndef/#define/#endif, \e instead of \x1B).
|
|
6
|
+
applies_to = c, h
|
|
7
|
+
severity = info
|
|
8
|
+
detection = automatic
|
|
9
|
+
status = stable
|
|
10
|
+
|
|
11
|
+
[epitech]
|
|
12
|
+
version = 8
|
|
13
|
+
|
|
14
|
+
[argument:extension_keywords]
|
|
15
|
+
type = list
|
|
16
|
+
default = __attribute__, __attribute, __extension__, __typeof__, __typeof, typeof, __auto_type, __label__, __builtin_expect, __builtin_unreachable, __restrict, __restrict__, __inline, __inline__, __volatile__, __const, __signed__, __alignof__
|
|
17
|
+
description = Compiler-specific keywords.
|
|
18
|
+
|
|
19
|
+
[check:keyword]
|
|
20
|
+
kind = token_forbid
|
|
21
|
+
values = @extension_keywords
|
|
22
|
+
message = {code}: language extension '{name}'
|
|
23
|
+
|
|
24
|
+
[check:pragma_once]
|
|
25
|
+
kind = directive
|
|
26
|
+
names = pragma
|
|
27
|
+
operand_forbid = ^once\b
|
|
28
|
+
message = {code}: language extension '#pragma once'
|
|
29
|
+
|
|
30
|
+
[check:escape_e]
|
|
31
|
+
kind = token_forbid
|
|
32
|
+
kinds = string, char
|
|
33
|
+
where.name = (?<!\\)(?:\\\\)*\\e
|
|
34
|
+
message = {code}: language extension '\e' (use \x1B)
|
|
35
|
+
|
|
36
|
+
[check:statement_expression]
|
|
37
|
+
kind = node_forbid
|
|
38
|
+
node = parenthesized_expression
|
|
39
|
+
pattern.fields = statement_expression=true
|
|
40
|
+
message = {code}: language extension: statement expression
|
|
41
|
+
|
|
42
|
+
[example:good_1]
|
|
43
|
+
kind = good
|
|
44
|
+
|
|
45
|
+
[example:good_1:file:ok.c]
|
|
46
|
+
code =
|
|
47
|
+
|[[noreturn]] void stop(void);
|
|
48
|
+
|
|
|
49
|
+
|int main(void)
|
|
50
|
+
|{
|
|
51
|
+
| return my_putstr("\\x1B[0m\\\\e");
|
|
52
|
+
|}
|
|
53
|
+
|
|
54
|
+
[example:bad_1]
|
|
55
|
+
kind = bad
|
|
56
|
+
expect =
|
|
57
|
+
1:1:keyword
|
|
58
|
+
2:1:pragma_once
|
|
59
|
+
6:23:escape_e
|
|
60
|
+
7:12:statement_expression
|
|
61
|
+
|
|
62
|
+
[example:bad_1:file:bad.c]
|
|
63
|
+
code =
|
|
64
|
+
|__attribute__((noreturn)) void stop(void);
|
|
65
|
+
|#pragma once
|
|
66
|
+
|
|
|
67
|
+
|int main(void)
|
|
68
|
+
|{
|
|
69
|
+
| int a = my_putstr("\\e[0m");
|
|
70
|
+
| return ({ a; });
|
|
71
|
+
|}
|