Refactoring rule if_002 for new parser.
This commit is contained in:
parent
e0ca2f59ae
commit
10459a862f
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -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;
|
||||
|
|
@ -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, [])
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 = []
|
||||
|
|
|
|||
Loading…
Reference in New Issue