From b2b5590f2b2baa6214746b62578af476f498efd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 17 Jan 2014 18:37:49 +0100 Subject: [PATCH] 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. --- lib/checkother.cpp | 20 +------------------- lib/token.cpp | 19 +++++++++++++++++++ lib/token.h | 2 ++ 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 2fde9fe15..a0675f089 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2208,24 +2208,6 @@ void CheckOther::zerodivError(const Token *tok) 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) { std::list callstack; @@ -2241,7 +2223,7 @@ void CheckOther::zerodivcondError(const Token *tokcond, const Token *tokdiv) } else if (Token::Match(tokcond, "%num% <|<=")) { condition = tokcond->strAt(2) + ((tokcond->strAt(1) == "<") ? ">" : ">=") + tokcond->str(); } else if (tokcond->isComparisonOp()) { - condition = astStringify(tokcond); + condition = tokcond->expressionString(); } else { if (tokcond->str() == "!") condition = tokcond->next()->str() + "==0"; diff --git a/lib/token.cpp b/lib/token.cpp index 77774cc5d..48cd66ea3 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -1182,6 +1182,25 @@ bool Token::isCalculation() const 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 { bool title = false; diff --git a/lib/token.h b/lib/token.h index 15258f83f..f9eb9c4a0 100644 --- a/lib/token.h +++ b/lib/token.h @@ -699,6 +699,8 @@ public: return ret + sep + _str; } + std::string expressionString() const; + void printAst() const; };