From 5647dbcb350cbc20ca8c244dcd3c5c4d707fa72d Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Thu, 11 Jul 2024 07:26:01 -0500 Subject: [PATCH] Issue#911: Improving detection of unary logical operators. (#1046) * Issue#911: Improving detection of unary logical operators. * Adding descriminator for keywords before unary operator. * Fixing style issues. --- tests/vhdlFile/test_unary_operator.py | 26 + .../unary_operator/classification_results.txt | 461 ++++++++++++++++++ .../classification_test_input.vhd | 51 ++ .../whitespace/rule_013_test_input.fixed.vhd | 27 + tests/whitespace/rule_013_test_input.vhd | 27 + vsg/vhdlFile/vhdlFile.py | 48 ++ 6 files changed, 640 insertions(+) create mode 100644 tests/vhdlFile/test_unary_operator.py create mode 100644 tests/vhdlFile/unary_operator/classification_results.txt create mode 100644 tests/vhdlFile/unary_operator/classification_test_input.vhd diff --git a/tests/vhdlFile/test_unary_operator.py b/tests/vhdlFile/test_unary_operator.py new file mode 100644 index 00000000..87e4604d --- /dev/null +++ b/tests/vhdlFile/test_unary_operator.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +import os +import unittest + +from tests import utils +from vsg import vhdlFile + +sLrmUnit = "unary_operator" + +lFile, eError = vhdlFile.utils.read_vhdlfile(os.path.join(os.path.dirname(__file__), sLrmUnit, "classification_test_input.vhd")) +oFile = vhdlFile.vhdlFile(lFile) + + +class test_token(unittest.TestCase): + def test_classification(self): + sTestDir = os.path.join(os.path.dirname(__file__), sLrmUnit) + + lExpected = [] + utils.read_file(os.path.join(sTestDir, "classification_results.txt"), lExpected, False) + + lActual = [] + + for oObject in utils.extract_objects(oFile, True): + lActual.append(str(oObject)) + + self.assertEqual(lExpected, lActual) diff --git a/tests/vhdlFile/unary_operator/classification_results.txt b/tests/vhdlFile/unary_operator/classification_results.txt new file mode 100644 index 00000000..d7822f4e --- /dev/null +++ b/tests/vhdlFile/unary_operator/classification_results.txt @@ -0,0 +1,461 @@ +-------------------------------------------------------------------------------- +0 | +-------------------------------------------------------------------------------- +1 | + +-------------------------------------------------------------------------------- +2 | architecture RTL of FIFO is + + + + + +-------------------------------------------------------------------------------- +3 | + +-------------------------------------------------------------------------------- +4 | begin + +-------------------------------------------------------------------------------- +5 | + +-------------------------------------------------------------------------------- +6 | a <= b and c or d xor e nand f nor g xor h xnor i; + + + + + + + + + + + + + + + + + + +-------------------------------------------------------------------------------- +7 | + +-------------------------------------------------------------------------------- +8 | a <= (b) and (c) or (d) xor (e) nand (f) nor (g) xor (h) xnor (i); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +-------------------------------------------------------------------------------- +9 | + +-------------------------------------------------------------------------------- +10 | -- Violations + +-------------------------------------------------------------------------------- +11 | + +-------------------------------------------------------------------------------- +12 | a <= b and c or d xor e nand f nor g xor h xnor i; + + + + + + + + + + + + + + + + + + +-------------------------------------------------------------------------------- +13 | + +-------------------------------------------------------------------------------- +14 | a <= (b)and(c)or(d)xor(e)nand(f)nor(g)xor(h)xnor(i); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +-------------------------------------------------------------------------------- +15 | + +-------------------------------------------------------------------------------- +16 | -- Unary operators should be ignored + +-------------------------------------------------------------------------------- +17 | a <= (others => func(and b, or b, nand b, or b, nor b, xnor b)); + + + + + + + + + + + + + + + + + + + + + + + + + + + +-------------------------------------------------------------------------------- +18 | a <= (others => func(nand b)); + + + + + + + + + + + + +-------------------------------------------------------------------------------- +19 | a <= (others => func(or b)); + + + + + + + + + + + + +-------------------------------------------------------------------------------- +20 | a <= (others => func(nor b)); + + + + + + + + + + + + +-------------------------------------------------------------------------------- +21 | a <= (others => func(xor b)); + + + + + + + + + + + + +-------------------------------------------------------------------------------- +22 | a <= (others => func(xnor b)); + + + + + + + + + + + + +-------------------------------------------------------------------------------- +23 | + +-------------------------------------------------------------------------------- +24 | a <= and(some_signal); + + + + + + + +-------------------------------------------------------------------------------- +25 | a <= nand(some_signal); + + + + + + + +-------------------------------------------------------------------------------- +26 | a <= or(some_signal); + + + + + + + +-------------------------------------------------------------------------------- +27 | a <= nor(some_signal); + + + + + + + +-------------------------------------------------------------------------------- +28 | a <= xor(some_signal); + + + + + + + +-------------------------------------------------------------------------------- +29 | a <= xnor(some_signal); + + + + + + + +-------------------------------------------------------------------------------- +30 | + +-------------------------------------------------------------------------------- +31 | a <= b and and(some_signal); + + + + + + + + + +-------------------------------------------------------------------------------- +32 | a <= c and nand(some_signal); + + + + + + + + + +-------------------------------------------------------------------------------- +33 | a <= d and or(some_signal); + + + + + + + + + +-------------------------------------------------------------------------------- +34 | a <= e and nor(some_signal); + + + + + + + + + +-------------------------------------------------------------------------------- +35 | a <= f and xor(some_signal); + + + + + + + + + +-------------------------------------------------------------------------------- +36 | a <= g and xnor(some_signal); + + + + + + + + + +-------------------------------------------------------------------------------- +37 | + +-------------------------------------------------------------------------------- +38 | process + +-------------------------------------------------------------------------------- +39 | begin + +-------------------------------------------------------------------------------- +40 | + +-------------------------------------------------------------------------------- +41 | if or(some_signal) then + + + + + + +-------------------------------------------------------------------------------- +42 | elsif and(some_signal) then + + + + + + +-------------------------------------------------------------------------------- +43 | elsif nand(some_signal) then + + + + + + +-------------------------------------------------------------------------------- +44 | elsif nor(some_signal) then + + + + + + +-------------------------------------------------------------------------------- +45 | elsif xor(some_signal) then + + + + + + +-------------------------------------------------------------------------------- +46 | elsif xnor(some_signal) then + + + + + + +-------------------------------------------------------------------------------- +47 | end if; + + + +-------------------------------------------------------------------------------- +48 | + +-------------------------------------------------------------------------------- +49 | end process; + + + +-------------------------------------------------------------------------------- +50 | + +-------------------------------------------------------------------------------- +51 | end architecture RTL; + + + + diff --git a/tests/vhdlFile/unary_operator/classification_test_input.vhd b/tests/vhdlFile/unary_operator/classification_test_input.vhd new file mode 100644 index 00000000..d47cfbbe --- /dev/null +++ b/tests/vhdlFile/unary_operator/classification_test_input.vhd @@ -0,0 +1,51 @@ + +architecture RTL of FIFO is + +begin + + a <= b and c or d xor e nand f nor g xor h xnor i; + + a <= (b) and (c) or (d) xor (e) nand (f) nor (g) xor (h) xnor (i); + + -- Violations + + a <= b and c or d xor e nand f nor g xor h xnor i; + + a <= (b)and(c)or(d)xor(e)nand(f)nor(g)xor(h)xnor(i); + + -- Unary operators should be ignored + a <= (others => func(and b, or b, nand b, or b, nor b, xnor b)); + a <= (others => func(nand b)); + a <= (others => func(or b)); + a <= (others => func(nor b)); + a <= (others => func(xor b)); + a <= (others => func(xnor b)); + + a <= and(some_signal); + a <= nand(some_signal); + a <= or(some_signal); + a <= nor(some_signal); + a <= xor(some_signal); + a <= xnor(some_signal); + + a <= b and and(some_signal); + a <= c and nand(some_signal); + a <= d and or(some_signal); + a <= e and nor(some_signal); + a <= f and xor(some_signal); + a <= g and xnor(some_signal); + + process + begin + + if or(some_signal) then + elsif and(some_signal) then + elsif nand(some_signal) then + elsif nor(some_signal) then + elsif xor(some_signal) then + elsif xnor(some_signal) then + end if; + + end process; + +end architecture RTL; diff --git a/tests/whitespace/rule_013_test_input.fixed.vhd b/tests/whitespace/rule_013_test_input.fixed.vhd index 4844f31c..6e0b9b85 100644 --- a/tests/whitespace/rule_013_test_input.fixed.vhd +++ b/tests/whitespace/rule_013_test_input.fixed.vhd @@ -21,4 +21,31 @@ begin a <= (others => func(xor b)); a <= (others => func(xnor b)); + a <= and(some_signal); + a <= nand(some_signal); + a <= or(some_signal); + a <= nor(some_signal); + a <= xor(some_signal); + a <= xnor(some_signal); + + a <= b and and(some_signal); + a <= c and nand(some_signal); + a <= d and or(some_signal); + a <= e and nor(some_signal); + a <= f and xor(some_signal); + a <= g and xnor(some_signal); + + process + begin + + if or(some_signal) then + elsif and(some_signal) then + elsif nand(some_signal) then + elsif nor(some_signal) then + elsif xor(some_signal) then + elsif xnor(some_signal) then + end if; + + end process; + end architecture RTL; diff --git a/tests/whitespace/rule_013_test_input.vhd b/tests/whitespace/rule_013_test_input.vhd index d3b05f24..d47cfbbe 100644 --- a/tests/whitespace/rule_013_test_input.vhd +++ b/tests/whitespace/rule_013_test_input.vhd @@ -21,4 +21,31 @@ begin a <= (others => func(xor b)); a <= (others => func(xnor b)); + a <= and(some_signal); + a <= nand(some_signal); + a <= or(some_signal); + a <= nor(some_signal); + a <= xor(some_signal); + a <= xnor(some_signal); + + a <= b and and(some_signal); + a <= c and nand(some_signal); + a <= d and or(some_signal); + a <= e and nor(some_signal); + a <= f and xor(some_signal); + a <= g and xnor(some_signal); + + process + begin + + if or(some_signal) then + elsif and(some_signal) then + elsif nand(some_signal) then + elsif nor(some_signal) then + elsif xor(some_signal) then + elsif xnor(some_signal) then + end if; + + end process; + end architecture RTL; diff --git a/vsg/vhdlFile/vhdlFile.py b/vsg/vhdlFile/vhdlFile.py index 13517296..f623724a 100644 --- a/vsg/vhdlFile/vhdlFile.py +++ b/vsg/vhdlFile/vhdlFile.py @@ -541,6 +541,14 @@ def post_token_assignments(lTokens): lTokens[iToken] = unary_logical_operator.and_operator(sValue) elif utils.are_previous_consecutive_token_types_ignoring_whitespace([parser.comma], iToken - 1, lTokens): lTokens[iToken] = unary_logical_operator.and_operator(sValue) + elif utils.are_previous_consecutive_token_types_ignoring_whitespace([logical_operator.logical_operator], iToken - 1, lTokens): + lTokens[iToken] = unary_logical_operator.and_operator(sValue) + elif utils.are_previous_consecutive_token_types_ignoring_whitespace( + [parser.keyword], + iToken - 1, + lTokens, + ) and utils.are_next_consecutive_token_types_ignoring_whitespace([parser.open_parenthesis], iToken + 1, lTokens): + lTokens[iToken] = unary_logical_operator.and_operator(sValue) else: lTokens[iToken] = logical_operator.and_operator(sValue) @@ -551,6 +559,14 @@ def post_token_assignments(lTokens): lTokens[iToken] = unary_logical_operator.or_operator(sValue) elif utils.are_previous_consecutive_token_types_ignoring_whitespace([parser.comma], iToken - 1, lTokens): lTokens[iToken] = unary_logical_operator.or_operator(sValue) + elif utils.are_previous_consecutive_token_types_ignoring_whitespace([logical_operator.logical_operator], iToken - 1, lTokens): + lTokens[iToken] = unary_logical_operator.or_operator(sValue) + elif utils.are_previous_consecutive_token_types_ignoring_whitespace( + [parser.keyword], + iToken - 1, + lTokens, + ) and utils.are_next_consecutive_token_types_ignoring_whitespace([parser.open_parenthesis], iToken + 1, lTokens): + lTokens[iToken] = unary_logical_operator.or_operator(sValue) else: lTokens[iToken] = logical_operator.or_operator(sValue) @@ -561,6 +577,14 @@ def post_token_assignments(lTokens): lTokens[iToken] = unary_logical_operator.nand_operator(sValue) elif utils.are_previous_consecutive_token_types_ignoring_whitespace([parser.comma], iToken - 1, lTokens): lTokens[iToken] = unary_logical_operator.nand_operator(sValue) + elif utils.are_previous_consecutive_token_types_ignoring_whitespace([logical_operator.logical_operator], iToken - 1, lTokens): + lTokens[iToken] = unary_logical_operator.nand_operator(sValue) + elif utils.are_previous_consecutive_token_types_ignoring_whitespace( + [parser.keyword], + iToken - 1, + lTokens, + ) and utils.are_next_consecutive_token_types_ignoring_whitespace([parser.open_parenthesis], iToken + 1, lTokens): + lTokens[iToken] = unary_logical_operator.nand_operator(sValue) else: lTokens[iToken] = logical_operator.nand_operator(sValue) @@ -571,6 +595,14 @@ def post_token_assignments(lTokens): lTokens[iToken] = unary_logical_operator.nor_operator(sValue) elif utils.are_previous_consecutive_token_types_ignoring_whitespace([parser.comma], iToken - 1, lTokens): lTokens[iToken] = unary_logical_operator.nor_operator(sValue) + elif utils.are_previous_consecutive_token_types_ignoring_whitespace([logical_operator.logical_operator], iToken - 1, lTokens): + lTokens[iToken] = unary_logical_operator.nor_operator(sValue) + elif utils.are_previous_consecutive_token_types_ignoring_whitespace( + [parser.keyword], + iToken - 1, + lTokens, + ) and utils.are_next_consecutive_token_types_ignoring_whitespace([parser.open_parenthesis], iToken + 1, lTokens): + lTokens[iToken] = unary_logical_operator.nor_operator(sValue) else: lTokens[iToken] = logical_operator.nor_operator(sValue) @@ -581,6 +613,14 @@ def post_token_assignments(lTokens): lTokens[iToken] = unary_logical_operator.xor_operator(sValue) elif utils.are_previous_consecutive_token_types_ignoring_whitespace([parser.comma], iToken - 1, lTokens): lTokens[iToken] = unary_logical_operator.xor_operator(sValue) + elif utils.are_previous_consecutive_token_types_ignoring_whitespace([logical_operator.logical_operator], iToken - 1, lTokens): + lTokens[iToken] = unary_logical_operator.xor_operator(sValue) + elif utils.are_previous_consecutive_token_types_ignoring_whitespace( + [parser.keyword], + iToken - 1, + lTokens, + ) and utils.are_next_consecutive_token_types_ignoring_whitespace([parser.open_parenthesis], iToken + 1, lTokens): + lTokens[iToken] = unary_logical_operator.xor_operator(sValue) else: lTokens[iToken] = logical_operator.xor_operator(sValue) @@ -591,6 +631,14 @@ def post_token_assignments(lTokens): lTokens[iToken] = unary_logical_operator.xnor_operator(sValue) elif utils.are_previous_consecutive_token_types_ignoring_whitespace([parser.comma], iToken - 1, lTokens): lTokens[iToken] = unary_logical_operator.xnor_operator(sValue) + elif utils.are_previous_consecutive_token_types_ignoring_whitespace([logical_operator.logical_operator], iToken - 1, lTokens): + lTokens[iToken] = unary_logical_operator.xnor_operator(sValue) + elif utils.are_previous_consecutive_token_types_ignoring_whitespace( + [parser.keyword], + iToken - 1, + lTokens, + ) and utils.are_next_consecutive_token_types_ignoring_whitespace([parser.open_parenthesis], iToken + 1, lTokens): + lTokens[iToken] = unary_logical_operator.xnor_operator(sValue) else: lTokens[iToken] = logical_operator.xnor_operator(sValue)