From 43e708cf168a10fb3700a4aef81af00fbdf7ef49 Mon Sep 17 00:00:00 2001 From: Jeremiah C Leary Date: Sun, 9 Jun 2019 21:08:47 -0500 Subject: [PATCH] Updates for issue #195: 1) Added classification of clock processes 2) Added classification for after keyword 3) Added tests to validate classifications --- vsg/line.py | 3 + vsg/tests/after/after_test_input.vhd | 77 +++++++++++++++++++++++ vsg/tests/after/test_rule_after.py | 24 +++++++ vsg/tests/vhdlFile/test_vhdlFile_after.py | 40 ++++++++++++ vsg/vhdlFile/classify/__init__.py | 1 + vsg/vhdlFile/classify/after.py | 8 +++ vsg/vhdlFile/classify/process.py | 9 +++ vsg/vhdlFile/update.py | 2 + vsg/vhdlFile/vhdlFile.py | 1 + 9 files changed, 165 insertions(+) create mode 100644 vsg/tests/after/after_test_input.vhd create mode 100644 vsg/tests/after/test_rule_after.py create mode 100644 vsg/tests/vhdlFile/test_vhdlFile_after.py create mode 100644 vsg/vhdlFile/classify/after.py diff --git a/vsg/line.py b/vsg/line.py index ebffe33f..82a17cdf 100644 --- a/vsg/line.py +++ b/vsg/line.py @@ -76,6 +76,7 @@ class line(): self.insideSensitivityList = False self.isSensitivityListBegin = False self.isSensitivityListEnd = False + self.insideClockProcess = False # Concurrent attributes self.insideConcurrent = False self.isConcurrentBegin = False @@ -219,6 +220,8 @@ class line(): # Code tags self.hasCodeTag = False self.codeTags = {} + # After attributes + self.hasAfterKeyword = False def update_line(self, sLine): ''' diff --git a/vsg/tests/after/after_test_input.vhd b/vsg/tests/after/after_test_input.vhd new file mode 100644 index 00000000..39d52c64 --- /dev/null +++ b/vsg/tests/after/after_test_input.vhd @@ -0,0 +1,77 @@ + +architecture ARCH of ENTITY is + +begin + + CLK_PROC : process (reset, clk) is + begin + + if (reset = '1') then + a <= '0'; + b <= '1'; + c <= '0'; + d <= '1'; + elsif (clk'event and clk = '1') then + a <= b after 1 ns; + b <= c after 1 ns; + c <= d after 1 ns; + d <= e after 1 ns; + end if; + end process CLK_PROC; + + -- This process checks for missing after statements + CLK_PROC : process (reset, clk) is + begin + + if (reset = '1') then + a <= '0'; + b <= '1'; + c <= '0'; + d <= '1'; + elsif (clk'event and clk = '1') then + a <= b after 1 ns; + b <= c; + c <= d; + d <= e after 1 ns; + end if; + end process CLK_PROC; + + -- This process checks for incorrect times + CLK_PROC : process (reset, clk) is + begin + + if (reset = '1') then + a <= '0'; + b <= '1'; + c <= '0'; + d <= '1'; + elsif (rising_edge(clk)) then + a <= b after 1 s; + b <= c after 1 ms; + c <= d after 200 ns; + d <= e after 10 ns; + end if; + end process CLK_PROC; + + -- This process checks for unalined after statements + CLK_PROC : process (reset, clk) is + begin + + if (reset = '1') then + a <= '0'; + b <= '1'; + c <= '0'; + d <= '1'; + elsif (falling_edge(clk)) then + a <= b after 1 ns; + b <= c after 1 ns; + c <= d after 1 ns; + d <= e after 1 ns; + end if; + end process CLK_PROC; + + -- This checks detection of after outside clock processes + a <= b after 10 ns; + +end architecture ARCH; + diff --git a/vsg/tests/after/test_rule_after.py b/vsg/tests/after/test_rule_after.py new file mode 100644 index 00000000..6c27d5c6 --- /dev/null +++ b/vsg/tests/after/test_rule_after.py @@ -0,0 +1,24 @@ +import os + +import unittest +import sys +sys.path.append('vsg') + +from vsg.rules import after +from vsg import vhdlFile +from vsg.tests import utils + +# Read in test file used for all tests +lFile = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'after_test_input.vhd')) +oFile = vhdlFile.vhdlFile(lFile) + +class testRuleAfterMethods(unittest.TestCase): + + def test_rule_001(self): + oRule = signal.rule_001() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'after') + self.assertEqual(oRule.identifier, '001') + lExpected = [32, 33] + oRule.analyze(oFile) + self.assertEqual(oRule.violations, lExpected) diff --git a/vsg/tests/vhdlFile/test_vhdlFile_after.py b/vsg/tests/vhdlFile/test_vhdlFile_after.py new file mode 100644 index 00000000..6ef5284c --- /dev/null +++ b/vsg/tests/vhdlFile/test_vhdlFile_after.py @@ -0,0 +1,40 @@ +import os + +import unittest +from vsg import vhdlFile +from vsg.tests import utils + +lFile = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'..','after','after_test_input.vhd')) +oFile = vhdlFile.vhdlFile(lFile) + +class testVhdlFileAfterMethods(unittest.TestCase): + + def test_after_detection(self): + lExpected = [] + lExpected.extend(range(15, 19)) + lExpected.extend([32,35]) + lExpected.extend(range(49, 53)) + lExpected.extend(range(66, 70)) + lExpected.append(74) + # Generic actual list + lActual = [] + for iIndex, oLine in enumerate(oFile.lines): + if oLine.hasAfterKeyword: + lActual.append(iIndex) + # Compare + self.assertEqual(lActual, lExpected) + + def test_inside_clock_process(self): + lExpected = [] + lExpected.extend(range(14, 21)) + lExpected.extend(range(31, 38)) + lExpected.extend(range(48, 55)) + lExpected.extend(range(65, 72)) + + # Generic actual list + lActual = [] + for iIndex, oLine in enumerate(oFile.lines): + if oLine.insideClockProcess: + lActual.append(iIndex) + # Compare + self.assertEqual(lActual, lExpected) diff --git a/vsg/vhdlFile/classify/__init__.py b/vsg/vhdlFile/classify/__init__.py index 431c034a..c9d0a1db 100644 --- a/vsg/vhdlFile/classify/__init__.py +++ b/vsg/vhdlFile/classify/__init__.py @@ -1,3 +1,4 @@ +from .after import after from .architecture import architecture from .assert_statement import assert_statement from .attribute import attribute diff --git a/vsg/vhdlFile/classify/after.py b/vsg/vhdlFile/classify/after.py new file mode 100644 index 00000000..f380b808 --- /dev/null +++ b/vsg/vhdlFile/classify/after.py @@ -0,0 +1,8 @@ +import re + + +def after(dVars, oLine): + + if oLine.isEndConcurrent or oLine.isSequentialEnd: + if re.match('^.*\safter\s', oLine.lineNoComment, flags=re.IGNORECASE): + oLine.hasAfterKeyword = True diff --git a/vsg/vhdlFile/classify/process.py b/vsg/vhdlFile/classify/process.py index 38d7ff21..16aacfb7 100644 --- a/vsg/vhdlFile/classify/process.py +++ b/vsg/vhdlFile/classify/process.py @@ -7,6 +7,7 @@ def process(dVars, oLine): classify_process_sensitivity_list(dVars, oLine) classify_process_begin_keyword(dVars, oLine) classify_process_end_keyword(dVars, oLine) + classify_clock_process(dVars, oLine) def classify_process_keyword(dVars, oLine): @@ -64,3 +65,11 @@ def classify_process_end_keyword(dVars, oLine): dVars['fFoundProcessBegin'] = False dVars['iCurrentIndentLevel'] = dVars['iCurrentIndentLevel'] - 1 dVars['SensitivityListFound'] = False + +def classify_clock_process(dVars, oLine): + if re.match('^\s*elsif\s*.*\'event\s+and\s+\w+\s*=\s*\'[0-1]\'', oLine.lineNoComment, flags=re.IGNORECASE): + oLine.insideClockProcess = True + if re.match('^\s*elsif\s*.*rising_edge', oLine.lineNoComment, flags=re.IGNORECASE): + oLine.insideClockProcess = True + if re.match('^\s*elsif\s*.*falling_edge', oLine.lineNoComment, flags=re.IGNORECASE): + oLine.insideClockProcess = True diff --git a/vsg/vhdlFile/update.py b/vsg/vhdlFile/update.py index 9a45412c..4ea5af6e 100644 --- a/vsg/vhdlFile/update.py +++ b/vsg/vhdlFile/update.py @@ -33,6 +33,8 @@ def update_process_attributes(oPreviousLine, oCurrentLine): if oPreviousLine.insideProcess and not oPreviousLine.isEndProcess: oCurrentLine.insideProcess = True + if oPreviousLine.insideClockProcess and not oPreviousLine.isEndProcess: + oCurrentLine.insideClockProcess = True def update_concurrent_attributes(oPreviousLine, oCurrentLine): diff --git a/vsg/vhdlFile/vhdlFile.py b/vsg/vhdlFile/vhdlFile.py index 2816cd9a..e4ab58fe 100644 --- a/vsg/vhdlFile/vhdlFile.py +++ b/vsg/vhdlFile/vhdlFile.py @@ -93,6 +93,7 @@ class vhdlFile(): classify.sequential(dVars, oLine) classify.variable_assignment(dVars, oLine) classify.wait(dVars, oLine) + classify.after(dVars, oLine) # Check instantiation statements if oLine.insideArchitecture and not oLine.insideProcess and \