Skip to content
Closed
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
48 changes: 0 additions & 48 deletions src/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,41 +189,6 @@ pub mod tests {
use std::str::FromStr;
use crate::{analysis::{parsing::{parser::FileInfo, structure::{self, TopAst}}, FileSpec}, vfs::TextFile};

pub static SOURCE: &str = "
dml 1.4;

bank sb_cr {
group monitor {

register MKTME_KEYID_MASK {
method get() -> (uint64) {
local uint64 physical_address_mask = mse.srv10nm_mse_mktme.get_key_addr_mask();
this.Mask.set(physical_address_mask);
this.function_with_args('some_string',
integer,
floater);
return this.val;
}
}

register TDX_KEYID_MASK {
method get() -> (uint64) {
local uint64 tdx_keyid_mask = mse.srv10nm_mse_tdx.get_key_addr_mask();
local uint64 some_uint = (is_this_real) ? then_you_might_like_this_value : or_this_one;
this.Mask.set(tdx_keyid_mask);
return this.val;
}
}
}
}

/*
This is ONEEEE VEEEEEERY LLOOOOOOONG COOOMMMEENTT ON A SINGLEEEE LINEEEEEEEEEEEEEE
and ANOTHEEEER VEEEEEERY LLOOOOOOONG COOOMMMEENTT ON A SINGLEEEE LINEEEEEEEEEEEEEE
*/

";

pub fn create_ast_from_snippet(source: &str) -> TopAst {
use logos::Logos;
use crate::analysis::parsing::lexer::TokenKind;
Expand Down Expand Up @@ -251,17 +216,4 @@ pub mod tests {
let example_cfg = parse_lint_cfg(example_path.into()).unwrap();
assert_eq!(example_cfg, LintCfg::default());
}

#[test]
#[ignore]
fn test_main() {
use crate::lint::{begin_style_check, LintCfg};
use crate::lint::rules:: instantiate_rules;
let ast = create_ast_from_snippet(SOURCE);
let cfg = LintCfg::default();
let rules = instantiate_rules(&cfg);
let _lint_errors = begin_style_check(ast, SOURCE.to_string(), &rules);
assert!(_lint_errors.is_ok());
assert!(!_lint_errors.unwrap().is_empty());
}
}
9 changes: 1 addition & 8 deletions src/lint/rules/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,7 @@ pub fn run_linter(source_code: &str, rules: &CurrentRules)
begin_style_check(ast, source_code.to_string(), rules)
}

pub fn assert_snippet(source_code: &str, expected_errors: usize, rules: &CurrentRules) {
let lint_errors = run_linter(source_code, rules);
assert!(lint_errors.is_ok());
assert_eq!(lint_errors.clone().unwrap().len(), expected_errors,
"{:#?}", lint_errors);
}

