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 :
9283 if not afile [- 5 :] == '.dump' :
9384 continue
94- print ('Checking ' + afile + '...' )
85+ if not args .cli :
86+ print ('Checking ' + afile + '...' )
9587 data = cppcheckdata .CppcheckData (afile )
9688
9789 # Check File naming
9890 if "RE_FILE" in conf and conf ["RE_FILE" ]:
9991 mockToken = DataStruct (afile [:- 5 ], "0" , afile [afile .rfind ('/' ) + 1 :- 5 ])
10092 msgType = 'File name'
10193 for exp in conf ["RE_FILE" ]:
102- evalExpr (conf ["RE_FILE" ], exp , mockToken , msgType , errors )
94+ evalExpr (conf ["RE_FILE" ], exp , mockToken , msgType )
10395
10496 # Check Namespace naming
10597 if "RE_NAMESPACE" in conf and conf ["RE_NAMESPACE" ]:
@@ -108,10 +100,11 @@ def process(dumpfiles, configfile, debugprint=False):
108100 mockToken = DataStruct (tk .next .file , tk .next .linenr , tk .next .str )
109101 msgType = 'Namespace'
110102 for exp in conf ["RE_NAMESPACE" ]:
111- evalExpr (conf ["RE_NAMESPACE" ], exp , mockToken , msgType , errors )
103+ evalExpr (conf ["RE_NAMESPACE" ], exp , mockToken , msgType )
112104
113105 for cfg in data .configurations :
114- print ('Checking %s, config %s...' % (afile , cfg .name ))
106+ if not args .cli :
107+ print ('Checking %s, config %s...' % (afile , cfg .name ))
115108 if "RE_VARNAME" in conf and conf ["RE_VARNAME" ]:
116109 for var in cfg .variables :
117110 if var .nameToken and var .access != 'Global' and var .access != 'Public' and var .access != 'Private' :
@@ -134,18 +127,15 @@ def process(dumpfiles, configfile, debugprint=False):
134127 continue
135128 if varType in conf ["var_prefixes" ]:
136129 if not var .nameToken .str .startswith (conf ["var_prefixes" ][varType ]):
137- errors .append (reportError (
138- var .typeStartToken .file ,
139- var .typeStartToken .linenr ,
140- 'style' ,
130+ reportNamingError (var .typeStartToken ,
141131 'Variable ' +
142132 var .nameToken .str +
143- ' violates naming convention' ))
133+ ' violates naming convention' )
144134
145135 mockToken = DataStruct (var .typeStartToken .file , var .typeStartToken .linenr , var .nameToken .str )
146136 msgType = 'Variable'
147137 for exp in conf ["RE_VARNAME" ]:
148- evalExpr (conf ["RE_VARNAME" ], exp , mockToken , msgType , errors )
138+ evalExpr (conf ["RE_VARNAME" ], exp , mockToken , msgType )
149139
150140 # Check Private Variable naming
151141 if "RE_PRIVATE_MEMBER_VARIABLE" in conf and conf ["RE_PRIVATE_MEMBER_VARIABLE" ]:
@@ -156,7 +146,7 @@ def process(dumpfiles, configfile, debugprint=False):
156146 mockToken = DataStruct (var .typeStartToken .file , var .typeStartToken .linenr , var .nameToken .str )
157147 msgType = 'Private member variable'
158148 for exp in conf ["RE_PRIVATE_MEMBER_VARIABLE" ]:
159- evalExpr (conf ["RE_PRIVATE_MEMBER_VARIABLE" ], exp , mockToken , msgType , errors )
149+ evalExpr (conf ["RE_PRIVATE_MEMBER_VARIABLE" ], exp , mockToken , msgType )
160150
161151 # Check Public Member Variable naming
162152 if "RE_PUBLIC_MEMBER_VARIABLE" in conf and conf ["RE_PUBLIC_MEMBER_VARIABLE" ]:
@@ -166,7 +156,7 @@ def process(dumpfiles, configfile, debugprint=False):
166156 mockToken = DataStruct (var .typeStartToken .file , var .typeStartToken .linenr , var .nameToken .str )
167157 msgType = 'Public member variable'
168158 for exp in conf ["RE_PUBLIC_MEMBER_VARIABLE" ]:
169- evalExpr (conf ["RE_PUBLIC_MEMBER_VARIABLE" ], exp , mockToken , msgType , errors )
159+ evalExpr (conf ["RE_PUBLIC_MEMBER_VARIABLE" ], exp , mockToken , msgType )
170160
171161 # Check Global Variable naming
172162 if "RE_GLOBAL_VARNAME" in conf and conf ["RE_GLOBAL_VARNAME" ]:
@@ -176,7 +166,7 @@ def process(dumpfiles, configfile, debugprint=False):
176166 mockToken = DataStruct (var .typeStartToken .file , var .typeStartToken .linenr , var .nameToken .str )
177167 msgType = 'Public member variable'
178168 for exp in conf ["RE_GLOBAL_VARNAME" ]:
179- evalExpr (conf ["RE_GLOBAL_VARNAME" ], exp , mockToken , msgType , errors )
169+ evalExpr (conf ["RE_GLOBAL_VARNAME" ], exp , mockToken , msgType )
180170
181171 # Check Functions naming
182172 if "RE_FUNCTIONNAME" in conf and conf ["RE_FUNCTIONNAME" ]:
@@ -194,12 +184,11 @@ def process(dumpfiles, configfile, debugprint=False):
194184
195185 if retval and retval in conf ["function_prefixes" ]:
196186 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' ))
187+ reportNamingError (token , 'Function ' + token .function .name + ' violates naming convention' )
199188 mockToken = DataStruct (token .file , token .linenr , token .function .name )
200189 msgType = 'Function'
201190 for exp in conf ["RE_FUNCTIONNAME" ]:
202- evalExpr (conf ["RE_FUNCTIONNAME" ], exp , mockToken , msgType , errors )
191+ evalExpr (conf ["RE_FUNCTIONNAME" ], exp , mockToken , msgType )
203192
204193 # Check Class naming
205194 if "RE_CLASS_NAME" in conf and conf ["RE_CLASS_NAME" ]:
@@ -209,14 +198,10 @@ def process(dumpfiles, configfile, debugprint=False):
209198 mockToken = DataStruct (fnc .tokenDef .file , fnc .tokenDef .linenr , fnc .name )
210199 msgType = 'Class ' + fnc .type
211200 for exp in conf ["RE_CLASS_NAME" ]:
212- evalExpr (conf ["RE_CLASS_NAME" ], exp , mockToken , msgType , errors )
213- return errors
214-
201+ evalExpr (conf ["RE_CLASS_NAME" ], exp , mockToken , msgType )
215202
216203if __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' )
204+ parser = cppcheckdata .ArgumentParser ()
220205 parser .add_argument ("--debugprint" , action = "store_true" , default = False ,
221206 help = "Add debug prints" )
222207 parser .add_argument ("--configfile" , type = str , default = "naming.json" ,
@@ -225,28 +210,39 @@ def process(dumpfiles, configfile, debugprint=False):
225210 help = "verify this script. Must be executed in test folder !" )
226211
227212 args = parser .parse_args ()
228- errors = process (args .dumpfiles , args .configfile , args .debugprint )
213+ process (args .dumpfile , args .configfile , args .debugprint )
229214
230215 if args .verify :
231- print (errors )
232216 if len (errors ) < 6 :
233217 print ("Not enough errors found" )
234218 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 ' ]
219+ if args .cli :
220+ target = [
221+ '{"file": "namingng_test.c", "linenr": 8, "column": 5, "severity": "style", "message": "Variable badui32 violates naming convention", "addon": "namingng", "errorId": "namingConvention", "extra": ""}\n ' ,
222+ '{"file": "namingng_test.c", "linenr": 11, "column": 5, "severity": "style", "message": "Variable a violates naming convention", "addon": "namingng", "errorId": "namingConvention", "extra": ""}\n ' ,
223+ '{"file": "namingng_test.c", "linenr": 29, "column": 5, "severity": "style", "message": "Variable badui32 violates naming convention", "addon": "namingng", "errorId": "namingConvention", "extra": ""}\n ' ,
224+ '{"file": "namingng_test.c", "linenr": 20, "column": 0, "severity": "style", "message": "Function ui16bad_underscore violates naming convention", "addon": "namingng", "errorId": "namingConvention", "extra": ""}\n ' ,
225+ '{"file": "namingng_test.c", "linenr": 25, "column": 10, "severity": "style", "message": "Function u32Bad violates naming convention", "addon": "namingng", "errorId": "namingConvention", "extra": ""}\n ' ,
226+ '{"file": "namingng_test.c", "linenr": 37, "column": 10, "severity": "style", "message": "Function Badui16 violates naming convention", "addon": "namingng", "errorId": "namingConvention", "extra": ""}\n ' ,
227+ ]
228+ else :
229+ target = [
230+ '[namingng_test.c:8] (style) Variable badui32 violates naming convention [namingng-namingConvention]\n ' ,
231+ '[namingng_test.c:11] (style) Variable a violates naming convention [namingng-namingConvention]\n ' ,
232+ '[namingng_test.c:29] (style) Variable badui32 violates naming convention [namingng-namingConvention]\n ' ,
233+ '[namingng_test.c:20] (style) Function ui16bad_underscore violates naming convention [namingng-namingConvention]\n ' ,
234+ '[namingng_test.c:25] (style) Function u32Bad violates naming convention [namingng-namingConvention]\n ' ,
235+ '[namingng_test.c:37] (style) Function Badui16 violates naming convention [namingng-namingConvention]\n ' ,
236+ ]
242237 diff = set (errors ) - set (target )
243238 if len (diff ):
244239 print ("Not the right errors found {}" .format (str (diff )))
245240 sys .exit (1 )
246- print ("Verification done\n " )
241+ if not args .cli :
242+ print ("Verification done\n " )
247243 sys .exit (0 )
248244
249- if len (errors ):
245+ if len (errors ) and not args . cli :
250246 print ('Found errors: {}' .format (len (errors )))
251247 sys .exit (1 )
252248
0 commit comments