@@ -7278,8 +7278,18 @@ void Tokenizer::simplifyEnum()
72787278{
72797279 std::string className;
72807280 int classLevel = 0 ;
7281+ bool goback = false ;
72817282 const bool printStyle = _settings->isEnabled (" style" );
72827283 for (Token *tok = list.front (); tok; tok = tok->next ()) {
7284+
7285+ if (goback) {
7286+ // jump back once, see the comment at the end of the function
7287+ goback = false ;
7288+ tok = tok->previous ();
7289+ if (!tok)
7290+ break ;
7291+ }
7292+
72837293 if (tok->next () &&
72847294 (!tok->previous () || (tok->previous ()->str () != " enum" )) &&
72857295 Token::Match (tok, " class|struct|namespace" )) {
@@ -7642,7 +7652,7 @@ void Tokenizer::simplifyEnum()
76427652 tempTok->insertToken (" ;" );
76437653 tempTok = tempTok->next ();
76447654 if (typeTokenStart == 0 )
7645- tempTok->insertToken (enumType ? enumType-> str () : std::string ( " int" ) );
7655+ tempTok->insertToken (" int" );
76467656 else {
76477657 Token *tempTok1 = typeTokenStart;
76487658
@@ -7656,6 +7666,83 @@ void Tokenizer::simplifyEnum()
76567666 }
76577667 }
76587668 }
7669+
7670+ if (enumType) {
7671+ const std::string pattern (className.empty () ? std::string (" " ) : (className + " :: " + enumType->str ()));
7672+
7673+ // count { and } for tok2
7674+ int level = 0 ;
7675+ bool inScope = true ;
7676+
7677+ bool exitThisScope = false ;
7678+ int exitScope = 0 ;
7679+ bool simplify = false ;
7680+ bool hasClass = false ;
7681+ for (Token *tok2 = end->next (); tok2; tok2 = tok2->next ()) {
7682+ if (tok2->str () == " }" ) {
7683+ --level;
7684+ if (level < 0 )
7685+ inScope = false ;
7686+
7687+ if (exitThisScope) {
7688+ if (level < exitScope)
7689+ exitThisScope = false ;
7690+ }
7691+ } else if (tok2->str () == " {" )
7692+ ++level;
7693+ else if (!pattern.empty () && ((tok2->str () == " enum" && Token::Match (tok2->next (), pattern.c_str ())) || Token::Match (tok2, pattern.c_str ()))) {
7694+ simplify = true ;
7695+ hasClass = true ;
7696+ } else if (inScope && !exitThisScope && (tok2->str () == enumType->str () || (tok2->str () == " enum" && tok2->next () && tok2->next ()->str () == enumType->str ()))) {
7697+ if (tok2->strAt (-1 ) == " ::" ) {
7698+ // Don't replace this enum if it's preceded by "::"
7699+ } else if (tok2->next () &&
7700+ (tok2->next ()->isName () || tok2->next ()->str () == " (" )) {
7701+ simplify = true ;
7702+ hasClass = false ;
7703+ } else if (tok2->previous ()->str () == " (" && tok2->next ()->str () == " )" ) {
7704+ simplify = true ;
7705+ hasClass = false ;
7706+ }
7707+ }
7708+
7709+ if (simplify) {
7710+ if (tok2->str () == " enum" )
7711+ tok2->deleteNext ();
7712+ if (typeTokenStart == 0 )
7713+ tok2->str (" int" );
7714+ else {
7715+ tok2->str (typeTokenStart->str ());
7716+ copyTokens (tok2, typeTokenStart->next (), typeTokenEnd);
7717+ }
7718+
7719+ if (hasClass) {
7720+ tok2->deleteNext (2 );
7721+ }
7722+
7723+ simplify = false ;
7724+ }
7725+ }
7726+ }
7727+
7728+ tok1 = start;
7729+ Token::eraseTokens (tok1, end->next ());
7730+ if (start != list.front ()) {
7731+ tok1 = start->previous ();
7732+ tok1->deleteNext ();
7733+ // no need to remove last token in the list
7734+ if (tok1->tokAt (2 ))
7735+ tok1->deleteNext ();
7736+ tok = tok1;
7737+ } else {
7738+ list.front ()->deleteThis ();
7739+ // no need to remove last token in the list
7740+ if (list.front ()->next ())
7741+ list.front ()->deleteThis ();
7742+ tok = list.front ();
7743+ // now the next token to process is 'tok', not 'tok->next()';
7744+ goback = true ;
7745+ }
76597746 }
76607747 }
76617748}
0 commit comments