Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Unknown fields in the lint configuration file are now detected and reported as errors, helping users identify and correct typos or unsupported configuration options.
- CLI tool DFA now uses default one-indexed line count for reporting warnings on analyzed files.
`--zero-indexed` flag can be set to `true` when executing DFA for using zero-indexed counting if required.
- Added support for spacing linting rules SpReserved, SpBinop, SpTernary, SpPtrDecl and NspPtrDecl.

## 0.9.12
- Added 'simics\_util\_vect' as a known provisional (with no DLS semantics)
Expand Down
5 changes: 5 additions & 0 deletions example_files/example_lint_cfg.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"sp_reserved": {},
"sp_brace": {},
"sp_punct": {},
"sp_binop": {},
"sp_ternary" : {},
"sp_ptrdecl": {},
"nsp_funpar": {},
"nsp_inparen": {},
"nsp_unary": {},
"nsp_trailing": {},
"nsp_ptrdecl": {},
"long_lines": { "max_length": 80 },
"indent_size": { "indentation_spaces": 4 },
"indent_no_tabs": {},
Expand Down
26 changes: 17 additions & 9 deletions src/analysis/parsing/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use crate::lint::{DMLStyleError,
rules::{spacing::{NspFunparArgs,
NspInparenArgs,
NspUnaryArgs,
SpBinopArgs,
SpTernaryArgs,
SpPunctArgs},
CurrentRules},
AuxParams};
Expand All @@ -40,7 +42,7 @@ impl TreeElement for UnaryExpressionContent {
create_subs!(&self.operation, &self.expr)
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.nsp_unary.check(acc, NspUnaryArgs::from_unary_expr(self));
rules.nsp_unary.check(NspUnaryArgs::from_unary_expr(self), acc);
}
}

Expand Down Expand Up @@ -74,7 +76,7 @@ impl TreeElement for PostUnaryExpressionContent {
create_subs!(&self.expr, &self.operation)
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.nsp_unary.check(acc, NspUnaryArgs::from_postunary_expr(self));
rules.nsp_unary.check(NspUnaryArgs::from_postunary_expr(self), acc);
}
}

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

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

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

Expand Down Expand Up @@ -215,10 +223,10 @@ impl TreeElement for FunctionCallContent {
}
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.nsp_funpar.check(acc, NspFunparArgs::from_function_call(self));
rules.nsp_inparen.check(acc, NspInparenArgs::from_function_call(self));
rules.sp_punct.check(acc, SpPunctArgs::from_function_call(self));
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_function_call(self));
rules.nsp_funpar.check(NspFunparArgs::from_function_call(self), acc);
rules.nsp_inparen.check(NspInparenArgs::from_function_call(self), acc);
rules.sp_punct.check(SpPunctArgs::from_function_call(self), acc);
rules.indent_paren_expr.check(IndentParenExprArgs::from_function_call(self), acc);
}
}

Expand Down Expand Up @@ -333,7 +341,7 @@ impl TreeElement for CastContent {
&self.comma, &self.to, &self.rparen)
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_cast(self));
rules.indent_paren_expr.check(IndentParenExprArgs::from_cast(self), acc);
}
}

Expand Down Expand Up @@ -430,7 +438,7 @@ impl TreeElement for IndexContent {
create_subs!(&self.array, &self.lbracket, &self.index, &self.rbracket)
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.nsp_inparen.check(acc, NspInparenArgs::from_index(self));
rules.nsp_inparen.check(NspInparenArgs::from_index(self), acc);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/analysis/parsing/misc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::lint::rules::spacing::{NspPtrDeclArgs, SpPtrDeclArgs};
use crate::lint::{rules::CurrentRules, AuxParams, DMLStyleError};
// © 2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0 and MIT
use crate::span::Range;
Expand Down Expand Up @@ -591,6 +593,10 @@ impl TreeElement for CDeclContent {
create_subs!(&self.consttok, &self.base,
&self.modifiers, &self.decl)
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.sp_ptrdecl.check(SpPtrDeclArgs::from_cdecl(self), acc);
rules.nsp_ptrdecl.check(NspPtrDeclArgs::from_cdecl(self), acc);
}
}

