"Fossies" - the Fresh Open Source Software Archive

Member "cppcheck-1.89/addons/naming.py" (1 Sep 2019, 2624 Bytes) of package /windows/misc/cppcheck-1.89.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "naming.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.87_vs_1.88.

    1 #!/usr/bin/env python
    2 #
    3 # cppcheck addon for naming conventions
    4 #
    5 # Example usage (variable name must start with lowercase, function name must start with uppercase):
    6 # $ cppcheck --dump path-to-src/
    7 # $ python addons/naming.py --var='[a-z].*' --function='[A-Z].*' path-to-src/*.dump
    8 #
    9 
   10 import cppcheckdata
   11 import sys
   12 import re
   13 
   14 
   15 def validate_regex(expr):
   16     try:
   17         re.compile(expr)
   18     except re.error:
   19         print('Error: "{}" is not a valid regular expression.'.format(expr))
   20         exit(1)
   21 
   22 
   23 RE_VARNAME = None
   24 RE_PRIVATE_MEMBER_VARIABLE = None
   25 RE_FUNCTIONNAME = None
   26 for arg in sys.argv[1:]:
   27     if arg[:6] == '--var=':
   28         RE_VARNAME = arg[6:]
   29         validate_regex(RE_VARNAME)
   30     elif arg.startswith('--private-member-variable='):
   31         RE_PRIVATE_MEMBER_VARIABLE = arg[arg.find('=')+1:]
   32         validate_regex(RE_PRIVATE_MEMBER_VARIABLE)
   33     elif arg[:11] == '--function=':
   34         RE_FUNCTIONNAME = arg[11:]
   35         validate_regex(RE_FUNCTIONNAME)
   36 
   37 def reportError(token, severity, msg, errorId):
   38     cppcheckdata.reportError(token, severity, msg, 'naming', errorId)
   39 
   40 for arg in sys.argv[1:]:
   41     if not arg.endswith('.dump'):
   42         continue
   43     print('Checking ' + arg + '...')
   44     data = cppcheckdata.parsedump(arg)
   45     for cfg in data.configurations:
   46         if len(data.configurations) > 1:
   47             print('Checking ' + arg + ', config "' + cfg.name + '"...')
   48         if RE_VARNAME:
   49             for var in cfg.variables:
   50                 if var.nameToken:
   51                     res = re.match(RE_VARNAME, var.nameToken.str)
   52                     if not res:
   53                         reportError(var.typeStartToken, 'style', 'Variable ' +
   54                                     var.nameToken.str + ' violates naming convention', 'varname')
   55         if RE_PRIVATE_MEMBER_VARIABLE:
   56             for var in cfg.variables:
   57                 if (var.access is None) or var.access != 'Private':
   58                     continue
   59                 res = re.match(RE_PRIVATE_MEMBER_VARIABLE, var.nameToken.str)
   60                 if not res:
   61                     reportError(var.typeStartToken, 'style', 'Private member variable ' +
   62                                 var.nameToken.str + ' violates naming convention', 'privateMemberVariable')
   63         if RE_FUNCTIONNAME:
   64             for scope in cfg.scopes:
   65                 if scope.type == 'Function':
   66                     res = re.match(RE_FUNCTIONNAME, scope.className)
   67                     if not res:
   68                         reportError(
   69                             scope.bodyStart, 'style', 'Function ' + scope.className + ' violates naming convention', 'functionName')
   70