diff --git a/modules/line.py b/modules/line.py index 45b9e01d..6aca7507 100644 --- a/modules/line.py +++ b/modules/line.py @@ -40,6 +40,9 @@ class line(): self.isProcessBegin = False self.isProcessKeyword = False self.isEndProcess = False + self.insideSensitivityList = False + self.isSensitivityListBegin = False + self.isSensitivityListEnd = False ## Concurrent attributes self.insideConcurrent = False self.isConcurrentBegin = False diff --git a/modules/rule_process.py b/modules/rule_process.py index 26cdfb4a..d81e39ca 100644 --- a/modules/rule_process.py +++ b/modules/rule_process.py @@ -174,13 +174,155 @@ class rule_011(process_rule): if not oFile.lines[iLineNumber + 1].isBlank: self.add_violation(iLineNumber) + +class rule_012(process_rule): + '''Process rule 012 checks for the existance of the "is" keyword.''' + + def __init__(self): + process_rule.__init__(self) + self.identifier = '012' + self.solution = 'Add "is" keyword after the closing parenthesis.' + + def analyze(self, oFile): + for iLineNumber, oLine in enumerate(oFile.lines): + if oLine.isSensitivityListEnd: + if not re.match('^.*\)\s*is', oLine.lineLower): + self.add_violation(iLineNumber) + + +class rule_013(process_rule): + '''Process rule 013 checks the "is" keyword is lowercase.''' + + def __init__(self): + process_rule.__init__(self) + self.identifier = '013' + self.solution = 'Lowercase "is" keyword.' + + def analyze(self, oFile): + for iLineNumber, oLine in enumerate(oFile.lines): + if oLine.isSensitivityListEnd: + if re.match('^.*\)\s*is', oLine.lineLower): + if not re.match('^.*\)\s*is', oLine.line): + self.add_violation(iLineNumber) + + +class rule_014(process_rule): + '''Process rule 014 checks for a single space between the ) and "is" keyword.''' + + def __init__(self): + process_rule.__init__(self) + self.identifier = '014' + self.solution = 'Ensure only a single space exists between the ) and "is" keyword.' + + def analyze(self, oFile): + for iLineNumber, oLine in enumerate(oFile.lines): + if oLine.isSensitivityListEnd: + if re.match('^.*\)\s*is', oLine.lineLower): + if not re.match('^.*\)\sis', oLine.lineLower): + self.add_violation(iLineNumber) + + +class rule_015(process_rule): + '''Process rule 015 checks for a blank line or a comment line above the "process" keyword.''' + + def __init__(self): + process_rule.__init__(self) + self.identifier = '015' + self.solution = 'Add a space or a comment above the "process" keyword.' + + def analyze(self, oFile): + for iLineNumber, oLine in enumerate(oFile.lines): + if oLine.isProcessKeyword: + if not oFile.lines[iLineNumber - 1].isBlank and not oFile.lines[iLineNumber - 1].isComment: + self.add_violation(iLineNumber) + + +class rule_016(process_rule): + '''Process rule 016 checks a process has a label.''' + + def __init__(self): + process_rule.__init__(self) + self.identifier = '016' + self.solution = 'Add a label for the process.' + + def analyze(self, oFile): + for iLineNumber, oLine in enumerate(oFile.lines): + if oLine.isProcessKeyword: + if not re.match('^\s*\S+\s*:', oLine.line): + self.add_violation(iLineNumber) + + +class rule_017(process_rule): + '''Process rule 017 checks the process label is uppercase.''' + + def __init__(self): + process_rule.__init__(self) + self.identifier = '017' + self.solution = 'Uppercase process label.' + + def analyze(self, oFile): + for iLineNumber, oLine in enumerate(oFile.lines): + if oLine.isProcessKeyword: + if re.match('^\s*\S+\s*:', oLine.line): + lLine = oLine.line.split(':') + if not lLine[0] == lLine[0].upper(): + self.add_violation(iLineNumber) + + +class rule_018(process_rule): + '''Process rule 018 checks the "end process" has a label.''' + + def __init__(self): + process_rule.__init__(self) + self.identifier = '018' + self.solution = 'Add a label for the "end process".' + + def analyze(self, oFile): + for iLineNumber, oLine in enumerate(oFile.lines): + if oLine.isEndProcess: + if not re.match('^\s*\w+\s+\w+\s+\w+', oLine.line): + self.add_violation(iLineNumber) + + +class rule_019(process_rule): + '''Process rule 019 checks the "end process" label is uppercase.''' + + def __init__(self): + process_rule.__init__(self) + self.identifier = '019' + self.solution = 'Uppercase the label.' + + def analyze(self, oFile): + for iLineNumber, oLine in enumerate(oFile.lines): + if oLine.isEndProcess: + if re.match('^\s*\w+\s+\w+\s+\w+', oLine.line): + lLine = oLine.line.split() + if not lLine[2] == lLine[2].upper(): + self.add_violation(iLineNumber) + + +class rule_020(process_rule): + '''Process rule 020 checks the indentation on multiline sensitivity lists.''' + + def __init__(self): + process_rule.__init__(self) + self.identifier = '020' + self.solution = 'Fix indentation of sensitivity list.' + + def analyze(self, oFile): + for iLineNumber, oLine in enumerate(oFile.lines): + if oLine.isSensitivityListBegin and oLine.isSensitivityListEnd: + continue + if oLine.insideSensitivityList: + if oLine.isSensitivityListBegin: + iAlignmentColumn = oLine.line.find('(') + elif oLine.isSensitivityListEnd and re.match('^\s*\)', oLine.line): + if not re.match('^\s{' + str(iAlignmentColumn) + '}\S', oLine.line): + self.add_violation(iLineNumber) + else: + if not re.match('^\s{' + str(iAlignmentColumn + 1) + '}\S', oLine.line): + self.add_violation(iLineNumber) + #TODO: # check for indentation of declarations between "process" and "begin" keywords -# check of "is" keyword -# Empty space or comment on line above "process" keyword -# Processes should be named -# Process names should be capitalized -# "end process" line should have Process name -# Process name in "end process" line should be capitalized -# Single space between ( and "is" keyword diff --git a/modules/vhdlFile.py b/modules/vhdlFile.py index 6e06de96..52614be5 100644 --- a/modules/vhdlFile.py +++ b/modules/vhdlFile.py @@ -16,6 +16,8 @@ class vhdlFile(): fFoundArchitectureBegin = False fInsideProcess = False fInsideConcurrent = False + fInsideSensitivityList = False + iOpenParenthesis = 0; iCloseParenthesis = 0; with open (filename) as oFile: @@ -24,6 +26,9 @@ class vhdlFile(): # Check for blank lines if re.match('^\s*$', oLine.line): oLine.isBlank = True + # Check for comment lines + if re.match('^\s*--', oLine.line): + oLine.isComment = True # Check for library lines if re.match('^\s*library', oLine.lineLower): oLine.isLibrary = True @@ -126,6 +131,19 @@ class vhdlFile(): oLine.indentLevel = 1 if fInsideProcess == True: oLine.insideProcess = True + # Check sensitivity list + if '(' in oLine.line and not fInsideSensitivityList: + fInsideSensitivityList = True + oLine.isSensitivityListBegin = True + if fInsideSensitivityList: + oLine.insideSensitivityList = True + iOpenParenthesis += oLine.line.count('(') + iCloseParenthesis += oLine.line.count(')') + if iOpenParenthesis == iCloseParenthesis: + fInsideSensitivityList = False + oLine.isSensitivityListEnd = True + iOpenParenthesis = 0 + iCloseParenthesis = 0 if re.match('^.*\s+begin', oLine.lineLower) or re.match('^\s*begin', oLine.lineLower): oLine.indentLevel = 1 oLine.isProcessBegin = True @@ -134,6 +152,7 @@ class vhdlFile(): oLine.indentLevel = 1 oLine.isEndProcess = True + # Check concurrent declarations if fInsideArchitecture and not fInsideProcess: if re.match('^\s*\w+\s*<=', oLine.line) or re.match('^\s*\w+\s*:\s*\w+\s*<=', oLine.line): diff --git a/tests/line/test_line.py b/tests/line/test_line.py index 7db29d9d..5b14a386 100644 --- a/tests/line/test_line.py +++ b/tests/line/test_line.py @@ -177,6 +177,24 @@ class testLineMethods(unittest.TestCase): oLine.isEndProcess = True self.assertEqual(oLine.isEndProcess, True) + def test_insideSensitivityList_attribute(self): + oLine = line.line('contents of Line') + self.assertEqual(oLine.insideSensitivityList, False) + oLine.insideSensitivityList = True + self.assertEqual(oLine.insideSensitivityList, True) + + def test_isSensitivityListBegin_attribute(self): + oLine = line.line('contents of Line') + self.assertEqual(oLine.isSensitivityListBegin, False) + oLine.isSensitivityListBegin = True + self.assertEqual(oLine.isSensitivityListBegin, True) + + def test_isSensitivityListEnd_attribute(self): + oLine = line.line('contents of Line') + self.assertEqual(oLine.isSensitivityListEnd, False) + oLine.isSensitivityListEnd = True + self.assertEqual(oLine.isSensitivityListEnd, True) + def test_insideConcurrent_attribute(self): oLine = line.line('contents of Line') self.assertEqual(oLine.insideConcurrent, False) diff --git a/tests/rule_process/process_test_input.vhd b/tests/rule_process/process_test_input.vhd index ae4fa189..7ecc3a46 100644 --- a/tests/rule_process/process_test_input.vhd +++ b/tests/rule_process/process_test_input.vhd @@ -3,40 +3,40 @@ architecture ARCH of ENTITY is begin - process (one, two, three) is begin + process (one, two, three) IS begin end process; process (one, two, - three) is + three) is begin eNd process; prOCess (one, - two, - three) is + two, + three) is begIN end proCEss; Process (one, - two, - three - ) is + two, + three + ) iS beGIn end process; proCEss (one, two, three - ) is + ) Is -- This is a comment begin End process; process (one, two, three - ) is + )is begin end Process; @@ -47,5 +47,22 @@ proc_name : process (one, two, three) is begin end process proc_name; +-- Checking for missing "is" keyword + process (one, two, three) + begin + end process; + + process (one, + two, + three) + + begin + end process; + + a<=b; + PROC_NAME:process (one) is + begin + end process PROC_NAME; + end architecture ARCH; diff --git a/tests/rule_process/test_rule_process.py b/tests/rule_process/test_rule_process.py index f1d20d49..186a3906 100644 --- a/tests/rule_process/test_rule_process.py +++ b/tests/rule_process/test_rule_process.py @@ -107,5 +107,87 @@ class testRuleConcurrentMethods(unittest.TestCase): oRule.analyze(oFile) self.assertEqual(oRule.violations, dExpected) + def test_rule_012(self): + oRule = rule_process.rule_012() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'process') + self.assertEqual(oRule.identifier, '012') + dExpected = [51,57] + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_rule_013(self): + oRule = rule_process.rule_013() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'process') + self.assertEqual(oRule.identifier, '013') + dExpected = [6,27,33] + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_rule_014(self): + oRule = rule_process.rule_014() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'process') + self.assertEqual(oRule.identifier, '014') + dExpected = [19,33,39] + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_rule_015(self): + oRule = rule_process.rule_015() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'process') + self.assertEqual(oRule.identifier, '015') + dExpected = [63] + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_rule_016(self): + oRule = rule_process.rule_016() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'process') + self.assertEqual(oRule.identifier, '016') + dExpected = [6,11,17,24,32,38,51,55] + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_rule_017(self): + oRule = rule_process.rule_017() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'process') + self.assertEqual(oRule.identifier, '017') + dExpected = [46] + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_rule_018(self): + oRule = rule_process.rule_018() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'process') + self.assertEqual(oRule.identifier, '018') + dExpected = [9,15,22,30,36,42,53,60] + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_rule_019(self): + oRule = rule_process.rule_019() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'process') + self.assertEqual(oRule.identifier, '019') + dExpected = [48] + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_rule_020(self): + oRule = rule_process.rule_020() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'process') + self.assertEqual(oRule.identifier, '020') + dExpected = [19,25,26,27,33] + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + if __name__ == '__main__': unittest.main() diff --git a/tests/vhdlFile/test_vhdlFile.py b/tests/vhdlFile/test_vhdlFile.py index 0a8d8bdb..8ff33364 100644 --- a/tests/vhdlFile/test_vhdlFile.py +++ b/tests/vhdlFile/test_vhdlFile.py @@ -34,6 +34,17 @@ class testVhdlFileMethods(unittest.TestCase): else: self.assertFalse(oLine.isBlank) + def test_comment_assignment(self): + oFile = vhdlFile.vhdlFile('tests/rule_process/process_test_input.vhd') + lExpected = [50] + # Generic actual list + lActual = [] + for iIndex, oLine in enumerate(oFile.lines): + if oLine.isComment: + lActual.append(iIndex) + # Compare + self.assertEqual(lActual, lExpected) + def test_library_assignment(self): oFile = vhdlFile.vhdlFile('tests/rule_library/library_test_input.vhd') lExpected = [3,7,9,13,20,21] @@ -246,7 +257,7 @@ class testVhdlFileMethods(unittest.TestCase): def test_insideProcess_assignment(self): oFile = vhdlFile.vhdlFile('tests/rule_process/process_test_input.vhd') - lExpected = [6,7,8,9,11,12,13,14,15,17,18,19,20,21,22,24,25,26,27,28,29,30,32,33,34,35,36,38,39,40,41,42,46,47,48] + lExpected = [6,7,8,9,11,12,13,14,15,17,18,19,20,21,22,24,25,26,27,28,29,30,32,33,34,35,36,38,39,40,41,42,46,47,48,51,52,53,55,56,57,58,59,60,63,64,65] # Generic actual list lActual = [] for iIndex, oLine in enumerate(oFile.lines): @@ -257,7 +268,7 @@ class testVhdlFileMethods(unittest.TestCase): def test_isProcessBegin_assignment(self): oFile = vhdlFile.vhdlFile('tests/rule_process/process_test_input.vhd') - lExpected = [6,13,20,28,34,40,47] + lExpected = [6,13,20,28,34,40,47,52,59,64] # Generic actual list lActual = [] for iIndex, oLine in enumerate(oFile.lines): @@ -268,7 +279,7 @@ class testVhdlFileMethods(unittest.TestCase): def test_isProcessKeyword_assignment(self): oFile = vhdlFile.vhdlFile('tests/rule_process/process_test_input.vhd') - lExpected = [6,11,17,24,32,38,46] + lExpected = [6,11,17,24,32,38,46,51,55,63] # Generic actual list lActual = [] for iIndex, oLine in enumerate(oFile.lines): @@ -279,7 +290,7 @@ class testVhdlFileMethods(unittest.TestCase): def test_isEndProcess_assignment(self): oFile = vhdlFile.vhdlFile('tests/rule_process/process_test_input.vhd') - lExpected = [9,15,22,30,36,42,48] + lExpected = [9,15,22,30,36,42,48,53,60,65] # Generic actual list lActual = [] for iIndex, oLine in enumerate(oFile.lines): @@ -288,6 +299,41 @@ class testVhdlFileMethods(unittest.TestCase): # Compare self.assertEqual(lActual, lExpected) + def test_insideSensitivityList_assignment(self): + oFile = vhdlFile.vhdlFile('tests/rule_process/process_test_input.vhd') + lExpected = [6,11,12,17,18,19,24,25,26,27,32,33,38,39,46,51,55,56,57,63] + # Generic actual list + lActual = [] + for iIndex, oLine in enumerate(oFile.lines): + if oLine.insideSensitivityList: + lActual.append(iIndex) + # Compare + self.assertEqual(lActual, lExpected) + + def test_isSensitivityListBegin_assignment(self): + oFile = vhdlFile.vhdlFile('tests/rule_process/process_test_input.vhd') + lExpected = [6,11,17,24,32,38,46,51,55,63] + # Generic actual list + lActual = [] + for iIndex, oLine in enumerate(oFile.lines): + if oLine.isSensitivityListBegin: + lActual.append(iIndex) + # Compare + self.assertEqual(lActual, lExpected) + + def test_isSensitivityListEnd_assignment(self): + oFile = vhdlFile.vhdlFile('tests/rule_process/process_test_input.vhd') + lExpected = [6,12,19,27,33,39,46,51,57,63] + # Generic actual list + lActual = [] + for iIndex, oLine in enumerate(oFile.lines): + if oLine.isSensitivityListEnd: + lActual.append(iIndex) + # Compare + self.assertEqual(lActual, lExpected) + + + def test_insideConcurrent_assignment(self): oFile = vhdlFile.vhdlFile('tests/rule_concurrent/concurrent_test_input.vhd') lExpected = [6,7,8,9,10,11,23,24,26,27,28,29,30,32,33,34,35,36]