diff --git a/vsg/check.py b/vsg/check.py index 4b670687..0e845ae2 100644 --- a/vsg/check.py +++ b/vsg/check.py @@ -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 + + + diff --git a/vsg/rules/whitespace/__init__.py b/vsg/rules/whitespace/__init__.py index 395595ca..72f26790 100644 --- a/vsg/rules/whitespace/__init__.py +++ b/vsg/rules/whitespace/__init__.py @@ -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 diff --git a/vsg/rules/whitespace/rule_012.py b/vsg/rules/whitespace/rule_012.py new file mode 100644 index 00000000..f37c9a79 --- /dev/null +++ b/vsg/rules/whitespace/rule_012.py @@ -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) diff --git a/vsg/tests/whitespace/identifier_alignment_input.vhd b/vsg/tests/whitespace/identifier_alignment_input.vhd new file mode 100644 index 00000000..b3013dcd --- /dev/null +++ b/vsg/tests/whitespace/identifier_alignment_input.vhd @@ -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; + diff --git a/vsg/tests/whitespace/test_rule_whitespace.py b/vsg/tests/whitespace/test_rule_whitespace.py index 3899f78a..0de4b103 100644 --- a/vsg/tests/whitespace/test_rule_whitespace.py +++ b/vsg/tests/whitespace/test_rule_whitespace.py @@ -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)