Refactorized code in simplifyEnum()

This commit is contained in:
PKEuS 2015-03-14 18:24:37 +01:00
parent 0efa113b6e
commit 430d8ca760
1 changed files with 91 additions and 111 deletions

View File

@ -7586,9 +7586,9 @@ void Tokenizer::simplifyEnum()
className = tok->next()->str(); className = tok->next()->str();
classLevel = 0; classLevel = 0;
} else if (tok->str() == "}") { } else if (tok->str() == "}") {
--classLevel; if (classLevel == 0)
if (classLevel < 0)
className = ""; className = "";
--classLevel;
} else if (tok->str() == "{") { } else if (tok->str() == "{") {
++classLevel; ++classLevel;
} else if (tok->str() == "enum") { } else if (tok->str() == "enum") {
@ -7679,7 +7679,8 @@ void Tokenizer::simplifyEnum()
Token * enumValueStart = 0; Token * enumValueStart = 0;
Token * enumValueEnd = 0; Token * enumValueEnd = 0;
if (Token::Match(tok1->previous(), ",|{ %type% ,|}")) { if (Token::Match(tok1->previous(), ",|{ %type%")) {
if (Token::Match(tok1->next(), ",|}")) {
// no value specified // no value specified
enumName = tok1; enumName = tok1;
++lastValue; ++lastValue;
@ -7703,14 +7704,14 @@ void Tokenizer::simplifyEnum()
tok1->insertToken(MathLib::toString(lastValue)); tok1->insertToken(MathLib::toString(lastValue));
enumValue = tok1->next(); enumValue = tok1->next();
} }
} else if (Token::Match(tok1->previous(), ",|{ %type% = %num% ,|}")) { } else if (Token::Match(tok1->next(), "= %num% ,|}")) {
// value is specified numeric value // value is specified numeric value
enumName = tok1; enumName = tok1;
lastValue = MathLib::toLongNumber(tok1->strAt(2)); lastValue = MathLib::toLongNumber(tok1->strAt(2));
enumValue = tok1->tokAt(2); enumValue = tok1->tokAt(2);
lastEnumValueStart = 0; lastEnumValueStart = 0;
lastEnumValueEnd = 0; lastEnumValueEnd = 0;
} else if (Token::Match(tok1->previous(), ",|{ %type% =")) { } else if (tok1->strAt(1) == "=") {
// value is specified expression // value is specified expression
enumName = tok1; enumName = tok1;
lastValue = 0; lastValue = 0;
@ -7726,7 +7727,7 @@ void Tokenizer::simplifyEnum()
if (Token::Match(enumValueEnd, "(|[")) { if (Token::Match(enumValueEnd, "(|[")) {
enumValueEnd = enumValueEnd->link(); enumValueEnd = enumValueEnd->link();
continue; continue;
} else if (Token::Match(enumValueEnd, "%type% <") && isCPP() && TemplateSimplifier::templateParameters(enumValueEnd->next()) > 1U) { } else if (isCPP() && Token::Match(enumValueEnd, "%type% <") && TemplateSimplifier::templateParameters(enumValueEnd->next()) > 1U) {
Token *endtoken = enumValueEnd->next(); Token *endtoken = enumValueEnd->next();
do { do {
endtoken = endtoken->next(); endtoken = endtoken->next();
@ -7753,6 +7754,7 @@ void Tokenizer::simplifyEnum()
// skip over expression // skip over expression
tok1 = enumValueEnd; tok1 = enumValueEnd;
} }
}
// add enumerator constant.. // add enumerator constant..
if (enumName && (enumValue || (enumValueStart && enumValueEnd))) { if (enumName && (enumValue || (enumValueStart && enumValueEnd))) {
@ -7814,15 +7816,11 @@ void Tokenizer::simplifyEnum()
// Not a duplicate enum.. // Not a duplicate enum..
++level; ++level;
// Create a copy of the shadow ids for the inner scope std::set<std::string> shadowVars = shadowId.empty() ? std::set<std::string>() : shadowId.top();
if (!shadowId.empty())
shadowId.push(shadowId.top());
// are there shadow arguments? // are there shadow arguments?
if (Token::simpleMatch(tok2->previous(), ") {") || Token::simpleMatch(tok2->tokAt(-2), ") const {")) { if (Token::simpleMatch(tok2->previous(), ") {") || Token::simpleMatch(tok2->tokAt(-2), ") const {")) {
std::set<std::string> shadowArg; for (const Token* arg = tok2->previous(); arg && arg->str() != "("; arg = arg->previous()) {
for (const Token* arg = tok2; arg && arg->str() != "("; arg = arg->previous()) { if (Token::Match(arg->previous(), "%type%|*|& %type% [,)=]") &&
if (Token::Match(arg->previous(), "%type%|*|& %type% [,)]") &&
enumValues.find(arg->str()) != enumValues.end()) { enumValues.find(arg->str()) != enumValues.end()) {
// is this a variable declaration // is this a variable declaration
const Token *prev = arg; const Token *prev = arg;
@ -7832,23 +7830,16 @@ void Tokenizer::simplifyEnum()
continue; continue;
if (prev->str() == "(" && (!Token::Match(prev->tokAt(-2), "%type%|::|*|& %type% (") || prev->strAt(-2) == "else")) if (prev->str() == "(" && (!Token::Match(prev->tokAt(-2), "%type%|::|*|& %type% (") || prev->strAt(-2) == "else"))
continue; continue;
shadowArg.insert(arg->str()); shadowVars.insert(arg->str());
if (inScope && _settings->isEnabled("style")) { if (inScope && _settings->isEnabled("style")) {
const EnumValue enumValue = enumValues.find(arg->str())->second; const EnumValue& enumValue = enumValues.find(arg->str())->second;
duplicateEnumError(arg, enumValue.name, "Function argument"); duplicateEnumError(arg, enumValue.name, "Function argument");
} }
} }
} }
if (!shadowArg.empty()) {
if (shadowId.empty())
shadowId.push(shadowArg);
else
shadowId.top().insert(shadowArg.begin(), shadowArg.end());
}
} }
// are there shadow variables in the scope? // are there shadow variables in the scope?
std::set<std::string> shadowVars;
for (const Token *tok3 = tok2->next(); tok3 && tok3->str() != "}"; tok3 = tok3->next()) { for (const Token *tok3 = tok2->next(); tok3 && tok3->str() != "}"; tok3 = tok3->next()) {
if (tok3->str() == "{") { if (tok3->str() == "{") {
tok3 = tok3->link(); // skip inner scopes tok3 = tok3->link(); // skip inner scopes
@ -7861,18 +7852,14 @@ void Tokenizer::simplifyEnum()
// variable declaration? // variable declaration?
shadowVars.insert(tok3->str()); shadowVars.insert(tok3->str());
if (inScope && _settings->isEnabled("style")) { if (inScope && _settings->isEnabled("style")) {
const EnumValue enumValue = enumValues.find(tok3->str())->second; const EnumValue& enumValue = enumValues.find(tok3->str())->second;
duplicateEnumError(tok3, enumValue.name, "Variable"); duplicateEnumError(tok3, enumValue.name, "Variable");
} }
} }
} }
} }
if (!shadowVars.empty()) {
if (shadowId.empty())
shadowId.push(shadowVars); shadowId.push(shadowVars);
else
shadowId.top().insert(shadowVars.begin(), shadowVars.end());
}
} }
// Function head // Function head
@ -7891,9 +7878,10 @@ void Tokenizer::simplifyEnum()
const Token* tok3 = tok2; const Token* tok3 = tok2;
while (tok3->strAt(1) == "::") while (tok3->strAt(1) == "::")
tok3 = tok3->tokAt(2); tok3 = tok3->tokAt(2);
if (enumValues.find(tok3->str()) != enumValues.end()) { std::map<std::string, EnumValue>::iterator it = enumValues.find(tok3->str());
if (it != enumValues.end()) {
simplify = true; simplify = true;
ev = &(enumValues.find(tok3->str())->second); ev = &(it->second);
} }
} else if (inScope && // enum is in scope } else if (inScope && // enum is in scope
(shadowId.empty() || shadowId.top().find(tok2->str()) == shadowId.top().end()) && // no shadow enum/var/etc of enum (shadowId.empty() || shadowId.top().find(tok2->str()) == shadowId.top().end()) && // no shadow enum/var/etc of enum
@ -7928,13 +7916,13 @@ void Tokenizer::simplifyEnum()
tok2 = tok2->previous(); tok2 = tok2->previous();
tok2->deleteNext(); tok2->deleteNext();
bool hasOp = false; bool hasOp = false;
int indentlevel = 0;
for (const Token *enumtok = ev->start; enumtok != ev->end; enumtok = enumtok->next()) { for (const Token *enumtok = ev->start; enumtok != ev->end; enumtok = enumtok->next()) {
if (enumtok->str() == "(") if (enumtok->str() == "(") {
++indentlevel; enumtok = enumtok->link();
else if (enumtok->str() == ")") if (enumtok == ev->end)
--indentlevel; break;
if (indentlevel == 0 && enumtok->isOp()) { }
if (enumtok->isOp()) {
hasOp = true; hasOp = true;
break; break;
} }
@ -8023,16 +8011,8 @@ void Tokenizer::simplifyEnum()
if (typeTokenStart == 0) if (typeTokenStart == 0)
tok2->str("int"); tok2->str("int");
else { else {
Token *tok3 = typeTokenStart; tok2->str(typeTokenStart->str());
copyTokens(tok2, typeTokenStart->next(), typeTokenEnd);
tok2->str(tok3->str());
while (tok3 != typeTokenEnd) {
tok3 = tok3->next();
tok2->insertToken(tok3->str());
tok2 = tok2->next();
}
} }
if (hasClass) { if (hasClass) {