2008-12-18 22:28:57 +01:00
|
|
|
/*
|
2008-12-19 22:15:06 +01:00
|
|
|
* cppcheck - c/c++ syntax checking
|
2009-01-02 20:02:35 +01:00
|
|
|
* Copyright (C) 2007-2009 Daniel Marjamäki, Reijo Tomperi, Nicolas Le Cam
|
2008-12-18 22:28:57 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#include "checkother.h"
|
2009-01-08 21:56:51 +01:00
|
|
|
#include "errormessage.h"
|
2008-12-18 22:28:57 +01:00
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdlib.h> // <- atoi
|
|
|
|
#include <cstring>
|
|
|
|
#include <cctype>
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Warning on C-Style casts.. p = (kalle *)foo;
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-01-12 18:12:14 +01:00
|
|
|
CheckOther::CheckOther(const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger)
|
|
|
|
: _settings(settings)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
_tokenizer = tokenizer;
|
|
|
|
_errorLogger = errorLogger;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckOther::~CheckOther()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckOther::WarningOldStylePointerCast()
|
|
|
|
{
|
2009-01-03 21:29:20 +01:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
// Old style pointer casting..
|
2009-01-03 21:29:20 +01:00
|
|
|
if (!Token::Match(tok, "( %type% * ) %var%"))
|
2008-12-18 22:28:57 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Is "type" a class?
|
2008-12-20 17:16:51 +01:00
|
|
|
const std::string pattern("class " + tok->next()->str());
|
2009-01-03 21:29:20 +01:00
|
|
|
if (!Token::findmatch(_tokenizer->tokens(), pattern.c_str()))
|
2008-12-18 22:28:57 +01:00
|
|
|
continue;
|
|
|
|
|
2009-01-08 22:08:14 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::cstyleCast(_tokenizer, tok));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Redundant code..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckOther::WarningRedundantCode()
|
|
|
|
{
|
|
|
|
|
|
|
|
// if (p) delete p
|
2009-01-03 21:29:20 +01:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (tok->str() != "if")
|
2008-12-18 22:28:57 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *varname1 = NULL;
|
2009-01-03 21:29:20 +01:00
|
|
|
const Token *tok2 = NULL;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok, "if ( %var% )"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
varname1 = tok->strAt(2);
|
2008-12-18 22:28:57 +01:00
|
|
|
tok2 = tok->tokAt(4);
|
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (Token::Match(tok, "if ( %var% != NULL )"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
varname1 = tok->strAt(2);
|
2008-12-18 22:28:57 +01:00
|
|
|
tok2 = tok->tokAt(6);
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (varname1 == NULL || tok2 == NULL)
|
2008-12-18 22:28:57 +01:00
|
|
|
continue;
|
|
|
|
|
2008-12-20 23:24:11 +01:00
|
|
|
bool err = false;
|
2009-01-05 16:49:57 +01:00
|
|
|
if (tok2->str() == "{")
|
2008-12-20 23:24:11 +01:00
|
|
|
{
|
2008-12-18 22:28:57 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok2, "delete %var% ; }"))
|
2008-12-20 23:24:11 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
err = (strcmp(tok2->strAt(1), varname1) == 0);
|
2008-12-20 23:24:11 +01:00
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (Token::Match(tok2, "delete [ ] %var% ; }"))
|
2008-12-20 23:24:11 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
err = (strcmp(tok2->strAt(1), varname1) == 0);
|
2008-12-20 23:24:11 +01:00
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (Token::Match(tok2, "free ( %var% ) ; }"))
|
2008-12-20 23:24:11 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
err = (strcmp(tok2->strAt(2), varname1) == 0);
|
2008-12-20 23:24:11 +01:00
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (Token::Match(tok2, "kfree ( %var% ) ; }"))
|
2008-12-20 23:24:11 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
err = (strcmp(tok2->strAt(2), varname1) == 0);
|
2008-12-20 23:24:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok2, "delete %var% ;"))
|
2008-12-20 23:24:11 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
err = (strcmp(tok2->strAt(1), varname1) == 0);
|
2008-12-20 23:24:11 +01:00
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (Token::Match(tok2, "delete [ ] %var% ;"))
|
2008-12-20 23:24:11 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
err = (strcmp(tok2->strAt(1), varname1) == 0);
|
2008-12-20 23:24:11 +01:00
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (Token::Match(tok2, "free ( %var% ) ;"))
|
2008-12-20 23:24:11 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
err = (strcmp(tok2->strAt(2), varname1) == 0);
|
2008-12-20 23:24:11 +01:00
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (Token::Match(tok2, "kfree ( %var% ) ;"))
|
2008-12-20 23:24:11 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
err = (strcmp(tok2->strAt(2), varname1) == 0);
|
2008-12-20 23:24:11 +01:00
|
|
|
}
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
if (err)
|
|
|
|
{
|
2009-01-08 22:08:14 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::redundantIfDelete0(_tokenizer, tok));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Redundant condition
|
|
|
|
// if (haystack.find(needle) != haystack.end())
|
2008-12-15 08:29:55 +01:00
|
|
|
// haystack.remove(needle);
|
|
|
|
redundantCondition2();
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-12-15 08:29:55 +01:00
|
|
|
void CheckOther::redundantCondition2()
|
|
|
|
{
|
2008-12-15 19:49:34 +01:00
|
|
|
const char pattern[] = "if ( %var% . find ( %any% ) != %var% . end ( ) ) "
|
2008-12-15 08:29:55 +01:00
|
|
|
"{|{|"
|
2008-12-15 19:49:34 +01:00
|
|
|
" %var% . remove ( %any% ) ; "
|
2008-12-15 08:29:55 +01:00
|
|
|
"}|}|";
|
2009-01-05 16:49:57 +01:00
|
|
|
const Token *tok = Token::findmatch(_tokenizer->tokens(), pattern);
|
|
|
|
while (tok)
|
2008-12-15 08:29:55 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
bool b = Token::Match(tok->tokAt(15), "{");
|
2008-12-15 08:29:55 +01:00
|
|
|
|
|
|
|
// Get tokens for the fields %var% and %any%
|
2009-01-03 21:29:20 +01:00
|
|
|
const Token *var1 = tok->tokAt(2);
|
|
|
|
const Token *any1 = tok->tokAt(6);
|
|
|
|
const Token *var2 = tok->tokAt(9);
|
|
|
|
const Token *var3 = tok->tokAt(b ? 16 : 15);
|
|
|
|
const Token *any2 = tok->tokAt(b ? 20 : 19);
|
2008-12-19 22:15:06 +01:00
|
|
|
|
2008-12-15 08:29:55 +01:00
|
|
|
// Check if all the "%var%" fields are the same and if all the "%any%" are the same..
|
2008-12-19 22:15:06 +01:00
|
|
|
if (var1->str() == var2->str() &&
|
|
|
|
var2->str() == var3->str() &&
|
2009-01-05 16:49:57 +01:00
|
|
|
any1->str() == any2->str())
|
2008-12-15 08:29:55 +01:00
|
|
|
{
|
2009-01-08 22:08:14 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::redundantIfRemove(_tokenizer, tok));
|
2008-12-15 08:29:55 +01:00
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
tok = Token::findmatch(tok->next(), pattern);
|
2008-12-15 08:29:55 +01:00
|
|
|
}
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// if (condition) ....
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckOther::WarningIf()
|
|
|
|
{
|
2009-01-14 20:43:28 +01:00
|
|
|
if (ErrorMessage::ifNoAction(_settings))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-14 20:43:28 +01:00
|
|
|
// Search for 'if (condition);'
|
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-14 20:43:28 +01:00
|
|
|
if (!Token::simpleMatch(tok, "if ("))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Search for the end paranthesis for the condition..
|
|
|
|
int parlevel = 0;
|
|
|
|
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-14 20:43:28 +01:00
|
|
|
if (tok2->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
else if (tok2->str() == ")")
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-14 20:43:28 +01:00
|
|
|
--parlevel;
|
|
|
|
if (parlevel <= 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-14 20:43:28 +01:00
|
|
|
if (Token::Match(tok2, ") ; !!else"))
|
|
|
|
{
|
|
|
|
_errorLogger->reportErr(ErrorMessage::ifNoAction(_tokenizer, tok));
|
|
|
|
}
|
|
|
|
break;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-14 20:43:28 +01:00
|
|
|
if (ErrorMessage::conditionAlwaysTrueFalse(_settings))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-14 20:43:28 +01:00
|
|
|
// Search for 'a=b; if (a==b)'
|
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
// Begin statement?
|
|
|
|
if (! Token::Match(tok, "[;{}]"))
|
|
|
|
continue;
|
|
|
|
tok = tok->next();
|
|
|
|
if (! tok)
|
|
|
|
break;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-14 20:43:28 +01:00
|
|
|
if (!Token::Match(tok, "%var% = %var% ; if ( %var%"))
|
|
|
|
continue;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-14 20:43:28 +01:00
|
|
|
if (strcmp(tok->strAt(9), ")") != 0)
|
|
|
|
continue;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-14 20:43:28 +01:00
|
|
|
// var1 = var2 ; if ( var3 cond var4 )
|
|
|
|
const char *var1 = tok->strAt(0);
|
|
|
|
const char *var2 = tok->strAt(2);
|
|
|
|
const char *var3 = tok->strAt(6);
|
|
|
|
const char *cond = tok->strAt(7);
|
|
|
|
const char *var4 = tok->strAt(8);
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-14 20:43:28 +01:00
|
|
|
// Check that var3 is equal with either var1 or var2
|
|
|
|
if (strcmp(var1, var3) && strcmp(var2, var3))
|
|
|
|
continue;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-14 20:43:28 +01:00
|
|
|
// Check that var4 is equal with either var1 or var2
|
|
|
|
if (strcmp(var1, var4) && strcmp(var2, var4))
|
|
|
|
continue;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-14 20:43:28 +01:00
|
|
|
// Check that there is a condition..
|
|
|
|
const char *p[6] = {"==", "<=", ">=", "!=", "<", ">"};
|
|
|
|
bool iscond = false;
|
|
|
|
for (int i = 0; i < 6; i++)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-14 20:43:28 +01:00
|
|
|
if (strcmp(cond, p[i]) == 0)
|
|
|
|
{
|
|
|
|
iscond = true;
|
|
|
|
break;
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
2009-01-14 20:43:28 +01:00
|
|
|
if (!iscond)
|
|
|
|
break;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-14 20:43:28 +01:00
|
|
|
// we found the error. Report.
|
|
|
|
bool b = false;
|
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
{
|
|
|
|
if (strcmp(cond, p[i]) == 0)
|
|
|
|
b = (i < 3);
|
|
|
|
}
|
|
|
|
_errorLogger->reportErr(ErrorMessage::conditionAlwaysTrueFalse(_tokenizer, tok->tokAt(4), b ? "True" : "False"));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// strtol(str, 0, radix) <- radix must be 0 or 2-36
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckOther::InvalidFunctionUsage()
|
|
|
|
{
|
2009-01-08 07:24:08 +01:00
|
|
|
// strtol and strtoul..
|
2009-01-05 16:49:57 +01:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
if ((tok->str() != "strtol") && (tok->str() != "strtoul"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Locate the third parameter of the function call..
|
|
|
|
int parlevel = 0;
|
|
|
|
int param = 1;
|
2009-01-05 16:49:57 +01:00
|
|
|
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok2, "("))
|
2009-01-01 23:22:28 +01:00
|
|
|
++parlevel;
|
2009-01-03 21:29:20 +01:00
|
|
|
else if (Token::Match(tok2, ")"))
|
2009-01-01 23:22:28 +01:00
|
|
|
--parlevel;
|
2009-01-03 21:29:20 +01:00
|
|
|
else if (parlevel == 1 && Token::Match(tok2, ","))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-01 23:22:28 +01:00
|
|
|
++param;
|
2009-01-05 16:49:57 +01:00
|
|
|
if (param == 3)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok2, ", %num% )"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
int radix = atoi(tok2->strAt(1));
|
|
|
|
if (!(radix == 0 || (radix >= 2 && radix <= 36)))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-09 20:15:54 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::dangerousUsageStrtol(_tokenizer, tok2));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-08 22:08:14 +01:00
|
|
|
|
2009-01-08 07:24:08 +01:00
|
|
|
// sprintf|snprintf overlapping data
|
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
// Get variable id of target buffer..
|
|
|
|
unsigned int varid = 0;
|
2009-01-08 22:08:14 +01:00
|
|
|
|
|
|
|
if (Token::Match(tok, "sprintf|snprintf ( %var% ,"))
|
2009-01-08 07:24:08 +01:00
|
|
|
varid = tok->tokAt(2)->varId();
|
|
|
|
|
2009-01-08 22:08:14 +01:00
|
|
|
else if (Token::Match(tok, "sprintf|snprintf ( %var% . %var% ,"))
|
2009-01-08 07:24:08 +01:00
|
|
|
varid = tok->tokAt(4)->varId();
|
2009-01-08 22:08:14 +01:00
|
|
|
|
|
|
|
if (varid == 0)
|
2009-01-08 07:24:08 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// goto ","
|
|
|
|
const Token *tok2 = tok->tokAt(3);
|
2009-01-08 22:08:14 +01:00
|
|
|
while (tok2 && tok2->str() != ",")
|
2009-01-08 07:24:08 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
// is any source buffer overlapping the target buffer?
|
2009-01-10 15:27:31 +01:00
|
|
|
int parlevel = 0;
|
2009-01-08 22:08:14 +01:00
|
|
|
while ((tok2 = tok2->next()) != NULL)
|
2009-01-08 07:24:08 +01:00
|
|
|
{
|
2009-01-08 22:08:14 +01:00
|
|
|
if (tok2->str() == "(")
|
2009-01-08 07:24:08 +01:00
|
|
|
++parlevel;
|
2009-01-08 22:08:14 +01:00
|
|
|
else if (tok2->str() == ")")
|
2009-01-08 07:24:08 +01:00
|
|
|
{
|
|
|
|
--parlevel;
|
2009-01-08 22:08:14 +01:00
|
|
|
if (parlevel < 0)
|
2009-01-08 07:24:08 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-01-10 12:19:17 +01:00
|
|
|
else if (parlevel == 0 && tok2->varId() == varid)
|
2009-01-08 07:24:08 +01:00
|
|
|
{
|
2009-01-12 07:52:50 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::sprintfOverlappingData(_tokenizer, tok2, tok2->str()));
|
2009-01-08 07:24:08 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Assignment in condition
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckOther::CheckIfAssignment()
|
|
|
|
{
|
2009-01-03 21:29:20 +01:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-03 21:29:20 +01:00
|
|
|
if (Token::Match(tok, "if ( %var% = %num% )") ||
|
|
|
|
Token::Match(tok, "if ( %var% = %str% )") ||
|
2009-01-05 16:49:57 +01:00
|
|
|
Token::Match(tok, "if ( %var% = %var% )"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-13 18:56:45 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::ifAssignment(_tokenizer, tok));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Check for unsigned divisions
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckOther::CheckUnsignedDivision()
|
|
|
|
{
|
|
|
|
// Check for "ivar / uvar" and "uvar / ivar"
|
|
|
|
std::map<std::string, char> varsign;
|
2009-01-05 16:49:57 +01:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok, "[{};(,] %type% %var% [;=,)]"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
const char *type = tok->strAt(1);
|
|
|
|
if (strcmp(type, "char") == 0 || strcmp(type, "short") == 0 || strcmp(type, "int") == 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
varsign[tok->strAt(2)] = 's';
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (Token::Match(tok, "[{};(,] unsigned %type% %var% [;=,)]"))
|
2008-12-18 22:28:57 +01:00
|
|
|
varsign[tok->strAt(3)] = 'u';
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (!Token::Match(tok, "[).]") && Token::Match(tok->next(), "%var% / %var%"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-12 18:12:14 +01:00
|
|
|
if (ErrorMessage::udivWarning(_settings))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-12 18:12:14 +01:00
|
|
|
const char *varname1 = tok->strAt(1);
|
|
|
|
const char *varname2 = tok->strAt(3);
|
|
|
|
char sign1 = varsign[varname1];
|
|
|
|
char sign2 = varsign[varname2];
|
|
|
|
|
|
|
|
if (sign1 && sign2 && sign1 != sign2)
|
|
|
|
{
|
|
|
|
// One of the operands are signed, the other is unsigned..
|
|
|
|
_errorLogger->reportErr(ErrorMessage::udivWarning(_tokenizer, tok->next()));
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (!Token::Match(tok, "[).]") && Token::Match(tok->next(), "%var% / - %num%"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-12 18:12:14 +01:00
|
|
|
if (ErrorMessage::udivError(_settings))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-12 18:12:14 +01:00
|
|
|
const char *varname1 = tok->strAt(1);
|
|
|
|
char sign1 = varsign[varname1];
|
|
|
|
if (sign1 == 'u')
|
|
|
|
{
|
|
|
|
_errorLogger->reportErr(ErrorMessage::udivError(_tokenizer, tok->next()));
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:29:20 +01:00
|
|
|
else if (Token::Match(tok, "[([=*/+-] - %num% / %var%"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-12 18:12:14 +01:00
|
|
|
if (ErrorMessage::udivError(_settings))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-12 18:12:14 +01:00
|
|
|
const char *varname2 = tok->strAt(4);
|
|
|
|
char sign2 = varsign[varname2];
|
|
|
|
if (sign2 == 'u')
|
|
|
|
{
|
|
|
|
_errorLogger->reportErr(ErrorMessage::udivError(_tokenizer, tok->next()));
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Check scope of variables..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
void CheckOther::CheckVariableScope()
|
|
|
|
{
|
|
|
|
// Walk through all tokens..
|
|
|
|
bool func = false;
|
|
|
|
int indentlevel = 0;
|
2009-01-05 16:49:57 +01:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
// Skip class and struct declarations..
|
2009-01-05 16:49:57 +01:00
|
|
|
if ((tok->str() == "class") || (tok->str() == "struct"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-03 21:29:20 +01:00
|
|
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (tok2->str() == "{")
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-01 23:22:28 +01:00
|
|
|
int indentlevel2 = 0;
|
2008-12-18 22:28:57 +01:00
|
|
|
for (tok = tok2; tok; tok = tok->next())
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (tok->str() == "{")
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-01 23:22:28 +01:00
|
|
|
++indentlevel2;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
if (tok->str() == "}")
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-01 23:22:28 +01:00
|
|
|
--indentlevel2;
|
2009-01-05 16:49:57 +01:00
|
|
|
if (indentlevel2 <= 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
tok = tok->next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-01-03 21:29:20 +01:00
|
|
|
if (Token::Match(tok2, "[,);]"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
if (! tok)
|
2008-12-18 22:28:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (tok->str() == "{")
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-02 00:43:23 +01:00
|
|
|
++indentlevel;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
if (tok->str() == "}")
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-02 00:43:23 +01:00
|
|
|
--indentlevel;
|
2009-01-05 16:49:57 +01:00
|
|
|
if (indentlevel == 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
func = false;
|
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
if (indentlevel == 0 && Token::Match(tok, ") {"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
func = true;
|
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
if (indentlevel > 0 && func && Token::Match(tok, "[{};]"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
// First token of statement..
|
2009-01-03 21:29:20 +01:00
|
|
|
const Token *tok1 = tok->next();
|
2009-01-05 16:49:57 +01:00
|
|
|
if (! tok1)
|
2008-12-18 22:28:57 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((tok1->str() == "return") ||
|
|
|
|
(tok1->str() == "delete") ||
|
|
|
|
(tok1->str() == "goto") ||
|
|
|
|
(tok1->str() == "else"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Variable declaration?
|
2009-01-03 21:29:20 +01:00
|
|
|
if (Token::Match(tok1, "%var% %var% ;") ||
|
2009-01-05 16:49:57 +01:00
|
|
|
Token::Match(tok1, "%var% %var% ="))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
CheckVariableScope_LookupVar(tok1, tok1->strAt(1));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
void CheckOther::CheckVariableScope_LookupVar(const Token *tok1, const char varname[])
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-03 21:29:20 +01:00
|
|
|
const Token *tok = tok1;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
// Skip the variable declaration..
|
2009-01-05 16:49:57 +01:00
|
|
|
while (tok && !Token::Match(tok, ";"))
|
2008-12-18 22:28:57 +01:00
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
// Check if the variable is used in this indentlevel..
|
|
|
|
bool used = false, used1 = false;
|
|
|
|
int indentlevel = 0;
|
|
|
|
int parlevel = 0;
|
|
|
|
bool for_or_while = false;
|
2009-01-05 16:49:57 +01:00
|
|
|
while (indentlevel >= 0 && tok)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (tok->str() == "{")
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-02 00:43:23 +01:00
|
|
|
++indentlevel;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (tok->str() == "}")
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-02 00:43:23 +01:00
|
|
|
--indentlevel;
|
2009-01-05 16:49:57 +01:00
|
|
|
if (indentlevel == 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (for_or_while && used)
|
2008-12-18 22:28:57 +01:00
|
|
|
return;
|
|
|
|
used1 = used;
|
|
|
|
used = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (tok->str() == "(")
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-02 00:43:23 +01:00
|
|
|
++parlevel;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (tok->str() == ")")
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-02 00:43:23 +01:00
|
|
|
--parlevel;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (tok->str() == varname)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (indentlevel == 0 || used1)
|
2008-12-18 22:28:57 +01:00
|
|
|
return;
|
|
|
|
used = true;
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (indentlevel == 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if ((tok->str() == "for") || (tok->str() == "while"))
|
2008-12-18 22:28:57 +01:00
|
|
|
for_or_while = true;
|
2009-01-05 16:49:57 +01:00
|
|
|
if (parlevel == 0 && (tok->str() == ";"))
|
2008-12-18 22:28:57 +01:00
|
|
|
for_or_while = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warning if "used" is true
|
2009-01-13 07:25:36 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::variableScope(_tokenizer, tok1, varname));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Check for constant function parameters
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckOther::CheckConstantFunctionParameter()
|
|
|
|
{
|
2009-01-03 21:29:20 +01:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok, "[,(] const std :: %type% %var% [,)]"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-12 18:32:53 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::passedByValue(_tokenizer, tok, tok->strAt(5)));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (Token::Match(tok, "[,(] const %type% %var% [,)]"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
// Check if type is a struct or class.
|
2008-12-20 17:16:51 +01:00
|
|
|
const std::string pattern(std::string("class|struct ") + tok->strAt(2));
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::findmatch(_tokenizer->tokens(), pattern.c_str()))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-12 18:32:53 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::passedByValue(_tokenizer, tok, tok->strAt(3)));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Check that all struct members are used
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckOther::CheckStructMemberUsage()
|
|
|
|
{
|
|
|
|
const char *structname = 0;
|
2009-01-05 16:49:57 +01:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (tok->fileIndex() != 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
continue;
|
2009-01-11 15:39:52 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok, "struct|union %type% {"))
|
2009-01-11 15:39:52 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
structname = tok->strAt(1);
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-11 15:39:52 +01:00
|
|
|
// Bail out if struct/union contain any functions
|
|
|
|
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
{
|
|
|
|
structname = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok2->str() == "}")
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->str() == "}")
|
|
|
|
structname = 0;
|
|
|
|
|
2009-01-03 21:29:20 +01:00
|
|
|
if (structname && Token::Match(tok, "[{;]"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-04 15:55:18 +01:00
|
|
|
const char *varname = 0;
|
2009-01-03 21:29:20 +01:00
|
|
|
if (Token::Match(tok->next(), "%type% %var% [;[]"))
|
2009-01-05 16:49:57 +01:00
|
|
|
varname = tok->strAt(2);
|
2009-01-03 21:29:20 +01:00
|
|
|
else if (Token::Match(tok->next(), "%type% %type% %var% [;[]"))
|
2009-01-10 15:39:20 +01:00
|
|
|
varname = tok->strAt(3);
|
2009-01-03 21:29:20 +01:00
|
|
|
else if (Token::Match(tok->next(), "%type% * %var% [;[]"))
|
2009-01-05 16:49:57 +01:00
|
|
|
varname = tok->strAt(3);
|
2009-01-03 21:29:20 +01:00
|
|
|
else if (Token::Match(tok->next(), "%type% %type% * %var% [;[]"))
|
2009-01-05 16:49:57 +01:00
|
|
|
varname = tok->strAt(4);
|
2008-12-18 22:28:57 +01:00
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
const std::string usagePattern(". " + std::string(varname));
|
2008-12-18 22:28:57 +01:00
|
|
|
bool used = false;
|
2009-01-05 16:49:57 +01:00
|
|
|
for (const Token *tok2 = _tokenizer->tokens(); tok2; tok2 = tok2->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::simpleMatch(tok2, usagePattern.c_str()))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
used = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (! used)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-12 18:21:25 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::unusedStructMember(_tokenizer, tok->next(), structname, varname));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Check usage of char variables..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckOther::CheckCharVariable()
|
|
|
|
{
|
2009-01-03 21:29:20 +01:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
// Declaring the variable..
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok, "[{};(,] char %var% [;=,)]"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-03 21:53:12 +01:00
|
|
|
// Set tok to point to the variable name
|
2009-01-05 16:49:57 +01:00
|
|
|
tok = tok->tokAt(2);
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
// Check usage of char variable..
|
|
|
|
int indentlevel = 0;
|
2009-01-05 16:49:57 +01:00
|
|
|
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (tok2->str() == "{")
|
2008-12-18 22:28:57 +01:00
|
|
|
++indentlevel;
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (tok2->str() == "}")
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
--indentlevel;
|
2009-01-05 16:49:57 +01:00
|
|
|
if (indentlevel <= 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-04 21:33:12 +01:00
|
|
|
std::string temp = "%var% [ " + tok->str() + " ]";
|
|
|
|
if ((tok2->str() != ".") && Token::Match(tok2->next(), temp.c_str()))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-12 19:52:43 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::charArrayIndex(_tokenizer, tok2->next()));
|
2008-12-18 22:28:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-04 21:33:12 +01:00
|
|
|
std::string tempFirst = "%var% [&|] " + tok->str();
|
|
|
|
std::string tempSecond = tok->str() + " [&|]";
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok2, tempFirst.c_str()) || Token::Match(tok2, tempSecond.c_str()))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-12 19:52:43 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::charBitOp(_tokenizer, tok2));
|
2008-12-18 22:28:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Incomplete statement..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckOther::CheckIncompleteStatement()
|
|
|
|
{
|
|
|
|
int parlevel = 0;
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (tok->str() == "(")
|
2008-12-18 22:28:57 +01:00
|
|
|
++parlevel;
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (tok->str() == ")")
|
2008-12-18 22:28:57 +01:00
|
|
|
--parlevel;
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (parlevel != 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
continue;
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if ((tok->str() != "#") && Token::Match(tok->next(), "; %str%") && !Token::Match(tok->tokAt(3), ","))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-12 18:46:24 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::constStatement(_tokenizer, tok->next(), "string"));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (!Token::Match(tok, "#") && Token::Match(tok->next(), "; %num%") && !Token::Match(tok->tokAt(3), ","))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-12 18:46:24 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::constStatement(_tokenizer, tok->next(), "numeric"));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Unreachable code below a 'return'
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckOther::unreachableCode()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
const Token *tok = Token::findmatch(_tokenizer->tokens(), "[;{}] return");
|
|
|
|
while (tok)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
// Goto the 'return' token
|
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
// Locate the end of the 'return' statement
|
2009-01-05 16:49:57 +01:00
|
|
|
while (tok && ! Token::Match(tok, ";"))
|
2008-12-18 22:28:57 +01:00
|
|
|
tok = tok->next();
|
2009-01-05 16:49:57 +01:00
|
|
|
while (tok && Token::Match(tok->next(), ";"))
|
2008-12-18 22:28:57 +01:00
|
|
|
tok = tok->next();
|
|
|
|
|
2009-01-14 02:29:33 +01:00
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
// If there is a statement below the return it is unreachable
|
2009-01-03 21:29:20 +01:00
|
|
|
if (!Token::Match(tok, "; case|default|}|#") && !Token::Match(tok, "; %var% :"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-12 18:28:04 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::unreachableCode(_tokenizer, tok->next()));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the next 'return' statement
|
2009-01-05 16:49:57 +01:00
|
|
|
tok = Token::findmatch(tok, "[;{}] return");
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Usage of function variables
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-01-03 21:29:20 +01:00
|
|
|
static bool isOp(const Token *tok)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
return bool(tok &&
|
|
|
|
(tok->str() == "&&" ||
|
|
|
|
tok->str() == "||" ||
|
|
|
|
tok->str() == "==" ||
|
|
|
|
tok->str() == "!=" ||
|
|
|
|
tok->str() == "<" ||
|
|
|
|
tok->str() == "<=" ||
|
|
|
|
tok->str() == ">" ||
|
|
|
|
tok->str() == ">=" ||
|
|
|
|
tok->str() == "<<" ||
|
2009-01-04 19:38:24 +01:00
|
|
|
Token::Match(tok, "[+-*/%&!~|^,[])?:]")));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckOther::functionVariableUsage()
|
|
|
|
{
|
|
|
|
// Parse all executing scopes..
|
2009-01-04 08:49:41 +01:00
|
|
|
const Token *tok1 = _tokenizer->tokens();
|
2009-01-04 17:30:25 +01:00
|
|
|
if (!tok1)
|
|
|
|
return;
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
while ((tok1 = Token::findmatch(tok1->next(), ") const| {")) != NULL)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
// Varname, usage {1=declare, 2=read, 4=write}
|
|
|
|
std::map<std::string, unsigned int> varUsage;
|
|
|
|
static const unsigned int USAGE_DECLARE = 1;
|
|
|
|
static const unsigned int USAGE_READ = 2;
|
|
|
|
static const unsigned int USAGE_WRITE = 4;
|
|
|
|
|
|
|
|
int indentlevel = 0;
|
2009-01-05 16:49:57 +01:00
|
|
|
for (const Token *tok = tok1; tok; tok = tok->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (tok->str() == "{")
|
2008-12-18 22:28:57 +01:00
|
|
|
++indentlevel;
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (tok->str() == "}")
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
--indentlevel;
|
2009-01-05 16:49:57 +01:00
|
|
|
if (indentlevel <= 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (Token::Match(tok, "struct|union|class {") ||
|
|
|
|
Token::Match(tok, "struct|union|class %type% {"))
|
2009-01-04 09:16:07 +01:00
|
|
|
{
|
2009-01-14 01:45:44 +01:00
|
|
|
int indentlevel0 = indentlevel;
|
|
|
|
|
|
|
|
while (tok->str() != "{")
|
2009-01-04 09:16:07 +01:00
|
|
|
tok = tok->next();
|
2009-01-14 01:45:44 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
|
|
|
indentlevel++;
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
indentlevel--;
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
while (tok && indentlevel > indentlevel0);
|
2009-01-04 09:16:07 +01:00
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok, "[;{}] bool|char|short|int|long|float|double %var% ;|="))
|
|
|
|
varUsage[ tok->strAt(2)] = USAGE_DECLARE;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok, "[;{}] bool|char|short|int|long|float|double * %var% ;|="))
|
|
|
|
varUsage[ tok->strAt(3)] = USAGE_DECLARE;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok, "delete|return %var%"))
|
|
|
|
varUsage[ tok->strAt(1)] |= USAGE_READ;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok, "%var% ="))
|
|
|
|
varUsage[ tok->str()] |= USAGE_WRITE;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok, "else %var% ="))
|
|
|
|
varUsage[ tok->strAt(1)] |= USAGE_WRITE;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok, ">>|& %var%"))
|
|
|
|
varUsage[ tok->strAt(1)] |= USAGE_WRITE;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if ((Token::Match(tok, "[(=&!]") || isOp(tok)) && Token::Match(tok->next(), "%var%"))
|
|
|
|
varUsage[ tok->strAt(1)] |= USAGE_READ;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-04 18:44:23 +01:00
|
|
|
if (Token::Match(tok, "-=|+=|*=|/=|&=|^= %var%") || Token::Match(tok, "|= %var%"))
|
2009-01-05 16:49:57 +01:00
|
|
|
varUsage[ tok->strAt(1)] |= USAGE_READ;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok, "%var%") && (tok->next()->str() == ")" || isOp(tok->next())))
|
|
|
|
varUsage[ tok->str()] |= USAGE_READ;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok, "[(,] %var% [,)]"))
|
|
|
|
varUsage[ tok->strAt(1)] |= USAGE_WRITE;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check usage of all variables in the current scope..
|
2009-01-05 16:49:57 +01:00
|
|
|
for (std::map<std::string, unsigned int>::const_iterator it = varUsage.begin(); it != varUsage.end(); ++it)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
std::string varname = it->first;
|
|
|
|
unsigned int usage = it->second;
|
|
|
|
|
|
|
|
if (!std::isalpha(varname[0]))
|
|
|
|
continue;
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (!(usage & USAGE_DECLARE))
|
2008-12-18 22:28:57 +01:00
|
|
|
continue;
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (usage == USAGE_DECLARE)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-12 18:40:20 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::unusedVariable(_tokenizer, tok1->next(), varname));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (!(usage & USAGE_READ))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-12 18:40:20 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::unreadVariable(_tokenizer, tok1->next(), varname));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (!(usage & USAGE_WRITE))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-12 18:40:20 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::unassignedVariable(_tokenizer, tok1->next(), varname));
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-15 17:57:51 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// str plus char
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckOther::strPlusChar()
|
|
|
|
{
|
2009-01-15 18:12:33 +01:00
|
|
|
bool charVars[10000] = {0};
|
|
|
|
|
2009-01-15 18:00:15 +01:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2009-01-15 17:57:51 +01:00
|
|
|
{
|
2009-01-15 18:12:33 +01:00
|
|
|
// Declaring char variable..
|
|
|
|
if (Token::Match(tok, "char %var% [;=]"))
|
2009-01-15 17:57:51 +01:00
|
|
|
{
|
2009-01-15 18:12:33 +01:00
|
|
|
unsigned int varid = tok->next()->varId();
|
2009-01-15 18:12:55 +01:00
|
|
|
if (varid > 0 && varid < 10000)
|
2009-01-15 18:12:33 +01:00
|
|
|
charVars[varid] = true;
|
|
|
|
}
|
2009-01-15 17:57:51 +01:00
|
|
|
|
2009-01-15 18:12:33 +01:00
|
|
|
//
|
|
|
|
else if (Token::Match(tok, "%str% + %any%"))
|
|
|
|
{
|
2009-01-15 17:57:51 +01:00
|
|
|
// char constant..
|
2009-01-15 18:12:33 +01:00
|
|
|
const char *s = tok->strAt(2);
|
2009-01-15 18:00:15 +01:00
|
|
|
if (*s == '\'')
|
2009-01-15 17:57:51 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::strPlusChar(_tokenizer, tok));
|
2009-01-15 18:12:33 +01:00
|
|
|
|
|
|
|
// char variable..
|
|
|
|
unsigned int varid = tok->tokAt(2)->varId();
|
2009-01-15 18:12:55 +01:00
|
|
|
if (varid > 0 && varid < 10000 && charVars[varid])
|
2009-01-15 18:12:33 +01:00
|
|
|
_errorLogger->reportErr(ErrorMessage::strPlusChar(_tokenizer, tok));
|
2009-01-15 17:57:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|