@@ -59,7 +59,7 @@ bool astIsFloat(const Token *tok, bool unknown)
5959 return unknown;
6060}
6161
62- static bool isConstExpression (const Token *tok, const std::set<std::string> &constFunctions)
62+ bool isConstExpression (const Token *tok, const std::set<std::string> &constFunctions)
6363{
6464 if (!tok)
6565 return true ;
@@ -77,21 +77,23 @@ static bool isConstExpression(const Token *tok, const std::set<std::string> &con
7777 return isConstExpression (tok->astOperand1 (),constFunctions) && isConstExpression (tok->astOperand2 (),constFunctions);
7878}
7979
80- bool isSameExpression (const Token *tok1, const Token *tok2, const std::set<std::string> &constFunctions)
80+ bool isSameExpression (const Tokenizer *tokenizer, const Token *tok1, const Token *tok2, const std::set<std::string> &constFunctions)
8181{
8282 if (tok1 == nullptr && tok2 == nullptr )
8383 return true ;
8484 if (tok1 == nullptr || tok2 == nullptr )
8585 return false ;
86- if (tok1->str () == " ." && tok1->astOperand1 () && tok1->astOperand1 ()->str () == " this" )
87- tok1 = tok1->astOperand2 ();
88- if (tok2->str () == " ." && tok2->astOperand1 () && tok2->astOperand1 ()->str () == " this" )
89- tok2 = tok2->astOperand2 ();
86+ if (tokenizer->isCPP ()) {
87+ if (tok1->str () == " ." && tok1->astOperand1 () && tok1->astOperand1 ()->str () == " this" )
88+ tok1 = tok1->astOperand2 ();
89+ if (tok2->str () == " ." && tok2->astOperand1 () && tok2->astOperand1 ()->str () == " this" )
90+ tok2 = tok2->astOperand2 ();
91+ }
9092 if (tok1->varId () != tok2->varId () || tok1->str () != tok2->str ()) {
9193 if ((Token::Match (tok1," <|>" ) && Token::Match (tok2," <|>" )) ||
9294 (Token::Match (tok1," <=|>=" ) && Token::Match (tok2," <=|>=" ))) {
93- return isSameExpression (tok1->astOperand1 (), tok2->astOperand2 (), constFunctions) &&
94- isSameExpression (tok1->astOperand2 (), tok2->astOperand1 (), constFunctions);
95+ return isSameExpression (tokenizer, tok1->astOperand1 (), tok2->astOperand2 (), constFunctions) &&
96+ isSameExpression (tokenizer, tok1->astOperand2 (), tok2->astOperand1 (), constFunctions);
9597 }
9698 return false ;
9799 }
@@ -145,18 +147,18 @@ bool isSameExpression(const Token *tok1, const Token *tok2, const std::set<std::
145147 return false ;
146148 }
147149 bool noncommuative_equals =
148- isSameExpression (tok1->astOperand1 (), tok2->astOperand1 (), constFunctions);
150+ isSameExpression (tokenizer, tok1->astOperand1 (), tok2->astOperand1 (), constFunctions);
149151 noncommuative_equals = noncommuative_equals &&
150- isSameExpression (tok1->astOperand2 (), tok2->astOperand2 (), constFunctions);
152+ isSameExpression (tokenizer, tok1->astOperand2 (), tok2->astOperand2 (), constFunctions);
151153
152154 if (noncommuative_equals)
153155 return true ;
154156
155157 const bool commutative = tok1->astOperand1 () && tok1->astOperand2 () && Token::Match (tok1, " %or%|%oror%|+|*|&|&&|^|==|!=" );
156158 bool commuative_equals = commutative &&
157- isSameExpression (tok1->astOperand2 (), tok2->astOperand1 (), constFunctions);
159+ isSameExpression (tokenizer, tok1->astOperand2 (), tok2->astOperand1 (), constFunctions);
158160 commuative_equals = commuative_equals &&
159- isSameExpression (tok1->astOperand1 (), tok2->astOperand2 (), constFunctions);
161+ isSameExpression (tokenizer, tok1->astOperand1 (), tok2->astOperand2 (), constFunctions);
160162
161163 return commuative_equals;
162164}
@@ -195,13 +197,13 @@ void CheckOther::checkCastIntToCharAndBack()
195197 if (var && var->typeEndToken ()->str () == " char" && !var->typeEndToken ()->isSigned ()) {
196198 checkCastIntToCharAndBackError (tok, tok->strAt (2 ));
197199 }
198- } else if (Token::Match (tok, " EOF %comp% ( %var% = std :: cin . get (" ) || Token::Match (tok, " EOF %comp% ( %var% = cin . get (" )) {
200+ } else if (_tokenizer-> isCPP () && Token::Match (tok, " EOF %comp% ( %var% = std :: cin . get (" ) || Token::Match (tok, " EOF %comp% ( %var% = cin . get (" )) {
199201 tok = tok->tokAt (3 );
200202 const Variable *var = tok->variable ();
201203 if (var && var->typeEndToken ()->str () == " char" && !var->typeEndToken ()->isSigned ()) {
202204 checkCastIntToCharAndBackError (tok, " cin.get" );
203205 }
204- } else if (Token::Match (tok, " %var% = std :: cin . get (" ) || Token::Match (tok, " %var% = cin . get (" )) {
206+ } else if (_tokenizer-> isCPP () && Token::Match (tok, " %var% = std :: cin . get (" ) || Token::Match (tok, " %var% = cin . get (" )) {
205207 const Variable *var = tok->variable ();
206208 if (var && var->typeEndToken ()->str () == " char" && !var->typeEndToken ()->isSigned ()) {
207209 vars[tok->varId ()] = " cin.get" ;
@@ -2181,7 +2183,7 @@ namespace {
21812183 }
21822184}
21832185
2184- static bool isWithoutSideEffects (const Tokenizer *tokenizer, const Token* tok)
2186+ bool isWithoutSideEffects (const Tokenizer *tokenizer, const Token* tok)
21852187{
21862188 if (!tokenizer->isCPP ())
21872189 return true ;
@@ -2217,7 +2219,7 @@ void CheckOther::checkDuplicateExpression()
22172219 if (tok->isOp () && tok->astOperand1 () && !Token::Match (tok, " +|*|<<|>>" )) {
22182220 if (Token::Match (tok, " ==|!=|-" ) && astIsFloat (tok->astOperand1 (), true ))
22192221 continue ;
2220- if (isSameExpression (tok->astOperand1 (), tok->astOperand2 (), _settings->library .functionpure )) {
2222+ if (isSameExpression (_tokenizer, tok->astOperand1 (), tok->astOperand2 (), _settings->library .functionpure )) {
22212223 if (isWithoutSideEffects (_tokenizer, tok->astOperand1 ())) {
22222224 const bool assignment = tok->str () == " =" ;
22232225 if (assignment)
@@ -2236,16 +2238,16 @@ void CheckOther::checkDuplicateExpression()
22362238 }
22372239 }
22382240 } else if (!Token::Match (tok, " [-/%]" )) { // These operators are not associative
2239- if (tok->astOperand2 () && tok->str () == tok->astOperand1 ()->str () && isSameExpression (tok->astOperand2 (), tok->astOperand1 ()->astOperand2 (), _settings->library .functionpure ) && isWithoutSideEffects (_tokenizer, tok->astOperand2 ()))
2241+ if (tok->astOperand2 () && tok->str () == tok->astOperand1 ()->str () && isSameExpression (_tokenizer, tok->astOperand2 (), tok->astOperand1 ()->astOperand2 (), _settings->library .functionpure ) && isWithoutSideEffects (_tokenizer, tok->astOperand2 ()))
22402242 duplicateExpressionError (tok->astOperand2 (), tok->astOperand2 (), tok->str ());
22412243 else if (tok->astOperand2 ()) {
22422244 const Token *ast1 = tok->astOperand1 ();
22432245 while (ast1 && tok->str () == ast1->str ()) {
2244- if (isSameExpression (ast1->astOperand1 (), tok->astOperand2 (), _settings->library .functionpure ) && isWithoutSideEffects (_tokenizer, ast1->astOperand1 ()))
2246+ if (isSameExpression (_tokenizer, ast1->astOperand1 (), tok->astOperand2 (), _settings->library .functionpure ) && isWithoutSideEffects (_tokenizer, ast1->astOperand1 ()))
22452247 // TODO: warn if variables are unchanged. See #5683
22462248 // Probably the message should be changed to 'duplicate expressions X in condition or something like that'.
22472249 ;// duplicateExpressionError(ast1->astOperand1(), tok->astOperand2(), tok->str());
2248- else if (isSameExpression (ast1->astOperand2 (), tok->astOperand2 (), _settings->library .functionpure ) && isWithoutSideEffects (_tokenizer, ast1->astOperand2 ()))
2250+ else if (isSameExpression (_tokenizer, ast1->astOperand2 (), tok->astOperand2 (), _settings->library .functionpure ) && isWithoutSideEffects (_tokenizer, ast1->astOperand2 ()))
22492251 duplicateExpressionError (ast1->astOperand2 (), tok->astOperand2 (), tok->str ());
22502252 if (!isConstExpression (ast1->astOperand2 (), _settings->library .functionpure ))
22512253 break ;
@@ -2254,7 +2256,7 @@ void CheckOther::checkDuplicateExpression()
22542256 }
22552257 }
22562258 } else if (tok->astOperand1 () && tok->astOperand2 () && tok->str () == " :" && tok->astParent () && tok->astParent ()->str () == " ?" ) {
2257- if (isSameExpression (tok->astOperand1 (), tok->astOperand2 (), temp))
2259+ if (isSameExpression (_tokenizer, tok->astOperand1 (), tok->astOperand2 (), temp))
22582260 duplicateExpressionTernaryError (tok);
22592261 }
22602262 }
0 commit comments