From 1a454256dc3f6a210bda9ccee7fe1936592f75bc Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Wed, 9 Nov 2011 21:45:59 +0100 Subject: [PATCH] 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. --- lib/token.cpp | 27 ++++++++++++++++++++++----- lib/token.h | 4 ++++ test/testtoken.cpp | 4 ++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index b9dec9427..be8f37ece 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -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) diff --git a/lib/token.h b/lib/token.h index 76762ad66..cccd064ef 100644 --- a/lib/token.h +++ b/lib/token.h @@ -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. diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 0bc6d5d97..d7b3a1b5e 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -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() {