diff --git a/docs/function_rules.rst b/docs/function_rules.rst index bb799b39..5ce1e6e4 100644 --- a/docs/function_rules.rst +++ b/docs/function_rules.rst @@ -209,3 +209,50 @@ This rule checks for a function parameter on the same line as the function keywo end; +function_010 +############ + +This rule checks for consistent capitalization of function names. + +**Violation** + +.. code-block:: vhdl + + architecture RTL of FIFO is + + function func_1 () + + begin + + OUT1 <= Func_1; + + PROC1 : process () is + begin + + sig1 <= FUNC_1; + + end process; + + end architecture RTL; + +**Violation** + +.. code-block:: vhdl + + architecture RTL of FIFO is + + function func_1 () + + begin + + OUT1 <= func_1; + + PROC1 : process () is + begin + + sig1 <= func_1; + + end process; + + end architecture RTL; + diff --git a/vsg/rules/function/__init__.py b/vsg/rules/function/__init__.py index 38027e85..1e68aaec 100644 --- a/vsg/rules/function/__init__.py +++ b/vsg/rules/function/__init__.py @@ -8,3 +8,4 @@ from .rule_006 import rule_006 from .rule_007 import rule_007 from .rule_008 import rule_008 from .rule_009 import rule_009 +from .rule_010 import rule_010 diff --git a/vsg/rules/function/rule_010.py b/vsg/rules/function/rule_010.py new file mode 100644 index 00000000..d16263fb --- /dev/null +++ b/vsg/rules/function/rule_010.py @@ -0,0 +1,69 @@ + +from vsg import rule +from vsg import utils + + +class rule_010(rule.rule): + ''' + General rule 010 checks capitalization consistency of function names. + ''' + + def __init__(self): + rule.rule.__init__(self, 'function', '010') + self.fixable = True + self.solution = 'Inconsistent capitalization of word' + self.phase = 6 + self.lLowerCaseWords = [] + self.lCaseWords = [] + self.dDatabase = create_database() + + def _analyze(self, oFile, oLine, iLineNumber): + if oLine.isFunctionKeyword: + self.dDatabase['function'].append(utils.extract_type_name(oLine)) + if oLine.insideArchitecture: + if oLine.insideProcess or oLine.insideConcurrent: + lWords = extract_word_list(oLine) + check_violations(self, lWords, iLineNumber) + + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + for sWord in self.dFix['violations'][iLineNumber]: + sReplacementWord = get_replacement_word(self, sWord) + utils.change_word(oFile.lines[iLineNumber], sWord, sReplacementWord, 20) + + def _get_solution(self, iLineNumber): + if len(self.dFix['violations'][iLineNumber]) > 1: + sSolution = self.solution + 's: ' + ', '.join(self.dFix['violations'][iLineNumber]) + else: + sSolution = self.solution + ': ' + ', '.join(self.dFix['violations'][iLineNumber]) + return sSolution + +def create_database(): + + dDatabase = {} + dDatabase['function'] = [] + + return dDatabase + +def extract_word_list(oLine): + + sLine = oLine.line + + lWords = utils.extract_non_keywords(sLine) + return lWords + +def check_violations(self, lWords, iLineNumber): + for sWord in lWords: + if sWord.lower() in map(str.lower, self.dDatabase['function']): + if not sWord in self.dDatabase['function']: + self.add_violation(iLineNumber) + try: + self.dFix['violations'][iLineNumber].append(sWord) + except KeyError: + self.dFix['violations'][iLineNumber] = [] + self.dFix['violations'][iLineNumber].append(sWord) + +def get_replacement_word(self, sWord): + for sNewWord in self.dDatabase['function']: + if sNewWord.lower() == sWord.lower(): + return sNewWord diff --git a/vsg/tests/function/consistent_case_test_input.vhd b/vsg/tests/function/consistent_case_test_input.vhd new file mode 100644 index 00000000..0a5b9667 --- /dev/null +++ b/vsg/tests/function/consistent_case_test_input.vhd @@ -0,0 +1,18 @@ + +architecture RTL of FIFO is + + function func_1 () + +begin + + OUT1 <= Func_1; + + PROC1 : process () is + begin + + sig1 <= FUNC_1; + + end process; + +end architecture RTL; + diff --git a/vsg/tests/function/test_consistent_case.py b/vsg/tests/function/test_consistent_case.py new file mode 100644 index 00000000..252d9bb2 --- /dev/null +++ b/vsg/tests/function/test_consistent_case.py @@ -0,0 +1,36 @@ +import os + +import unittest +import sys +sys.path.append('vsg') + +from vsg.rules import function +from vsg import vhdlFile +from vsg.tests import utils + + +class testConsistentCase(unittest.TestCase): + + def setUp(self): + # Read in test file used for all tests + lFile = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'consistent_case_test_input.vhd')) + self.oFile = vhdlFile.vhdlFile(lFile) + + def test_rule_010(self): + oRule = function.rule_010() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'function') + self.assertEqual(oRule.identifier, '010') + dExpected = [8, 13] + oRule.analyze(self.oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_010(self): + oRule = function.rule_010() + oRule.fix(self.oFile) + oRule.analyze(self.oFile) + + self.assertEqual(self.oFile.lines[8].line, ' OUT1 <= func_1;') + self.assertEqual(self.oFile.lines[13].line, ' sig1 <= func_1;') + + self.assertEqual(oRule.violations, [])