// corresponds to cdecl in grammar
Expand Down
39 changes: 23 additions & 16 deletions src/analysis/parsing/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use log::error;

use crate::lint::rules::indentation::IndentEmptyLoopArgs;
use crate::lint::rules::spacing::SpReservedArgs;
use crate::span::Range;
use crate::analysis::parsing::lexer::TokenKind;
use crate::analysis::parsing::statement;
Expand Down Expand Up @@ -145,9 +146,9 @@ impl TreeElement for CompoundContent {
create_subs!(&self.lbrace, &self.statements, &self.rbrace)
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
rules.sp_brace.check(acc, SpBracesArgs::from_compound(self));
rules.indent_code_block.check(acc, IndentCodeBlockArgs::from_compound_content(self, aux.depth));
rules.indent_closing_brace.check(acc, IndentClosingBraceArgs::from_compound_content(self, aux.depth));
rules.sp_brace.check(SpBracesArgs::from_compound(self), acc);
rules.indent_code_block.check(IndentCodeBlockArgs::from_compound_content(self, aux.depth), acc);
rules.indent_closing_brace.check(IndentClosingBraceArgs::from_compound_content(self, aux.depth), acc);
}
fn should_increment_depth(&self) -> bool {
true
Expand Down Expand Up @@ -203,7 +204,7 @@ impl TreeElement for VariableDeclContent {
self.decls.ensure_named()
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.sp_punct.check(acc, SpPunctArgs::from_variable_decl(self));
rules.sp_punct.check(SpPunctArgs::from_variable_decl(self), acc);
}
}

Expand Down Expand Up @@ -435,8 +436,9 @@ impl TreeElement for IfContent {
&self.elsebranch)
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.nsp_inparen.check(acc, NspInparenArgs::from_if(self));
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_if(self));
rules.nsp_inparen.check(NspInparenArgs::from_if(self), acc);
rules.indent_paren_expr.check(IndentParenExprArgs::from_if(self), acc);
rules.sp_reserved.check(SpReservedArgs::from_if(self), acc);
}
}

Expand Down Expand Up @@ -548,8 +550,9 @@ impl TreeElement for WhileContent {
&self.statement)
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_while(self));
rules.indent_empty_loop.check(acc, IndentEmptyLoopArgs::from_while_content(self, aux.depth));
rules.indent_paren_expr.check(IndentParenExprArgs::from_while(self), acc);
rules.indent_empty_loop.check(IndentEmptyLoopArgs::from_while_content(self, aux.depth), acc);
rules.sp_reserved.check(SpReservedArgs::from_while(self), acc);
}
}

Expand Down Expand Up @@ -599,7 +602,7 @@ impl TreeElement for DoContent {
&self.semi)
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_do_while(self));
rules.indent_paren_expr.check(IndentParenExprArgs::from_do_while(self), acc);
}
}

Expand Down Expand Up @@ -864,8 +867,9 @@ impl TreeElement for ForContent {
&self.statement)
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_for(self));
rules.indent_empty_loop.check(acc, IndentEmptyLoopArgs::from_for_content(self, aux.depth));
rules.indent_paren_expr.check(IndentParenExprArgs::from_for(self), acc);
rules.indent_empty_loop.check(IndentEmptyLoopArgs::from_for_content(self, aux.depth), acc);
rules.sp_reserved.check(SpReservedArgs::from_for(self), acc);
}
}

Expand Down Expand Up @@ -1024,7 +1028,7 @@ impl TreeElement for SwitchCase {
}
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
rules.indent_switch_case.check(acc, IndentSwitchCaseArgs::from_switch_case(self, aux.depth));
rules.indent_switch_case.check(IndentSwitchCaseArgs::from_switch_case(self, aux.depth), acc);
}
fn should_increment_depth(&self) -> bool {
matches!(self, SwitchCase::Statement(statement)
Expand Down Expand Up @@ -1114,8 +1118,8 @@ impl TreeElement for SwitchContent {
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>,
rules: &CurrentRules, aux: AuxParams)
{
rules.indent_closing_brace.check(acc, IndentClosingBraceArgs::from_switch_content(self, aux.depth));
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_switch(self));
rules.indent_closing_brace.check(IndentClosingBraceArgs::from_switch_content(self, aux.depth), acc);
rules.indent_paren_expr.check(IndentParenExprArgs::from_switch(self), acc);
}
}

