Refactoring Tokenizer::simplifyEnum. The enum handling is now somewhat faster since all enum values of a enum are simplified at the same time.

This commit is contained in:
Daniel Marjamäki 2012-07-06 15:43:50 +02:00
parent 3c4ce7b43a
commit 6ad5fc8456
2 changed files with 174 additions and 105 deletions

View File

@ -1772,7 +1772,12 @@ bool Tokenizer::tokenize(std::istream &code,
} }
// enum.. // enum..
simplifyEnum(); if (m_timerResults) {
Timer t("Tokenizer::tokenize::simplifyEnum", _settings->_showtime, m_timerResults);
simplifyEnum();
} else {
simplifyEnum();
}
// Remove __asm.. // Remove __asm..
simplifyAsm(); simplifyAsm();
@ -6841,6 +6846,79 @@ bool Tokenizer::duplicateDefinition(Token ** tokPtr, const Token * name) const
return false; return false;
} }
class EnumValue {
public:
EnumValue() {
name = 0;
value = 0;
start = 0;
end = 0;
}
EnumValue(const EnumValue &ev) {
name = ev.name;
value = ev.value;
start = ev.start;
end = ev.end;
}
EnumValue(Token *name_, Token *value_, Token *start_, Token *end_) {
name = name_;
value = value_;
start = start_;
end = end_;
}
void simplify(const std::map<std::string, EnumValue> &enumValues) {
for (Token *tok = start; tok; tok = tok->next()) {
if (enumValues.find(tok->str()) != enumValues.end()) {
const EnumValue &other = enumValues.find(tok->str())->second;
if (other.value != NULL)
tok->str(other.value->str());
else {
bool islast = (tok == end);
Token *last = Tokenizer::copyTokens(tok, other.start, other.end);
tok->deleteThis();
if (islast) {
end = last;
}
tok = last;
}
}
if (tok == end)
break;
}
// Simplify calculations..
while (Token::Match(start, "%num% %op% %num% %op%") &&
start->strAt(1) == start->strAt(3)) {
const std::string &op = start->strAt(1);
if (op.size() != 1U)
break;
const std::string &val1 = start->str();
const std::string &val2 = start->strAt(2);
const std::string result = MathLib::calculate(val1, val2, op[0]);
start->str(result);
start->deleteNext(2);
}
if (Token::Match(start, "%num% %op% %num% [,}]")) {
const std::string &op = start->strAt(1);
if (op.size() == 1U) {
const std::string &val1 = start->str();
const std::string &val2 = start->strAt(2);
const std::string result = MathLib::calculate(val1, val2, op[0]);
start->str(result);
start->deleteNext(2);
value = start;
start = end = 0;
}
}
}
Token *name;
Token *value;
Token *start;
Token *end;
};
void Tokenizer::simplifyEnum() void Tokenizer::simplifyEnum()
{ {
// Don't simplify enums in java files // Don't simplify enums in java files
@ -6943,6 +7021,7 @@ void Tokenizer::simplifyEnum()
// iterate over all enumerators between { and } // iterate over all enumerators between { and }
// Give each enumerator the const value specified or if not specified, 1 + the // Give each enumerator the const value specified or if not specified, 1 + the
// previous value or 0 if it is the first one. // previous value or 0 if it is the first one.
std::map<std::string,EnumValue> enumValues;
for (; tok1 && tok1 != end; tok1 = tok1->next()) { for (; tok1 && tok1 != end; tok1 = tok1->next()) {
Token * enumName = 0; Token * enumName = 0;
Token * enumValue = 0; Token * enumValue = 0;
@ -7017,111 +7096,101 @@ void Tokenizer::simplifyEnum()
tok1 = enumValueEnd; tok1 = enumValueEnd;
} }
// find all uses of this enumerator and substitute it's value for it's name // add enumerator constant..
if (enumName && (enumValue || (enumValueStart && enumValueEnd))) { if (enumName && (enumValue || (enumValueStart && enumValueEnd))) {
const std::string pattern = className.empty() ? EnumValue ev(enumName, enumValue, enumValueStart, enumValueEnd);
std::string("") : ev.simplify(enumValues);
std::string(className + " :: " + enumName->str()); enumValues[enumName->str()] = ev;
int level = 1; if (ev.start == NULL) {
bool inScope = true; tok1 = ev.value;
lastEnumValueStart = lastEnumValueEnd = NULL;
}
}
}
bool exitThisScope = false; // Substitute enum values
int exitScope = 0; {
bool simplify = false; const std::string pattern = className.empty() ?
bool hasClass = false; std::string("") :
const Token *endScope = 0; std::string(className + " :: ");
for (Token *tok2 = tok1->next(); tok2; tok2 = tok2->next()) { int level = 1;
if (tok2->str() == "}") { bool inScope = true;
--level;
if (level < 0)
inScope = false;
if (exitThisScope) { std::stack<std::set<std::string> > shadowId; // duplicate ids in inner scope
if (level < exitScope) bool simplify = false;
exitThisScope = false; bool hasClass = false;
} EnumValue *ev = NULL;
} else if (tok2->str() == "{") { for (Token *tok2 = tok1->next(); tok2; tok2 = tok2->next()) {
// Is the same enum redefined? if (tok2->str() == "}") {
const Token *begin = end->link(); --level;
if (tok2->fileIndex() == begin->fileIndex() && if (level < 0)
tok2->linenr() == begin->linenr() && inScope = false;
Token::Match(begin->tokAt(-2), "enum %type% {") &&
Token::Match(tok2->tokAt(-2), "enum %type% {") && if (!shadowId.empty())
begin->previous()->str() == tok2->previous()->str()) { shadowId.pop();
// remove duplicate enum } else if (tok2->str() == "{") {
Token * startToken = tok2->tokAt(-3); // Is the same enum redefined?
tok2 = tok2->link()->next(); const Token *begin = end->link();
Token::eraseTokens(startToken, tok2); if (tok2->fileIndex() == begin->fileIndex() &&
if (!tok2) tok2->linenr() == begin->linenr() &&
break; Token::Match(begin->tokAt(-2), "enum %type% {") &&
Token::Match(tok2->tokAt(-2), "enum %type% {") &&
begin->previous()->str() == tok2->previous()->str()) {
// remove duplicate enum
Token * startToken = tok2->tokAt(-3);
tok2 = tok2->link()->next();
Token::eraseTokens(startToken, tok2);
if (!tok2)
break;
} else {
// Not a duplicate enum..
++level;
// Create a copy of the shadow ids for the inner scope
if (!shadowId.empty())
shadowId.push(shadowId.top());
}
} else if (!pattern.empty() && Token::Match(tok2, pattern.c_str()) && enumValues.find(tok2->strAt(2)) != enumValues.end()) {
simplify = true;
hasClass = true;
ev = &(enumValues.find(tok2->strAt(2))->second);
} else if (inScope && // enum is in scope
(shadowId.empty() || shadowId.top().find(tok2->str()) == shadowId.top().end()) && // no shadow enum/var/etc of enum
enumValues.find(tok2->str()) != enumValues.end()) { // tok2 is a enum id with a known value
ev = &(enumValues.find(tok2->str())->second);
if (!duplicateDefinition(&tok2, ev->name)) {
if (tok2->strAt(-1) == "::" ||
Token::Match(tok2->next(), "::|[")) {
// Don't replace this enum if:
// * it's preceded or followed by "::"
// * it's followed by "["
} else { } else {
// Not a duplicate enum.. simplify = true;
++level; hasClass = false;
} ev = &(enumValues.find(tok2->str())->second);
endScope = tok2->link();
} else if (!pattern.empty() && Token::Match(tok2, pattern.c_str())) {
simplify = true;
hasClass = true;
} else if (inScope && !exitThisScope && tok2->str() == enumName->str()) {
if (!duplicateDefinition(&tok2, enumName)) {
if (tok2->strAt(-1) == "::" ||
Token::Match(tok2->next(), "::|[")) {
// Don't replace this enum if:
// * it's preceded or followed by "::"
// * it's followed by "["
} else {
simplify = true;
hasClass = false;
}
} else {
// something with the same name.
exitScope = level;
if (endScope)
tok2 = endScope->previous();
} }
} else {
// something with the same name.
if (shadowId.empty())
shadowId.push(std::set<std::string>());
shadowId.top().insert(tok2->str());
}
}
if (simplify) {
if (ev->value)
tok2->str(ev->value->str());
else {
tok2 = tok2->previous();
tok2->deleteNext();
tok2 = copyTokens(tok2, ev->start, ev->end);
} }
if (simplify) { if (hasClass) {
// Simplify calculations.. tok2->deleteNext(2);
while (Token::Match(enumValueStart, "%num% %op% %num% %op%") &&
enumValueStart->strAt(1) == enumValueStart->strAt(3)) {
const std::string &op = enumValueStart->strAt(1);
if (op.size() != 1U)
break;
const std::string &val1 = enumValueStart->str();
const std::string &val2 = enumValueStart->strAt(2);
const std::string result = MathLib::calculate(val1, val2, op[0]);
enumValueStart->str(result);
enumValueStart->deleteNext(2);
}
if (Token::Match(enumValueStart, "%num% %op% %num% [,}]")) {
const std::string &op = enumValueStart->strAt(1);
if (op.size() == 1U) {
const std::string &val1 = enumValueStart->str();
const std::string &val2 = enumValueStart->strAt(2);
const std::string result = MathLib::calculate(val1, val2, op[0]);
enumValueStart->str(result);
enumValueStart->deleteNext(2);
enumValue = tok1 = enumValueStart;
enumValueStart = enumValueEnd = 0;
}
}
if (enumValue)
tok2->str(enumValue->str());
else {
tok2 = tok2->previous();
tok2->deleteNext();
tok2 = copyTokens(tok2, enumValueStart, enumValueEnd);
}
if (hasClass) {
tok2->deleteNext(2);
}
simplify = false;
} }
simplify = false;
} }
} }
} }

View File

@ -723,13 +723,6 @@ public:
return list.front(); return list.front();
} }
private:
/** Disable copy constructor, no implementation */
Tokenizer(const Tokenizer &);
/** Disable assignment operator, no implementation */
Tokenizer &operator=(const Tokenizer &);
/** /**
* Copy tokens. * Copy tokens.
* @param dest destination token where copied tokens will be inserted after * @param dest destination token where copied tokens will be inserted after
@ -739,6 +732,13 @@ private:
*/ */
static Token *copyTokens(Token *dest, const Token *first, const Token *last, bool one_line = true); static Token *copyTokens(Token *dest, const Token *first, const Token *last, bool one_line = true);
private:
/** Disable copy constructor, no implementation */
Tokenizer(const Tokenizer &);
/** Disable assignment operator, no implementation */
Tokenizer &operator=(const Tokenizer &);
/** settings */ /** settings */
const Settings * _settings; const Settings * _settings;