@@ -7692,6 +7692,52 @@ static void addToErrorPath(ValueFlow::Value& value, const ValueFlow::Value& from
76927692 });
76937693}
76947694
7695+ static std::vector<Token*> findAllUsages (const Variable* var, Token* start)
7696+ {
7697+ std::vector<Token*> result;
7698+ const Scope* scope = var->scope ();
7699+ if (!scope)
7700+ return result;
7701+ Token* tok2 = Token::findmatch (start, " %varid%" , scope->bodyEnd , var->declarationId ());
7702+ while (tok2) {
7703+ result.push_back (tok2);
7704+ tok2 = Token::findmatch (tok2->next (), " %varid%" , scope->bodyEnd , var->declarationId ());
7705+ }
7706+ return result;
7707+ }
7708+
7709+ static Token* findStartToken (const Variable* var, Token* start)
7710+ {
7711+ std::vector<Token*> uses = findAllUsages (var, start);
7712+ if (uses.empty ())
7713+ return start;
7714+ Token* first = uses.front ();
7715+ if (Token::findmatch (start, " goto|asm|setjmp|longjmp" , first))
7716+ return start;
7717+ const Scope* scope = first->scope ();
7718+ // If there is only one usage or the first usage is in the same scope
7719+ if (uses.size () == 1 || scope == var->scope ())
7720+ return first->previous ();
7721+ // If all uses are in the same scope
7722+ if (std::all_of (uses.begin () + 1 , uses.end (), [&](const Token* tok) {
7723+ return tok->scope () == scope;
7724+ }))
7725+ return first->previous ();
7726+ // Compute the outer scope
7727+ while (scope && scope->nestedIn != var->scope ())
7728+ scope = scope->nestedIn ;
7729+ if (!scope)
7730+ return start;
7731+ Token* tok = const_cast <Token*>(scope->bodyStart );
7732+ if (!tok)
7733+ return start;
7734+ if (Token::simpleMatch (tok->tokAt (-2 ), " } else {" ))
7735+ tok = tok->linkAt (-2 );
7736+ if (Token::simpleMatch (tok->previous (), " ) {" ))
7737+ return tok->linkAt (-1 )->previous ();
7738+ return tok;
7739+ }
7740+
76957741static void valueFlowUninit (TokenList* tokenlist, SymbolDatabase* /* symbolDatabase*/ , const Settings* settings)
76967742{
76977743 for (Token *tok = tokenlist->front (); tok; tok = tok->next ()) {
@@ -7718,6 +7764,8 @@ static void valueFlowUninit(TokenList* tokenlist, SymbolDatabase* /*symbolDataba
77187764
77197765 bool partial = false ;
77207766
7767+ Token* start = findStartToken (var, tok->next ());
7768+
77217769 std::map<Token*, ValueFlow::Value> partialReads;
77227770 if (const Scope* scope = var->typeScope ()) {
77237771 if (Token::findsimplematch (scope->bodyStart , " union" , scope->bodyEnd ))
@@ -7733,7 +7781,7 @@ static void valueFlowUninit(TokenList* tokenlist, SymbolDatabase* /*symbolDataba
77337781 continue ;
77347782 }
77357783 MemberExpressionAnalyzer analyzer (memVar.nameToken ()->str (), tok, uninitValue, tokenlist, settings);
7736- valueFlowGenericForward (tok-> next () , tok->scope ()->bodyEnd , analyzer, *settings);
7784+ valueFlowGenericForward (start , tok->scope ()->bodyEnd , analyzer, *settings);
77377785
77387786 for (auto && p : *analyzer.partialReads ) {
77397787 Token* tok2 = p.first ;
@@ -7763,7 +7811,7 @@ static void valueFlowUninit(TokenList* tokenlist, SymbolDatabase* /*symbolDataba
77637811 if (partial)
77647812 continue ;
77657813
7766- valueFlowForward (tok-> next () , tok->scope ()->bodyEnd , var->nameToken (), uninitValue, tokenlist, settings);
7814+ valueFlowForward (start , tok->scope ()->bodyEnd , var->nameToken (), uninitValue, tokenlist, settings);
77677815 }
77687816}
77697817
0 commit comments