pub fn robust_assert_snippet(source_code: &str, expected_errors: Vec<ExpectedDMLStyleError>, rules: &CurrentRules) {
pub fn assert_snippet(source_code: &str, expected_errors: Vec<ExpectedDMLStyleError>, rules: &CurrentRules) {
let lint_errors = run_linter(source_code, rules);
assert!(lint_errors.is_ok());
let lint_errors = lint_errors.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/lint/rules/tests/indentation/closing_brace.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::lint::rules::tests::common::{set_up, robust_assert_snippet as assert_snippet};
use crate::lint::rules::tests::common::{set_up, assert_snippet};
use crate::lint::rules::RuleType;

static BASIC_COMPOUND_CORRECT: &str = "
Expand Down
2 changes: 1 addition & 1 deletion src/lint/rules/tests/indentation/code_block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::lint::rules::tests::common::{set_up, robust_assert_snippet as assert_snippet};
use crate::lint::rules::tests::common::{set_up, assert_snippet};
use crate::lint::rules::RuleType;

static FUNCTION_CONTENTS_INDENT_CORRECT: &str = "
Expand Down
2 changes: 1 addition & 1 deletion src/lint/rules/tests/indentation/empty_loop.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::lint::rules::tests::common::{set_up, robust_assert_snippet as assert_snippet};
use crate::lint::rules::tests::common::{set_up, assert_snippet};
use crate::lint::rules::RuleType;

static EMPTY_LOOP_INCORRECT: &str = "
Expand Down
15 changes: 12 additions & 3 deletions src/lint/rules/tests/indentation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod switch_case;
mod empty_loop;

use crate::lint::rules::tests::common::assert_snippet;
use crate::lint::rules::RuleType;
use crate::lint::LintCfg;
use crate::lint::LongLineOptions;
use crate::lint::rules::instantiate_rules;
Expand All @@ -20,15 +21,23 @@ param some_parameter_name_in_this_device = some_long_name_bank.some_long_name_gr
fn line_length_incorrect() {
let mut cfg = LintCfg::default();
let mut rules = instantiate_rules(&cfg);
assert_snippet(LINE_LENGTH_INCORRECT, 1, &rules);
let expected_errors = define_expected_errors!(
RuleType::LL1,
(1, 1, 80, 103),
);
assert_snippet(LINE_LENGTH_INCORRECT, expected_errors, &rules);
// Test rule disable
cfg.long_lines = None;
rules = instantiate_rules(&cfg);
assert_snippet(LINE_LENGTH_INCORRECT, 0, &rules);
assert_snippet(LINE_LENGTH_INCORRECT, vec![], &rules);
// Test lower max_length
cfg.long_lines = Some(LongLineOptions{
max_length: (LINE_LENGTH_INCORRECT.len()-3).try_into().unwrap()
});
rules = instantiate_rules(&cfg);
assert_snippet(LINE_LENGTH_INCORRECT, 1, &rules);
let expected_errors = define_expected_errors!(
RuleType::LL1,
(1, 1, 102, 103),
);
assert_snippet(LINE_LENGTH_INCORRECT, expected_errors, &rules);
}
2 changes: 1 addition & 1 deletion src/lint/rules/tests/indentation/no_tabs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::lint::rules::tests::common::{set_up, robust_assert_snippet as assert_snippet};
use crate::lint::rules::tests::common::{set_up, assert_snippet};
use crate::lint::rules::RuleType;

static USING_TAB_INDENT_INCORRECT: &str = "
Expand Down
2 changes: 1 addition & 1 deletion src/lint/rules/tests/indentation/paren_expr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::lint::rules::tests::common::{set_up, robust_assert_snippet as assert_snippet};
use crate::lint::rules::tests::common::{set_up, assert_snippet};
use crate::lint::rules::RuleType;

// A continuation line that is broken inside a parenthesized expression
Expand Down
2 changes: 1 addition & 1 deletion src/lint/rules/tests/indentation/switch_case.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::lint::rules::tests::common::{set_up, robust_assert_snippet as assert_snippet};
use crate::lint::rules::tests::common::{set_up, assert_snippet};
use crate::lint::rules::RuleType;

static SWITCH_CASE_INDENT_CORRECT: &str = "
Expand Down
155 changes: 6 additions & 149 deletions src/lint/rules/tests/spacing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::lint::rules::tests::common::assert_snippet;
use crate::lint::rules::instantiate_rules;
use crate::lint::LintCfg;
mod nsp_funpar;
mod nsp_inparen;
mod nsp_trailing;
mod nsp_unary;
mod sp_braces;
mod sp_punct;

// Put whitespace (space or newline):
// SP.reserved around reserved words, such as if, else, default,
Expand All @@ -15,51 +18,6 @@ if(this_some_integer == 0x666)
}
";

// SP.braces around braces ({ and })
static SP_BRACES: &str = "
method this_is_some_method() {return 0;}

method this_is_empty_method() { }

bank pcie_config {register command {field mem {
method pcie_write(uint64 value) {
if (value != 0) {value = value + 1;}
default(value);
map_memory_alt();
}
}
}
}
";
#[test]
fn style_check_sp_braces() {
let mut cfg = LintCfg::default();
let mut rules = instantiate_rules(&cfg);
assert_snippet(SP_BRACES, 6, &rules);
// Test rule disable
cfg.sp_brace = None;
rules = instantiate_rules(&cfg);
assert_snippet(SP_BRACES, 0, &rules);

}

static SP_BRACES_02: &str = "
typedef struct {uint16 idx;} hqm_cq_list_release_ctx_t;

typedef layout \"little-endian\" {bitfields 1 {uint1 cq @ [0:0];} byte;} q_t;
";
#[test]
fn style_check_sp_braces_02() {
let mut cfg = LintCfg::default();
let mut rules = instantiate_rules(&cfg);
assert_snippet(SP_BRACES_02, 6, &rules);
// Test rule disable
cfg.sp_brace = None;
rules = instantiate_rules(&cfg);
assert_snippet(SP_BRACES_02, 0, &rules);

}

// SP.binop around binary operators except the dereferencing operators dot
// (a.b) and arrow (a->b)
#[allow(dead_code)]
Expand All @@ -79,27 +37,6 @@ local int this_some_integer = (flag?5:7));
}
";

// SP.punct after but not before colon, semicolon and comma
#[allow(dead_code)]
static SP_PUNCT: &str = "
method this_is_some_method(bool flag ,int8 var) {
local int this_some_integer = 0x666 ;
if(this_some_integer == 0x666)
return;
some_func(arg1 ,arg2 ,arg3 ,arg4);
}
";
#[test]
fn style_check_sp_punct_rule() {
let mut cfg = LintCfg::default();
let mut rules = instantiate_rules(&cfg);
assert_snippet(SP_PUNCT, 9, &rules);
// Test rule disable
cfg.sp_punct = None;
rules = instantiate_rules(&cfg);
assert_snippet(SP_PUNCT, 0, &rules);
}

