Refactoring rule process_021 for new parser.

This commit is contained in:
Jeremiah Leary 2020-10-26 21:07:33 -05:00
parent 10459a862f
commit dbe74ff551
7 changed files with 214 additions and 58 deletions

View File

@ -458,12 +458,22 @@ This rule checks the indentation of multiline sensitivity lists.
process_021
###########
This rule checks for blank lines between the end of the sensitivity list and before the **begin** keyword.
This rule checks for blank lines before the **begin** keyword if there are no process declarative items.
**Violation**
.. code-block:: vhdl
proc_a : process
begin
proc_a : process (rd_en, wr_en)
begin
proc_a : process (rd_en, wr_en, data_in, data_out,
rd_full, wr_full
) is
@ -476,7 +486,15 @@ This rule checks for blank lines between the end of the sensitivity list and bef
.. code-block:: vhdl
PROC_A : process (rd_en, wr_en, data_in, data_out,
proc_a : process
begin
proc_a : process (rd_en, wr_en)
begin
proc_a : process (rd_en, wr_en, data_in, data_out,
rd_full, wr_full
) is
begin

View File

@ -1,51 +1,73 @@
from vsg import rule
from vsg import fix
from vsg import utils
from vsg import rule_item
from vsg import parser
from vsg import violation
from vsg.token import process_statement as token
from vsg.vhdlFile import utils
class rule_021(rule.rule):
class rule_021(rule_item.Rule):
'''
Process rule 021 checks for blank lines between the end of the sensitivity list and before the "begin" keyword.
Checks for blank lines before the begin keyword if there are no process declarative items.
'''
def __init__(self):
rule.rule.__init__(self, 'process', '021')
self.solution = 'Remove blank lines between the end of the sensitivity list and before the "begin" keyword.'
self.phase = 3
rule_item.Rule.__init__(self, 'process', '021')
self.solution = 'Remove blank lines above begin keyword'
self.phase = 1
def analyze(self, oFile):
fCheckForBlanks = False
fBlanksFound = False
fNonBlanksFound = False
fSkipProcess = False
for iLineNumber, oLine in enumerate(oFile.lines):
if not self._is_vsg_off(oLine):
if oLine.insideProcess:
if oLine.isProcessBegin and oLine.isSensitivityListEnd:
fSkipProcess = True
if oLine.isSensitivityListEnd and oFile.lines[iLineNumber + 1].isProcessBegin:
fSkipProcess = True
if fSkipProcess:
if oLine.isEndProcess:
fSkipProcess = False
continue # pragma: no cover
if oLine.isProcessBegin:
if fBlanksFound and not fNonBlanksFound:
dViolation = utils.create_violation_dict(iLineNumber)
self.add_violation(dViolation)
fCheckForBlanks = False
fBlanksFound = False
fNonBlanksFound = False
fSkipProcess = True
if fCheckForBlanks:
if oLine.isBlank:
fBlanksFound = True
else:
fNonBlanksFound = True
if oLine.isSensitivityListEnd:
fCheckForBlanks = True
lToi = oFile.get_tokens_bounded_by(token.process_keyword, token.begin_keyword)
for oToi in lToi:
lTokens = oToi.get_tokens()
iLine = oToi.get_line_number() + utils.count_carriage_returns(lTokens)
for iToken, oToken in enumerate(lTokens):
if utils.are_next_consecutive_token_types_ignoring_whitespace([token.process_keyword, token.begin_keyword], iToken, lTokens):
if blank_lines_exist(iToken, lTokens):
oViolation = violation.New(iLine, oToi, self.solution)
self.add_violation(oViolation)
break
if utils.are_next_consecutive_token_types_ignoring_whitespace([token.close_parenthesis, token.begin_keyword], iToken, lTokens):
if blank_lines_exist(iToken, lTokens):
oViolation = violation.New(iLine, oToi, self.solution)
self.add_violation(oViolation)
break
if utils.are_next_consecutive_token_types_ignoring_whitespace([token.is_keyword, token.begin_keyword], iToken, lTokens):
if blank_lines_exist(iToken, lTokens):
oViolation = violation.New(iLine, oToi, self.solution)
self.add_violation(oViolation)
break
def _fix_violations(self, oFile):
for dViolation in self.violations[::-1]:
fix.remove_blank_lines_above(self, oFile, utils.get_violation_line_number(dViolation))
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.reverse()
lNewTokens = []
for iToken, oToken in enumerate(lTokens):
if isinstance(oToken, parser.blank_line):
lNewTokens.pop()
continue
lNewTokens.append(oToken)
lNewTokens.reverse()
oViolation.set_tokens(lNewTokens)
oFile.update(self.violations)
def blank_lines_exist(iToken, lTokens):
for iIndex in range(iToken, len(lTokens)):
if isinstance(lTokens[iIndex], parser.blank_line):
return True
return False

View File

@ -0,0 +1,32 @@
architecture RTL of FIFO is
begin
process
begin
end process;
process (a, b)
begin
end process;
process (a, b) is
begin
end process;
-- Violations below
process
begin
end process;
process (a, b)
begin
end process;
process (a, b) is
begin
end process;
end architecture RTL;

View File

@ -0,0 +1,38 @@
architecture RTL of FIFO is
begin
process
begin
end process;
process (a, b)
begin
end process;
process (a, b) is
begin
end process;
-- Violations below
process
begin
end process;
process (a, b)
begin
end process;
process (a, b) is
begin
end process;
end architecture RTL;

View File

@ -0,0 +1,46 @@
import os
import unittest
from vsg.rules import process
from vsg import vhdlFile
from vsg.tests import utils
sTestDir = os.path.dirname(__file__)
lFile = utils.read_vhdlfile(os.path.join(sTestDir,'rule_021_test_input.vhd'))
lExpected = []
lExpected.append('')
utils.read_file(os.path.join(sTestDir, 'rule_021_test_input.fixed.vhd'), lExpected)
class test_process_rule(unittest.TestCase):
def setUp(self):
self.oFile = vhdlFile.vhdlFile(lFile)
def test_rule_021(self):
oRule = process.rule_021()
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'process')
self.assertEqual(oRule.identifier, '021')
lExpected = [24, 30, 35]
oRule.analyze(self.oFile)
self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations))
def test_fix_rule_021(self):
oRule = process.rule_021()
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, [])

View File

@ -34,15 +34,6 @@ class testRuleProcessMethods(unittest.TestCase):
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_rule_021(self):
oRule = process.rule_021()
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'process')
self.assertEqual(oRule.identifier, '021')
dExpected = utils.add_violation_list([59,70])
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_rule_026(self):
oRule = process.rule_026()
self.assertTrue(oRule)

View File

@ -288,11 +288,7 @@ def find_next_non_whitespace_token(iToken, lObjects):
iEnd = len(lObjects)
for iIndex in range(iToken, len(lObjects)):
oToken = lObjects[iIndex]
if isinstance(oToken, parser.whitespace):
continue
if isinstance(oToken, parser.carriage_return):
continue
if isinstance(oToken, parser.comment):
if token_is_whitespace_or_comment(oToken):
continue
return iIndex
return iCurrent
@ -621,7 +617,20 @@ def remove_leading_whitespace_and_comments(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):
isinstance(oToken, parser.comment) or \
isinstance(oToken, parser.blank_line):
return True
else:
return False
def increment_line_number(iLine, oToken):
if isinstance(oToken, parser.carriage_return):
return iLine + 1
return iLine
def count_carriage_returns(lTokens):
iReturn = 0
for oToken in lTokens:
iReturn = increment_line_number(iReturn, oToken)
return iReturn