Skip to content

Commit a13c1d8

Browse files
authored
Merge pull request #108 from kevv87/feature/71-spacing-pending-rules
Add new spacing rules pending from DML style guide
2 parents a59dae7 + 1db4874 commit a13c1d8

32 files changed

+1312
-370
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Unknown fields in the lint configuration file are now detected and reported as errors, helping users identify and correct typos or unsupported configuration options.
2020
- CLI tool DFA now uses default one-indexed line count for reporting warnings on analyzed files.
2121
`--zero-indexed` flag can be set to `true` when executing DFA for using zero-indexed counting if required.
22+
- Added support for spacing linting rules SpReserved, SpBinop, SpTernary, SpPtrDecl and NspPtrDecl.
2223

2324
## 0.9.12
2425
- Added 'simics\_util\_vect' as a known provisional (with no DLS semantics)

example_files/example_lint_cfg.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
{
2+
"sp_reserved": {},
23
"sp_brace": {},
34
"sp_punct": {},
5+
"sp_binop": {},
6+
"sp_ternary" : {},
7+
"sp_ptrdecl": {},
48
"nsp_funpar": {},
59
"nsp_inparen": {},
610
"nsp_unary": {},
711
"nsp_trailing": {},
12+
"nsp_ptrdecl": {},
813
"long_lines": { "max_length": 80 },
914
"indent_size": { "indentation_spaces": 4 },
1015
"indent_no_tabs": {},

src/analysis/parsing/expression.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use crate::lint::{DMLStyleError,
2121
rules::{spacing::{NspFunparArgs,
2222
NspInparenArgs,
2323
NspUnaryArgs,
24+
SpBinopArgs,
25+
SpTernaryArgs,
2426
SpPunctArgs},
2527
CurrentRules},
2628
AuxParams};
@@ -40,7 +42,7 @@ impl TreeElement for UnaryExpressionContent {
4042
create_subs!(&self.operation, &self.expr)
4143
}
4244
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
43-
rules.nsp_unary.check(acc, NspUnaryArgs::from_unary_expr(self));
45+
rules.nsp_unary.check(NspUnaryArgs::from_unary_expr(self), acc);
4446
}
4547
}
4648

@@ -74,7 +76,7 @@ impl TreeElement for PostUnaryExpressionContent {
7476
create_subs!(&self.expr, &self.operation)
7577
}
7678
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
77-
rules.nsp_unary.check(acc, NspUnaryArgs::from_postunary_expr(self));
79+
rules.nsp_unary.check(NspUnaryArgs::from_postunary_expr(self), acc);
7880
}
7981
}
8082

@@ -92,6 +94,9 @@ impl TreeElement for BinaryExpressionContent {
9294
fn subs(&self) -> TreeElements<'_> {
9395
create_subs!(&self.left, &self.operation, &self.right)
9496
}
97+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
98+
rules.sp_binop.check(SpBinopArgs::from_binary_expression_content(self), acc);
99+
}
95100
}
96101

97102
#[derive(Debug, Clone, PartialEq)]
@@ -151,6 +156,9 @@ impl TreeElement for TertiaryExpressionContent {
151156
create_subs!(&self.left, &self.left_operation,
152157
&self.middle, &self.right_operation, &self.right)
153158
}
159+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
160+
rules.sp_ternary.check(SpTernaryArgs::from_tertiary_expression_content(self), acc);
161+
}
154162
}
155163

156164
#[derive(Debug, Clone, PartialEq)]
@@ -168,7 +176,7 @@ impl TreeElement for ParenExpressionContent {
168176
create_subs!(&self.lparen, &self.expr, &self.rparen)
169177
}
170178
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
171-
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_paren_expression(self));
179+
rules.indent_paren_expr.check(IndentParenExprArgs::from_paren_expression(self), acc);
172180
}
173181
}
174182