Expand Down Expand Up @@ -1285,6 +1289,9 @@ impl TreeElement for AfterContent {
&self.callexpression,
&self.semi)
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.sp_reserved.check(SpReservedArgs::from_after_content(self), acc);
}
}

impl Parse<StatementContent> for AfterContent {
Expand Down Expand Up @@ -1611,7 +1618,7 @@ impl TreeElement for ForeachContent {
&self.statement)
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_foreach(self));
rules.indent_paren_expr.check(IndentParenExprArgs::from_foreach(self), acc);
}
}

Expand Down Expand Up @@ -1750,7 +1757,7 @@ impl TreeElement for ExpressionStmtContent {
create_subs!(&self.expression, &self.semi)
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.sp_punct.check(acc, SpPunctArgs::from_expression_stmt(self));
rules.sp_punct.check(SpPunctArgs::from_expression_stmt(self), acc);
}
}

Expand Down
19 changes: 8 additions & 11 deletions src/analysis/parsing/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ use crate::analysis::parsing::parser::{doesnt_understand_tokens,
FileParser, Parse, ParseContext,
FileInfo};
use crate::analysis::LocalDMLError;
use crate::lint::rules::spacing::{SpBracesArgs,
NspInparenArgs,
NspFunparArgs,
SpPunctArgs};
use crate::lint::rules::spacing::{NspFunparArgs, NspInparenArgs, SpBracesArgs, SpPunctArgs};
use crate::lint::rules::indentation::{IndentCodeBlockArgs, IndentClosingBraceArgs, IndentParenExprArgs};
use crate::lint::{rules::CurrentRules, AuxParams, DMLStyleError};
use crate::analysis::reference::{Reference, ReferenceKind};
Expand Down Expand Up @@ -236,10 +233,10 @@ impl TreeElement for MethodContent {
errors
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.nsp_funpar.check(acc, NspFunparArgs::from_method(self));
rules.nsp_inparen.check(acc, NspInparenArgs::from_method(self));
rules.sp_punct.check(acc, SpPunctArgs::from_method(self));
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_method(self));
rules.nsp_funpar.check(NspFunparArgs::from_method(self), acc);
rules.nsp_inparen.check(NspInparenArgs::from_method(self), acc);
rules.sp_punct.check(SpPunctArgs::from_method(self), acc);
rules.indent_paren_expr.check(IndentParenExprArgs::from_method(self), acc);
}
}

