2828import argparse
2929import json
3030
31+ errors = []
3132
3233# Auxiliary class
3334class DataStruct :
3435 def __init__ (self , file , linenr , string ):
3536 self .file = file
3637 self .linenr = linenr
3738 self .str = string
39+ self .column = 0
3840
39-
40- def reportError (filename , linenr , severity , msg ):
41- message = "[{filename}:{linenr}] ( {severity} ) naming.py: {msg}\n " .format (
42- filename = filename ,
43- linenr = linenr ,
44- severity = severity ,
45- msg = msg
46- )
47- sys .stderr .write (message )
48- return message
49-
41+ def reportNamingError (location ,message ,errorId = 'namingConvention' ,severity = 'style' ,extra = '' ):
42+ line = cppcheckdata .reportError (location ,severity ,message ,'namingng' ,errorId ,extra )
43+ errors .append (line )
5044
5145def loadConfig (configfile ):
5246 with open (configfile ) as fh :
5347 data = json .load (fh )
5448 return data
5549
5650
57- def checkTrueRegex (data , expr , msg , errors ):
51+ def checkTrueRegex (data , expr , msg ):
5852 res = re .match (expr , data .str )
5953 if res :
60- errors . append ( reportError ( data . file , data . linenr , 'style' , msg ) )
54+ reportNamingError ( data , msg )
6155
6256
63- def checkFalseRegex (data , expr , msg , errors ):
57+ def checkFalseRegex (data , expr , msg ):
6458 res = re .match (expr , data .str )
6559 if not res :
66- errors . append ( reportError ( data . file , data . linenr , 'style' , msg ) )
60+ reportNamingError ( data , msg )
6761
6862
69- def evalExpr (conf , exp , mockToken , msgType , errors ):
63+ def evalExpr (conf , exp , mockToken , msgType ):
7064 if isinstance (conf , dict ):
7165 if conf [exp ][0 ]:
7266 msg = msgType + ' ' + mockToken .str + ' violates naming convention : ' + conf [exp ][1 ]
73- checkTrueRegex (mockToken , exp , msg , errors )
67+ checkTrueRegex (mockToken , exp , msg )
7468 elif ~ conf [exp ][0 ]:
7569 msg = msgType + ' ' + mockToken .str + ' violates naming convention : ' + conf [exp ][1 ]
76- checkFalseRegex (mockToken , exp , msg , errors )
70+ checkFalseRegex (mockToken , exp , msg )
7771 else :
7872 msg = msgType + ' ' + mockToken .str + ' violates naming convention : ' + conf [exp ][0 ]
79- checkFalseRegex (mockToken , exp , msg , errors )
73+ checkFalseRegex (mockToken , exp , msg )
8074 else :
8175 msg = msgType + ' ' + mockToken .str + ' violates naming convention'
82- checkFalseRegex (mockToken , exp , msg , errors )
76+ checkFalseRegex (mockToken , exp , msg )
8377
8478
8579def process (dumpfiles , configfile , debugprint = False ):
86-
87- errors = []
88-
8980 conf = loadConfig (configfile )
9081
9182 for afile in dumpfiles :
@@ -99,7 +90,7 @@ def process(dumpfiles, configfile, debugprint=False):
9990 mockToken = DataStruct (afile [:- 5 ], "0" , afile [afile .rfind ('/' ) + 1 :- 5 ])
10091 msgType = 'File name'
10192 for exp in conf ["RE_FILE" ]:
102- evalExpr (conf ["RE_FILE" ], exp , mockToken , msgType , errors )
93+ evalExpr (conf ["RE_FILE" ], exp , mockToken , msgType )
10394
10495 # Check Namespace naming
10596 if "RE_NAMESPACE" in conf and conf ["RE_NAMESPACE" ]:
@@ -108,7 +99,7 @@ def process(dumpfiles, configfile, debugprint=False):
10899 mockToken = DataStruct (tk .next .file , tk .next .linenr , tk .next .str )
109100 msgType = 'Namespace'
110101 for exp in conf ["RE_NAMESPACE" ]:
111- evalExpr (conf ["RE_NAMESPACE" ], exp , mockToken , msgType , errors )
102+ evalExpr (conf ["RE_NAMESPACE" ], exp , mockToken , msgType )
112103
113104 for cfg in data .configurations :
114105 print ('Checking %s, config %s...' % (afile , cfg .name ))
@@ -134,18 +125,15 @@ def process(dumpfiles, configfile, debugprint=False):
134125 continue
135126 if varType in conf ["var_prefixes" ]:
136127 if not var .nameToken .str .startswith (conf ["var_prefixes" ][varType ]):
137- errors .append (reportError (
138- var .typeStartToken .file ,
139- var .typeStartToken .linenr ,
140- 'style' ,
128+ reportNamingError (var .typeStartToken ,
141129 'Variable ' +
142130 var .nameToken .str +
143- ' violates naming convention' ))
131+ ' violates naming convention' )
144132
145133 mockToken = DataStruct (var .typeStartToken .file , var .typeStartToken .linenr , var .nameToken .str )
146134 msgType = 'Variable'
147135 for exp in conf ["RE_VARNAME" ]:
148- evalExpr (conf ["RE_VARNAME" ], exp , mockToken , msgType , errors )
136+ evalExpr (conf ["RE_VARNAME" ], exp , mockToken , msgType )
149137
150138 # Check Private Variable naming
151139 if "RE_PRIVATE_MEMBER_VARIABLE" in conf and conf ["RE_PRIVATE_MEMBER_VARIABLE" ]:
@@ -156,7 +144,7 @@ def process(dumpfiles, configfile, debugprint=False):
156144 mockToken = DataStruct (var .typeStartToken .file , var .typeStartToken .linenr , var .nameToken .str )
157145 msgType = 'Private member variable'
158146 for exp in conf ["RE_PRIVATE_MEMBER_VARIABLE" ]:
159- evalExpr (conf ["RE_PRIVATE_MEMBER_VARIABLE" ], exp , mockToken , msgType , errors )
147+ evalExpr (conf ["RE_PRIVATE_MEMBER_VARIABLE" ], exp , mockToken , msgType )
160148
161149 # Check Public Member Variable naming
162150 if "RE_PUBLIC_MEMBER_VARIABLE" in conf and conf ["RE_PUBLIC_MEMBER_VARIABLE" ]:
@@ -166,7 +154,7 @@ def process(dumpfiles, configfile, debugprint=False):
166154 mockToken = DataStruct (var .typeStartToken .file , var .typeStartToken .linenr , var .nameToken .str )
167155 msgType = 'Public member variable'
168156 for exp in conf ["RE_PUBLIC_MEMBER_VARIABLE" ]:
169- evalExpr (conf ["RE_PUBLIC_MEMBER_VARIABLE" ], exp , mockToken , msgType , errors )
157+ evalExpr (conf ["RE_PUBLIC_MEMBER_VARIABLE" ], exp , mockToken , msgType )
170158
171159 # Check Global Variable naming
172160 if "RE_GLOBAL_VARNAME" in conf and conf ["RE_GLOBAL_VARNAME" ]:
@@ -176,7 +164,7 @@ def process(dumpfiles, configfile, debugprint=False):
176164 mockToken = DataStruct (var .typeStartToken .file , var .typeStartToken .linenr , var .nameToken .str )
177165 msgType = 'Public member variable'
178166 for exp in conf ["RE_GLOBAL_VARNAME" ]:
179- evalExpr (conf ["RE_GLOBAL_VARNAME" ], exp , mockToken , msgType , errors )
167+ evalExpr (conf ["RE_GLOBAL_VARNAME" ], exp , mockToken , msgType )
180168
181169 # Check Functions naming
182170 if "RE_FUNCTIONNAME" in conf and conf ["RE_FUNCTIONNAME" ]:
@@ -194,12 +182,11 @@ def process(dumpfiles, configfile, debugprint=False):
194182
195183 if retval and retval in conf ["function_prefixes" ]:
196184 if not token .function .name .startswith (conf ["function_prefixes" ][retval ]):
197- errors .append (reportError (
198- token .file , token .linenr , 'style' , 'Function ' + token .function .name + ' violates naming convention' ))
185+ reportNamingError (token , 'Function ' + token .function .name + ' violates naming convention' )
199186 mockToken = DataStruct (token .file , token .linenr , token .function .name )
200187 msgType = 'Function'
201188 for exp in conf ["RE_FUNCTIONNAME" ]:
202- evalExpr (conf ["RE_FUNCTIONNAME" ], exp , mockToken , msgType , errors )
189+ evalExpr (conf ["RE_FUNCTIONNAME" ], exp , mockToken , msgType )
203190
204191 # Check Class naming
205192 if "RE_CLASS_NAME" in conf and conf ["RE_CLASS_NAME" ]:
@@ -209,14 +196,10 @@ def process(dumpfiles, configfile, debugprint=False):
209196 mockToken = DataStruct (fnc .tokenDef .file , fnc .tokenDef .linenr , fnc .name )
210197 msgType = 'Class ' + fnc .type
211198 for exp in conf ["RE_CLASS_NAME" ]:
212- evalExpr (conf ["RE_CLASS_NAME" ], exp , mockToken , msgType , errors )
213- return errors
214-
199+ evalExpr (conf ["RE_CLASS_NAME" ], exp , mockToken , msgType )
215200
216201if __name__ == "__main__" :
217- parser = argparse .ArgumentParser (description = 'Naming verification' )
218- parser .add_argument ('dumpfiles' , type = str , nargs = '+' ,
219- help = 'A set of dumpfiles to process' )
202+ parser = cppcheckdata .ArgumentParser ()
220203 parser .add_argument ("--debugprint" , action = "store_true" , default = False ,
221204 help = "Add debug prints" )
222205 parser .add_argument ("--configfile" , type = str , default = "naming.json" ,
@@ -225,28 +208,38 @@ def process(dumpfiles, configfile, debugprint=False):
225208 help = "verify this script. Must be executed in test folder !" )
226209
227210 args = parser .parse_args ()
228- errors = process (args .dumpfiles , args .configfile , args .debugprint )
211+ process (args .dumpfile , args .configfile , args .debugprint )
229212
230213 if args .verify :
231- print (errors )
232214 if len (errors ) < 6 :
233215 print ("Not enough errors found" )
234216 sys .exit (1 )
235- target = [
236- '[namingng_test.c:8] ( style ) naming.py: Variable badui32 violates naming convention\n ' ,
237- '[namingng_test.c:11] ( style ) naming.py: Variable a violates naming convention\n ' ,
238- '[namingng_test.c:29] ( style ) naming.py: Variable badui32 violates naming convention\n ' ,
239- '[namingng_test.c:20] ( style ) naming.py: Function ui16bad_underscore violates naming convention\n ' ,
240- '[namingng_test.c:25] ( style ) naming.py: Function u32Bad violates naming convention\n ' ,
241- '[namingng_test.c:37] ( style ) naming.py: Function Badui16 violates naming convention\n ' ]
217+ if args .cli :
218+ target = [
219+ '{"file": "namingng_test.c", "linenr": 8, "column": 5, "severity": "style", "message": "Variable badui32 violates naming convention", "addon": "namingng", "errorId": "namingConvention", "extra": ""}\n ' ,
220+ '{"file": "namingng_test.c", "linenr": 11, "column": 5, "severity": "style", "message": "Variable a violates naming convention", "addon": "namingng", "errorId": "namingConvention", "extra": ""}\n ' ,
221+ '{"file": "namingng_test.c", "linenr": 29, "column": 5, "severity": "style", "message": "Variable badui32 violates naming convention", "addon": "namingng", "errorId": "namingConvention", "extra": ""}\n ' ,
222+ '{"file": "namingng_test.c", "linenr": 20, "column": 0, "severity": "style", "message": "Function ui16bad_underscore violates naming convention", "addon": "namingng", "errorId": "namingConvention", "extra": ""}\n ' ,
223+ '{"file": "namingng_test.c", "linenr": 25, "column": 10, "severity": "style", "message": "Function u32Bad violates naming convention", "addon": "namingng", "errorId": "namingConvention", "extra": ""}\n ' ,
224+ '{"file": "namingng_test.c", "linenr": 37, "column": 10, "severity": "style", "message": "Function Badui16 violates naming convention", "addon": "namingng", "errorId": "namingConvention", "extra": ""}\n ' ,
225+ ]
226+ else :
227+ target = [
228+ '[namingng_test.c:8] (style) Variable badui32 violates naming convention [namingng-namingConvention]\n ' ,
229+ '[namingng_test.c:11] (style) Variable a violates naming convention [namingng-namingConvention]\n ' ,
230+ '[namingng_test.c:29] (style) Variable badui32 violates naming convention [namingng-namingConvention]\n ' ,
231+ '[namingng_test.c:20] (style) Function ui16bad_underscore violates naming convention [namingng-namingConvention]\n ' ,
232+ '[namingng_test.c:25] (style) Function u32Bad violates naming convention [namingng-namingConvention]\n ' ,
233+ '[namingng_test.c:37] (style) Function Badui16 violates naming convention [namingng-namingConvention]\n ' ,
234+ ]
242235 diff = set (errors ) - set (target )
243236 if len (diff ):
244237 print ("Not the right errors found {}" .format (str (diff )))
245238 sys .exit (1 )
246239 print ("Verification done\n " )
247240 sys .exit (0 )
248241
249- if len (errors ):
242+ if len (errors ) and not args . cli :
250243 print ('Found errors: {}' .format (len (errors )))
251244 sys .exit (1 )
252245
0 commit comments