2008-12-10 20:34:04 +01:00
|
|
|
/*
|
2008-10-26 08:55:15 +01:00
|
|
|
* c++check - c/c++ syntax checking
|
|
|
|
* Copyright (C) 2007 Daniel Marjamäki
|
|
|
|
*
|
|
|
|
* 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/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2007-05-25 08:51:18 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#include "CheckOther.h"
|
2008-11-11 07:42:09 +01:00
|
|
|
|
2007-06-05 22:29:03 +02:00
|
|
|
#include <list>
|
2008-09-15 08:40:26 +02:00
|
|
|
#include <map>
|
2007-05-25 08:51:18 +02:00
|
|
|
#include <sstream>
|
2008-09-15 08:40:26 +02:00
|
|
|
#include <stdlib.h> // <- atoi
|
2008-08-30 20:29:37 +02:00
|
|
|
#include <cstring>
|
2007-05-25 08:51:18 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Warning on C-Style casts.. p = (kalle *)foo;
|
|
|
|
//---------------------------------------------------------------------------
|
2008-11-15 09:53:51 +01:00
|
|
|
|
2008-11-22 21:00:36 +01:00
|
|
|
CheckOther::CheckOther( const Tokenizer *tokenizer, ErrorLogger *errorLogger )
|
2008-11-15 09:53:51 +01:00
|
|
|
{
|
2008-11-22 21:00:36 +01:00
|
|
|
_tokenizer = tokenizer;
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger = errorLogger;
|
2008-11-15 09:53:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CheckOther::~CheckOther()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2007-05-25 08:51:18 +02:00
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckOther::WarningOldStylePointerCast()
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
|
|
|
// Old style pointer casting..
|
2008-11-22 23:49:14 +01:00
|
|
|
if (!TOKEN::Match(tok, "( %type% * ) %var%"))
|
2007-05-25 08:51:18 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Is "type" a class?
|
|
|
|
const char *pattern[] = {"class","",NULL};
|
2008-11-23 11:09:16 +01:00
|
|
|
pattern[1] = tok->strAt(1);
|
2008-11-22 23:49:14 +01:00
|
|
|
if (!TOKEN::findtoken(_tokenizer->tokens(), pattern))
|
2007-05-25 08:51:18 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
std::ostringstream ostr;
|
2008-11-20 21:54:52 +01:00
|
|
|
ostr << _tokenizer->fileLine(tok) << ": C-style pointer casting";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2007-05-25 08:51:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Use standard function "isdigit" instead
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckOther::WarningIsDigit()
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
|
|
|
bool err = false;
|
2008-11-22 23:49:14 +01:00
|
|
|
err |= TOKEN::Match(tok, "%var% >= '0' && %var% <= '9'");
|
|
|
|
err |= TOKEN::Match(tok, "* %var% >= '0' && * %var% <= '9'");
|
|
|
|
err |= TOKEN::Match(tok, "( %var% >= '0' ) && ( %var% <= '9' )");
|
|
|
|
err |= TOKEN::Match(tok, "( * %var% >= '0' ) && ( * %var% <= '9' )");
|
2007-05-25 08:51:18 +02:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-11-20 21:54:52 +01:00
|
|
|
ostr << _tokenizer->fileLine(tok) << ": The condition can be simplified; use 'isdigit'";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2007-05-25 08:51:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2007-06-05 06:51:01 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Use standard function "isalpha" instead
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckOther::WarningIsAlpha()
|
2007-06-05 06:51:01 +02:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2007-06-05 06:51:01 +02:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->str() != "(" )
|
2008-11-06 01:04:52 +01:00
|
|
|
continue;
|
|
|
|
|
2007-06-05 06:51:01 +02:00
|
|
|
bool err = false;
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
err |= TOKEN::Match(tok, "( %var% >= 'A' && %var% <= 'Z' )");
|
|
|
|
err |= TOKEN::Match(tok, "( * %var% >= 'A' && * %var% <= 'Z' )");
|
|
|
|
err |= TOKEN::Match(tok, "( ( %var% >= 'A' ) && ( %var% <= 'Z' ) )");
|
|
|
|
err |= TOKEN::Match(tok, "( ( * %var% >= 'A' ) && ( * %var% <= 'Z' ) )");
|
2008-11-06 01:04:52 +01:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-11-20 21:54:52 +01:00
|
|
|
ostr << _tokenizer->fileLine(tok) << ": The condition can be simplified; use 'isupper'";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2007-06-05 06:51:01 +02:00
|
|
|
continue;
|
2008-11-06 01:04:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = false;
|
2008-11-22 23:49:14 +01:00
|
|
|
err |= TOKEN::Match(tok, "( %var% >= 'a' && %var% <= 'z' )");
|
|
|
|
err |= TOKEN::Match(tok, "( * %var% >= 'a' && * %var% <= 'z' )");
|
|
|
|
err |= TOKEN::Match(tok, "( ( %var% >= 'a' ) && ( %var% <= 'z' ) )");
|
|
|
|
err |= TOKEN::Match(tok, "( ( * %var% >= 'a' ) && ( * %var% <= 'z' ) )");
|
2008-11-06 01:04:52 +01:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-11-20 21:54:52 +01:00
|
|
|
ostr << _tokenizer->fileLine(tok) << ": The condition can be simplified; use 'islower'";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2008-11-06 01:04:52 +01:00
|
|
|
continue;
|
|
|
|
}
|
2007-06-05 06:51:01 +02:00
|
|
|
|
2008-11-06 01:04:52 +01:00
|
|
|
err = false;
|
2008-11-22 23:49:14 +01:00
|
|
|
err |= TOKEN::Match(tok, "( %var% >= 'A' && %var% <= 'Z' ) || ( %var% >= 'a' && %var% <= 'z' )");
|
|
|
|
err |= TOKEN::Match(tok, "( %var% >= 'a' && %var% <= 'z' ) || ( %var% >= 'A' && %var% <= 'Z' )");
|
|
|
|
err |= TOKEN::Match(tok, "( * %var% >= 'A' && * %var% <= 'Z' ) || ( * %var% >= 'a' && * %var% <= 'z' )");
|
|
|
|
err |= TOKEN::Match(tok, "( * %var% >= 'a' && * %var% <= 'z' ) || ( * %var% >= 'A' && * %var% <= 'Z' )");
|
|
|
|
err |= TOKEN::Match(tok, "( ( %var% >= 'A' ) && ( %var% <= 'Z' ) ) || ( ( %var% >= 'a' ) && ( %var% <= 'z' ) )");
|
|
|
|
err |= TOKEN::Match(tok, "( ( %var% >= 'a' ) && ( %var% <= 'z' ) ) || ( ( %var% >= 'A' ) && ( %var% <= 'Z' ) )");
|
|
|
|
err |= TOKEN::Match(tok, "( ( * %var% >= 'A' ) && ( * %var% <= 'Z' ) ) || ( ( * var >= 'a' ) && ( * %var% <= 'z' ) )");
|
|
|
|
err |= TOKEN::Match(tok, "( ( * %var% >= 'a' ) && ( * %var% <= 'z' ) ) || ( ( * var >= 'A' ) && ( * %var% <= 'Z' ) )");
|
2007-06-05 06:51:01 +02:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-11-20 21:54:52 +01:00
|
|
|
ostr << _tokenizer->fileLine(tok) << ": The condition can be simplified; use 'isalpha'";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2007-06-05 06:51:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2007-05-25 08:51:18 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Redundant code..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckOther::WarningRedundantCode()
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
// if (p) delete p
|
2008-12-08 22:49:05 +01:00
|
|
|
for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->str() != "if" )
|
2007-05-25 08:51:18 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *varname1 = NULL;
|
2008-03-22 12:46:06 +01:00
|
|
|
const TOKEN *tok2 = NULL;
|
2007-05-25 08:51:18 +02:00
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok,"if ( %var% )"))
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
varname1 = tok->strAt( 2);
|
|
|
|
tok2 = tok->tokAt(4);
|
2007-05-25 08:51:18 +02:00
|
|
|
}
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (TOKEN::Match(tok,"if ( %var% != NULL )"))
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
varname1 = tok->strAt( 2);
|
|
|
|
tok2 = tok->tokAt(6);
|
2007-05-25 08:51:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (varname1==NULL || tok2==NULL)
|
|
|
|
continue;
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok2->str() == "{" )
|
2008-12-08 22:49:05 +01:00
|
|
|
tok2 = tok2->next();
|
2008-08-12 08:40:55 +02:00
|
|
|
|
2007-05-25 08:51:18 +02:00
|
|
|
bool err = false;
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok2,"delete %var% ;"))
|
2008-11-23 11:09:16 +01:00
|
|
|
err = (strcmp(tok2->strAt(1),varname1)==0);
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (TOKEN::Match(tok2,"delete [ ] %var% ;"))
|
2008-11-23 11:09:16 +01:00
|
|
|
err = (strcmp(tok2->strAt(1),varname1)==0);
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (TOKEN::Match(tok2,"free ( %var% )"))
|
2008-11-23 11:09:16 +01:00
|
|
|
err = (strcmp(tok2->strAt(2),varname1)==0);
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (TOKEN::Match(tok2,"kfree ( %var% )"))
|
2008-11-23 11:09:16 +01:00
|
|
|
err = (strcmp(tok2->strAt(2),varname1)==0);
|
2007-05-25 08:51:18 +02:00
|
|
|
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-11-20 21:54:52 +01:00
|
|
|
ostr << _tokenizer->fileLine(tok) << ": Redundant condition. It is safe to deallocate a NULL pointer";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2007-05-25 08:51:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-28 08:18:03 +01:00
|
|
|
// TODO: Redundant condition
|
2007-05-25 08:51:18 +02:00
|
|
|
// if (haystack.find(needle) != haystack.end())
|
|
|
|
// haystack.remove(needle);
|
|
|
|
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2007-06-08 18:46:51 +02:00
|
|
|
// if (condition) ....
|
2007-05-25 08:51:18 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckOther::WarningIf()
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
2007-06-02 18:29:05 +02:00
|
|
|
|
|
|
|
// Search for 'if (condition);'
|
2008-12-08 22:49:05 +01:00
|
|
|
for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if (tok->str() == "if")
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
|
|
|
int parlevel = 0;
|
2008-12-08 22:49:05 +01:00
|
|
|
for (const TOKEN *tok2 = tok->next(); tok2; tok2 = tok2->next())
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if (tok2->str() == "(")
|
2007-05-25 08:51:18 +02:00
|
|
|
parlevel++;
|
2008-11-24 20:38:08 +01:00
|
|
|
else if (tok2->str() == ")")
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
|
|
|
parlevel--;
|
|
|
|
if (parlevel<=0)
|
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
if (strcmp(tok2->strAt(1), ";") == 0 &&
|
|
|
|
strcmp(tok2->strAt(2), "else") != 0)
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-11-20 21:54:52 +01:00
|
|
|
ostr << _tokenizer->fileLine(tok) << ": Found \"if (condition);\"";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2007-05-25 08:51:18 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-02 18:29:05 +02:00
|
|
|
|
|
|
|
// Search for 'a=b; if (a==b)'
|
2008-12-08 22:49:05 +01:00
|
|
|
for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2007-06-02 18:29:05 +02:00
|
|
|
{
|
2008-03-23 14:38:01 +01:00
|
|
|
// Begin statement?
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( ! TOKEN::Match(tok, "[;{}]") )
|
2008-03-23 14:38:01 +01:00
|
|
|
continue;
|
2008-12-08 22:49:05 +01:00
|
|
|
tok = tok->next();
|
2008-03-23 14:38:01 +01:00
|
|
|
if ( ! tok )
|
|
|
|
break;
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if (!TOKEN::Match(tok,"%var% = %var% ; if ( %var%"))
|
2007-06-02 18:29:05 +02:00
|
|
|
continue;
|
|
|
|
|
2008-11-23 11:09:16 +01:00
|
|
|
if ( strcmp(tok->strAt( 9), ")") != 0 )
|
2007-06-02 18:29:05 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// var1 = var2 ; if ( var3 cond var4 )
|
2008-11-23 11:09:16 +01:00
|
|
|
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);
|
2007-06-02 18:29:05 +02:00
|
|
|
|
|
|
|
// Check that var3 is equal with either var1 or var2
|
|
|
|
if (strcmp(var1,var3) && strcmp(var2,var3))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check that var4 is equal with either var1 or var2
|
|
|
|
if (strcmp(var1,var4) && strcmp(var2,var4))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check that there is a condition..
|
|
|
|
const char *p[6] = {"==","<=",">=","!=","<",">"};
|
|
|
|
bool iscond = false;
|
2008-12-09 01:49:32 +01:00
|
|
|
for (int i = 0; i < 6; i++)
|
2008-12-05 19:17:51 +01:00
|
|
|
{
|
2008-12-09 01:49:32 +01:00
|
|
|
if (strcmp(cond, p[i]) == 0)
|
|
|
|
{
|
2008-12-05 19:17:51 +01:00
|
|
|
iscond = true;
|
2008-12-09 01:49:32 +01:00
|
|
|
break;
|
|
|
|
}
|
2008-12-05 19:17:51 +01:00
|
|
|
}
|
2007-06-02 18:29:05 +02:00
|
|
|
if (!iscond)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// we found the error. Report.
|
|
|
|
std::ostringstream ostr;
|
2008-11-23 11:09:16 +01:00
|
|
|
ostr << _tokenizer->fileLine(tok->tokAt(4)) << ": The condition is always ";
|
2007-06-02 18:29:05 +02:00
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
{
|
|
|
|
if (strcmp(cond, p[i]) == 0)
|
|
|
|
ostr << (i < 3 ? "True" : "False");
|
|
|
|
}
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2008-08-12 08:40:55 +02:00
|
|
|
}
|
2007-05-25 08:51:18 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-06-05 20:58:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// strtol(str, 0, radix) <- radix must be 0 or 2-36
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckOther::InvalidFunctionUsage()
|
2007-06-05 20:58:27 +02:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next() )
|
2007-06-05 20:58:27 +02:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ((tok->str() != "strtol") && (tok->str() != "strtoul"))
|
2007-06-05 20:58:27 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Locate the third parameter of the function call..
|
|
|
|
int parlevel = 0;
|
|
|
|
int param = 1;
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( const TOKEN *tok2 = tok->next(); tok2; tok2 = tok2->next() )
|
2007-06-05 20:58:27 +02:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok2, "(") )
|
2007-06-05 20:58:27 +02:00
|
|
|
parlevel++;
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (TOKEN::Match(tok2, ")"))
|
2007-06-05 20:58:27 +02:00
|
|
|
parlevel--;
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (parlevel == 1 && TOKEN::Match(tok2, ","))
|
2007-06-05 20:58:27 +02:00
|
|
|
{
|
|
|
|
param++;
|
|
|
|
if (param==3)
|
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok2, ", %num% )") )
|
2007-06-05 20:58:27 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
int radix = atoi(tok2->strAt( 1));
|
2007-06-05 20:58:27 +02:00
|
|
|
if (!(radix==0 || (radix>=2 && radix<=36)))
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-11-20 21:54:52 +01:00
|
|
|
ostr << _tokenizer->fileLine(tok2) << ": Invalid radix in call to strtol or strtoul. Must be 0 or 2-36";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2007-06-05 20:58:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-05 22:29:03 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2007-07-24 08:23:28 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Assignment in condition
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckOther::CheckIfAssignment()
|
2007-07-24 08:23:28 +02:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2007-07-24 08:23:28 +02:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok, "if ( %var% = %num% )") ||
|
|
|
|
TOKEN::Match(tok, "if ( %var% = %str% )") ||
|
|
|
|
TOKEN::Match(tok, "if ( %var% = %var% )") )
|
2007-07-24 08:23:28 +02:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-11-20 21:54:52 +01:00
|
|
|
ostr << _tokenizer->fileLine(tok) << ": Possible bug. Should it be '==' instead of '='?";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2007-07-24 08:23:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2008-02-20 19:20:59 +01:00
|
|
|
//---------------------------------------------------------------------------
|
2008-02-22 08:26:23 +01:00
|
|
|
// Check for unsigned divisions
|
2008-02-20 19:20:59 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckOther::CheckUnsignedDivision()
|
2008-02-20 19:20:59 +01:00
|
|
|
{
|
2008-03-20 08:23:01 +01:00
|
|
|
// Check for "ivar / uvar" and "uvar / ivar"
|
2008-09-15 08:40:26 +02:00
|
|
|
std::map<std::string, char> varsign;
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next() )
|
2008-02-20 19:20:59 +01:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "[{};(,] %type% %var% [;=,)]") )
|
2008-10-04 14:12:24 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
const char *type = tok->strAt( 1);
|
2008-10-04 14:12:24 +02:00
|
|
|
if (strcmp(type,"char")==0 || strcmp(type,"short")==0 || strcmp(type,"int")==0)
|
2008-11-23 11:09:16 +01:00
|
|
|
varsign[tok->strAt(2)] = 's';
|
2008-10-04 14:12:24 +02:00
|
|
|
}
|
2008-03-20 08:23:01 +01:00
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( TOKEN::Match(tok, "[{};(,] unsigned %type% %var% [;=,)]") )
|
2008-11-23 11:09:16 +01:00
|
|
|
varsign[tok->strAt(3)] = 'u';
|
2008-03-20 08:23:01 +01:00
|
|
|
|
2008-12-08 22:49:05 +01:00
|
|
|
else if (!TOKEN::Match(tok,"[).]") && TOKEN::Match(tok->next(), "%var% / %var%"))
|
2008-09-15 08:40:26 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
const char *varname1 = tok->strAt(1);
|
|
|
|
const char *varname2 = tok->strAt(3);
|
2008-09-15 08:40:26 +02:00
|
|
|
char sign1 = varsign[varname1];
|
|
|
|
char sign2 = varsign[varname2];
|
2008-03-20 08:23:01 +01:00
|
|
|
|
2008-09-15 08:40:26 +02:00
|
|
|
if ( sign1 && sign2 && sign1 != sign2 )
|
2008-02-20 19:20:59 +01:00
|
|
|
{
|
2008-03-20 08:23:01 +01:00
|
|
|
// One of the operands are signed, the other is unsigned..
|
2008-02-20 19:20:59 +01:00
|
|
|
std::ostringstream ostr;
|
2008-12-08 22:49:05 +01:00
|
|
|
ostr << _tokenizer->fileLine(tok->next()) << ": Warning: Division with signed and unsigned operators";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2008-02-20 19:20:59 +01:00
|
|
|
}
|
|
|
|
}
|
2008-10-06 18:42:42 +02:00
|
|
|
|
2008-12-08 22:49:05 +01:00
|
|
|
else if (!TOKEN::Match(tok,"[).]") && TOKEN::Match(tok->next(), "%var% / - %num%"))
|
2008-10-06 18:42:42 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
const char *varname1 = tok->strAt(1);
|
2008-10-06 18:42:42 +02:00
|
|
|
char sign1 = varsign[varname1];
|
|
|
|
if ( sign1 == 'u' )
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-12-08 22:49:05 +01:00
|
|
|
ostr << _tokenizer->fileLine(tok->next()) << ": Unsigned division. The result will be wrong.";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2008-10-06 18:42:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (TOKEN::Match(tok, "[([=*/+-] - %num% / %var%"))
|
2008-10-06 18:42:42 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
const char *varname2 = tok->strAt(4);
|
2008-10-06 18:42:42 +02:00
|
|
|
char sign2 = varsign[varname2];
|
|
|
|
if ( sign2 == 'u' )
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-12-08 22:49:05 +01:00
|
|
|
ostr << _tokenizer->fileLine(tok->next()) << ": Unsigned division. The result will be wrong.";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2008-10-06 18:42:42 +02:00
|
|
|
}
|
|
|
|
}
|
2008-02-20 19:20:59 +01:00
|
|
|
}
|
|
|
|
}
|
2008-03-16 14:17:43 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Check scope of variables..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckOther::CheckVariableScope()
|
2008-03-16 14:17:43 +01:00
|
|
|
{
|
|
|
|
// Walk through all tokens..
|
|
|
|
bool func = false;
|
|
|
|
int indentlevel = 0;
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next() )
|
2008-03-16 14:17:43 +01:00
|
|
|
{
|
2008-03-21 07:44:52 +01:00
|
|
|
// Skip class and struct declarations..
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( (tok->str() == "class") || (tok->str() == "struct") )
|
2008-03-21 07:44:52 +01:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
for (const TOKEN *tok2 = tok; tok2; tok2 = tok2->next())
|
2008-03-22 12:46:06 +01:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok2->str() == "{" )
|
2008-03-21 07:44:52 +01:00
|
|
|
{
|
|
|
|
int _indentlevel = 0;
|
2008-12-08 22:49:05 +01:00
|
|
|
for (tok = tok2; tok; tok = tok->next())
|
2008-03-21 07:44:52 +01:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->str() == "{" )
|
2008-03-23 14:38:01 +01:00
|
|
|
{
|
|
|
|
_indentlevel++;
|
|
|
|
}
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->str() == "}" )
|
2008-03-21 07:44:52 +01:00
|
|
|
{
|
2008-03-23 14:38:01 +01:00
|
|
|
_indentlevel--;
|
|
|
|
if ( _indentlevel <= 0 )
|
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
tok = tok->next();
|
2008-03-23 14:38:01 +01:00
|
|
|
break;
|
|
|
|
}
|
2008-03-21 07:44:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok2, "[,);]"))
|
2008-03-21 07:44:52 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( ! tok )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->str() == "{" )
|
2008-03-16 14:17:43 +01:00
|
|
|
{
|
2008-03-23 14:38:01 +01:00
|
|
|
indentlevel++;
|
|
|
|
}
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->str() == "}" )
|
2008-03-23 14:38:01 +01:00
|
|
|
{
|
|
|
|
indentlevel--;
|
|
|
|
if ( indentlevel == 0 )
|
|
|
|
func = false;
|
2008-03-16 14:17:43 +01:00
|
|
|
}
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( indentlevel == 0 && TOKEN::Match(tok, ") {") )
|
2008-03-16 14:17:43 +01:00
|
|
|
{
|
|
|
|
func = true;
|
|
|
|
}
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( indentlevel > 0 && func && TOKEN::Match(tok, "[{};]") )
|
2008-03-16 14:17:43 +01:00
|
|
|
{
|
2008-03-16 18:39:38 +01:00
|
|
|
// First token of statement..
|
2008-12-08 22:49:05 +01:00
|
|
|
const TOKEN *tok1 = tok->next();
|
2008-03-29 18:27:02 +01:00
|
|
|
if ( ! tok1 )
|
|
|
|
continue;
|
2008-03-16 18:39:38 +01:00
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
if ((tok1->str() == "return") ||
|
|
|
|
(tok1->str() == "delete") ||
|
|
|
|
(tok1->str() == "goto") ||
|
|
|
|
(tok1->str() == "else"))
|
2008-03-16 18:39:38 +01:00
|
|
|
continue;
|
2008-03-16 14:17:43 +01:00
|
|
|
|
|
|
|
// Variable declaration?
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok1, "%var% %var% ;") ||
|
|
|
|
TOKEN::Match(tok1, "%var% %var% =") )
|
2008-03-16 18:39:38 +01:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
CheckVariableScope_LookupVar( tok1, tok1->strAt( 1) );
|
2008-03-16 18:39:38 +01:00
|
|
|
}
|
2008-03-16 14:17:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckOther::CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[] )
|
2008-03-16 14:17:43 +01:00
|
|
|
{
|
|
|
|
const TOKEN *tok = tok1;
|
|
|
|
|
|
|
|
// Skip the variable declaration..
|
2008-11-22 23:49:14 +01:00
|
|
|
while (tok && !TOKEN::Match(tok,";"))
|
2008-12-08 22:49:05 +01:00
|
|
|
tok = tok->next();
|
2008-03-16 14:17:43 +01:00
|
|
|
|
|
|
|
// Check if the variable is used in this indentlevel..
|
2008-03-17 12:05:30 +01:00
|
|
|
bool used = false, used1 = false;
|
2008-03-23 08:57:27 +01:00
|
|
|
int indentlevel = 0;
|
2008-04-01 08:21:12 +02:00
|
|
|
int parlevel = 0;
|
2008-03-23 14:38:01 +01:00
|
|
|
bool for_or_while = false;
|
|
|
|
while ( indentlevel >= 0 && tok )
|
2008-03-16 14:17:43 +01:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->str() == "{" )
|
2008-03-16 14:17:43 +01:00
|
|
|
{
|
2008-03-23 14:38:01 +01:00
|
|
|
indentlevel++;
|
|
|
|
}
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
else if ( tok->str() == "}" )
|
2008-03-23 14:38:01 +01:00
|
|
|
{
|
|
|
|
indentlevel--;
|
|
|
|
if ( indentlevel == 0 )
|
|
|
|
{
|
|
|
|
if ( for_or_while && used )
|
|
|
|
return;
|
|
|
|
used1 = used;
|
|
|
|
used = false;
|
|
|
|
}
|
2008-03-16 14:17:43 +01:00
|
|
|
}
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
else if ( tok->str() == "(" )
|
2008-04-01 08:21:12 +02:00
|
|
|
{
|
|
|
|
parlevel++;
|
|
|
|
}
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
else if ( tok->str() == ")" )
|
2008-04-01 08:21:12 +02:00
|
|
|
{
|
|
|
|
parlevel--;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
else if ( tok->str() == varname )
|
2008-03-16 14:17:43 +01:00
|
|
|
{
|
2008-03-17 12:05:30 +01:00
|
|
|
if ( indentlevel == 0 || used1 )
|
2008-03-16 14:17:43 +01:00
|
|
|
return;
|
|
|
|
used = true;
|
|
|
|
}
|
|
|
|
|
2008-03-21 07:44:52 +01:00
|
|
|
else if ( indentlevel==0 )
|
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( (tok->str() == "for") || (tok->str() == "while") )
|
2008-03-21 07:44:52 +01:00
|
|
|
for_or_while = true;
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( parlevel == 0 && (tok->str() == ";") )
|
2008-03-21 07:44:52 +01:00
|
|
|
for_or_while = false;
|
|
|
|
}
|
2008-03-23 14:38:01 +01:00
|
|
|
|
2008-12-08 22:49:05 +01:00
|
|
|
tok = tok->next();
|
2008-03-16 14:17:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Warning if "used" is true
|
|
|
|
std::ostringstream errmsg;
|
2008-11-20 21:54:52 +01:00
|
|
|
errmsg << _tokenizer->fileLine(tok1) << " The scope of the variable '" << varname << "' can be limited";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr( errmsg.str() );
|
2008-03-16 14:17:43 +01:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-07-24 08:23:28 +02:00
|
|
|
|
2008-05-03 09:20:25 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Check for constant function parameters
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckOther::CheckConstantFunctionParameter()
|
2008-05-03 09:20:25 +02:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2008-05-03 09:20:25 +02:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok,"[,(] const std :: %type% %var% [,)]") )
|
2008-05-03 09:20:25 +02:00
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
2008-11-23 11:09:16 +01:00
|
|
|
errmsg << _tokenizer->fileLine(tok) << " " << tok->strAt(5) << " is passed by value, it could be passed by reference/pointer instead";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr( errmsg.str() );
|
2008-05-03 09:20:25 +02:00
|
|
|
}
|
2008-05-03 11:44:20 +02:00
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( TOKEN::Match(tok,"[,(] const %type% %var% [,)]") )
|
2008-05-03 11:44:20 +02:00
|
|
|
{
|
|
|
|
// Check if type is a struct or class.
|
|
|
|
const char *pattern[3] = {"class","type",0};
|
2008-11-23 11:09:16 +01:00
|
|
|
pattern[1] = tok->strAt( 2);
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::findtoken(_tokenizer->tokens(), pattern) )
|
2008-05-03 11:44:20 +02:00
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
2008-11-23 11:09:16 +01:00
|
|
|
errmsg << _tokenizer->fileLine(tok) << " " << tok->strAt(3) << " is passed by value, it could be passed by reference/pointer instead";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr( errmsg.str() );
|
2008-05-03 11:44:20 +02:00
|
|
|
}
|
|
|
|
pattern[0] = "struct";
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::findtoken(_tokenizer->tokens(), pattern) )
|
2008-05-03 11:44:20 +02:00
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
2008-11-23 11:09:16 +01:00
|
|
|
errmsg << _tokenizer->fileLine(tok) << " " << tok->strAt(3) << " is passed by value, it could be passed by reference/pointer instead";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr( errmsg.str() );
|
2008-05-03 11:44:20 +02:00
|
|
|
}
|
|
|
|
}
|
2008-05-03 09:20:25 +02:00
|
|
|
}
|
|
|
|
}
|
2008-05-09 20:29:42 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2008-08-12 08:40:55 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2008-05-09 20:29:42 +02:00
|
|
|
// Check that all struct members are used
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckOther::CheckStructMemberUsage()
|
2008-08-12 08:40:55 +02:00
|
|
|
{
|
|
|
|
const char *structname = 0;
|
|
|
|
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next() )
|
2008-08-12 08:40:55 +02:00
|
|
|
{
|
2008-12-08 23:02:37 +01:00
|
|
|
if ( tok->fileIndex() != 0 )
|
2008-08-12 08:40:55 +02:00
|
|
|
continue;
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->str() == "}" )
|
2008-08-12 08:40:55 +02:00
|
|
|
structname = 0;
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "struct %type% {") )
|
2008-11-23 11:09:16 +01:00
|
|
|
structname = tok->strAt( 1);
|
2008-08-12 08:40:55 +02:00
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if (structname && TOKEN::Match(tok, "[{;]"))
|
2008-08-12 08:40:55 +02:00
|
|
|
{
|
|
|
|
const char *varname = 0;
|
2008-12-08 22:49:05 +01:00
|
|
|
if (TOKEN::Match(tok->next(), "%type% %var% [;[]"))
|
2008-11-23 11:09:16 +01:00
|
|
|
varname = tok->strAt( 2 );
|
2008-12-08 22:49:05 +01:00
|
|
|
else if (TOKEN::Match(tok->next(), "%type% %type% %var% [;[]"))
|
2008-11-23 11:09:16 +01:00
|
|
|
varname = tok->strAt( 2 );
|
2008-12-08 22:49:05 +01:00
|
|
|
else if (TOKEN::Match(tok->next(), "%type% * %var% [;[]"))
|
2008-11-23 11:09:16 +01:00
|
|
|
varname = tok->strAt( 3 );
|
2008-12-08 22:49:05 +01:00
|
|
|
else if (TOKEN::Match(tok->next(), "%type% %type% * %var% [;[]"))
|
2008-11-23 11:09:16 +01:00
|
|
|
varname = tok->strAt( 4 );
|
2008-08-12 08:40:55 +02:00
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *varnames[2];
|
|
|
|
varnames[0] = varname;
|
|
|
|
varnames[1] = 0;
|
|
|
|
bool used = false;
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( const TOKEN *tok2 = _tokenizer->tokens(); tok2; tok2 = tok2->next() )
|
2008-08-12 08:40:55 +02:00
|
|
|
{
|
2008-12-08 23:02:37 +01:00
|
|
|
if ( tok->fileIndex() != 0 )
|
2008-08-12 08:40:55 +02:00
|
|
|
continue;
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok2, ". %var%", varnames))
|
2008-08-12 08:40:55 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
if ( strcmp("=", tok2->strAt(2)) == 0 )
|
2008-08-12 08:40:55 +02:00
|
|
|
continue;
|
|
|
|
used = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! used )
|
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
2008-11-20 21:54:52 +01:00
|
|
|
errmsg << _tokenizer->fileLine(tok) << ": struct member '" << structname << "::" << varname << "' is never read";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(errmsg.str());
|
2008-08-12 08:40:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-09 20:29:42 +02:00
|
|
|
}
|
2007-06-05 22:29:03 +02:00
|
|
|
|
2008-08-28 08:36:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Check usage of char variables..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckOther::CheckCharVariable()
|
2008-08-28 08:36:30 +02:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
2008-08-28 08:36:30 +02:00
|
|
|
{
|
|
|
|
// Declaring the variable..
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "[{};(,] char %var% [;=,)]") )
|
2008-08-28 08:36:30 +02:00
|
|
|
{
|
|
|
|
const char *varname[2] = {0};
|
2008-11-23 11:09:16 +01:00
|
|
|
varname[0] = tok->strAt( 2);
|
2008-08-28 08:36:30 +02:00
|
|
|
|
|
|
|
// Check usage of char variable..
|
|
|
|
int indentlevel = 0;
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( const TOKEN *tok2 = tok->next(); tok2; tok2 = tok2->next() )
|
2008-08-28 08:36:30 +02:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok2->str() == "{" )
|
2008-08-28 08:36:30 +02:00
|
|
|
++indentlevel;
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
else if ( tok2->str() == "}" )
|
2008-08-28 08:36:30 +02:00
|
|
|
{
|
|
|
|
--indentlevel;
|
2008-10-04 14:12:24 +02:00
|
|
|
if ( indentlevel <= 0 )
|
2008-08-28 08:36:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-12-08 22:49:05 +01:00
|
|
|
if ((tok2->str() != ".") && TOKEN::Match(tok2->next(), "%var% [ %var1% ]", varname))
|
2008-08-28 08:36:30 +02:00
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
2008-12-08 22:49:05 +01:00
|
|
|
errmsg << _tokenizer->fileLine(tok2->next()) << ": Warning - using char variable as array index";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(errmsg.str());
|
2008-08-28 08:36:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok2, "%var% [&|] %var1%", varname) || TOKEN::Match(tok2, "%var1% [&|]", varname) )
|
2008-08-28 08:36:30 +02:00
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
2008-11-20 21:54:52 +01:00
|
|
|
errmsg << _tokenizer->fileLine(tok2) << ": Warning - using char variable in bit operation";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(errmsg.str());
|
2008-08-28 08:36:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-09-20 19:34:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Incomplete statement..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckOther::CheckIncompleteStatement()
|
2008-09-20 19:34:37 +02:00
|
|
|
{
|
2008-09-23 18:03:39 +02:00
|
|
|
int parlevel = 0;
|
2008-09-20 19:34:37 +02:00
|
|
|
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next() )
|
2008-09-23 18:03:39 +02:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->str() == "(" )
|
2008-09-23 18:03:39 +02:00
|
|
|
++parlevel;
|
2008-11-24 20:38:08 +01:00
|
|
|
else if ( tok->str() == ")" )
|
2008-09-23 18:03:39 +02:00
|
|
|
--parlevel;
|
|
|
|
|
|
|
|
if ( parlevel != 0 )
|
|
|
|
continue;
|
2008-09-20 19:34:37 +02:00
|
|
|
|
2008-12-08 22:49:05 +01:00
|
|
|
if ( (tok->str() != "#") && TOKEN::Match(tok->next(),"; %str%") && !TOKEN::Match(tok->tokAt(3), ",") )
|
2008-09-23 18:03:39 +02:00
|
|
|
{
|
2008-09-20 19:34:37 +02:00
|
|
|
std::ostringstream errmsg;
|
2008-12-08 22:49:05 +01:00
|
|
|
errmsg << _tokenizer->fileLine(tok->next()) << ": Redundant code: Found a statement that begins with string constant";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(errmsg.str());
|
2008-09-23 18:03:39 +02:00
|
|
|
}
|
|
|
|
|
2008-12-08 22:49:05 +01:00
|
|
|
if ( !TOKEN::Match(tok,"#") && TOKEN::Match(tok->next(),"; %num%") && !TOKEN::Match(tok->tokAt(3), ",") )
|
2008-09-23 18:03:39 +02:00
|
|
|
{
|
2008-09-20 19:34:37 +02:00
|
|
|
std::ostringstream errmsg;
|
2008-12-08 22:49:05 +01:00
|
|
|
errmsg << _tokenizer->fileLine(tok->next()) << ": Redundant code: Found a statement that begins with numeric constant";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(errmsg.str());
|
2008-09-23 18:03:39 +02:00
|
|
|
}
|
|
|
|
}
|
2008-12-09 01:49:32 +01:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Unreachable code below a 'return'
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckOther::unreachableCode()
|
|
|
|
{
|
|
|
|
const TOKEN *tok = TOKEN::findmatch( _tokenizer->tokens(), "[;{}] return" );
|
|
|
|
while ( tok )
|
|
|
|
{
|
|
|
|
// Goto the 'return' token
|
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
// Locate the end of the 'return' statement
|
|
|
|
while ( tok && ! TOKEN::Match(tok, ";") )
|
|
|
|
tok = tok->next();
|
|
|
|
while ( tok && TOKEN::Match(tok->next(), ";") )
|
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
// If there is a statement below the return it is unreachable
|
|
|
|
if (!TOKEN::Match(tok, "; case|default|}|#") && !TOKEN::Match(tok, "; %var% :"))
|
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
|
|
|
errmsg << _tokenizer->fileLine(tok->next()) << ": Unreachable code below a 'return'";
|
|
|
|
_errorLogger->reportErr(errmsg.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the next 'return' statement
|
|
|
|
tok = TOKEN::findmatch( tok, "[;{}] return" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Usage of function variables
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static bool isOp(const TOKEN *tok)
|
|
|
|
{
|
|
|
|
return bool(tok &&
|
|
|
|
(tok->str() == "&&" ||
|
|
|
|
tok->str() == "||" ||
|
|
|
|
tok->str() == "==" ||
|
|
|
|
tok->str() == "!=" ||
|
|
|
|
tok->str() == "<" ||
|
|
|
|
tok->str() == "<=" ||
|
|
|
|
tok->str() == ">" ||
|
|
|
|
tok->str() == ">=" ||
|
|
|
|
tok->str() == "<<" ||
|
2008-12-09 08:25:00 +01:00
|
|
|
TOKEN::Match(tok, "[+-*/&|,[]]")));
|
2008-12-09 01:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckOther::functionVariableUsage()
|
|
|
|
{
|
|
|
|
// Parse all executing scopes..
|
|
|
|
const TOKEN *tok1 = TOKEN::findmatch( _tokenizer->tokens(), ") const| {" );
|
|
|
|
while ( tok1 )
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
for ( const TOKEN *tok = tok1; tok; tok = tok->next() )
|
|
|
|
{
|
|
|
|
if ( tok->str() == "{" )
|
|
|
|
++indentlevel;
|
|
|
|
else if ( tok->str() == "}" )
|
|
|
|
{
|
|
|
|
--indentlevel;
|
|
|
|
if ( indentlevel <= 0 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( TOKEN::Match(tok, "[;{}] bool|char|short|int|long|float|double %var% ;|=") )
|
|
|
|
varUsage[ tok->strAt(2) ] = USAGE_DECLARE;
|
|
|
|
|
|
|
|
if ( TOKEN::Match(tok, "[;{}] bool|char|short|int|long|float|double * %var% ;|=") )
|
|
|
|
varUsage[ tok->strAt(3) ] = USAGE_DECLARE;
|
|
|
|
|
|
|
|
if ( TOKEN::Match(tok, "delete|return %var%") )
|
|
|
|
varUsage[ tok->strAt(1) ] |= USAGE_READ;
|
|
|
|
|
|
|
|
if ( TOKEN::Match(tok, "%var% =") )
|
|
|
|
varUsage[ tok->str() ] |= USAGE_WRITE;
|
|
|
|
|
|
|
|
if ( TOKEN::Match(tok, "else %var% =") )
|
|
|
|
varUsage[ tok->strAt(1) ] |= USAGE_WRITE;
|
|
|
|
|
|
|
|
if ( TOKEN::Match(tok, ">>|& %var%") )
|
|
|
|
varUsage[ tok->strAt(1) ] |= USAGE_WRITE;
|
|
|
|
|
|
|
|
if ((TOKEN::Match(tok,"[(=&!]") || isOp(tok)) && TOKEN::Match(tok->next(), "%var%"))
|
|
|
|
varUsage[ tok->strAt(1) ] |= USAGE_READ;
|
|
|
|
|
2008-12-09 08:25:00 +01:00
|
|
|
if (TOKEN::Match(tok, "-=|+=|*=|/= %var%"))
|
|
|
|
varUsage[ tok->strAt(1) ] |= USAGE_READ;
|
2008-12-10 20:14:38 +01:00
|
|
|
|
2008-12-09 01:49:32 +01:00
|
|
|
if (TOKEN::Match(tok, "%var%") && (tok->next()->str()==")" || isOp(tok->next())))
|
|
|
|
varUsage[ tok->str() ] |= USAGE_READ;
|
|
|
|
|
|
|
|
if ( TOKEN::Match(tok, "[(,] %var% [,)]") )
|
|
|
|
varUsage[ tok->strAt(1) ] |= USAGE_WRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check usage of all variables in the current scope..
|
|
|
|
for ( std::map<std::string, unsigned int>::const_iterator it = varUsage.begin(); it != varUsage.end(); ++it )
|
|
|
|
{
|
|
|
|
std::string varname = it->first;
|
|
|
|
unsigned int usage = it->second;
|
|
|
|
|
2008-12-10 20:14:38 +01:00
|
|
|
if (!std::isalpha(varname[0]))
|
2008-12-09 01:49:32 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if ( ! ( usage & USAGE_DECLARE ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ( usage == USAGE_DECLARE )
|
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
|
|
|
errmsg << _tokenizer->fileLine(tok1->next()) << ": Unused variable '" << varname << "'";
|
|
|
|
_errorLogger->reportErr(errmsg.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( ! (usage & USAGE_READ) )
|
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
|
|
|
errmsg << _tokenizer->fileLine(tok1->next()) << ": Variable '" << varname << "' is assigned a value that is never used";
|
|
|
|
_errorLogger->reportErr(errmsg.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( ! (usage & USAGE_WRITE) )
|
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
|
|
|
errmsg << _tokenizer->fileLine(tok1->next()) << ": Variable '" << varname << "' is not assigned a value";
|
|
|
|
_errorLogger->reportErr(errmsg.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Goto next executing scope..
|
|
|
|
tok1 = TOKEN::findmatch( tok1->next(), ") const| {" );
|
|
|
|
}
|
2008-12-07 20:17:36 +01:00
|
|
|
}
|