Merge branch 'issue-577'

This commit is contained in:
Jeremiah Leary 2021-05-06 15:29:05 -05:00
commit 8d38e7989d
6 changed files with 184 additions and 19 deletions

View File

@ -41,6 +41,11 @@ if_002
This rule checks the bolean expression is enclosed in ().
.. NOTE:: There is a configuration option **parenthesis** which will either insert or remove the parenthesis.
parenthesis set to 'insert' (Default)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
**Violation**
.. code-block:: vhdl
@ -53,6 +58,21 @@ This rule checks the bolean expression is enclosed in ().
if (a = '1') then
parenthesis set to 'remove'
^^^^^^^^^^^^^^^^^^^^^^^^^^^
**Violation**
.. code-block:: vhdl
if (a = '1') then
**Fix**
.. code-block:: vhdl
if a = '1' then
if_003
######

View File

@ -18,30 +18,102 @@ class rule_002(rule.Rule):
identifier : string
unique identifier. Usually in the form of 00N.
trigger : parser object type
object type to apply the case check against
'''
def __init__(self):
rule.Rule.__init__(self, 'if', '002')
self.solution = None
self.phase = 1
self.solution = 'Enclose condition in ()\'s.'
self.parenthesis = 'insert'
self.configuration.append('parenthesis')
def _get_tokens_of_interest(self, oFile):
return oFile.get_if_statement_conditions()
def _analyze(self, lToi):
for oToi in lToi:
lTokens = oToi.get_tokens()
if not isinstance(lTokens[0], parser.open_parenthesis):
oViolation = violation.New(oToi.get_line_number(), oToi, self.solution)
self.add_violation(oViolation)
if insert_parenthesis(self.parenthesis):
self._check_insert_parenthesis(oToi)
else:
self._check_remove_parenthesis(oToi)
def _fix_violation(self, oViolation):
lTokens = oViolation.get_tokens()
rules_utils.insert_token(lTokens, 0, parser.open_parenthesis())
lTokens.append(parser.close_parenthesis())
dAction = oViolation.get_action()
if insert_parenthesis(dAction['action']):
rules_utils.insert_token(lTokens, 0, parser.open_parenthesis())
lTokens.append(parser.close_parenthesis())
oViolation.set_tokens(lTokens)
else:
lNewTokens = lTokens[1:-1]
oViolation.set_tokens(lNewTokens)
oViolation.set_tokens(lTokens)
def _check_insert_parenthesis(self, oToi):
lTokens = oToi.get_tokens()
if not isinstance(lTokens[0], parser.open_parenthesis):
sSolution = 'Enclose condition in ()\'s.'
dAction = {}
dAction['action'] = 'insert'
oViolation = violation.New(oToi.get_line_number(), oToi, sSolution)
oViolation.set_action(dAction)
self.add_violation(oViolation)
else:
lParens = build_parenthesis_list(lTokens)
lNewParens = remove_inner_parenthesis(lParens)
if len(lNewParens) == 0:
sSolution = 'Enclose condition in ()\'s.'
dAction = {}
dAction['action'] = 'insert'
oViolation = violation.New(oToi.get_line_number(), oToi, sSolution)
oViolation.set_action(dAction)
self.add_violation(oViolation)
def _check_remove_parenthesis(self, oToi):
lTokens = oToi.get_tokens()
if isinstance(lTokens[0], parser.open_parenthesis):
lParens = build_parenthesis_list(lTokens)
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)
def insert_parenthesis(option):
if option == 'insert':
return True
return False
def build_parenthesis_list(lTokens):
lParens = []
for oToken in lTokens:
if isinstance(oToken, parser.open_parenthesis):
lParens.append(oToken)
elif isinstance(oToken, parser.close_parenthesis):
lParens.append(oToken)
return lParens
def remove_inner_parenthesis(lParens):
lNewParens = []
bSkipCloseParen = False
if len(lParens) <= 2:
return lParens
for iParen, oParen in enumerate(lParens[:-1]):
if bSkipCloseParen:
bSkipCloseParen = False
continue
if isinstance(oParen, parser.open_parenthesis) and isinstance(lParens[iParen + 1], parser.close_parenthesis):
bSkipCloseParen = True
continue
lNewParens.append(oParen)
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

View File

@ -15,6 +15,8 @@ begin
b <= '0';
elsif (a(3 downto 0) + f(34, 56, 72) - g(f(35, 25, 60) downto h(45, 32))) then
b <= '1';
elsif ((a or b) and (c or d)) then
b <= '0';
end if;
-- Violations below
@ -27,6 +29,8 @@ begin
b <= '0';
elsif (a(3 downto 0) + f(34, 56, 72) - g(f(35, 25, 60) downto h(45, 32))) then
b <= '1';
elsif ((a or b) and (c or d)) then
b <= '0';
end if;
end process;

View File

@ -0,0 +1,38 @@
architecture RTL of FIFO is
begin
process
begin
if a = '1' then
b <= '0';
elsif c = '1' then
b <= '1';
elsif a(3 downto 0) = 0 then
b <= '0';
elsif a(3 downto 0) + f(34, 56, 72) - g(f(35, 25, 60) downto h(45, 32)) then
b <= '1';
elsif (a or b) and (c or d) then
b <= '0';
end if;
-- Violations below
if a = '1' then
b <= '0';
elsif c = '1' then
b <= '1';
elsif a(3 downto 0) = 0 then
b <= '0';
elsif a(3 downto 0) + f(34, 56, 72) - g(f(35, 25, 60) downto h(45, 32)) then
b <= '1';
elsif (a or b) and (c or d) then
b <= '0';
end if;
end process;
end architecture RTL;

View File

@ -15,6 +15,8 @@ begin
b <= '0';
elsif (a(3 downto 0) + f(34, 56, 72) - g(f(35, 25, 60) downto h(45, 32))) then
b <= '1';
elsif ((a or b) and (c or d)) then
b <= '0';
end if;
-- Violations below
@ -27,6 +29,8 @@ begin
b <= '0';
elsif a(3 downto 0) + f(34, 56, 72) - g(f(35, 25, 60) downto h(45, 32)) then
b <= '1';
elsif (a or b) and (c or d) then
b <= '0';
end if;
end process;

View File

@ -10,9 +10,13 @@ sTestDir = os.path.dirname(__file__)
lFile, eError =vhdlFile.utils.read_vhdlfile(os.path.join(sTestDir,'rule_002_test_input.vhd'))
lExpected = []
lExpected.append('')
utils.read_file(os.path.join(sTestDir, 'rule_002_test_input.fixed.vhd'), lExpected)
lExpected_parenthesis_insert = []
lExpected_parenthesis_insert.append('')
utils.read_file(os.path.join(sTestDir, 'rule_002_test_input.fixed_parenthesis_insert.vhd'), lExpected_parenthesis_insert)
lExpected_parenthesis_remove = []
lExpected_parenthesis_remove.append('')
utils.read_file(os.path.join(sTestDir, 'rule_002_test_input.fixed_parenthesis_remove.vhd'), lExpected_parenthesis_remove)
class test_if_statement_rule(unittest.TestCase):
@ -21,25 +25,48 @@ class test_if_statement_rule(unittest.TestCase):
self.oFile = vhdlFile.vhdlFile(lFile)
self.assertIsNone(eError)
def test_rule_002(self):
def test_rule_002_parenthesis_insert(self):
oRule = if_statement.rule_002()
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'if')
self.assertEqual(oRule.identifier, '002')
oRule.parenthesis = 'insert'
lExpected = [22, 24, 26, 28]
lExpected = [24, 26, 28, 30, 32]
oRule.analyze(self.oFile)
self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations))
def test_fix_rule_002(self):
def test_fix_rule_002_parenthesis_insert(self):
oRule = if_statement.rule_002()
oRule.fix(self.oFile)
lActual = self.oFile.get_lines()
self.assertEqual(lExpected, lActual)
self.assertEqual(lExpected_parenthesis_insert, lActual)
oRule.analyze(self.oFile)
self.assertEqual(oRule.violations, [])
def test_rule_002_parenthesis_remove(self):
oRule = if_statement.rule_002()
oRule.parenthesis = 'remove'
lExpected = [10, 12, 14, 16, 18]
oRule.analyze(self.oFile)
self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations))
def test_fix_rule_002_parenthesis_remove(self):
oRule = if_statement.rule_002()
oRule.parenthesis = 'remove'
oRule.fix(self.oFile)
lActual = self.oFile.get_lines()
self.assertEqual(lExpected_parenthesis_remove, lActual)
oRule.analyze(self.oFile)
self.assertEqual(oRule.violations, [])