// SP.ptrdecl between a type and the * marking a pointer
#[allow(dead_code)]
static SP_PTRDECL: &str = "
Expand All @@ -120,65 +57,6 @@ if(!dummy_obj)//Not null
}
";

// There should be no space:
// NSP.funpar between a function/method name and its opening parenthesis
static NSP_FUNPAR: &str = "
method this_is_some_method (conf_object_t *dummy_obj) {
if(!dummy_obj)
other_method_called ();
}
";
#[test]
fn style_check_nsp_funpar() {
let mut cfg = LintCfg::default();
let mut rules = instantiate_rules(&cfg);
assert_snippet(NSP_FUNPAR, 2, &rules);
// Test rule disable
cfg.nsp_funpar = None;
rules = instantiate_rules(&cfg);
assert_snippet(NSP_FUNPAR, 0, &rules);
}

// NSP.inparen immediately inside parentheses or brackets
static NSP_INPAREN: &str = "
method this_is_some_method( conf_object_t *dummy_obj ) {
if( !dummy_obj[ 0 ] )
return;
}
";
#[test]
fn style_check_nsp_inparen() {
let mut cfg = LintCfg::default();
let mut rules = instantiate_rules(&cfg);
assert_snippet(NSP_INPAREN, 6, &rules);
// Test rule disable
cfg.nsp_inparen = None;
rules = instantiate_rules(&cfg);
assert_snippet(NSP_INPAREN, 0, &rules);
}

// NSP.unary between a unary operator and its operand
static NSP_UNARY: &str = "
method this_is_some_method(conf_object_t *dummy_obj) {
if(! dummy_obj)
return;
local uint64 p = & dummy_obj;
p ++;
-- p;
local int64 neg = - 1;
}
";
#[test]
fn style_check_nsp_unary() {
let mut cfg = LintCfg::default();
let mut rules = instantiate_rules(&cfg);
assert_snippet(NSP_UNARY, 5, &rules);
// Test rule disable
cfg.nsp_unary = None;
rules = instantiate_rules(&cfg);
assert_snippet(NSP_UNARY, 0, &rules);
}

// NSP.ptrdecl after the * marking a pointer in a declaration
#[allow(dead_code)]
static NSP_PTRDECL: &str = "
Expand All @@ -187,24 +65,3 @@ if(!dummy_obj)
return;
}
";

// Adding trailing whitespace removal to spacing rules:
// no whitespaces should be left at the end of a line between the last token
// and the newline \n
static NSP_TRAILING: &str = "
method this_is_some_method(int64 num) {
local int this_some_integer = 0x666;
if (this_some_integer == 0x666)
return;
}
";
#[test]
fn style_check_nsp_trailing() {
let mut cfg = LintCfg::default();
let mut rules = instantiate_rules(&cfg);
assert_snippet(NSP_TRAILING, 4, &rules);
// Test rule disable
cfg.nsp_trailing = None;
rules = instantiate_rules(&cfg);
assert_snippet(NSP_TRAILING, 0, &rules);
}
36 changes: 36 additions & 0 deletions src/lint/rules/tests/spacing/nsp_funpar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::lint::rules::tests::common::{set_up, assert_snippet};
use crate::lint::rules::RuleType;

// There should be no space:
// NSP.funpar between a function/method name and its opening parenthesis
static NO_SPACE_METHOD_FUNC_INCORRECT: &str = "
method this_is_some_method (conf_object_t *dummy_obj) {
if(!dummy_obj)
other_method_called ();
}
";
#[test]
fn no_space_method_func_incorrect() {
let mut rules = set_up();
let expected_errors = define_expected_errors!(
RuleType::NspFunpar,
(1, 1, 26, 27),
(3, 3, 27, 28),
);
assert_snippet(NO_SPACE_METHOD_FUNC_INCORRECT, expected_errors, &rules);
// Test rule disable
rules.nsp_funpar.enabled = false;
assert_snippet(NO_SPACE_METHOD_FUNC_INCORRECT, vec![], &rules);
}

static NO_SPACE_METHOD_FUNC_CORRECT: &str = "
method this_is_some_method(conf_object_t *dummy_obj) {
if(!dummy_obj)
other_method_called();
}
";
#[test]
fn no_space_method_func_correct() {
let rules = set_up();
assert_snippet(NO_SPACE_METHOD_FUNC_CORRECT, vec![], &rules);
}
Loading