Refactoring in tokenize.cpp: Fix cppcheck warning, add some const, fix typo from previous refactoring

This commit is contained in:
Alexander Mai 2015-05-24 11:43:46 +02:00
parent 227a73eb45
commit 7f15873759
1 changed files with 53 additions and 56 deletions

View File

@ -2787,9 +2787,7 @@ void Tokenizer::setVarId()
for (Token *tok = list.front(); tok; tok = tok->next()) { for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "namespace|class|struct %name% {|:")) { if (Token::Match(tok, "namespace|class|struct %name% {|:")) {
const std::string &classname(tok->next()->str()); const std::string &classname(tok->next()->str());
const bool namesp = tok->str() == "namespace";
bool namesp = tok->str() == "namespace";
const Token* tokStart = tok->tokAt(2); const Token* tokStart = tok->tokAt(2);
while (tokStart && tokStart->str() != "{") { while (tokStart && tokStart->str() != "{") {
if (Token::Match(tokStart, "public|private|protected %name%")) if (Token::Match(tokStart, "public|private|protected %name%"))
@ -4795,8 +4793,8 @@ bool Tokenizer::simplifyConditions()
else else
result = !eq; result = !eq;
} else { } else {
double op1 = MathLib::toDoubleNumber(tok->next()->str()); const double op1 = MathLib::toDoubleNumber(tok->next()->str());
double op2 = MathLib::toDoubleNumber(tok->strAt(3)); const double op2 = MathLib::toDoubleNumber(tok->strAt(3));
if (cmp == ">=") if (cmp == ">=")
result = (op1 >= op2); result = (op1 >= op2);
else if (cmp == ">") else if (cmp == ">")
@ -4810,8 +4808,8 @@ bool Tokenizer::simplifyConditions()
} }
} else { } else {
// Compare boolean // Compare boolean
bool op1 = (tok->next()->str() == std::string("true")); const bool op1 = (tok->next()->str() == std::string("true"));
bool op2 = (tok->strAt(3) == std::string("true")); const bool op2 = (tok->strAt(3) == std::string("true"));
if (cmp == "==") if (cmp == "==")
result = (op1 == op2); result = (op1 == op2);
@ -5751,7 +5749,7 @@ void Tokenizer::simplifyStdType()
for (Token *tok = list.front(); tok; tok = tok->next()) { for (Token *tok = list.front(); tok; tok = tok->next()) {
// long unsigned => unsigned long // long unsigned => unsigned long
if (Token::Match(tok, "char|short|int|long unsigned|signed")) { if (Token::Match(tok, "char|short|int|long unsigned|signed")) {
bool isUnsigned = tok->next()->str() == "unsigned"; const bool isUnsigned = tok->next()->str() == "unsigned";
tok->deleteNext(); tok->deleteNext();
tok->isUnsigned(isUnsigned); tok->isUnsigned(isUnsigned);
tok->isSigned(!isUnsigned); tok->isSigned(!isUnsigned);
@ -5762,7 +5760,7 @@ void Tokenizer::simplifyStdType()
// check if signed or unsigned specified // check if signed or unsigned specified
if (Token::Match(tok, "unsigned|signed")) { if (Token::Match(tok, "unsigned|signed")) {
bool isUnsigned = tok->str() == "unsigned"; const bool isUnsigned = tok->str() == "unsigned";
// unsigned i => unsigned int i // unsigned i => unsigned int i
if (!Token::Match(tok->next(), "char|short|int|long")) if (!Token::Match(tok->next(), "char|short|int|long"))
@ -7977,14 +7975,14 @@ void Tokenizer::simplifyStd()
if (isC()) if (isC())
return; return;
std::set<std::string> f; const static std::set<std::string> f = make_container< std::set<std::string> > () <<
f.insert("strcat"); "strcat" <<
f.insert("strcpy"); "strcpy" <<
f.insert("strncat"); "strncat" <<
f.insert("strncpy"); "strncpy" <<
f.insert("free"); "free" <<
f.insert("malloc"); "malloc" <<
f.insert("strdup"); "strdup";
for (Token *tok = list.front(); tok; tok = tok->next()) { for (Token *tok = list.front(); tok; tok = tok->next()) {
if (tok->str() != "std") if (tok->str() != "std")
@ -8007,7 +8005,7 @@ void Tokenizer::simplifyStd()
bool Tokenizer::IsScopeNoReturn(const Token *endScopeToken, bool *unknown) const bool Tokenizer::IsScopeNoReturn(const Token *endScopeToken, bool *unknown) const
{ {
std::string unknownFunc; std::string unknownFunc;
bool ret = _settings->library.isScopeNoReturn(endScopeToken,&unknownFunc); const bool ret = _settings->library.isScopeNoReturn(endScopeToken,&unknownFunc);
if (unknown) if (unknown)
*unknown = !unknownFunc.empty(); *unknown = !unknownFunc.empty();
if (!unknownFunc.empty() && _settings->checkLibrary && _settings->isEnabled("information")) { if (!unknownFunc.empty() && _settings->checkLibrary && _settings->isEnabled("information")) {
@ -8271,7 +8269,7 @@ void Tokenizer::eraseDeadCode(Token *begin, const Token *end)
} else if (isgoto && Token::Match(tok, "[{};] do|while|for|BOOST_FOREACH")) { } else if (isgoto && Token::Match(tok, "[{};] do|while|for|BOOST_FOREACH")) {
//it's possible that code inside loop is not dead, //it's possible that code inside loop is not dead,
//because of the possible presence of the label pointed by 'goto' //because of the possible presence of the label pointed by 'goto'
Token *start = tok->tokAt(2); const Token *start = tok->tokAt(2);
if (start && start->str() == "(") if (start && start->str() == "(")
start = start->link()->next(); start = start->link()->next();
if (start && start->str() == "{") { if (start && start->str() == "{") {
@ -9712,13 +9710,12 @@ void Tokenizer::simplifyMicrosoftStringFunctions()
{ {
// skip if not Windows // skip if not Windows
if (_settings->platformType != Settings::Win32A && if (_settings->platformType != Settings::Win32A &&
_settings->platformType != Settings::Win32W & _settings->platformType != Settings::Win32W &&
_settings->platformType != Settings::Win64) _settings->platformType != Settings::Win64)
return; return;
struct triplet { struct triplet {
triplet(const char* t, const char* m, const char* u) : tchar(t), mbcs(m), unicode(u) {} triplet(const char* t, const char* m="", const char* u="") : tchar(t), mbcs(m), unicode(u) {}
triplet(const std::string& t) : tchar(t) {}
bool operator <(const triplet& rhs) const { bool operator <(const triplet& rhs) const {
return tchar < rhs.tchar; return tchar < rhs.tchar;
} }
@ -9761,7 +9758,7 @@ void Tokenizer::simplifyMicrosoftStringFunctions()
const bool ansi = _settings->platformType == Settings::Win32A; const bool ansi = _settings->platformType == Settings::Win32A;
for (Token *tok = list.front(); tok; tok = tok->next()) { for (Token *tok = list.front(); tok; tok = tok->next()) {
std::set<triplet>::const_iterator match = apis.find(tok->str()); std::set<triplet>::const_iterator match = apis.find(tok->str().c_str());
if (match!=apis.end()) { if (match!=apis.end()) {
const std::string pattern(match->tchar + " ("); const std::string pattern(match->tchar + " (");
if (Token::simpleMatch(tok, pattern.c_str())) { if (Token::simpleMatch(tok, pattern.c_str())) {