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.
This commit is contained in:
parent
7289b70eb1
commit
1affb0c96b
|
@ -279,20 +279,6 @@ const Token *Token::tokAt(int index) const
|
||||||
return tok;
|
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 *Token::linkAt(int index) const
|
||||||
{
|
{
|
||||||
const Token *tok = this->tokAt(index);
|
const Token *tok = this->tokAt(index);
|
||||||
|
@ -302,15 +288,6 @@ const Token *Token::linkAt(int index) const
|
||||||
return tok->link();
|
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
|
const std::string &Token::strAt(int index) const
|
||||||
{
|
{
|
||||||
static const std::string empty_str;
|
static const std::string empty_str;
|
||||||
|
|
20
lib/token.h
20
lib/token.h
|
@ -84,14 +84,18 @@ public:
|
||||||
* would return next from that one.
|
* would return next from that one.
|
||||||
*/
|
*/
|
||||||
const Token *tokAt(int index) const;
|
const Token *tokAt(int index) const;
|
||||||
Token *tokAt(int index);
|
Token *tokAt(int index) {
|
||||||
|
return const_cast<Token *>(static_cast<const Token *>(this)->tokAt(index));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the link to the token in given index, related to this token.
|
* Returns the link to the token in given index, related to this token.
|
||||||
* For example index 1 would return the link to next token.
|
* For example index 1 would return the link to next token.
|
||||||
*/
|
*/
|
||||||
const Token *linkAt(int index) const;
|
const Token *linkAt(int index) const;
|
||||||
Token *linkAt(int index);
|
Token *linkAt(int index) {
|
||||||
|
return const_cast<Token *>(static_cast<const Token *>(this)->linkAt(index));
|
||||||
|
}
|
||||||
|
|
||||||
const std::string &strAt(int index) const;
|
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 *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[], unsigned int varId = 0);
|
||||||
static const Token *findmatch(const Token *tok, const char pattern[], const Token *end, 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<Token *>(findsimplematch(static_cast<const Token *>(tok), pattern));
|
||||||
|
}
|
||||||
|
static Token *findsimplematch(Token *tok, const char pattern[], const Token *end) {
|
||||||
|
return const_cast<Token *>(findsimplematch(static_cast<const Token *>(tok), pattern, end));
|
||||||
|
}
|
||||||
|
static Token *findmatch(Token *tok, const char pattern[], unsigned int varId = 0) {
|
||||||
|
return const_cast<Token *>(findmatch(static_cast<const Token *>(tok), pattern, varId));
|
||||||
|
}
|
||||||
|
static Token *findmatch(Token *tok, const char pattern[], const Token *end, unsigned int varId = 0) {
|
||||||
|
return const_cast<Token *>(findmatch(static_cast<const Token *>(tok), pattern, end, varId));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Needle is build from multiple alternatives. If one of
|
* Needle is build from multiple alternatives. If one of
|
||||||
|
|
|
@ -8745,7 +8745,7 @@ void Tokenizer::simplifyNamespaceStd()
|
||||||
};
|
};
|
||||||
static const std::set<std::string> stdFunctions(stdFunctions_, stdFunctions_+sizeof(stdFunctions_)/sizeof(*stdFunctions_));
|
static const std::set<std::string> stdFunctions(stdFunctions_, stdFunctions_+sizeof(stdFunctions_)/sizeof(*stdFunctions_));
|
||||||
|
|
||||||
for (Token* tok = const_cast<Token*>(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;
|
bool insert = false;
|
||||||
if (Token::Match(tok, "%var% (") && !Token::Match(tok->previous(), ".|::") && stdFunctions.find(tok->str()) != stdFunctions.end())
|
if (Token::Match(tok, "%var% (") && !Token::Match(tok->previous(), ".|::") && stdFunctions.find(tok->str()) != stdFunctions.end())
|
||||||
insert = true;
|
insert = true;
|
||||||
|
|
Loading…
Reference in New Issue