Refactoring: Moved astStringify to the Token class and renamed it to expressionString. This can often be useful when reporting error messages that contains source code.

This commit is contained in:
Daniel Marjamäki 2014-01-17 18:37:49 +01:00
parent 36acfb6d20
commit b2b5590f2b
3 changed files with 22 additions and 19 deletions

View File

@ -2208,24 +2208,6 @@ void CheckOther::zerodivError(const Token *tok)
reportError(tok, Severity::error, "zerodiv", "Division by zero."); reportError(tok, Severity::error, "zerodiv", "Division by zero.");
} }
// TODO: this utility function should probably be moved to some common file
static std::string astStringify(const Token *top)
{
const Token *start = top;
while (start->astOperand1() && start->astOperand2())
start = start->astOperand1();
const Token *end = top;
while (end->astOperand1() && end->astOperand2())
end = end->astOperand2();
std::string str;
for (const Token *tok = start; tok && tok != end; tok = tok->next()) {
str += tok->str();
if (Token::Match(tok, "%var%|%num% %var%|%num%"))
str += " ";
}
return str + end->str();
}
void CheckOther::zerodivcondError(const Token *tokcond, const Token *tokdiv) void CheckOther::zerodivcondError(const Token *tokcond, const Token *tokdiv)
{ {
std::list<const Token *> callstack; std::list<const Token *> callstack;
@ -2241,7 +2223,7 @@ void CheckOther::zerodivcondError(const Token *tokcond, const Token *tokdiv)
} else if (Token::Match(tokcond, "%num% <|<=")) { } else if (Token::Match(tokcond, "%num% <|<=")) {
condition = tokcond->strAt(2) + ((tokcond->strAt(1) == "<") ? ">" : ">=") + tokcond->str(); condition = tokcond->strAt(2) + ((tokcond->strAt(1) == "<") ? ">" : ">=") + tokcond->str();
} else if (tokcond->isComparisonOp()) { } else if (tokcond->isComparisonOp()) {
condition = astStringify(tokcond); condition = tokcond->expressionString();
} else { } else {
if (tokcond->str() == "!") if (tokcond->str() == "!")
condition = tokcond->next()->str() + "==0"; condition = tokcond->next()->str() + "==0";

View File

@ -1182,6 +1182,25 @@ bool Token::isCalculation() const
return true; return true;
} }
std::string Token::expressionString() const
{
const Token * const top = this;
const Token *start = top;
while (start->astOperand1() && start->astOperand2())
start = start->astOperand1();
const Token *end = top;
while (end->astOperand1() && end->astOperand2())
end = end->astOperand2();
std::string ret;
for (const Token *tok = start; tok && tok != end; tok = tok->next()) {
ret += tok->str();
if (Token::Match(tok, "%var%|%num% %var%|%num%"))
ret += " ";
}
return ret + end->str();
}
void Token::printAst() const void Token::printAst() const
{ {
bool title = false; bool title = false;

View File

@ -699,6 +699,8 @@ public:
return ret + sep + _str; return ret + sep + _str;
} }
std::string expressionString() const;
void printAst() const; void printAst() const;
}; };