Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rule generate_022 (require keyword **begin** in for generate) #1239

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/configuring_disabled_rules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Rules Disabled by Default
* `function_600 <function_rules.html#function-600>`_
* `function_601 <function_rules.html#function-601>`_
* `generate_017 <generate_rules.html#generate-017>`_
* `generate_022 <generate_rules.html#generate-022>`_
* `generate_600 <generate_rules.html#generate-600>`_
* `generate_601 <generate_rules.html#generate-601>`_
* `generate_602 <generate_rules.html#generate-602>`_
Expand Down
1 change: 1 addition & 0 deletions docs/configuring_optional_items.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Rules Enforcing Optional Items
* `function_018 <function_rules.html#function-018>`_
* `function_020 <function_rules.html#function-020>`_
* `generate_011 <generate_rules.html#generate-011>`_
* `generate_022 <generate_rules.html#generate-022>`_
* `instantiation_033 <instantiation_rules.html#instantiation-033>`_
* `loop_statement_007 <loop_statement_rules.html#loop-statement-007>`_
* `package_007 <package_rules.html#package-007>`_
Expand Down
23 changes: 23 additions & 0 deletions docs/generate_rules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,29 @@ This rule checks a label colon is on the same line as the **case**, **if**, and
label
: case

generate_022
############

|phase_1| |disabled| |error| |structure| |structure_optional|

This rule checks for the existence of the **begin** keyword in for generate statements.

|configuring_optional_items_link|

**Violation**

.. code-block:: vhdl

ram_array : for i in 0 to 7 generate
end generate;

**Fix**

.. code-block:: vhdl

ram_array : for i in 0 to 7 generate begin
end generate;

generate_400
############

Expand Down
1 change: 1 addition & 0 deletions docs/rule_groups/structure_optional_rule_group.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Rules Enforcing Structure::Optional Rule Group
* `function_018 <../function_rules.html#function-018>`_
* `function_020 <../function_rules.html#function-020>`_
* `generate_011 <../generate_rules.html#generate-011>`_
* `generate_022 <../generate_rules.html#generate-022>`_
* `instantiation_033 <../instantiation_rules.html#instantiation-033>`_
* `loop_statement_007 <../loop_statement_rules.html#loop-statement-007>`_
* `package_007 <../package_rules.html#package-007>`_
Expand Down
1 change: 1 addition & 0 deletions docs/rule_groups/structure_rule_group.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Rules Enforcing Structure Rule Group
* `generate_019 <../generate_rules.html#generate-019>`_
* `generate_020 <../generate_rules.html#generate-020>`_
* `generate_021 <../generate_rules.html#generate-021>`_
* `generate_022 <../generate_rules.html#generate-022>`_
* `generic_010 <../generic_rules.html#generic-010>`_
* `generic_013 <../generic_rules.html#generic-013>`_
* `generic_016 <../generic_rules.html#generic-016>`_
Expand Down
16 changes: 16 additions & 0 deletions tests/generate/rule_022_test_input.fixed.vhd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
architecture RTL of FIFO is

begin

gen0 : for i in 0 to 7 generate
begin
end generate;

-- Violations below

gen1 : for i in 0 to 7 generate begin
end generate;

gen2 : for i in 0 to 7 generate begin end generate;

end;
16 changes: 16 additions & 0 deletions tests/generate/rule_022_test_input.vhd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
architecture RTL of FIFO is

begin

gen0 : for i in 0 to 7 generate
begin
end generate;

-- Violations below

gen1 : for i in 0 to 7 generate
end generate;

gen2 : for i in 0 to 7 generate end generate;

end;
45 changes: 45 additions & 0 deletions tests/generate/test_rule_022.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-

import os
import unittest

from tests import utils
from vsg import vhdlFile
from vsg.rules import generate

sTestDir = os.path.dirname(__file__)

lFile, eError = vhdlFile.utils.read_vhdlfile(os.path.join(sTestDir, "rule_022_test_input.vhd"))

lExpected = []
lExpected.append("")
utils.read_file(os.path.join(sTestDir, "rule_022_test_input.fixed.vhd"), lExpected)


class test_generate_rule(unittest.TestCase):
def setUp(self):
self.oFile = vhdlFile.vhdlFile(lFile)
self.assertIsNone(eError)

def test_rule_022(self):
oRule = generate.rule_022()
self.assertTrue(oRule)
self.assertEqual(oRule.name, "generate")
self.assertEqual(oRule.identifier, "022")

lExpected = [11, 14]

oRule.analyze(self.oFile)
self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations))

def test_fix_rule_022(self):
oRule = generate.rule_022()

oRule.fix(self.oFile)

lActual = self.oFile.get_lines()

self.assertEqual(lExpected, lActual)

oRule.analyze(self.oFile)
self.assertEqual(oRule.violations, [])
1 change: 1 addition & 0 deletions vsg/rules/generate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .rule_019 import rule_019
from .rule_020 import rule_020
from .rule_021 import rule_021
from .rule_022 import rule_022
from .rule_400 import rule_400
from .rule_401 import rule_401
from .rule_402 import rule_402
Expand Down
39 changes: 39 additions & 0 deletions vsg/rules/generate/rule_022.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-

from vsg.rules import (
insert_token_right_of_possible_tokens_if_it_does_not_exist_before_token,
)
from vsg.token import for_generate_statement, generate_statement_body

oInsertToken = generate_statement_body.begin_keyword("begin")
lRightTokens = [for_generate_statement.generate_keyword]
oBeforeToken = for_generate_statement.end_keyword


class rule_022(insert_token_right_of_possible_tokens_if_it_does_not_exist_before_token):
"""
This rule checks for the existence of the **begin** keyword in for generate statements.

|configuring_optional_items_link|

**Violation**

.. code-block:: vhdl

ram_array : for i in 0 to 7 generate
end generate;

**Fix**

.. code-block:: vhdl

ram_array : for i in 0 to 7 generate begin
end generate;
"""

def __init__(self):
super().__init__(oInsertToken, lRightTokens, oBeforeToken)
insert_token_right_of_possible_tokens_if_it_does_not_exist_before_token.__init__(self, oInsertToken, lRightTokens, oBeforeToken)
self.solution = "*begin* keyword"
self.groups.append("structure::optional")
self.disable = True