@@ -215,10 +223,10 @@ impl TreeElement for FunctionCallContent {
215223
}
216224
}
217225
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
218-
rules.nsp_funpar.check(acc, NspFunparArgs::from_function_call(self));
219-
rules.nsp_inparen.check(acc, NspInparenArgs::from_function_call(self));
220-
rules.sp_punct.check(acc, SpPunctArgs::from_function_call(self));
221-
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_function_call(self));
226+
rules.nsp_funpar.check(NspFunparArgs::from_function_call(self), acc);
227+
rules.nsp_inparen.check(NspInparenArgs::from_function_call(self), acc);
228+
rules.sp_punct.check(SpPunctArgs::from_function_call(self), acc);
229+
rules.indent_paren_expr.check(IndentParenExprArgs::from_function_call(self), acc);
222230
}
223231
}
224232

@@ -333,7 +341,7 @@ impl TreeElement for CastContent {
333341
&self.comma, &self.to, &self.rparen)
334342
}
335343
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
336-
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_cast(self));
344+
rules.indent_paren_expr.check(IndentParenExprArgs::from_cast(self), acc);
337345
}
338346
}
339347

@@ -430,7 +438,7 @@ impl TreeElement for IndexContent {
430438
create_subs!(&self.array, &self.lbracket, &self.index, &self.rbracket)
431439
}
432440
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
433-
rules.nsp_inparen.check(acc, NspInparenArgs::from_index(self));
441+
rules.nsp_inparen.check(NspInparenArgs::from_index(self), acc);
434442
}
435443
}
436444

src/analysis/parsing/misc.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::lint::rules::spacing::{NspPtrDeclArgs, SpPtrDeclArgs};
2+
use crate::lint::{rules::CurrentRules, AuxParams, DMLStyleError};
13
// © 2024 Intel Corporation
24
// SPDX-License-Identifier: Apache-2.0 and MIT
35
use crate::span::Range;
@@ -591,6 +593,10 @@ impl TreeElement for CDeclContent {
591593
create_subs!(&self.consttok, &self.base,
592594
&self.modifiers, &self.decl)
593595
}
596+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
597+
rules.sp_ptrdecl.check(SpPtrDeclArgs::from_cdecl(self), acc);
598+
rules.nsp_ptrdecl.check(NspPtrDeclArgs::from_cdecl(self), acc);
599+
}
594600
}
595601

596602
// corresponds to cdecl in grammar

src/analysis/parsing/statement.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use log::error;
44

55
use crate::lint::rules::indentation::IndentEmptyLoopArgs;
6+
use crate::lint::rules::spacing::SpReservedArgs;
67
use crate::span::Range;
78
use crate::analysis::parsing::lexer::TokenKind;
89
use crate::analysis::parsing::statement;
@@ -145,9 +146,9 @@ impl TreeElement for CompoundContent {
145146
create_subs!(&self.lbrace, &self.statements, &self.rbrace)
146147
}
147148
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
148-
rules.sp_brace.check(acc, SpBracesArgs::from_compound(self));
149-
rules.indent_code_block.check(acc, IndentCodeBlockArgs::from_compound_content(self, aux.depth));
150-
rules.indent_closing_brace.check(acc, IndentClosingBraceArgs::from_compound_content(self, aux.depth));
149+
rules.sp_brace.check(SpBracesArgs::from_compound(self), acc);
150+
rules.indent_code_block.check(IndentCodeBlockArgs::from_compound_content(self, aux.depth), acc);
151+
rules.indent_closing_brace.check(IndentClosingBraceArgs::from_compound_content(self, aux.depth), acc);
151152
}
152153
fn should_increment_depth(&self) -> bool {
153154
true
@@ -203,7 +204,7 @@ impl TreeElement for VariableDeclContent {
203204
self.decls.ensure_named()
204205
}
205206
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
206-
rules.sp_punct.check(acc, SpPunctArgs::from_variable_decl(self));
207+
rules.sp_punct.check(SpPunctArgs::from_variable_decl(self), acc);
207208
}
208209
}
209210

