@@ -2359,6 +2359,37 @@ static void setVarIdStructMembers(Token **tok1,
23592359}
23602360
23612361
2362+ static const Token * findInitListEndToken (const Token *tok)
2363+ {
2364+ if (!Token::Match (tok, " ) :" ))
2365+ return nullptr ;
2366+
2367+ tok = tok->tokAt (2 );
2368+
2369+ while (tok) {
2370+ if (tok && tok->str ()==" ::" )
2371+ tok = tok->next ();
2372+
2373+ while (tok && Token::Match (tok, " %type% ::" ))
2374+ tok = tok->tokAt (2 );
2375+
2376+ if (Token::Match (tok, " %var% [({]" )) {
2377+ tok = tok->linkAt (1 );
2378+ if (!tok)
2379+ return nullptr ;
2380+ tok = tok->next ();
2381+ }
2382+ if (tok && tok->str ()==" ," )
2383+ tok = tok->next ();
2384+ else if (tok && tok->str ()==" {" )
2385+ return tok; // End of init list found
2386+ else
2387+ return nullptr ;
2388+ }
2389+
2390+ return nullptr ;
2391+ }
2392+
23622393static void setVarIdClassDeclaration (Token * const startToken,
23632394 const std::map<std::string, unsigned int > &variableId,
23642395 const unsigned int scopeStartVarId,
@@ -2381,14 +2412,15 @@ static void setVarIdClassDeclaration(Token * const startToken,
23812412
23822413 // replace varids..
23832414 unsigned int indentlevel = 0 ;
2384- bool initList = false ;
2415+ const Token * initListEndToken = nullptr ;
23852416 for (Token *tok = startToken->next (); tok != endToken; tok = tok->next ()) {
23862417 if (tok->str () == " {" ) {
2387- initList = false ;
2418+ if (tok == initListEndToken)
2419+ initListEndToken = nullptr ;
23882420 ++indentlevel;
23892421 } else if (tok->str () == " }" )
23902422 --indentlevel;
2391- else if (initList && indentlevel == 0 && Token::Match (tok->previous (), " [,:] %var% ( " )) {
2423+ else if (initListEndToken && indentlevel == 0 && Token::Match (tok->previous (), " [,:] %var% [({] " )) {
23922424 const std::map<std::string, unsigned int >::const_iterator it = variableId.find (tok->str ());
23932425 if (it != variableId.end ()) {
23942426 tok->varId (it->second );
@@ -2411,7 +2443,7 @@ static void setVarIdClassDeclaration(Token * const startToken,
24112443 }
24122444 }
24132445 } else if (indentlevel == 0 && tok->str () == " :" )
2414- initList = true ;
2446+ initListEndToken = findInitListEndToken (tok-> previous ()) ;
24152447 }
24162448}
24172449
@@ -2444,18 +2476,6 @@ static void setVarIdClassFunction(const std::string &classname,
24442476 }
24452477}
24462478
2447- static bool isInitList (const Token *tok)
2448- {
2449- if (!Token::Match (tok, " ) : %var% (" ))
2450- return false ;
2451-
2452- tok = tok->linkAt (3 );
2453- while (Token::Match (tok, " ) , %var% (" ))
2454- tok = tok->linkAt (3 );
2455-
2456- return Token::simpleMatch (tok, " ) {" );
2457- }
2458-
24592479void Tokenizer::setVarId ()
24602480{
24612481 // Clear all variable ids
@@ -2486,14 +2506,22 @@ void Tokenizer::setVarId()
24862506 executableScope.push (false );
24872507 std::stack<unsigned int > scopestartvarid; // varid when scope starts
24882508 scopestartvarid.push (0 );
2489- bool initlist = false ;
2509+ const Token * initListEndToken = nullptr ;
24902510 for (Token *tok = list.front (); tok; tok = tok->next ()) {
24912511
24922512 // scope info to handle shadow variables..
2493- if (!initlist && tok->str () == " (" &&
2494- (Token::simpleMatch (tok->link (), " ) {" ) || Token::Match (tok->link (), " ) %type% {" ) || isInitList (tok->link ()))) {
2513+ bool newScope = false ;
2514+ if (!initListEndToken && tok->str () == " (" ) {
2515+ if (Token::simpleMatch (tok->link (), " ) {" ) || Token::Match (tok->link (), " ) %type% {" ))
2516+ newScope = true ;
2517+ else {
2518+ initListEndToken = findInitListEndToken (tok->link ());
2519+ if (initListEndToken)
2520+ newScope = true ;
2521+ }
2522+ }
2523+ if (newScope) {
24952524 scopeInfo.push (variableId);
2496- initlist = Token::simpleMatch (tok->link (), " ) :" );
24972525
24982526 // function declarations
24992527 } else if (!executableScope.top () && tok->str () == " (" && Token::Match (tok->link (), " ) const| ;" )) {
@@ -2503,22 +2531,24 @@ void Tokenizer::setVarId()
25032531 scopeInfo.pop ();
25042532
25052533 } else if (tok->str () == " {" ) {
2506- initlist = false ;
25072534 // parse anonymous unions as part of the current scope
25082535 if (!(tok->strAt (-1 ) == " union" && Token::simpleMatch (tok->link (), " } ;" ))) {
25092536 scopestartvarid.push (_varId);
2510- if (tok->strAt (-1 ) == " )" || Token::Match (tok->tokAt (-2 ), " ) %type%" )) {
2537+ if (tok->strAt (-1 ) == " )" || Token::Match (tok->tokAt (-2 ), " ) %type%" ) ||
2538+ tok == initListEndToken) {
25112539 executableScope.push (true );
25122540 } else {
25132541 executableScope.push (tok->strAt (-1 ) == " else" );
25142542 scopeInfo.push (variableId);
25152543 }
25162544 }
2545+ if (tok == initListEndToken)
2546+ initListEndToken= nullptr ;
25172547 } else if (tok->str () == " }" ) {
25182548 // parse anonymous unions as part of the current scope
25192549 if (!(Token::simpleMatch (tok, " } ;" ) && tok->link () && Token::simpleMatch (tok->link ()->previous (), " union {" ))) {
25202550 // Set variable ids in class declaration..
2521- if (!isC () && !executableScope.top () && tok->link ()) {
2551+ if (!initListEndToken && ! isC () && !executableScope.top () && tok->link ()) {
25222552 setVarIdClassDeclaration (tok->link (),
25232553 variableId,
25242554 scopestartvarid.top (),
0 commit comments