@@ -897,6 +897,14 @@ def isConstantExpression(expr):
897897 return False
898898 return True
899899
900+ def isUnknownConstantExpression (expr ):
901+ if expr .isName and not isEnumConstant (expr ) and expr .variable is None :
902+ return True
903+ if expr .astOperand1 and isUnknownConstantExpression (expr .astOperand1 ):
904+ return True
905+ if expr .astOperand2 and isUnknownConstantExpression (expr .astOperand2 ):
906+ return True
907+ return False
900908
901909def isUnsignedInt (expr ):
902910 return expr and expr .valueType and expr .valueType .type in ('short' , 'int' ) and expr .valueType .sign == 'unsigned'
@@ -3258,6 +3266,32 @@ def misra_17_3(self, cfg):
32583266 tok = tok .next
32593267
32603268 def misra_config (self , data ):
3269+ for var in data .variables :
3270+ if not var .isArray or var .nameToken is None or not cppcheckdata .simpleMatch (var .nameToken .next , '[' ):
3271+ continue
3272+ tok = var .nameToken .next
3273+ while tok .str == '[' :
3274+ sz = tok .astOperand2
3275+ if sz and sz .getKnownIntValue () is None :
3276+ has_var = False
3277+ unknown_constant = False
3278+ tokens = [sz ]
3279+ while len (tokens ) > 0 :
3280+ t = tokens [- 1 ]
3281+ tokens = tokens [:- 1 ]
3282+ if t :
3283+ if t .isName and t .getKnownIntValue () is None :
3284+ if t .varId or t .variable :
3285+ has_var = True
3286+ continue
3287+ unknown_constant = True
3288+ self .report_config_error (tok , 'Unknown constant {}, please review configuration' .format (t .str ))
3289+ if t .isArithmeticalOp :
3290+ tokens += [t .astOperand1 , t .astOperand2 ]
3291+ if not unknown_constant and not has_var :
3292+ self .report_config_error (tok , 'Unknown array size, please review configuration' )
3293+ tok = tok .link .next
3294+
32613295 for token in data .tokenlist :
32623296 if token .str not in ("while" , "if" ):
32633297 continue
@@ -3367,7 +3401,7 @@ def misra_18_8(self, data):
33673401 # Unknown define or syntax error
33683402 if not typetok .astOperand2 :
33693403 continue
3370- if not isConstantExpression (typetok .astOperand2 ):
3404+ if not isConstantExpression (typetok .astOperand2 ) and not isUnknownConstantExpression ( typetok . astOperand2 ) :
33713405 self .reportError (var .nameToken , 18 , 8 )
33723406
33733407 def misra_19_2 (self , data ):
0 commit comments