Updates for issue #237

Added rule constant_014 to handle multiline non array constants:

  1)  Added documentation
  2)  Fixed rule constant_012 so it does not fire on non array constants
  3)  Added rule constant_014
  4)  Added tests
  5)  Added classification of array constants
This commit is contained in:
Jeremiah C Leary 2019-06-08 11:22:31 -05:00
parent 7c68c55ca5
commit a7d0aa556d
11 changed files with 144 additions and 32 deletions

View File

@ -192,7 +192,7 @@ This rule checks the constant type is lowercase.
constant_012
############
This rule checks the indent of multiline constants.
This rule checks the indent of multiline constants that contain arrays.
**Violation**
@ -279,3 +279,22 @@ This rule checks for consistent capitalization of constant names.
end architecture RTL;
constant_014
############
This rule checks the indent of multiline constants that do not contain arrays.
**Violation**
.. code-block:: vhdl
constant width : integer := a + b +
c + d;
**Fix**
.. code-block:: vhdl
constant width : integer := a + b +
c + d;

View File

@ -63,6 +63,7 @@ class line():
self.insideConstant = False
self.isConstant = False
self.isConstantEnd = False
self.isConstantArray = False
# Variable attributes
self.isVariable = False
# Process attributes

View File

@ -12,3 +12,4 @@ from .rule_010 import rule_010
from .rule_011 import rule_011
from .rule_012 import rule_012
from .rule_013 import rule_013
from .rule_014 import rule_014

View File

