vhdl-style-guide/vsg/rules/entity.py

429 lines
14 KiB
Python

from vsg import rule
from vsg import line
import re
class entity_rule(rule.rule):
def __init__(self):
rule.rule.__init__(self)
self.name = 'entity'
class rule_001(entity_rule):
'''
Entity rule 001 checks for spaces before the entity keyword.
'''
def __init__(self):
entity_rule.__init__(self)
self.identifier = '001'
self.solution = 'Ensure proper indentation.'
self.phase = 4
def analyze(self, oFile):
lFailureLines = []
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isEntityDeclaration:
self._check_indent(oLine, iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
self._fix_indent(oFile.lines[iLineNumber])
class rule_002(entity_rule):
'''
Entity rule 002 checks for a single space after the entity keyword.
'''
def __init__(self):
entity_rule.__init__(self)
self.identifier = '002'
self.solution = 'Remove extra spaces after entity keyword.'
self.phase = 2
def analyze(self, oFile):
lFailureLines = []
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isEntityDeclaration:
if re.match('^\s*\S+\s\s+', oLine.line):
self.add_violation(iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
oLine = oFile.lines[iLineNumber]
oLine.line = re.sub(r'^(\s*entity)\s+', r'\1 ', oLine.line, flags=re.IGNORECASE)
oLine.lineLower = oLine.line.lower()
class rule_003(entity_rule):
'''
Entity rule 003 checks for a blank line above the entity keyword.
'''
def __init__(self):
entity_rule.__init__(self)
self.identifier = '003'
self.solution = 'Add blank line above entity keyword.'
self.phase = 3
def analyze(self, oFile):
lFailureLines = []
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isEntityDeclaration:
self._is_blank_line_before(oFile, iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations[::-1]:
oFile.lines.insert(iLineNumber, line.blank_line())
class rule_004(entity_rule):
'''
Entity rule 004 checks the entity keyword is lower case.
'''
def __init__(self):
entity_rule.__init__(self)
self.identifier = '004'
self.solution = 'Change "entity" keyword to lowercase.'
self.phase = 6
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isEntityDeclaration:
self._is_lowercase(oLine.line.split()[0], iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
self._lower_case(oFile.lines[iLineNumber], 'entity')
class rule_005(entity_rule):
'''
Entity rule 005 checks the "is" keyword is on the same line as the entity keyword.
'''
def __init__(self):
entity_rule.__init__(self)
self.identifier = '005'
self.solution = 'Add "is" keyword to same line as "entity" keyword.'
self.phase = 1
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isEntityDeclaration:
if not re.match('^.*\s\s*is', oLine.lineLower):
self.add_violation(iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
oLine = oFile.lines[iLineNumber]
oLine.line = re.sub(r'^(\s*entity\s+\w+)', r'\1 is', oLine.line, re.IGNORECASE)
oLine.lineLower = oLine.line.lower()
### Search for "is" on the next line
iSearchIndex = iLineNumber
while True:
iSearchIndex += 1
oLine = oFile.lines[iSearchIndex]
if re.match('^\s*is', oLine.line, re.IGNORECASE):
oLine.line = re.sub(r'^(\s*)is', r'\1 ', oLine.line)
oLine.lineLower = oLine.line.lower()
if re.match('^\s*$', oLine.line):
oLine.line = ''
oLine.lineLower = ''
oLine.isBlank = True
if oFile.lines[iSearchIndex].isGenericKeyword or oFile.lines[iSearchIndex].isPortKeyword:
break
class rule_006(entity_rule):
'''
Entity rule 006 checks the "is" keyword is lower case.
'''
def __init__(self):
entity_rule.__init__(self)
self.identifier = '006'
self.solution = 'Change "is" keyword to lowercase.'
self.phase = 6
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isEntityDeclaration:
lLine = oLine.line.split()
if len(lLine) > 2:
if not re.match('^\s*\S+\s+\S+\s\s*is', oLine.line):
self.add_violation(iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
self._lower_case(oFile.lines[iLineNumber], 'is')
class rule_007(entity_rule):
'''
Entity rule 007 checks for a single space before the "is" keyword.
'''
def __init__(self):
entity_rule.__init__(self)
self.identifier = '007'
self.solution = 'Remove extra spaces before "is" keyword.'
self.phase = 2
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isEntityDeclaration:
if re.match('^.*\s\s+is', oLine.lineLower):
self.add_violation(iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
oLine = oFile.lines[iLineNumber]
oLine.line = re.sub(r'\s+(is)', r' \1', oLine.line, flags=re.IGNORECASE)
oLine.lineLower = oLine.line.lower()
class rule_008(entity_rule):
'''
Entity rule 008 checks the entity name is uppercase in the entity declaration line.
'''
def __init__(self):
entity_rule.__init__(self)
self.identifier = '008'
self.solution = 'Change entity name to all uppercase.'
self.phase = 6
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isEntityDeclaration:
self._is_uppercase(oLine.line.split()[1], iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
self._upper_case(oFile.lines[iLineNumber], oFile.lines[iLineNumber].line.split()[1])
class rule_009(entity_rule):
'''
Entity rule 009 checks for spaces before the "end" keyword.
'''
def __init__(self):
entity_rule.__init__(self)
self.identifier = '009'
self.solution = 'Ensure proper indentation.'
self.phase = 4
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isEndEntityDeclaration:
self._check_indent(oLine, iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
self._fix_indent(oFile.lines[iLineNumber])
class rule_010(entity_rule):
'''
Entity rule 010 checks the "end" keyword is lowercase.
'''
def __init__(self):
entity_rule.__init__(self)
self.identifier = '010'
self.solution = 'Change "end" keyword to lowercase.'
self.phase = 6
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isEndEntityDeclaration:
self._is_lowercase(oLine.line.split()[0], iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
self._lower_case(oFile.lines[iLineNumber], 'end')
class rule_011(entity_rule):
'''
Entity rule 011 checks for a single space after the "end" keyword
'''
def __init__(self):
entity_rule.__init__(self)
self.identifier = '011'
self.solution = 'Reduce spaces after "end" keyword to one.'
self.phase = 2
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isEndEntityDeclaration:
if re.match('^\s*\S+\s\s+\S', oLine.line):
self.add_violation(iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
oLine = oFile.lines[iLineNumber]
oLine.line = re.sub(r'^(\s*end)\s+', r'\1 ', oLine.line, flags=re.IGNORECASE)
oLine.lineLower = oLine.line.lower()
class rule_012(entity_rule):
'''
Entity rule 012 checks entity name is uppercase in "end" keyword line.
'''
def __init__(self):
entity_rule.__init__(self)
self.identifier = '012'
self.solution = 'Uppercase entity name.'
self.phase = 6
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isEndEntityDeclaration:
lLine = oLine.line.split()
if len(lLine) > 2:
self._is_uppercase(lLine[2], iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
self._upper_case(oFile.lines[iLineNumber], oFile.lines[iLineNumber].line.split()[2])
class rule_013(entity_rule):
'''
Entity rule 013 checks for a single space after the "entity" keyword in the closing of the entity.
'''
def __init__(self):
entity_rule.__init__(self)
self.identifier = '013'
self.solution = 'Reduce spaces after "entity" keyword to one.'
self.phase = 2
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isEndEntityDeclaration:
if re.match('^\s*\S+\s\s*\S+\s\s+', oLine.line):
self.add_violation(iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
oLine = oFile.lines[iLineNumber]
oLine.line = re.sub(r'^(\s*end\s+entity)\s+', r'\1 ', oLine.line, flags=re.IGNORECASE)
oLine.lineLower = oLine.line.lower()
class rule_014(entity_rule):
'''
Entity rule 014 checks the "entity" keyword is lower case in the closing of the entity.
'''
def __init__(self):
entity_rule.__init__(self)
self.identifier = '014'
self.solution = 'Change "entity" keyword to lower case.'
self.phase = 6
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isEndEntityDeclaration:
lLine = oLine.line.split()
if len(lLine) >= 3:
self._is_lowercase(lLine[1], iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
self._lower_case(oFile.lines[iLineNumber], 'entity')
class rule_015(entity_rule):
'''
Entity rule 015 checks the "end" keyword, "entity" keyword, and entity name are on the same line.
'''
def __init__(self):
entity_rule.__init__(self)
self.identifier = '015'
self.solution = 'The "end" keyword, "entity" keyword and entity name need to be on the same line.'
self.phase = 1
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isEndEntityDeclaration:
lLine = oLine.line.split()
if not len(lLine) >= 3:
if not (lLine[0] == 'end' and lLine[1] == 'entity' and not lLine[2].startswith('--')):
self.add_violation(iLineNumber)
def _fix_violations(self, oFile):
self.analyze(oFile)
for iLineNumber in self.violations:
oLine = oFile.lines[iLineNumber]
oLine.line = re.sub(r'^(\s*end)', r'\1 entity', oLine.line, flags=re.IGNORECASE)
oLine.lineLower = oLine.line.lower()
self._clear_violations()
class rule_016(entity_rule):
'''
Entity rule 016 checks for a blank line above the "end entity" keywords.
'''
def __init__(self):
entity_rule.__init__(self)
self.identifier = '016'
self.solution = 'Remove blank line(s) above "end entity" keywords.'
self.phase = 3
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isEndEntityDeclaration:
self._is_no_blank_line_before(oFile, iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations[::-1]:
self._remove_blank_lines_above(oFile, iLineNumber)
class rule_017(entity_rule):
'''
Entity rule 017 ensures the alignment of the : operator for every port in the entity.
'''
def __init__(self):
entity_rule.__init__(self)
self.identifier = '017'
self.solution = 'Inconsistent alignment of ":" in port declaration of entity.'
self.phase = 5
def analyze(self, oFile):
lGroup = []
fGroupFound = False
iStartGroupIndex = None
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isPortKeyword and not fGroupFound and oLine.insideEntity:
fGroupFound = True
iStartGroupIndex = iLineNumber
if oLine.isEndPortMap:
lGroup.append(oLine)
fGroupFound = False
self._check_keyword_alignment(iStartGroupIndex, ':', lGroup)
lGroup = []
iStartGroupIndex = None
if fGroupFound:
if oLine.isPortDeclaration:
lGroup.append(oLine)
else:
lGroup.append(line.line('Removed line'))
def _fix_violations(self, oFile):
self._fix_keyword_alignment(oFile)