@@ -1998,7 +1998,7 @@ static bool valueFlowForward(Token * const startToken,
19981998 ++indentlevel;
19991999 else if (indentlevel >= 0 && tok2->str () == " }" ) {
20002000 --indentlevel;
2001- if (indentlevel <= 0 && isReturnScope (tok2) && Token::Match (tok2->link ()->previous (), " else|) {" )) {
2001+ if (indentlevel <= 0 && isReturnScope (tok2, settings ) && Token::Match (tok2->link ()->previous (), " else|) {" )) {
20022002 const Token *condition = tok2->link ();
20032003 const bool iselse = Token::simpleMatch (condition->tokAt (-2 ), " } else {" );
20042004 if (iselse)
@@ -2039,7 +2039,7 @@ static bool valueFlowForward(Token * const startToken,
20392039 return true ;
20402040 } else if (indentlevel <= 0 &&
20412041 Token::simpleMatch (tok2->link ()->previous (), " else {" ) &&
2042- !isReturnScope (tok2->link ()->tokAt (-2 )) &&
2042+ !isReturnScope (tok2->link ()->tokAt (-2 ), settings ) &&
20432043 isVariableChanged (tok2->link (), tok2, varid, var->isGlobal (), settings, tokenlist->isCPP ())) {
20442044 changeKnownToPossible (values);
20452045 }
@@ -2149,9 +2149,7 @@ static bool valueFlowForward(Token * const startToken,
21492149 truevalues.push_back (v);
21502150 else if (!conditionIsFalse (condTok, programMemory))
21512151 truevalues.push_back (v);
2152- if (condAlwaysFalse)
2153- falsevalues.push_back (v);
2154- else if (conditionIsFalse (condTok, programMemory))
2152+ if (conditionIsFalse (condTok, programMemory))
21552153 falsevalues.push_back (v);
21562154 else if (!subFunction && !conditionIsTrue (condTok, programMemory))
21572155 falsevalues.push_back (v);
@@ -2179,7 +2177,7 @@ static bool valueFlowForward(Token * const startToken,
21792177 // goto '}'
21802178 tok2 = startToken1->link ();
21812179
2182- if (isReturnScope (tok2) || !vfresult) {
2180+ if (isReturnScope (tok2, settings ) || !vfresult) {
21832181 if (condAlwaysTrue)
21842182 return false ;
21852183 removeValues (values, truevalues);
@@ -2207,7 +2205,7 @@ static bool valueFlowForward(Token * const startToken,
22072205 // goto '}'
22082206 tok2 = startTokenElse->link ();
22092207
2210- if (isReturnScope (tok2) || !vfresult) {
2208+ if (isReturnScope (tok2, settings ) || !vfresult) {
22112209 if (condAlwaysFalse)
22122210 return false ;
22132211 removeValues (values, falsevalues);
@@ -2283,7 +2281,7 @@ static bool valueFlowForward(Token * const startToken,
22832281 }
22842282
22852283 // stop after conditional return scopes that are executed
2286- if (isReturnScope (end)) {
2284+ if (isReturnScope (end, settings )) {
22872285 std::list<ValueFlow::Value>::iterator it;
22882286 for (it = values.begin (); it != values.end ();) {
22892287 if (conditionIsTrue (tok2->next ()->astOperand2 (), getProgramMemory (tok2, varid, *it)))
@@ -3722,6 +3720,35 @@ static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldat
37223720 }
37233721}
37243722
3723+ static void valueFlowSetConditionToKnown (const Token* tok, std::list<ValueFlow::Value>& values, bool then)
3724+ {
3725+ if (values.size () != 1U )
3726+ return ;
3727+ if (values.front ().isKnown ())
3728+ return ;
3729+ if (then && !Token::Match (tok, " ==|!|(" ))
3730+ return ;
3731+ if (!then && !Token::Match (tok, " !=|%var%|(" ))
3732+ return ;
3733+ const char * op = " ||" ;
3734+ if (then)
3735+ op = " &&" ;
3736+ const Token* parent = tok->astParent ();
3737+ while (parent && parent->str () == op)
3738+ parent = parent->astParent ();
3739+ if (parent && parent->str () == " (" )
3740+ values.front ().setKnown ();
3741+ }
3742+
3743+ static bool isBreakScope (const Token* const endToken)
3744+ {
3745+ if (!Token::simpleMatch (endToken, " }" ))
3746+ return false ;
3747+ if (!Token::simpleMatch (endToken->link (), " {" ))
3748+ return false ;
3749+ return Token::findmatch (endToken->link (), " break|goto" , endToken);
3750+ }
3751+
37253752struct ValueFlowConditionHandler {
37263753 struct Condition {
37273754 const Token *vartok;
@@ -3835,6 +3862,10 @@ struct ValueFlowConditionHandler {
38353862 tok2 = parent;
38363863 }
38373864 }
3865+ if (cond.true_values != cond.false_values ) {
3866+ check_if = true ;
3867+ check_else = true ;
3868+ }
38383869
38393870 // determine startToken(s)
38403871 if (check_if && Token::simpleMatch (top->link (), " ) {" ))
@@ -3849,13 +3880,7 @@ struct ValueFlowConditionHandler {
38493880 if (!startToken)
38503881 continue ;
38513882 std::list<ValueFlow::Value> &values = (i == 0 ? cond.true_values : cond.false_values );
3852- if (values.size () == 1U && Token::Match (tok, " ==|!|(" )) {
3853- const Token *parent = tok->astParent ();
3854- while (parent && parent->str () == " &&" )
3855- parent = parent->astParent ();
3856- if (parent && parent->str () == " (" )
3857- values.front ().setKnown ();
3858- }
3883+ valueFlowSetConditionToKnown (tok, values, i == 0 );
38593884
38603885 bool changed = forward (startTokens[i], startTokens[i]->link (), var, values, true );
38613886 values.front ().setPossible ();
@@ -3867,7 +3892,6 @@ struct ValueFlowConditionHandler {
38673892 startTokens[i]->link (),
38683893 " valueFlowAfterCondition: " + var->name () + " is changed in conditional block" );
38693894 bail = true ;
3870- break ;
38713895 }
38723896 }
38733897 if (bail)
@@ -3883,7 +3907,9 @@ struct ValueFlowConditionHandler {
38833907 continue ;
38843908 }
38853909
3886- const bool dead_if = isReturnScope (after);
3910+ bool dead_if = isReturnScope (after, settings) ||
3911+ (tok->astParent () && Token::simpleMatch (tok->astParent ()->previous (), " while (" ) &&
3912+ !isBreakScope (after));
38873913 bool dead_else = false ;
38883914
38893915 if (Token::simpleMatch (after, " } else {" )) {
@@ -3893,7 +3919,7 @@ struct ValueFlowConditionHandler {
38933919 bailout (tokenlist, errorLogger, after, " possible noreturn scope" );
38943920 continue ;
38953921 }
3896- dead_else = isReturnScope (after);
3922+ dead_else = isReturnScope (after, settings );
38973923 }
38983924
38993925 std::list<ValueFlow::Value> *values = nullptr ;
@@ -3903,6 +3929,10 @@ struct ValueFlowConditionHandler {
39033929 values = &cond.false_values ;
39043930
39053931 if (values) {
3932+ if ((dead_if || dead_else) && !Token::Match (tok->astParent (), " &&|&" )) {
3933+ valueFlowSetConditionToKnown (tok, *values, true );
3934+ valueFlowSetConditionToKnown (tok, *values, false );
3935+ }
39063936 // TODO: constValue could be true if there are no assignments in the conditional blocks and
39073937 // perhaps if there are no && and no || in the condition
39083938 bool constValue = false ;
@@ -3980,14 +4010,24 @@ static void valueFlowAfterCondition(TokenList *tokenlist,
39804010 vartok = vartok->astOperand1 ();
39814011 if (!vartok->isName ())
39824012 return cond;
4013+ if (astIsPointer (vartok) && true_value.intvalue == 0 ) {
4014+ if (Token::simpleMatch (tok, " ==" ))
4015+ false_value.intvalue = 1 ;
4016+ if (Token::simpleMatch (tok, " !=" ))
4017+ true_value.intvalue = 1 ;
4018+ }
4019+
39834020 cond.true_values .push_back (true_value);
39844021 cond.false_values .push_back (false_value);
39854022 cond.vartok = vartok;
39864023 return cond;
39874024 }
39884025
4026+ long long falseIntValue = 0LL ;
39894027 if (tok->str () == " !" ) {
39904028 vartok = tok->astOperand1 ();
4029+ if (astIsPointer (vartok))
4030+ falseIntValue = 1LL ;
39914031
39924032 } else if (tok->isName () && (Token::Match (tok->astParent (), " %oror%|&&" ) ||
39934033 Token::Match (tok->tokAt (-2 ), " if|while ( %var% [)=]" ))) {
@@ -3997,7 +4037,7 @@ static void valueFlowAfterCondition(TokenList *tokenlist,
39974037 if (!vartok || !vartok->isName ())
39984038 return cond;
39994039 cond.true_values .emplace_back (tok, 0LL );
4000- cond.false_values .emplace_back (tok, 0LL );
4040+ cond.false_values .emplace_back (tok, falseIntValue );
40014041 cond.vartok = vartok;
40024042
40034043 return cond;
0 commit comments