@ -8,4 +8,4 @@ class rule_012(indent_rule):
'''
def __init__(self):
indent_rule.__init__(self, 'constant', '012', 'insideConstant', 'isConstant')
indent_rule.__init__(self, 'constant', '012', 'isConstantArray', 'isConstant')

View File

@ -0,0 +1,42 @@
import re
from vsg import rule
from vsg import fix
from vsg import check
class rule_014(rule.rule):
'''
Constant rule 014 checks the indent of multiline constants that are not arrays.
'''
def __init__(self):
rule.rule.__init__(self)
self.name = 'constant'
self.identifier = '014'
self.solution = 'Align with := keyword on constant declaration line.'
self.phase = 5
def _pre_analyze(self):
self.alignmentColumn = 0
self.fKeywordFound = False
def _analyze(self, oFile, oLine, iLineNumber):
if not oLine.isConstantArray and oLine.insideConstant:
if oLine.isConstant and ':=' in oLine.line:
self.alignmentColumn = oLine.line.index(':=') + len(':= ')
self.fKeywordFound = True
elif not oLine.isConstant and self.fKeywordFound:
sMatch = ' ' * self.alignmentColumn
if not re.match('^' + sMatch + '\w', oLine.line):
self.add_violation(iLineNumber)
self.dFix['violations'][iLineNumber] = self.alignmentColumn
if oLine.isConstantEnd:
self.fKeywordFound = False
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
sLine = oFile.lines[iLineNumber].line
sNewLine = ' ' * self.dFix['violations'][iLineNumber] + sLine.strip()
oFile.lines[iLineNumber].update_line(sNewLine)

View File

@ -37,4 +37,10 @@ package PACK is
constant const : std_logic := '0';
constant c_length_constant : integer := I_FOO'length + I_BAR'length +
I_MOREFOO'length + I_MOREBAR'length + 1;
constant c_length_constant : integer := I_FOO'length + I_BAR'length +
I_MOREFOO'length + I_MOREBAR'length + 1;
end package PACK;

View File

@ -5,15 +5,14 @@ from vsg.rules import constant
from vsg import vhdlFile
from vsg.tests import utils
lFile = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'constant_test_input.vhd'))
class testFixRuleConstantMethods(unittest.TestCase):
def setUp(self):
# Read in test file used for all tests
self.lFile = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'constant_test_input.vhd'))
self.oFile = vhdlFile.vhdlFile(self.lFile)
# Process the test file used for all tests
self.oFile = vhdlFile.vhdlFile(lFile)
def test_fix_rule_001(self):
oRule = constant.rule_001()
@ -155,3 +154,14 @@ class testFixRuleConstantMethods(unittest.TestCase):
self.assertEqual(self.oFile.lines[34].line, ' 2,')
self.assertEqual(self.oFile.lines[35].line, ' 3')
self.assertEqual(self.oFile.lines[36].line, ' );')
def test_fix_rule_014(self):
oRule = constant.rule_014()
dExpected = []
oRule.fix(self.oFile)
oRule.analyze(self.oFile)
self.assertEqual(self.oFile.lines[40].line, ' constant c_length_constant : integer := I_FOO\'length + I_BAR\'length +')
self.assertEqual(self.oFile.lines[41].line, ' I_MOREFOO\'length + I_MOREBAR\'length + 1;')
self.assertEqual(self.oFile.lines[43].line, ' constant c_length_constant : integer := I_FOO\'length + I_BAR\'length +')
self.assertEqual(self.oFile.lines[44].line, ' I_MOREFOO\'length + I_MOREBAR\'length + 1;')
self.assertEqual(oRule.violations, dExpected)

View File

@ -17,96 +17,105 @@ class testRuleConstantMethods(unittest.TestCase):
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'constant')
self.assertEqual(oRule.identifier, '001')
dExpected = [8,9,10]
lExpected = [8,9,10]
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
self.assertEqual(oRule.violations, lExpected)
def test_rule_002(self):
oRule = constant.rule_002()
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'constant')
self.assertEqual(oRule.identifier, '002')
dExpected = [7,8]
lExpected = [7,8]
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
self.assertEqual(oRule.violations, lExpected)
def test_rule_003(self):
oRule = constant.rule_003()
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'constant')
self.assertEqual(oRule.identifier, '003')
dExpected = [7]
lExpected = [7]
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
self.assertEqual(oRule.violations, lExpected)
def test_rule_004(self):
oRule = constant.rule_004()
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'constant')
self.assertEqual(oRule.identifier, '004')
dExpected = [8]
lExpected = [8]
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
self.assertEqual(oRule.violations, lExpected)
def test_rule_005(self):
oRule = constant.rule_005()
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'constant')
self.assertEqual(oRule.identifier, '005')
dExpected = [8,9]
lExpected = [8,9]
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
self.assertEqual(oRule.violations, lExpected)
def test_rule_006(self):
oRule = constant.rule_006()
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'constant')
self.assertEqual(oRule.identifier, '006')
dExpected = [10]
lExpected = [10]
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
self.assertEqual(oRule.violations, lExpected)
def test_rule_007(self):
oRule = constant.rule_007()
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'constant')
self.assertEqual(oRule.identifier, '007')
dExpected = [10]
lExpected = [10]
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
self.assertEqual(oRule.violations, lExpected)
def test_rule_009(self):
oRule = constant.rule_009()
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'constant')
self.assertEqual(oRule.identifier, '009')
dExpected = ['3-13']
lExpected = ['3-13']
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
self.assertEqual(oRule.violations, lExpected)
def test_rule_010(self):
oRule = constant.rule_010()
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'constant')
self.assertEqual(oRule.identifier, '010')
dExpected = [9,38]
lExpected = [9,38]
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
self.assertEqual(oRule.violations, lExpected)
def test_rule_011(self):
oRule = constant.rule_011()
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'constant')
self.assertEqual(oRule.identifier, '011')
dExpected = [9]
lExpected = [9]
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
self.assertEqual(oRule.violations, lExpected)
def test_rule_012(self):
oRule = constant.rule_012()
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'constant')
self.assertEqual(oRule.identifier, '012')
dExpected = [11,31,32,34,36]
lExpected = [31,32,34,36]
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
self.assertEqual(oRule.violations, lExpected)
def test_rule_014(self):
oRule = constant.rule_014()
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'constant')
self.assertEqual(oRule.identifier, '014')
lExpected = [44]
oRule.analyze(oFile)
self.assertEqual(oRule.violations, lExpected)

View File

@ -10,7 +10,7 @@ oFileConstant = vhdlFile.vhdlFile(lFileConstant)
class testVhdlFileConstantMethods(unittest.TestCase):
def test_isConstant_assignment(self):
lExpected = [5,6,7,8,9,10,17,18,28,30,38]
lExpected = [5,6,7,8,9,10,17,18,28,30,38,40,43]
# Generic actual list
lActual = []
for iIndex, oLine in enumerate(oFileConstant.lines):
@ -20,7 +20,7 @@ class testVhdlFileConstantMethods(unittest.TestCase):
self.assertEqual(lActual, lExpected)
def test_isConstantEnd_assignment(self):
lExpected = [5,6,7,8,9,11,17,18,28,36,38]
lExpected = [5,6,7,8,9,11,17,18,28,36,38,41,44]
# Generic actual list
lActual = []
for iIndex, oLine in enumerate(oFileConstant.lines):
@ -31,6 +31,8 @@ class testVhdlFileConstantMethods(unittest.TestCase):
def test_insideConstant_assignment(self):
lExpected = [5,6,7,8,9,10,11,17,18,28,30,31,32,33,34,35,36,38]
lExpected.extend(range(40, 42))
lExpected.extend(range(43, 45))
# Generic actual list
lActual = []
for iIndex, oLine in enumerate(oFileConstant.lines):
@ -38,3 +40,13 @@ class testVhdlFileConstantMethods(unittest.TestCase):
lActual.append(iIndex)
# Compare
self.assertEqual(lActual, lExpected)
def test_isConstantArray_assignment(self):
lExpected = []
lExpected.extend(range(30,37))
lActual = []
for iIndex, oLine in enumerate(oFileConstant.lines):
if oLine.isConstantArray:
lActual.append(iIndex)
# Compare
self.assertEqual(lActual, lExpected)

View File

@ -1,7 +1,7 @@
import re
def constant(dVars, oLine):
def constant(dVars, oLine, oLinePrevious):
if re.match('^\s*constant', oLine.lineLower) and \
not oLine.insideFunction and not oLine.insideProcedure:
@ -9,13 +9,24 @@ def constant(dVars, oLine):
oLine.indentLevel = dVars['iCurrentIndentLevel']
oLine.insideConstant = True
dVars['iCurrentIndentLevel'] += 1
if re.match('^.*:=\s*\(', oLine.lineNoComment):
dVars['fConstantArray'] = True
if oLine.insideConstant:
if ';' in oLine.line:
dVars['iCurrentIndentLevel'] -= 1
oLine.indentLevel = dVars['iCurrentIndentLevel']
oLine.isConstantEnd = True
if dVars['fConstantArray']:
oLine.isConstantArray = True
dVars['fConstantArray'] = False
elif not oLine.isConstant:
if re.match('^\s*\(', oLine.line):
oLine.indentLevel = dVars['iCurrentIndentLevel'] - 1
dVars['fConstantArray'] = True
oLinePrevious.isConstantArray = True
else:
oLine.indentLevel = dVars['iCurrentIndentLevel']
if dVars['fConstantArray']:
oLine.isConstantArray = True

View File

@ -43,6 +43,7 @@ class vhdlFile():
dVars['iCurrentIndentLevel'] = 0
dVars['iGenerateLevel'] = 0
dVars['iIfLevel'] = 0
dVars['fConstantArray'] = False
oLinePrevious = line.blank_line()
@ -67,7 +68,7 @@ class vhdlFile():
classify.package(dVars, oLine)
classify.component(dVars, oLine)
classify.signal(dVars, oLine)
classify.constant(dVars, oLine)
classify.constant(dVars, oLine, oLinePrevious)
classify.variable(dVars, oLine)
classify.procedure(dVars, oLine, oLinePrevious)
classify.process(dVars, oLine)