From cd29da00d0e073d539b7ee2cd246601f01683a04 Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Sat, 29 May 2021 07:26:08 -0500 Subject: [PATCH] Updates for issue #590 Removing enclosed parenthesis would result in invalid VHDL. 1) Updated tests to expose the issue 2) Updated rule if_002 --- vsg/rules/if_statement/rule_002.py | 39 ++++++++++++++----- ...02_test_input.fixed_parenthesis_insert.vhd | 12 ++++++ ...02_test_input.fixed_parenthesis_remove.vhd | 12 ++++++ .../if_statement/rule_002_test_input.vhd | 12 ++++++ vsg/tests/if_statement/test_rule_002.py | 2 +- 5 files changed, 67 insertions(+), 10 deletions(-) diff --git a/vsg/rules/if_statement/rule_002.py b/vsg/rules/if_statement/rule_002.py index ffc8ea45..9c3bcf07 100644 --- a/vsg/rules/if_statement/rule_002.py +++ b/vsg/rules/if_statement/rule_002.py @@ -59,7 +59,7 @@ class rule_002(rule.Rule): else: lParens = build_parenthesis_list(lTokens) lNewParens = remove_inner_parenthesis(lParens) - if len(lNewParens) == 0: + if missing_enclosed_parens(lParens, lNewParens): sSolution = 'Enclose condition in ()\'s.' dAction = {} dAction['action'] = 'insert' @@ -74,12 +74,13 @@ class rule_002(rule.Rule): lNewParens = remove_inner_parenthesis(lParens) if len(lNewParens) == 2: - sSolution = 'Remove enclosing ()\'s' - dAction = {} - dAction['action'] = 'remove' - oViolation = violation.New(oToi.get_line_number(), oToi, sSolution) - oViolation.set_action(dAction) - self.add_violation(oViolation) + if parens_match(lParens, lNewParens): + sSolution = 'Remove enclosing ()\'s' + dAction = {} + dAction['action'] = 'remove' + oViolation = violation.New(oToi.get_line_number(), oToi, sSolution) + oViolation.set_action(dAction) + self.add_violation(oViolation) def insert_parenthesis(option): @@ -99,9 +100,12 @@ def build_parenthesis_list(lTokens): def remove_inner_parenthesis(lParens): +# print('--> remove_inner_parenthesis [' + '-'*80) +# print(f'lParens = {lParens}') lNewParens = [] bSkipCloseParen = False if len(lParens) <= 2: +# print('<-- remove_inner_parenthesis::lParens <= 2 [' + '-'*80) return lParens for iParen, oParen in enumerate(lParens[:-1]): @@ -115,5 +119,22 @@ def remove_inner_parenthesis(lParens): if isinstance(lParens[-2], parser.close_parenthesis) and isinstance(lParens[-1], parser.close_parenthesis): lNewParens.append(lParens[-1]) - lNewParens = remove_inner_parenthesis(lNewParens) - return lNewParens +# print(f'lNewParens = {lNewParens}') + lReturnParens = remove_inner_parenthesis(lNewParens) +# print(f'lReturnParens = {lReturnParens}') +# print('<-- remove_inner_parenthesis [' + '-'*80) + return lReturnParens + + +def parens_match(lParens, lNewParens): + if lParens[0] == lNewParens[0] and lParens[-1] == lNewParens[-1]: + return True + return False + + +def missing_enclosed_parens(lParens, lNewParens): + if len(lNewParens) == 0: + return True + if not parens_match(lParens, lNewParens): + return True + return False diff --git a/vsg/tests/if_statement/rule_002_test_input.fixed_parenthesis_insert.vhd b/vsg/tests/if_statement/rule_002_test_input.fixed_parenthesis_insert.vhd index 11cc7976..1fda3377 100644 --- a/vsg/tests/if_statement/rule_002_test_input.fixed_parenthesis_insert.vhd +++ b/vsg/tests/if_statement/rule_002_test_input.fixed_parenthesis_insert.vhd @@ -35,4 +35,16 @@ begin end process; + process begin + + if ((x(k) = '1') and (v_y = '0')) then + b <= '0'; + end if; + + if (((ctrl_done_d1 = '0') and (CTRL_DONE = '1')) or (dev_addr = dev_addr_prv)) then + b <= '0'; + end if; + + end process; + end architecture RTL; diff --git a/vsg/tests/if_statement/rule_002_test_input.fixed_parenthesis_remove.vhd b/vsg/tests/if_statement/rule_002_test_input.fixed_parenthesis_remove.vhd index 0a04e34b..476ff547 100644 --- a/vsg/tests/if_statement/rule_002_test_input.fixed_parenthesis_remove.vhd +++ b/vsg/tests/if_statement/rule_002_test_input.fixed_parenthesis_remove.vhd @@ -35,4 +35,16 @@ begin end process; + process begin + + if (x(k) = '1') and (v_y = '0') then + b <= '0'; + end if; + + if ((ctrl_done_d1 = '0') and (CTRL_DONE = '1')) or (dev_addr = dev_addr_prv) then + b <= '0'; + end if; + + end process; + end architecture RTL; diff --git a/vsg/tests/if_statement/rule_002_test_input.vhd b/vsg/tests/if_statement/rule_002_test_input.vhd index c0b16217..7b1914f9 100644 --- a/vsg/tests/if_statement/rule_002_test_input.vhd +++ b/vsg/tests/if_statement/rule_002_test_input.vhd @@ -35,4 +35,16 @@ begin end process; + process begin + + if (x(k) = '1') and (v_y = '0') then + b <= '0'; + end if; + + if ((ctrl_done_d1 = '0') and (CTRL_DONE = '1')) or (dev_addr = dev_addr_prv) then + b <= '0'; + end if; + + end process; + end architecture RTL; diff --git a/vsg/tests/if_statement/test_rule_002.py b/vsg/tests/if_statement/test_rule_002.py index 1e4262e7..083cb1d3 100644 --- a/vsg/tests/if_statement/test_rule_002.py +++ b/vsg/tests/if_statement/test_rule_002.py @@ -32,7 +32,7 @@ class test_if_statement_rule(unittest.TestCase): self.assertEqual(oRule.identifier, '002') oRule.parenthesis = 'insert' - lExpected = [24, 26, 28, 30, 32] + lExpected = [24, 26, 28, 30, 32, 40, 44] oRule.analyze(self.oFile) self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations))