From 1affb0c96b878f6a025c7c9f0e13e5209549c4c1 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Thu, 3 Jan 2013 22:26:20 +0100 Subject: [PATCH] Use more Effective C++ code style in Token class: 1)The non-const version of Token::tokAt and Token::linkAt now will use a combination of const_cast and static_cast to remove duplicate code. 2)The non-const versions of Token::find(simple)match are added, in order to avoid the usage of const_cast outside the Token code. 3)As a consequence of 2), an useless const_cast applied to a call of Token::findsimplematch in Tokenizer code is removed. --- lib/token.cpp | 23 ----------------------- lib/token.h | 20 ++++++++++++++++++-- lib/tokenize.cpp | 2 +- 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index 163d66ab1..560a98d20 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -279,20 +279,6 @@ const Token *Token::tokAt(int index) const return tok; } -Token *Token::tokAt(int index) -{ - Token *tok = this; - int num = std::abs(index); - while (num > 0 && tok) { - if (index > 0) - tok = tok->next(); - else - tok = tok->previous(); - --num; - } - return tok; -} - const Token *Token::linkAt(int index) const { const Token *tok = this->tokAt(index); @@ -302,15 +288,6 @@ const Token *Token::linkAt(int index) const return tok->link(); } -Token *Token::linkAt(int index) -{ - Token *tok = this->tokAt(index); - if (!tok) { - throw InternalError(this, "Internal error. Token::linkAt called with index outside the tokens range."); - } - return tok->link(); -} - const std::string &Token::strAt(int index) const { static const std::string empty_str; diff --git a/lib/token.h b/lib/token.h index 6c0df8707..a2c06ab87 100644 --- a/lib/token.h +++ b/lib/token.h @@ -84,14 +84,18 @@ public: * would return next from that one. */ const Token *tokAt(int index) const; - Token *tokAt(int index); + Token *tokAt(int index) { + return const_cast(static_cast(this)->tokAt(index)); + } /** * Returns the link to the token in given index, related to this token. * For example index 1 would return the link to next token. */ const Token *linkAt(int index) const; - Token *linkAt(int index); + Token *linkAt(int index) { + return const_cast(static_cast(this)->linkAt(index)); + } const std::string &strAt(int index) const; @@ -255,6 +259,18 @@ public: static const Token *findsimplematch(const Token *tok, const char pattern[], const Token *end); static const Token *findmatch(const Token *tok, const char pattern[], unsigned int varId = 0); static const Token *findmatch(const Token *tok, const char pattern[], const Token *end, unsigned int varId = 0); + static Token *findsimplematch(Token *tok, const char pattern[]) { + return const_cast(findsimplematch(static_cast(tok), pattern)); + } + static Token *findsimplematch(Token *tok, const char pattern[], const Token *end) { + return const_cast(findsimplematch(static_cast(tok), pattern, end)); + } + static Token *findmatch(Token *tok, const char pattern[], unsigned int varId = 0) { + return const_cast(findmatch(static_cast(tok), pattern, varId)); + } + static Token *findmatch(Token *tok, const char pattern[], const Token *end, unsigned int varId = 0) { + return const_cast(findmatch(static_cast(tok), pattern, end, varId)); + } /** * Needle is build from multiple alternatives. If one of diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 87a3c71f8..439af6022 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8745,7 +8745,7 @@ void Tokenizer::simplifyNamespaceStd() }; static const std::set stdFunctions(stdFunctions_, stdFunctions_+sizeof(stdFunctions_)/sizeof(*stdFunctions_)); - for (Token* tok = const_cast(Token::findsimplematch(list.front(), "using namespace std ;")); tok; tok = tok->next()) { + for (Token* tok = Token::findsimplematch(list.front(), "using namespace std ;"); tok; tok = tok->next()) { bool insert = false; if (Token::Match(tok, "%var% (") && !Token::Match(tok->previous(), ".|::") && stdFunctions.find(tok->str()) != stdFunctions.end()) insert = true;