3939import argparse
4040import json
4141
42+ addon_name = os .path .basename (sys .argv [0 ]).replace ('.py' ,'' )
43+
4244# Auxiliary class
4345class DataStruct :
4446 def __init__ (self , file , linenr , string , column = 0 ):
@@ -48,7 +50,7 @@ def __init__(self, file, linenr, string, column=0):
4850 self .column = column
4951
5052def reportNamingError (location ,message ,errorId = 'namingConvention' ,severity = 'style' ,extra = '' ,column = None ):
51- cppcheckdata .reportError (location ,severity ,message ,'namingng' ,errorId ,extra ,columnOverride = column )
53+ cppcheckdata .reportError (location ,severity ,message ,addon_name ,errorId ,extra ,columnOverride = column )
5254
5355def configError (error ,fatal = True ):
5456 print ('config error: %s' % error )
@@ -75,6 +77,56 @@ def validateConfigREs(list_or_dict,json_key):
7577
7678 return have_error
7779
80+ def configMapping ():
81+ return {
82+ 'file' : ('RE_FILE' , (list ,)),
83+ 'namespace' : ('RE_NAMESPACE' , (list ,dict )),
84+ 'include_guard' : ('include_guard' , (dict ,)),
85+ 'variable' : ('RE_VARNAME' , (list ,dict )),
86+ 'variable_prefixes' : ('var_prefixes' , (dict ,), {}),
87+ 'private_member' : ('RE_PRIVATE_MEMBER_VARIABLE' , (list ,dict )),
88+ 'const' : ('RE_CONST' , (list ,dict )),
89+ 'public_member' : ('RE_PUBLIC_MEMBER_VARIABLE' , (list ,dict )),
90+ 'global_variable' : ('RE_GLOBAL_VARNAME' , (list ,dict )),
91+ 'function_name' : ('RE_FUNCTIONNAME' , (list ,dict )),
92+ 'function_prefixes' : ('function_prefixes' , (dict ,), {}),
93+ 'class_name' : ('RE_CLASS_NAME' , (list ,dict )),
94+ 'skip_one_char_variables' : ('skip_one_char_variables' , (bool ,)),
95+ }
96+
97+ def genDeprecatedConfig ():
98+ class Config :
99+ pass
100+ config = Config ()
101+
102+ mapping = configMapping ()
103+ for key ,opts in mapping .items ():
104+ default = None if len (opts )< 3 else opts [2 ]
105+ setattr (config ,key ,default )
106+
107+ have_error = False
108+ def validate (re ):
109+ have_error |= validateConfigRE ([re ])
110+
111+ if args .var :
112+ setattr (config ,'variable' ,[args .var ])
113+ setattr (config ,'global_variable' ,[args .var ])
114+
115+ if args .const :
116+ setattr (config ,'const' ,[args .const ])
117+
118+ if args .private_member_variable :
119+ setattr (config ,'private_member' ,[args .private_member_variable ])
120+
121+ if args .function :
122+ setattr (config ,'function' ,[args .function ])
123+
124+ if have_error :
125+ sys .exit (0 )
126+
127+ return config
128+
129+
78130def loadConfig (configfile ):
79131 if not os .path .exists (configfile ):
80132 configError ("cannot find config file '%s'" % configfile )
@@ -100,20 +152,7 @@ class Config:
100152 pass
101153 config = Config ()
102154
103- mapping = {
104- 'file' : ('RE_FILE' , (list ,)),
105- 'namespace' : ('RE_NAMESPACE' , (list ,dict )),
106- 'include_guard' : ('include_guard' , (dict ,)),
107- 'variable' : ('RE_VARNAME' , (list ,dict )),
108- 'variable_prefixes' : ('var_prefixes' , (dict ,), {}),
109- 'private_member' : ('RE_PRIVATE_MEMBER_VARIABLE' , (list ,dict )),
110- 'public_member' : ('RE_PUBLIC_MEMBER_VARIABLE' , (list ,dict )),
111- 'global_variable' : ('RE_GLOBAL_VARNAME' , (list ,dict )),
112- 'function_name' : ('RE_FUNCTIONNAME' , (list ,dict )),
113- 'function_prefixes' : ('function_prefixes' , (dict ,), {}),
114- 'class_name' : ('RE_CLASS_NAME' , (list ,dict )),
115- 'skip_one_char_variables' : ('skip_one_char_variables' , (bool ,)),
116- }
155+ mapping = configMapping ()
117156
118157 # parse defined keys and store as members of config object
119158 for key ,opts in mapping .items ():
@@ -263,9 +302,7 @@ def report_pending_ifndef(directive,column):
263302 if pending_ifndef :
264303 report_pending_ifndef (pending_ifndef ,guard_column )
265304
266- def process (dumpfiles , configfile ):
267- conf = loadConfig (configfile )
268-
305+ def process (dumpfiles , conf ):
269306 if conf .include_guard :
270307 global include_guard_header_re
271308 include_guard_header_re = conf .include_guard .get ('RE_HEADERFILE' ,"[^/].*\\ .h\\ Z" )
@@ -334,10 +371,12 @@ def check_variable_naming(conf,cfg):
334371 evalExpr (conf .variable , exp , mockToken , 'Variable' )
335372
336373# Naming check for Global, Private and Public member variables
337- def check_gpp_naming (conf_list ,cfg ,access ,message ):
374+ def check_gpp_naming (conf_list ,cfg ,access ,message , require_const = False ):
338375 for var in cfg .variables :
339376 if var .access != access :
340377 continue
378+ if require_const and not var .isConst :
379+ continue
341380 mockToken = DataStruct (var .typeStartToken .file , var .typeStartToken .linenr , var .nameToken .str , var .nameToken .column )
342381 for exp in conf_list :
343382 evalExpr (conf_list , exp , mockToken , message )
@@ -393,6 +432,8 @@ def process_data(conf,data):
393432 check_gpp_naming (conf .private_member ,cfg ,'Private' ,'Private member variable' )
394433 if conf .public_member :
395434 check_gpp_naming (conf .public_member ,cfg ,'Public' ,'Public member variable' )
435+ if conf .const :
436+ check_gpp_naming (conf .const ,cfg ,'Public' ,'Constant' ,require_const = True )
396437 if conf .global_variable :
397438 check_gpp_naming (conf .global_variable ,cfg ,'Global' ,'Global variable' )
398439 if conf .function_name :
@@ -406,14 +447,32 @@ def process_data(conf,data):
406447 mockToken = DataStruct (fn ,0 ,os .path .basename (fn ))
407448 reportNamingError (mockToken ,'Missing include guard' ,'includeGuardMissing' )
408449
409- if __name__ == "__main__" :
450+ def main () :
410451 parser = cppcheckdata .ArgumentParser ()
411452 parser .add_argument ("--debugprint" , action = "store_true" , default = False ,
412453 help = "Add debug prints" )
413454 parser .add_argument ("--configfile" , type = str , default = "namingng.config.json" ,
414455 help = "Naming check config file" )
415-
456+ # The following flags are implemented to provide backward compatibility with the former naming.py addon.
457+ parser .add_argument ("--var" , type = str ,
458+ help = "[deprecated] specify RE_VARNAME and RE_GLOBAL_VARIABLE" )
459+ parser .add_argument ("--const" , type = str ,
460+ help = "[deprecated] specify RE_CONSTNAME" )
461+ parser .add_argument ("--private-member-variable" , type = str ,
462+ help = "[deprecated] specify RE_PRIVATE_MEMBER_VARIABLE" )
463+ parser .add_argument ("--function" , type = str ,
464+ help = "[deprecated] specify RE_FUNCTIONNAME" )
465+
466+ global args
416467 args = parser .parse_args ()
417- process (args .dumpfile , args .configfile )
418468
469+ if args .var or args .const or args .private_member_variable or args .function :
470+ conf = genDeprecatedConfig ()
471+ else :
472+ conf = loadConfig (args .configfile )
473+
474+ process (args .dumpfile , conf )
475+
476+ if __name__ == "__main__" :
477+ main ()
419478 sys .exit (0 )
0 commit comments