@@ -435,8 +436,9 @@ impl TreeElement for IfContent {
435436
&self.elsebranch)
436437
}
437438
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
438-
rules.nsp_inparen.check(acc, NspInparenArgs::from_if(self));
439-
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_if(self));
439+
rules.nsp_inparen.check(NspInparenArgs::from_if(self), acc);
440+
rules.indent_paren_expr.check(IndentParenExprArgs::from_if(self), acc);
441+
rules.sp_reserved.check(SpReservedArgs::from_if(self), acc);
440442
}
441443
}
442444

@@ -548,8 +550,9 @@ impl TreeElement for WhileContent {
548550
&self.statement)
549551
}
550552
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
551-
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_while(self));
552-
rules.indent_empty_loop.check(acc, IndentEmptyLoopArgs::from_while_content(self, aux.depth));
553+
rules.indent_paren_expr.check(IndentParenExprArgs::from_while(self), acc);
554+
rules.indent_empty_loop.check(IndentEmptyLoopArgs::from_while_content(self, aux.depth), acc);
555+
rules.sp_reserved.check(SpReservedArgs::from_while(self), acc);
553556
}
554557
}
555558

@@ -599,7 +602,7 @@ impl TreeElement for DoContent {
599602
&self.semi)
600603
}
601604
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
602-
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_do_while(self));
605+
rules.indent_paren_expr.check(IndentParenExprArgs::from_do_while(self), acc);
603606
}
604607
}
605608

@@ -864,8 +867,9 @@ impl TreeElement for ForContent {
864867
&self.statement)
865868
}
866869
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
867-
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_for(self));
868-
rules.indent_empty_loop.check(acc, IndentEmptyLoopArgs::from_for_content(self, aux.depth));
870+
rules.indent_paren_expr.check(IndentParenExprArgs::from_for(self), acc);
871+
rules.indent_empty_loop.check(IndentEmptyLoopArgs::from_for_content(self, aux.depth), acc);
872+
rules.sp_reserved.check(SpReservedArgs::from_for(self), acc);
869873
}
870874
}
871875

@@ -1024,7 +1028,7 @@ impl TreeElement for SwitchCase {
10241028
}
10251029
}
10261030
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
1027-
rules.indent_switch_case.check(acc, IndentSwitchCaseArgs::from_switch_case(self, aux.depth));
1031+
rules.indent_switch_case.check(IndentSwitchCaseArgs::from_switch_case(self, aux.depth), acc);
10281032
}
10291033
fn should_increment_depth(&self) -> bool {
10301034
matches!(self, SwitchCase::Statement(statement)
@@ -1114,8 +1118,8 @@ impl TreeElement for SwitchContent {
11141118
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>,
11151119
rules: &CurrentRules, aux: AuxParams)
11161120
{
1117-
rules.indent_closing_brace.check(acc, IndentClosingBraceArgs::from_switch_content(self, aux.depth));
1118-
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_switch(self));
1121+
rules.indent_closing_brace.check(IndentClosingBraceArgs::from_switch_content(self, aux.depth), acc);
1122+
rules.indent_paren_expr.check(IndentParenExprArgs::from_switch(self), acc);
11191123
}
11201124
}
11211125

@@ -1285,6 +1289,9 @@ impl TreeElement for AfterContent {
12851289
&self.callexpression,
12861290
&self.semi)
12871291
}
1292+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
1293+
rules.sp_reserved.check(SpReservedArgs::from_after_content(self), acc);
1294+
}
12881295
}
12891296

12901297
impl Parse<StatementContent> for AfterContent {
@@ -1611,7 +1618,7 @@ impl TreeElement for ForeachContent {
16111618
&self.statement)
16121619
}
16131620
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
1614-
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_foreach(self));
1621+
rules.indent_paren_expr.check(IndentParenExprArgs::from_foreach(self), acc);
16151622
}
16161623
}
16171624

