@@ -3382,15 +3382,22 @@ bool Tokenizer::simplifyTokenList()
33823382
33833383 simplifyUndefinedSizeArray ();
33843384
3385+ simplifyCasts ();
3386+
3387+ // Simplify simple calculations before replace constants, this allows the replacement of constants that are calculated
3388+ // e.g. const static int value = sizeof(X)/sizeof(Y);
3389+ simplifyCalculations ();
3390+
33853391 // Replace constants..
33863392 for (Token *tok = list.front (); tok; tok = tok->next ()) {
3387- if (Token::Match (tok, " const static| %type% %var% = %num% ;" )) {
3393+ if (Token::Match (tok, " const static| %type% %var% = %num% ;" ) ||
3394+ Token::Match (tok, " const static| %type% %var% ( %num% ) ;" )) {
33883395 unsigned int offset = 0 ;
33893396 if (tok->strAt (1 ) == " static" )
33903397 offset = 1 ;
33913398 const unsigned int varId (tok->tokAt (2 + offset)->varId ());
33923399 if (varId == 0 ) {
3393- tok = tok->tokAt (5 );
3400+ tok = tok->tokAt (5 + offset );
33943401 continue ;
33953402 }
33963403
@@ -3413,8 +3420,6 @@ bool Tokenizer::simplifyTokenList()
34133420 }
34143421 }
34153422
3416- simplifyCasts ();
3417-
34183423 // Simplify simple calculations..
34193424 simplifyCalculations ();
34203425
@@ -5937,9 +5942,10 @@ bool Tokenizer::simplifyKnownVariables()
59375942 tok = tok->previous ();
59385943 goback = false ;
59395944 }
5940- if (tok->isName () && Token::Match (tok, " static| const| static| %type% const| %var% = %any% ;" )) {
5945+ if (tok->isName () && (Token::Match (tok, " static| const| static| %type% const| %var% = %any% ;" ) ||
5946+ Token::Match (tok, " static| const| static| %type% const| %var% ( %any% ) ;" ))) {
59415947 bool isconst = false ;
5942- for (const Token *tok2 = tok; tok2->str () != " =" ; tok2 = tok2->next ()) {
5948+ for (const Token *tok2 = tok; ( tok2->str () != " =" ) && (tok2-> str () != " ( " ) ; tok2 = tok2->next ()) {
59435949 if (tok2->str () == " const" ) {
59445950 isconst = true ;
59455951 break ;
@@ -5962,7 +5968,7 @@ bool Tokenizer::simplifyKnownVariables()
59625968
59635969 const Token * const vartok = (tok->next () && tok->next ()->str () == " const" ) ? tok->tokAt (2 ) : tok->next ();
59645970 const Token * const valuetok = vartok->tokAt (2 );
5965- if (Token::Match (valuetok, " %bool%|%char%|%num%|%str% ;" )) {
5971+ if (Token::Match (valuetok, " %bool%|%char%|%num%|%str% )| ;" )) {
59665972 // check if there's not a reference usage inside the code
59675973 bool withreference = false ;
59685974 for (const Token *tok2 = valuetok->tokAt (2 ); tok2; tok2 = tok2->next ()) {
0 commit comments