Updates for issue #192

Added initial whitespace_012 to detect alignment of identifiers in processes.

  1)  Added test
  2)  Added rule
This commit is contained in:
Jeremiah C Leary 2018-12-15 10:02:43 -06:00
parent 30fe7d85fa
commit aa6e59bcdc
5 changed files with 171 additions and 0 deletions

View File

@ -416,3 +416,52 @@ def get_package_name(oLine):
if sWord.lower() == 'package' and not lLine[iIndex + 1] == '--':
return lLine[iIndex + 1].rstrip(';')
return ''
def identifier_alignment(self, iLineNumber, lGroup):
'''
Checks identifiers in a group of line objects are aligned in the same column.
Parameters:
self: (rule object)
iLineNumber: (integer)
lGroup: (list of line objects)
'''
iIdentfierAlignment = None
iMaximumIdentifierColumn = 0
sViolationRange = str(iLineNumber) + '-' + str(iLineNumber + len(lGroup) - 1)
self.dFix['violations'][sViolationRange] = {}
self.dFix['violations'][sViolationRange]['line'] = {}
for iIndex, oGroupLine in enumerate(lGroup):
if oGroupLine.isSignal or oGroupLine.isConstant or oGroupLine.isVariable or oGroupLine.isFileKeyword:
self.dFix['violations'][sViolationRange]['line'][iLineNumber + iIndex] = {}
iIdentifierAlignment = find_identifier(oGroupLine.line)
self.dFix['violations'][sViolationRange]['line'][iLineNumber + iIndex]['identifierColumn'] = iIdentifierAlignment
if iIdentifierAlignment > iMaximumIdentifierColumn:
iMaximumIdentifierColumn = iIdentifierAlignment
if not iIdentifierAlignment == iMaximumIdentifierColumn:
add_range_violation(self, sViolationRange)
self.dFix['violations'][sViolationRange]['maximumIdentifierColumn'] = iMaximumIdentifierColumn
def find_identifier(sString):
fKeywordFound = False
fSpaceAfterKeywordFound = False
for iIndex, sChar in enumerate(sString):
if not sChar.isspace() and not fKeywordFound:
fKeywordFound = True
if sChar.isspace() and fKeywordFound:
fSpaceAfterKeywordFound = True
if not sChar.isspace() and fSpaceAfterKeywordFound:
return iIndex

View File

@ -10,3 +10,4 @@ from .rule_008 import rule_008
from .rule_009 import rule_009
from .rule_010 import rule_010
from .rule_011 import rule_011
from .rule_012 import rule_012

View File

@ -0,0 +1,56 @@
from vsg import rule
from vsg import check
from vsg import fix
from vsg import line
import re
class rule_012(rule.rule):
'''Whitespace rule 012 checks for alignment of identifiers.'''
def __init__(self):
rule.rule.__init__(self)
self.name = 'whitespace'
self.identifier = '012'
self.phase = 2
self.solution = 'Align the first character of each identifier.'
self.fixable = False
def analyze(self, oFile):
lGroup = []
fGroupFound = False
iStartGroupIndex = None
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isProcessKeyword and not fGroupFound:
fGroupFound = True
iStartGroupIndex = iLineNumber
if fGroupFound:
if oLine.isSignal or oLine.isConstant or oLine.isVariable or oLine.isFileKeyword:
lGroup.append(oLine)
else:
lGroup.append(line.blank_line())
if oLine.isProcessBegin and fGroupFound:
fGroupFound = False
check.identifier_alignment(self, iStartGroupIndex, lGroup)
lGroup = []
iStartGroupIndex = None
#
# def _fix_violations(self, oFile):
# for iLineNumber in self.violations:
# oLine = oFile.lines[iLineNumber]
# iCommentIndex = oLine.line.find('--')
# if iCommentIndex == -1:
# oLine.update_line(re.sub(r'(\w+)([+|\-|/|*])', r'\1 \2', oLine.line))
# oLine.update_line(re.sub(r'\)([+|\-|/|*])', r') \1', oLine.line))
# oLine.update_line(re.sub(r'([+|\-|/|*])(\w+)', r'\1 \2', oLine.line))
# oLine.update_line(re.sub(r'([+|\-|/|*])\(', r'\1 (', oLine.line))
# else:
# sLine = oLine.line[:iCommentIndex]
# sLine = re.sub(r'(\w+)([+|\-|/|*])', r'\1 \2', sLine)
# sLine = re.sub(r'\)([+|\-|/|*])', r') \1', sLine)
# sLine = re.sub(r'([+|\-|/|*])(\w+)', r'\1 \2', sLine)
# sLine = re.sub(r'([+|\-|/|*])\(', r'\1 (', sLine)
# sLine = sLine + oLine.line[iCommentIndex:]
# oLine.update_line(sLine)

View File

@ -0,0 +1,56 @@
architecture ARCH of ENTITY1 is
variable var1 : boolean;
constant con1 : integer;
signal sig1 : std_logic;
file file1 : load_file_file open read_mode is load_file_name;
variable var12 : boolean;
constant con12 : integer;
signal sig12 : std_logic;
file file12 : load_file_file open read_mode is load_file_name;
-- This should not fail
variable var12 : boolean;
constant con12 : integer;
signal sig12 : std_logic;
file file12 : load_file_file open read_mode is load_file_name;
begin
PROC1 : process (A) is
variable var1 : boolean;
constant con1 : integer;
signal sig1 : std_logic;
file file1 : load_file_file open read_mode is load_file_name;
variable var12 : boolean;
constant con12 : integer;
signal sig12 : std_logic;
file file12 : load_file_file open read_mode is load_file_name;
begin
end process PROC1;
-- This should not fail
PROC1 : process (A) is
variable var1 : boolean;
constant con1 : integer;
signal sig1 : std_logic;
file file1 : load_file_file open read_mode is load_file_name;
variable var12 : boolean;
constant con12 : integer;
signal sig12 : std_logic;
file file12 : load_file_file open read_mode is load_file_name;
begin
end process PROC1;
end architecture ARCH;

View File

@ -250,3 +250,12 @@ class testRuleWhitespaceMethods(unittest.TestCase):
oFile.lines.append(line.line(' a <= )+(')) #26
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_012(self):
sFileIdentifier = os.path.join(os.path.dirname(__file__),'identifier_alignment_input.vhd')
oFileIdentifier = vhdlFile.vhdlFile(sFileIdentifier)
oRule = whitespace.rule_012()
oRule.analyze(oFileIdentifier)
lExpected = ['22-34']
self.assertEqual(oRule.violations, lExpected)