@@ -1750,7 +1757,7 @@ impl TreeElement for ExpressionStmtContent {
17501757
create_subs!(&self.expression, &self.semi)
17511758
}
17521759
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
1753-
rules.sp_punct.check(acc, SpPunctArgs::from_expression_stmt(self));
1760+
rules.sp_punct.check(SpPunctArgs::from_expression_stmt(self), acc);
17541761
}
17551762
}
17561763

src/analysis/parsing/structure.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ use crate::analysis::parsing::parser::{doesnt_understand_tokens,
1616
FileParser, Parse, ParseContext,
1717
FileInfo};
1818
use crate::analysis::LocalDMLError;
19-
use crate::lint::rules::spacing::{SpBracesArgs,
20-
NspInparenArgs,
21-
NspFunparArgs,
22-
SpPunctArgs};
19+
use crate::lint::rules::spacing::{NspFunparArgs, NspInparenArgs, SpBracesArgs, SpPunctArgs};
2320
use crate::lint::rules::indentation::{IndentCodeBlockArgs, IndentClosingBraceArgs, IndentParenExprArgs};
2421
use crate::lint::{rules::CurrentRules, AuxParams, DMLStyleError};
2522
use crate::analysis::reference::{Reference, ReferenceKind};
@@ -236,10 +233,10 @@ impl TreeElement for MethodContent {
236233
errors
237234
}
238235
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
239-
rules.nsp_funpar.check(acc, NspFunparArgs::from_method(self));
240-
rules.nsp_inparen.check(acc, NspInparenArgs::from_method(self));
241-
rules.sp_punct.check(acc, SpPunctArgs::from_method(self));
242-
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_method(self));
236+
rules.nsp_funpar.check(NspFunparArgs::from_method(self), acc);
237+
rules.nsp_inparen.check(NspInparenArgs::from_method(self), acc);
238+
rules.sp_punct.check(SpPunctArgs::from_method(self), acc);
239+
rules.indent_paren_expr.check(IndentParenExprArgs::from_method(self), acc);
243240
}
244241
}
245242