Expand Down Expand Up @@ -723,9 +720,9 @@ impl TreeElement for ObjectStatementsContent {
}
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
rules.sp_brace.check(acc, SpBracesArgs::from_obj_stmts(self));
rules.indent_code_block.check(acc, IndentCodeBlockArgs::from_obj_stmts_content(self, aux.depth));
rules.indent_closing_brace.check(acc, IndentClosingBraceArgs::from_obj_stmts_content(self, aux.depth));
rules.sp_brace.check(SpBracesArgs::from_obj_stmts(self), acc);
rules.indent_code_block.check(IndentCodeBlockArgs::from_obj_stmts_content(self, aux.depth), acc);
rules.indent_closing_brace.check(IndentClosingBraceArgs::from_obj_stmts_content(self, aux.depth), acc);
}
fn should_increment_depth(&self) -> bool {
matches!(self, ObjectStatementsContent::List(lbrace, list, rbrace)
Expand Down
18 changes: 9 additions & 9 deletions src/analysis/parsing/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ impl TreeElement for StructTypeContent {
errors
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
rules.indent_code_block.check(acc, IndentCodeBlockArgs::from_struct_type_content(self, aux.depth));
rules.indent_closing_brace.check(acc, IndentClosingBraceArgs::from_struct_type_content(self, aux.depth));
rules.sp_brace.check(acc, SpBracesArgs::from_struct_type_content(self));
rules.indent_code_block.check(IndentCodeBlockArgs::from_struct_type_content(self, aux.depth), acc);
rules.indent_closing_brace.check(IndentClosingBraceArgs::from_struct_type_content(self, aux.depth), acc);
rules.sp_brace.check(SpBracesArgs::from_struct_type_content(self), acc);
}
fn should_increment_depth(&self) -> bool {
true
Expand Down Expand Up @@ -138,9 +138,9 @@ impl TreeElement for LayoutContent {
errors
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
rules.indent_code_block.check(acc, IndentCodeBlockArgs::from_layout_content(self, aux.depth));
rules.indent_closing_brace.check(acc, IndentClosingBraceArgs::from_layout_content(self, aux.depth));
rules.sp_brace.check(acc, SpBracesArgs::from_layout_content(self));
rules.indent_code_block.check(IndentCodeBlockArgs::from_layout_content(self, aux.depth), acc);
rules.indent_closing_brace.check(IndentClosingBraceArgs::from_layout_content(self, aux.depth), acc);
rules.sp_brace.check(SpBracesArgs::from_layout_content(self), acc);
}
fn should_increment_depth(&self) -> bool {
true
Expand Down Expand Up @@ -315,9 +315,9 @@ impl TreeElement for BitfieldsContent {
errors
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
rules.sp_brace.check(acc, SpBracesArgs::from_bitfields_content(self));
rules.indent_code_block.check(acc, IndentCodeBlockArgs::from_bitfields_content(self, aux.depth));
rules.indent_closing_brace.check(acc, IndentClosingBraceArgs::from_bitfields_content(self, aux.depth));
rules.sp_brace.check(SpBracesArgs::from_bitfields_content(self), acc);
rules.indent_code_block.check(IndentCodeBlockArgs::from_bitfields_content(self, aux.depth), acc);
rules.indent_closing_brace.check(IndentClosingBraceArgs::from_bitfields_content(self, aux.depth), acc);
}
fn should_increment_depth(&self) -> bool {
true
Expand Down
17 changes: 11 additions & 6 deletions src/lint/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
Below are listed the currently supported rules for linting:

## Spacing
- SpBraces, `sp_brace`: spaces around braces (`{` and `}`)
- SpPunct, `sp_punct`: spaces after but not before colon, semicolon and comma
- NspFunpar, `nsp_funpar`: no spaces between a function/method name and its opening parenthesis
- NspInparen, `nsp_inparen`: no spaces immediately inside parentheses or brackets
- NspUnary, `nsp_unary`: no spaces between a unary operator and its operand
- NspTrailing, `nsp_trailing`: no spaces between the last token in a line and the corresponding newline `\n`
- **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`.
- **SpBinop**, `sp_binop`: spaces around binary operators except for derefencing operators (dot `a.b` and arrow `a->b` )
- **SpTernary**, `sp_ternary`: spaces around `?` and `:` in ternary conditional expressions
- **SpBraces**, `sp_brace`: spaces around braces (`{` and `}`)
- **SpPunct**, `sp_punct`: spaces after but not before colon, semicolon and comma
- **SpPtrDecl**, `sp_ptrdecl`: spaces between a type and the `*` marking a pointer
- **NspFunpar**, `nsp_funpar`: no spaces between a function/method name and its opening parenthesis
- **NspInparen**, `nsp_inparen`: no spaces immediately inside parentheses or brackets
- **NspUnary**, `nsp_unary`: no spaces between a unary operator and its operand
- **NspPtrDecl**, `nsp_ptrdecl`: no spaces after the `*` marking a pointer in a declaration
- **NspTrailing**, `nsp_trailing`: no spaces between the last token in a line and the corresponding newline `\n`

## Indentation
- **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)
Expand Down
Loading