Skip to content

Commit d88dc3e

Browse files
committed
Reverted 00c54df (don't remove enum declarations) because it caused unexpected false positives
1 parent 00c54df commit d88dc3e

7 files changed

Lines changed: 169 additions & 118 deletions

lib/tokenize.cpp

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
}

test/testconstructors.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,7 @@ class TestConstructors : public TestFixture {
14231423
" ECODES _code;\n"
14241424
"};");
14251425

1426-
TODO_ASSERT_EQUALS("[test.cpp:10]: (warning) Member variable 'Fred::_code' is not initialized in the constructor.\n", "", errout.str());
1426+
ASSERT_EQUALS("[test.cpp:10]: (warning) Member variable 'Fred::_code' is not initialized in the constructor.\n", errout.str());
14271427

14281428

14291429
check("class A{};\n"

test/testsimplifytemplate.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,18 +1017,7 @@ class TestSimplifyTemplate : public TestFixture {
10171017
"{\n"
10181018
" enum {value = !type_equal<T, typename Unconst<T>::type>::value };\n"
10191019
"};";
1020-
const char expected1[] = "template < class T > struct Unconst { } ; "
1021-
"template < class T > struct type_equal<T,T> { enum { value = 1 } ; } ; "
1022-
"template < class T > struct template_is_const { enum { value = ! type_equal < T , Unconst < T > :: type > :: value } ; } ; "
1023-
"struct type_equal<T,T> { enum { value = 0 } ; } ; "
1024-
"struct Unconst<constT*const> { } ; "
1025-
"struct Unconst<constT&*const> { } ; "
1026-
"struct Unconst<T*const*const> { } ; "
1027-
"struct Unconst<T*const> { } ; "
1028-
"struct Unconst<T*const> { } ; "
1029-
"struct Unconst<T*const> { } ; "
1030-
"struct Unconst<constT&><};template<T> { } ; "
1031-
"struct Unconst<constT><};template<T> { } ;";
1020+
const char expected1[]="template < class T > struct Unconst { } ; template < class T > struct type_equal<T,T> { } ; template < class T > struct template_is_const { } ; struct type_equal<T,T> { } ; struct Unconst<constT*const> { } ; struct Unconst<constT&*const> { } ; struct Unconst<T*const*const> { } ; struct Unconst<T*const> { } ; struct Unconst<T*const> { } ; struct Unconst<T*const> { } ; struct Unconst<constT&><};template<T> { } ; struct Unconst<constT><};template<T> { } ;";
10321021
ASSERT_EQUALS(expected1, tok(code1));
10331022
}
10341023

0 commit comments

Comments
 (0)