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,79 +7679,81 @@ 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%")) {
// no value specified if (Token::Match(tok1->next(), ",|}")) {
enumName = tok1; // no value specified
++lastValue; enumName = tok1;
tok1->insertToken("="); ++lastValue;
tok1 = tok1->next(); tok1->insertToken("=");
if (lastEnumValueStart && lastEnumValueEnd) {
// previous value was an expression
Token *valueStart = tok1;
tok1 = copyTokens(tok1, lastEnumValueStart, lastEnumValueEnd);
// value is previous expression + 1
tok1->insertToken("+");
tok1 = tok1->next(); tok1 = tok1->next();
tok1->insertToken("1");
enumValue = 0;
enumValueStart = valueStart->next();
enumValueEnd = tok1->next();
} else {
// value is previous numeric value + 1
tok1->insertToken(MathLib::toString(lastValue));
enumValue = tok1->next();
}
} else if (Token::Match(tok1->previous(), ",|{ %type% = %num% ,|}")) {
// value is specified numeric value
enumName = tok1;
lastValue = MathLib::toLongNumber(tok1->strAt(2));
enumValue = tok1->tokAt(2);
lastEnumValueStart = 0;
lastEnumValueEnd = 0;
} else if (Token::Match(tok1->previous(), ",|{ %type% =")) {
// value is specified expression
enumName = tok1;
lastValue = 0;
tok1 = tok1->tokAt(2);
if (Token::Match(tok1, ",|{|}")) {
syntaxError(tok1);
break;
}
enumValueStart = tok1; if (lastEnumValueStart && lastEnumValueEnd) {
enumValueEnd = tok1; // previous value was an expression
while (enumValueEnd->next() && (!Token::Match(enumValueEnd->next(), "[},]"))) { Token *valueStart = tok1;
if (Token::Match(enumValueEnd, "(|[")) { tok1 = copyTokens(tok1, lastEnumValueStart, lastEnumValueEnd);
enumValueEnd = enumValueEnd->link();
continue; // value is previous expression + 1
} else if (Token::Match(enumValueEnd, "%type% <") && isCPP() && TemplateSimplifier::templateParameters(enumValueEnd->next()) > 1U) { tok1->insertToken("+");
Token *endtoken = enumValueEnd->next(); tok1 = tok1->next();
do { tok1->insertToken("1");
endtoken = endtoken->next(); enumValue = 0;
if (Token::Match(endtoken, "*|,|::|typename")) enumValueStart = valueStart->next();
endtoken = endtoken->next(); enumValueEnd = tok1->next();
if (endtoken->str() == "<" && TemplateSimplifier::templateParameters(endtoken)) } else {
endtoken = endtoken->findClosingBracket(); // value is previous numeric value + 1
} while (Token::Match(endtoken, "%name%|%num% *| [,>]") || Token::Match(endtoken, "%name%|%num% ::|< %any%")); tok1->insertToken(MathLib::toString(lastValue));
if (endtoken->str() == ">") { enumValue = tok1->next();
enumValueEnd = endtoken; }
if (Token::simpleMatch(endtoken, "> ( )")) } else if (Token::Match(tok1->next(), "= %num% ,|}")) {
enumValueEnd = enumValueEnd->next(); // value is specified numeric value
} else { enumName = tok1;
syntaxError(enumValueEnd); lastValue = MathLib::toLongNumber(tok1->strAt(2));
return; enumValue = tok1->tokAt(2);
} lastEnumValueStart = 0;
lastEnumValueEnd = 0;
} else if (tok1->strAt(1) == "=") {
// value is specified expression
enumName = tok1;
lastValue = 0;
tok1 = tok1->tokAt(2);
if (Token::Match(tok1, ",|{|}")) {
syntaxError(tok1);
break;
} }
enumValueEnd = enumValueEnd->next(); enumValueStart = tok1;
enumValueEnd = tok1;
while (enumValueEnd->next() && (!Token::Match(enumValueEnd->next(), "[},]"))) {
if (Token::Match(enumValueEnd, "(|[")) {
enumValueEnd = enumValueEnd->link();
continue;
} else if (isCPP() && Token::Match(enumValueEnd, "%type% <") && TemplateSimplifier::templateParameters(enumValueEnd->next()) > 1U) {
Token *endtoken = enumValueEnd->next();
do {
endtoken = endtoken->next();
if (Token::Match(endtoken, "*|,|::|typename"))
endtoken = endtoken->next();
if (endtoken->str() == "<" && TemplateSimplifier::templateParameters(endtoken))
endtoken = endtoken->findClosingBracket();
} while (Token::Match(endtoken, "%name%|%num% *| [,>]") || Token::Match(endtoken, "%name%|%num% ::|< %any%"));
if (endtoken->str() == ">") {
enumValueEnd = endtoken;
if (Token::simpleMatch(endtoken, "> ( )"))
enumValueEnd = enumValueEnd->next();
} else {
syntaxError(enumValueEnd);
return;
}
}
enumValueEnd = enumValueEnd->next();
}
// remember this expression in case it needs to be incremented
lastEnumValueStart = enumValueStart;
lastEnumValueEnd = enumValueEnd;
// skip over expression
tok1 = enumValueEnd;
} }
// remember this expression in case it needs to be incremented
lastEnumValueStart = enumValueStart;
lastEnumValueEnd = enumValueEnd;
// skip over expression
tok1 = enumValueEnd;
} }
// add enumerator constant.. // add enumerator constant..
@ -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) {