Refactoring rule if_009 for new parser.

This commit is contained in:
Jeremiah Leary 2020-11-01 11:35:03 -06:00
parent 065ec579c6
commit cf9a338ce7
7 changed files with 133 additions and 267 deletions

View File

@ -1,47 +1,17 @@
from vsg import rule
from vsg import check
from vsg import fix
from vsg.rules import multiline_alignment_between_tokens
import re
from vsg import token
lTokenPairs = []
lTokenPairs.append([token.if_statement.if_keyword, token.if_statement.then_keyword])
lTokenPairs.append([token.if_statement.elsif_keyword, token.if_statement.then_keyword])
class rule_009(rule.rule):
class rule_009(multiline_alignment_between_tokens):
'''
If rule 009 checks the alignment of multiline boolean expressions.
Checks the alignment of multiline if signal assignments.
'''
def __init__(self):
rule.rule.__init__(self)
self.name = 'if'
self.identifier = '009'
self.solution = 'Align with space after ( in first line of boolean expression.'
self.phase = 5
def analyze(self, oFile):
fCheckAlignment = False
for iLineNumber, oLine in enumerate(oFile.lines):
if self._is_vsg_off(oLine):
continue
if oLine.isIfKeyword and oLine.isThenKeyword:
continue
if oLine.isElseIfKeyword and oLine.isThenKeyword:
continue
if oLine.isIfKeyword:
if re.match('^\s+if\s\(', oLine.lineLower):
iAlignmentColumn = oLine.lineLower.find('(')
fCheckAlignment = True
continue # pragma: no cover
if oLine.isElseIfKeyword:
if re.match('^\s+elsif\s\(', oLine.lineLower):
iAlignmentColumn = oLine.lineLower.find('(')
fCheckAlignment = True
continue # pragma: no cover
if oLine.insideIf and fCheckAlignment:
check.multiline_alignment(self, iAlignmentColumn + 1, oLine, iLineNumber)
if oLine.isThenKeyword:
fCheckAlignment = False
def _fix_violations(self, oFile):
for iLineNumber in self.dFix['violations']:
fix.multiline_alignment(self, oFile, iLineNumber)
multiline_alignment_between_tokens.__init__(self, 'if', '009', lTokenPairs)

View File

@ -1,137 +0,0 @@
architecture ARCH of ENTITY is
begin
PROC_NAME : process (a, b, c) is
begin
if a = 1 then
b <= d;
c <= x;
end if;
IF a = 1 or d = 20 or -- this if should not be replaced
g = 34 or x = 3000 THEN
b <= e;
c <= y;
END if;
if (a = 1 or d = 20 or
g = 34 or x = 3000 or
c = 34) then -- else <-- this should not be classified as an else keyword
b <= e;
c <= y;
ELSIF z = 45 and f = 45 then
b <= g;
c <= o;
end IF;
if (a = 1 or d = 20 or
g = 34 or x = 3000 or
c = 34)then
if b = 1 then
b <= e;
c <= y;
end if;
b <= e;
c <= y;
end if;
if a = 1 or d = 20 or
g = 34 or x = 3000 or
c = 34 then
b <= e;
c <= y;
if b = 1 then
b <= e;
c <= y;
end if;
end if;
if a = 1 or d = 20 or
g = 34 or x = 3000 or
c = 34 then
b <= e;
c <= y;
if(b = 1)then
b <= e;
c <= y;
end if;
b <= e;
end if;
-- Test blank line requirements
if (a = 1 or d = 20 or
g = 34 or x = 3000 or
c = 34) then
b <= e;
c <= y;
elsif (z = 45 and f = 45) then
b <= g;
c <= o;
end if;
if (a = 1) then
b <= d;
c <= x;
else
g <= z;
end if;
if (a = 1) then
b <= d;
c <= x;
ELSE
g <= z;
end if;
if (a = 2) then b <= d;
elsif (b = 3) then c <= e;
elsif (c = 4) then -- Not an error
c <= f;
else g <= x;
end if;
if (a = 2) then b <= '1'; else b <= '0'; end if;
if (a = 2) then
a <= '0'; elsif
(b = 3) then b <= '1'; else
c <= '1'; end if;
if (a = 2) then
a <= '0';
elsif (a = 3 or
b = 4) then
a <= '1';
end if;
end process PROC_NAME;
-- checking for whens under and if statment
PROC_NAME : process () is
begin
if (a = '1') then
d <= sig1 when b = '1'
else sig2 when c = '0'
else sig3 when d = '1'
else sig4;
else
d <= '0';
end if;
end process;
end architecture ARCH;