@@ -723,9 +720,9 @@ impl TreeElement for ObjectStatementsContent {
723720
}
724721
}
725722
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
726-
rules.sp_brace.check(acc, SpBracesArgs::from_obj_stmts(self));
727-
rules.indent_code_block.check(acc, IndentCodeBlockArgs::from_obj_stmts_content(self, aux.depth));
728-
rules.indent_closing_brace.check(acc, IndentClosingBraceArgs::from_obj_stmts_content(self, aux.depth));
723+
rules.sp_brace.check(SpBracesArgs::from_obj_stmts(self), acc);
724+
rules.indent_code_block.check(IndentCodeBlockArgs::from_obj_stmts_content(self, aux.depth), acc);
725+
rules.indent_closing_brace.check(IndentClosingBraceArgs::from_obj_stmts_content(self, aux.depth), acc);
729726
}
730727
fn should_increment_depth(&self) -> bool {
731728
matches!(self, ObjectStatementsContent::List(lbrace, list, rbrace)

src/analysis/parsing/types.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ impl TreeElement for StructTypeContent {
5555
errors
5656
}
5757
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
58-
rules.indent_code_block.check(acc, IndentCodeBlockArgs::from_struct_type_content(self, aux.depth));
59-
rules.indent_closing_brace.check(acc, IndentClosingBraceArgs::from_struct_type_content(self, aux.depth));
60-
rules.sp_brace.check(acc, SpBracesArgs::from_struct_type_content(self));
58+
rules.indent_code_block.check(IndentCodeBlockArgs::from_struct_type_content(self, aux.depth), acc);
59+
rules.indent_closing_brace.check(IndentClosingBraceArgs::from_struct_type_content(self, aux.depth), acc);
60+
rules.sp_brace.check(SpBracesArgs::from_struct_type_content(self), acc);
6161
}
6262
fn should_increment_depth(&self) -> bool {
6363
true
@@ -138,9 +138,9 @@ impl TreeElement for LayoutContent {
138138
errors
139139
}
140140
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
141-
rules.indent_code_block.check(acc, IndentCodeBlockArgs::from_layout_content(self, aux.depth));
142-
rules.indent_closing_brace.check(acc, IndentClosingBraceArgs::from_layout_content(self, aux.depth));
143-
rules.sp_brace.check(acc, SpBracesArgs::from_layout_content(self));
141+
rules.indent_code_block.check(IndentCodeBlockArgs::from_layout_content(self, aux.depth), acc);
142+
rules.indent_closing_brace.check(IndentClosingBraceArgs::from_layout_content(self, aux.depth), acc);
143+
rules.sp_brace.check(SpBracesArgs::from_layout_content(self), acc);
144144
}
145145
fn should_increment_depth(&self) -> bool {
146146
true
@@ -315,9 +315,9 @@ impl TreeElement for BitfieldsContent {
315315
errors
316316
}
317317
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
318-
rules.sp_brace.check(acc, SpBracesArgs::from_bitfields_content(self));
319-
rules.indent_code_block.check(acc, IndentCodeBlockArgs::from_bitfields_content(self, aux.depth));
320-
rules.indent_closing_brace.check(acc, IndentClosingBraceArgs::from_bitfields_content(self, aux.depth));
318+
rules.sp_brace.check(SpBracesArgs::from_bitfields_content(self), acc);
319+
rules.indent_code_block.check(IndentCodeBlockArgs::from_bitfields_content(self, aux.depth), acc);
320+
rules.indent_closing_brace.check(IndentClosingBraceArgs::from_bitfields_content(self, aux.depth), acc);
321321
}
322322
fn should_increment_depth(&self) -> bool {
323323
true

src/lint/features.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
Below are listed the currently supported rules for linting:
44

55
## Spacing
6-
- SpBraces, `sp_brace`: spaces around braces (`{` and `}`)
7-
- SpPunct, `sp_punct`: spaces after but not before colon, semicolon and comma
8-
- NspFunpar, `nsp_funpar`: no spaces between a function/method name and its opening parenthesis
9-
- NspInparen, `nsp_inparen`: no spaces immediately inside parentheses or brackets
10-
- NspUnary, `nsp_unary`: no spaces between a unary operator and its operand
11-
- NspTrailing, `nsp_trailing`: no spaces between the last token in a line and the corresponding newline `\n`
6+
- **SpReserved**, `sp_reserved`: spaces around reserved words, such as `if`, `else`, `default`, `size`, `const` and `in`, except when a reserved word is used as an identifier (e.g., `local uint8 *data;`). Currently supported reserved words: `if`, `for` and `while`.
7+
- **SpBinop**, `sp_binop`: spaces around binary operators except for derefencing operators (dot `a.b` and arrow `a->b` )
8+
- **SpTernary**, `sp_ternary`: spaces around `?` and `:` in ternary conditional expressions
9+
- **SpBraces**, `sp_brace`: spaces around braces (`{` and `}`)
10+
- **SpPunct**, `sp_punct`: spaces after but not before colon, semicolon and comma
11+
- **SpPtrDecl**, `sp_ptrdecl`: spaces between a type and the `*` marking a pointer
12+
- **NspFunpar**, `nsp_funpar`: no spaces between a function/method name and its opening parenthesis
13+
- **NspInparen**, `nsp_inparen`: no spaces immediately inside parentheses or brackets
14+
- **NspUnary**, `nsp_unary`: no spaces between a unary operator and its operand
15+
- **NspPtrDecl**, `nsp_ptrdecl`: no spaces after the `*` marking a pointer in a declaration
16+
- **NspTrailing**, `nsp_trailing`: no spaces between the last token in a line and the corresponding newline `\n`
1217

1318
## Indentation
1419
- **IN1**, `indent_size`: Lines are indented a fixed amount of spaces for each indentation level. Defaults to 4, can be set to a custom value by defining field "indentation_spaces" lint configuration [json file](../../example_files/example_lint_cfg.README)

0 commit comments

Comments
 (0)