Cache result of tok->isStandardType()

Also contains test order improvements from Johan Samuelson (#3116).

Run time went down from 15.15s to 14.95s for the case below.

Benchmarked using these settings:
- CXXFLAGS="-O2 -g -NDEBUG"
- time -p cppcheck.git -q --std=posix --enable=all --inconclusive -Dfoo .

on the "rpm" codebase.
This commit is contained in:
Thomas Jarosch 2011-11-09 21:45:59 +01:00
parent 5fefaf4166
commit 1a454256dc
3 changed files with 30 additions and 5 deletions

View File

@ -41,6 +41,7 @@ Token::Token(Token **t) :
_isPointerCompare(false),
_isLong(false),
_isUnused(false),
_isStandardType(false),
_varId(0),
_fileIndex(0),
_linenr(0),
@ -75,8 +76,27 @@ void Token::update_property_info()
_isNumber = false;
_isBoolean = false;
}
update_property_isStandardType();
}
void Token::update_property_isStandardType()
{
_isStandardType = false;
if (_str.size() < 3)
return;
static const char * const type[] = {"int", "char", "bool", "long", "short", "float", "double", "size_t", 0};
for (int i = 0; type[i]; i++) {
if (_str == type[i]) {
_isStandardType = true;
break;
}
}
}
void Token::str(const std::string &s)
{
_str = s;
@ -124,6 +144,7 @@ void Token::deleteThis()
_isPointerCompare = _next->_isPointerCompare;
_isLong = _next->_isLong;
_isUnused = _next->_isUnused;
_isStandardType = _next->_isStandardType;
_varId = _next->_varId;
_fileIndex = _next->_fileIndex;
_linenr = _next->_linenr;
@ -702,11 +723,7 @@ size_t Token::getStrLength(const Token *tok)
bool Token::isStandardType() const
{
bool ret = false;
const char *type[] = {"bool", "char", "short", "int", "long", "float", "double", "size_t", 0};
for (int i = 0; type[i]; i++)
ret |= (_str == type[i]);
return ret;
return _isStandardType;
}
void Token::move(Token *srcStart, Token *srcEnd, Token *newLocation)

View File

@ -445,6 +445,7 @@ private:
bool _isPointerCompare;
bool _isLong;
bool _isUnused;
bool _isStandardType;
unsigned int _varId;
unsigned int _fileIndex;
unsigned int _linenr;
@ -453,6 +454,9 @@ private:
Called after any _str() modification. */
void update_property_info();
/** Update internal property cache about isStandardType() */
void update_property_isStandardType();
/**
* A value from 0-100 that provides a rough idea about where in the token
* list this token is located.

View File

@ -542,6 +542,10 @@ private:
Token tok(0);
tok.str("string");
ASSERT_EQUALS(false, tok.isStandardType());
// Change back to standard type
tok.str("int");
ASSERT_EQUALS(true, tok.isStandardType());
}
void updateProperties() {