View File

@ -0,0 +1,39 @@
architecture RTL of FIFO is
begin
process
begin
if (a = '1' or b = '0' and
c = '1' xor d = '1' and
g = x) then
b <= '0';
elsif (a = '1' or b = '0' and
c = '1' xor d = '1' and
g = x) then
b <= '1';
else
b <= '1';
end if;
-- Violations below
if (a = '1' or b = '0' and
c = '1' xor d = '1' and
g = x) then
b <= '0';
elsif (a = '1' or b = '0' and
c = '1' xor d = '1' and
g = x) then
b <= '1';
else
b <= '1';
end if;
end process;
end architecture RTL;

View File

@ -0,0 +1,39 @@
architecture RTL of FIFO is
begin
process
begin
if (a = '1' or b = '0' and
c = '1' xor d = '1' and
g = x) then
b <= '0';
elsif (a = '1' or b = '0' and
c = '1' xor d = '1' and
g = x) then
b <= '1';
else
b <= '1';
end if;
-- Violations below
if (a = '1' or b = '0' and
c = '1' xor d = '1' and
g = x) then
b <= '0';
elsif (a = '1' or b = '0' and
c = '1' xor d = '1' and
g = x) then
b <= '1';
else
b <= '1';
end if;
end process;
end architecture RTL;

View File

@ -1,65 +0,0 @@
import os
import unittest
from vsg.rules import if_statement
from vsg import vhdlFile
from vsg import rule_list
from vsg import severity
from vsg.tests import utils
# Read in test file used for all tests
lFile = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'..','if_statement','if_test_input.vhd'))
oFile = vhdlFile.vhdlFile(lFile)
lFileCase = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'..','if_statement','if_case_test_input.vhd'))
oFileCase = vhdlFile.vhdlFile(lFileCase)
lFileCompress = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'if_compressed_line_test_input.vhd'))
oFileCompress = vhdlFile.vhdlFile(lFileCompress)
lFileNested = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'if_nested_test_input.vhd'))
oFileNested = vhdlFile.vhdlFile(lFileNested)
lFileIf = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'comment_if_input.vhd'))
oFileIf = vhdlFile.vhdlFile(lFileIf)
oSeverityList = severity.create_list({})
class testFixRuleIfMethods(unittest.TestCase):
@unittest.skip('This test will eventually be re-written for the new parser.')
def test_fix_rule_002(self):
oRule = if_statement.rule_002()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oFile.lines[13].line, ' if ( a = 1 or d = 20 or -- this if should not be replaced')
self.assertEqual(oFile.lines[14].line, ' g = 34 or x = 3000 ) then')
self.assertEqual(oFile.lines[24].line, ' elsif ( z = 45 and f = 45 ) then')
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_003(self):
oRule = if_statement.rule_003()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_004(self):
oRule = if_statement.rule_004()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_005(self):
oRule = if_statement.rule_005()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_009(self):
oRule = if_statement.rule_009()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)

View File

@ -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_009_test_input.vhd'))
lExpected = []
lExpected.append('')
utils.read_file(os.path.join(sTestDir, 'rule_009_test_input.fixed.vhd'), lExpected)
class test_if_statement_rule(unittest.TestCase):
def setUp(self):
self.oFile = vhdlFile.vhdlFile(lFile)
def test_rule_009(self):
oRule = if_statement.rule_009()
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'if')
self.assertEqual(oRule.identifier, '009')
lExpected = [25, 26, 29, 30]
oRule.analyze(self.oFile)
self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations))
def test_fix_rule_009(self):
oRule = if_statement.rule_009()
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

@ -1,26 +0,0 @@
import os
import unittest
from vsg.rules import if_statement
from vsg import vhdlFile
from vsg.tests import utils
# Read in test file used for all tests
lFile = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'..','if_statement','if_test_input.vhd'))
oFile = vhdlFile.vhdlFile(lFile)
lFileCase = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'..','if_statement','if_case_test_input.vhd'))
oFileCase = vhdlFile.vhdlFile(lFileCase)
lFileIf = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'comment_if_input.vhd'))
oFileIf = vhdlFile.vhdlFile(lFileIf)
class testRuleIfMethods(unittest.TestCase):
def test_rule_009(self):
oRule = if_statement.rule_009()
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'if')
self.assertEqual(oRule.identifier, '009')
dExpected = utils.add_violation_list([20,21,67,68,115])
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)