diff --git a/vsg/rules/if_statement/rule_002.py b/vsg/rules/if_statement/rule_002.py index 2362e707..4dd580d8 100644 --- a/vsg/rules/if_statement/rule_002.py +++ b/vsg/rules/if_statement/rule_002.py @@ -1,40 +1,65 @@ -from vsg import rule -from vsg import utils +import sys -import re +from vsg import rule_item +from vsg import parser +from vsg import token +from vsg import vhdlFile +from vsg import violation + +from vsg.token.ieee.std_logic_1164 import function + +from vsg.vhdlFile import utils -class rule_002(rule.rule): +class rule_002(rule_item.Rule): ''' - If rule 002 checks the if boolean expression is enclosed in ()'s. + Checks the expressions in if statements are enclosed in ()'s. + + Parameters + ---------- + + name : string + The group the rule belongs to. + + 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) - self.name = 'if' - self.identifier = '002' - self.solution = 'Enclose boolean expression in ()\'s.' + rule_item.Rule.__init__(self, 'if', '002') + self.solution = None self.phase = 1 + self.solution = 'Enclose condition in ()\'s.' - def _analyze(self, oFile, oLine, iLineNumber): - if oLine.isIfKeyword and not re.match('^\s*if\s*\(', oLine.lineNoComment.lower()): - dViolation = utils.create_violation_dict(iLineNumber) - self.add_violation(dViolation) - if oLine.isElseIfKeyword and re.match('^\s*elsif\s+\w', oLine.lineNoComment.lower()): - dViolation = utils.create_violation_dict(iLineNumber) - self.add_violation(dViolation) + def analyze(self, oFile): + lToi = oFile.get_if_statement_conditions() - def _fix_violations(self, oFile): - for dViolation in self.violations: - iLineNumber = utils.get_violation_line_number(dViolation) - oLine = oFile.lines[iLineNumber] - if oLine.isIfKeyword: - oLine.update_line(re.sub(r'^(\s*)[i|I][f|F]', r'\1if (', oLine.line)) - if oLine.isElseIfKeyword: - oLine.update_line(re.sub('^(\s*)[e|E][l|L][s|S][i|I][f|F]', r'\1elsif (', oLine.line)) - iThenLineIndex = iLineNumber - while not oLine.isThenKeyword: - iThenLineIndex += 1 - oLine = oFile.lines[iThenLineIndex] - oLine.update_line(re.sub('[t|T][h|H][e|E][n|N]', ') then', oLine.line)) + 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) + + + def fix(self, oFile): + ''' + Applies fixes for any rule violations. + ''' + if self.fixable: + self.analyze(oFile) + self._print_debug_message('Fixing rule: ' + self.name + '_' + self.identifier) + self._fix_violation(oFile) + self.violations = [] + + def _fix_violation(self, oFile): + for oViolation in self.violations: + lTokens = oViolation.get_tokens() + lTokens.insert(0, parser.open_parenthesis()) + lTokens.append(parser.close_parenthesis()) + + oViolation.set_tokens(lTokens) + oFile.update(self.violations) diff --git a/vsg/tests/if_statement/rule_002_test_input.fixed.vhd b/vsg/tests/if_statement/rule_002_test_input.fixed.vhd new file mode 100644 index 00000000..ff3fe58c --- /dev/null +++ b/vsg/tests/if_statement/rule_002_test_input.fixed.vhd @@ -0,0 +1,34 @@ + +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'; + 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'; + 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 new file mode 100644 index 00000000..b7491039 --- /dev/null +++ b/vsg/tests/if_statement/rule_002_test_input.vhd @@ -0,0 +1,34 @@ + +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'; + 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'; + 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 new file mode 100644 index 00000000..35ff6ea1 --- /dev/null +++ b/vsg/tests/if_statement/test_rule_002.py @@ -0,0 +1,46 @@ + +import os +import unittest + +from vsg.rules import if_statement +from vsg import vhdlFile +from vsg.tests import utils + +sTestDir = os.path.dirname(__file__) + +lFile = 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) + + +class test_if_statement_rule(unittest.TestCase): + + def setUp(self): + self.oFile = vhdlFile.vhdlFile(lFile) + + def test_rule_002(self): + oRule = if_statement.rule_002() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'if') + self.assertEqual(oRule.identifier, '002') + + lExpected = [22, 24, 26, 28] + + oRule.analyze(self.oFile) + self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations)) + + def test_fix_rule_002(self): + oRule = if_statement.rule_002() + + oRule.fix(self.oFile) + + lActual = [] + for oLine in self.oFile.lines: + lActual.append(oLine.line) + + self.assertEqual(lExpected, lActual) + + oRule.analyze(self.oFile) + self.assertEqual(oRule.violations, []) diff --git a/vsg/tests/if_statement/test_rule_if.py b/vsg/tests/if_statement/test_rule_if.py index 94658be5..815cfc94 100644 --- a/vsg/tests/if_statement/test_rule_if.py +++ b/vsg/tests/if_statement/test_rule_if.py @@ -16,15 +16,6 @@ oFileIf = vhdlFile.vhdlFile(lFileIf) class testRuleIfMethods(unittest.TestCase): - def test_rule_002(self): - oRule = if_statement.rule_002() - self.assertTrue(oRule) - self.assertEqual(oRule.name, 'if') - self.assertEqual(oRule.identifier, '002') - dExpected = utils.add_violation_list([8,13,24,33,41,46,52]) - oRule.analyze(oFile) - self.assertEqual(oRule.violations, dExpected) - def test_rule_009(self): oRule = if_statement.rule_009() self.assertTrue(oRule) diff --git a/vsg/vhdlFile/extract/__init__.py b/vsg/vhdlFile/extract/__init__.py index 382233f9..750f8bf4 100644 --- a/vsg/vhdlFile/extract/__init__.py +++ b/vsg/vhdlFile/extract/__init__.py @@ -8,3 +8,4 @@ from .get_line_above_line_starting_with_token import get_line_above_line_startin from .get_line_above_line_starting_with_token_with_hierarchy import get_line_above_line_starting_with_token_with_hierarchy from .get_line_below_line_ending_with_token import get_line_below_line_ending_with_token from .get_line_below_line_ending_with_token_with_hierarchy import get_line_below_line_ending_with_token_with_hierarchy +from .get_if_statement_conditions import get_if_statement_conditions diff --git a/vsg/vhdlFile/extract/get_if_statement_conditions.py b/vsg/vhdlFile/extract/get_if_statement_conditions.py new file mode 100644 index 00000000..2fb7a3ed --- /dev/null +++ b/vsg/vhdlFile/extract/get_if_statement_conditions.py @@ -0,0 +1,41 @@ + +from vsg import parser +from vsg.token import if_statement as token + +from vsg.vhdlFile import utils +from vsg.vhdlFile import vhdlFile + +lStartTokens = [] +lStartTokens.append(token.if_keyword) +lStartTokens.append(token.elsif_keyword) + +lEndTokens = [] +lEndTokens.append(token.then_keyword) + + +def get_if_statement_conditions(lAllTokens): + iLine = 1 + lReturn = [] + bTokenFound = False + for iIndex in range(0, len(lAllTokens)): + for oToken in lEndTokens: + if isinstance(lAllTokens[iIndex], oToken) and bTokenFound: + bTokenFound = False + iStartIndex, lTemp = utils.remove_leading_whitespace_and_comments(iStartIndex, lTemp) + lTemp = utils.remove_trailing_whitespace_and_comments(lTemp) + lReturn.append(vhdlFile.Tokens(iStartIndex, iLine, lTemp)) + break + if bTokenFound: + lTemp.append(lAllTokens[iIndex]) + for oToken in lStartTokens: + if isinstance(lAllTokens[iIndex], oToken) and not bTokenFound: + iStartIndex = iIndex + lTemp = [] + bTokenFound = True + break + + if isinstance(lAllTokens[iIndex], parser.carriage_return): + iLine +=1 + + return lReturn + diff --git a/vsg/vhdlFile/utils.py b/vsg/vhdlFile/utils.py index 3045c8e9..565e10b1 100644 --- a/vsg/vhdlFile/utils.py +++ b/vsg/vhdlFile/utils.py @@ -592,3 +592,36 @@ def does_token_type_match(oToken, oType): if isinstance(oToken, oType): return True return False + + +def remove_trailing_whitespace_and_comments(lTokens): + lReturn = [] + lTokens.reverse() + for iIndex in range(0, len(lTokens)): + if token_is_whitespace_or_comment(lTokens[iIndex]): + continue + else: + lMyTokens = lTokens[iIndex:] + lMyTokens.reverse() + return lMyTokens + else: + return lTokens + + +def remove_leading_whitespace_and_comments(iToken, lTokens): + lReturn = [] + for iIndex in range(0, len(lTokens)): + if token_is_whitespace_or_comment(lTokens[iIndex]): + continue + else: + return iToken + iIndex + 1, lTokens[iIndex:] + else: + return iToken, lTokens + +def token_is_whitespace_or_comment(oToken): + if isinstance(oToken, parser.whitespace) or \ + isinstance(oToken, parser.carriage_return) or \ + isinstance(oToken, parser.comment): + return True + else: + return False diff --git a/vsg/vhdlFile/vhdlFile.py b/vsg/vhdlFile/vhdlFile.py index 6f50c3d0..70d4c023 100644 --- a/vsg/vhdlFile/vhdlFile.py +++ b/vsg/vhdlFile/vhdlFile.py @@ -639,6 +639,9 @@ class vhdlFile(): def get_line_below_line_ending_with_token_with_hierarchy(self, lTokens, lHierarchy): return extract.get_line_below_line_ending_with_token_with_hierarchy(lTokens, self.lAllObjects, lHierarchy) + def get_if_statement_conditions(self): + return extract.get_if_statement_conditions(self.lAllObjects) + def get_sequence_of_tokens_not_matching(self, lTokens): iLine = 1 lTemp = []