cppcheck/main.cpp

358 lines
8.9 KiB
C++
Raw Normal View History

2007-05-07 19:31:35 +02:00
2007-05-16 19:16:25 +02:00
// Todo: Output progress? Using commandline option "--progress"?
2007-05-07 19:31:35 +02:00
#include <iostream>
#include <sstream>
2007-05-24 07:40:45 +02:00
#include "tokenize.h" // <- Tokenizer
#include "statements.h" // <- Statement list
#include "CommonCheck.h"
#include "CheckMemoryLeak.h"
#include "CheckBufferOverrun.h"
#include "CheckClass.h"
#include "CheckHeaders.h"
2007-05-24 07:40:45 +02:00
2007-05-07 19:31:35 +02:00
//---------------------------------------------------------------------------
bool Debug = false;
2007-05-21 19:16:35 +02:00
static bool ShowWarnings = false;
//---------------------------------------------------------------------------
2007-05-07 19:31:35 +02:00
//---------------------------------------------------------------------------
2007-05-15 20:31:44 +02:00
2007-05-07 19:31:35 +02:00
// Casting
void WarningOldStylePointerCast();
// Use standard functions instead
void WarningIsDigit();
// Redundant code
void WarningRedundantCode();
2007-05-19 21:21:14 +02:00
// Warning upon: if (condition);
void WarningIf();
// Using dangerous functions
void WarningDangerousFunctions();
2007-05-07 19:31:35 +02:00
//---------------------------------------------------------------------------
static void CppCheck(const char FileName[]);
2007-05-07 19:31:35 +02:00
int main(int argc, char* argv[])
{
2007-05-21 19:16:35 +02:00
const char *fname = NULL;
for (int i = 1; i < argc; i++)
{
if (stricmp(argv[i],"--debug") == 0)
Debug = true;
else if (stricmp(argv[i],"-w") == 0)
ShowWarnings = true;
else
fname = argv[i];
}
2007-05-21 19:16:35 +02:00
if (!fname)
2007-05-07 19:31:35 +02:00
{
2007-05-21 19:16:35 +02:00
std::cout << "checkcode [-w] filename\n";
std::cout << "-w : enables extra warnings\n";
2007-05-07 19:31:35 +02:00
return 0;
}
2007-05-21 19:16:35 +02:00
CppCheck(fname);
return 0;
}
static void CppCheck(const char FileName[])
{
2007-05-07 19:31:35 +02:00
tokens = tokens_back = NULL;
Files.clear();
Tokenize(FileName);
2007-05-07 19:31:35 +02:00
2007-05-16 19:16:25 +02:00
CreateStatementList();
2007-05-15 20:31:44 +02:00
// Memory leak
CheckMemoryLeak();
// Buffer overruns..
CheckBufferOverrun();
2007-05-15 20:31:44 +02:00
//std::ofstream f("tokens.txt");
//for (TOKEN *tok = tokens; tok; tok = tok->next)
// f << "[" << Files[tok->FileIndex] << ":" << tok->linenr << "]:" << tok->str << '\n';
//f.close();
2007-05-07 19:31:35 +02:00
// Check that all private functions are called.
// Temporarily inactivated to avoid any false positives
CheckUnusedPrivateFunctions();
2007-05-07 19:31:35 +02:00
// Check that the memsets are valid.
// This function can do dangerous things if used wrong.
CheckMemset();
2007-05-21 19:16:35 +02:00
// Warnings
if (ShowWarnings)
{
// Found implementation in header
WarningHeaderWithImplementation();
// Warning upon c-style pointer casts
const char *ext = strrchr(FileName, '.');
if (ext && stricmp(ext,".c"))
WarningOldStylePointerCast();
// Use standard functions instead
WarningIsDigit();
2007-05-21 19:16:35 +02:00
// Including header
//WarningIncludeHeader();
2007-05-09 20:10:10 +02:00
2007-05-21 19:16:35 +02:00
CheckOperatorEq1();
2007-05-07 19:31:35 +02:00
2007-05-21 19:16:35 +02:00
// Check that all class constructors are ok.
// Temporarily inactivated to avoid any false positives
//CheckConstructors();
}
2007-05-07 19:31:35 +02:00
// if (a) delete a;
2007-05-07 19:31:35 +02:00
WarningRedundantCode();
2007-05-19 21:21:14 +02:00
// if (condition);
WarningIf();
// Dangerous functions, such as 'gets' and 'scanf'
WarningDangerousFunctions();
// Clean up tokens..
while (tokens)
{
TOKEN *next = tokens->next;
free(tokens->str);
delete tokens;
tokens = next;
}
2007-05-07 19:31:35 +02:00
}
//---------------------------------------------------------------------------
bool IsName(const char str[])
{
return (str[0]=='_' || std::isalpha(str[0]));
2007-05-07 19:31:35 +02:00
}
bool IsNumber(const char str[])
{
return std::isdigit(str[0]);
2007-05-07 19:31:35 +02:00
}
//---------------------------------------------------------------------------
2007-05-16 12:32:13 +02:00
2007-05-07 19:31:35 +02:00
//---------------------------------------------------------------------------
// Warning on C-Style casts.. p = (kalle *)foo;
//---------------------------------------------------------------------------
void WarningOldStylePointerCast()
{
for (TOKEN *tok = tokens; tok; tok = tok->next)
{
// Old style pointer casting..
if (!match(tok, "( type * ) var"))
continue;
// Is "type" a class?
const char *pattern[] = {"class","",NULL};
pattern[1] = getstr(tok, 1);
if (!findtoken(tokens, pattern))
continue;
std::ostringstream ostr;
ostr << FileLine(tok) << ": C-style pointer casting";
ReportErr(ostr.str());
}
}
//---------------------------------------------------------------------------
// Use standard function "isdigit" instead
//---------------------------------------------------------------------------
void WarningIsDigit()
{
for (TOKEN *tok = tokens; tok; tok = tok->next)
{
bool err = false;
err |= match(tok, "var >= '0' && var <= '9'");
err |= match(tok, "* var >= '0' && * var <= '9'");
err |= match(tok, "( var >= '0' ) && ( var <= '9' )");
err |= match(tok, "( * var >= '0' ) && ( * var <= '9' )");
if (err)
{
std::ostringstream ostr;
ostr << FileLine(tok) << ": The condition can be simplified; use 'isdigit'";
ReportErr(ostr.str());
}
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Redundant code..
//---------------------------------------------------------------------------
2007-05-07 19:31:35 +02:00
void WarningRedundantCode()
{
2007-05-16 19:16:25 +02:00
// if (p) delete p
2007-05-07 19:31:35 +02:00
for (TOKEN *tok = tokens; tok; tok = tok->next)
{
if (strcmp(tok->str,"if"))
continue;
const char *varname1 = NULL;
TOKEN *tok2 = NULL;
if (match(tok,"if ( var )"))
{
varname1 = getstr(tok, 2);
tok2 = gettok(tok, 4);
}
else if (match(tok,"if ( var != NULL )"))
{
varname1 = getstr(tok, 2);
tok2 = gettok(tok, 6);
}
if (varname1==NULL || tok2==NULL)
continue;
bool err = false;
if (match(tok2,"delete var ;"))
err = (strcmp(getstr(tok2,1),varname1)==0);
else if (match(tok2,"{ delete var ; }"))
err = (strcmp(getstr(tok2,2),varname1)==0);
2007-05-16 19:16:25 +02:00
else if (match(tok2,"delete [ ] var ;"))
err = (strcmp(getstr(tok2,1),varname1)==0);
else if (match(tok2,"{ delete [ ] var ; }"))
err = (strcmp(getstr(tok2,2),varname1)==0);
2007-05-07 19:31:35 +02:00
else if (match(tok2,"free ( var )"))
err = (strcmp(getstr(tok2,2),varname1)==0);
else if (match(tok2,"{ free ( var ) ; }"))
err = (strcmp(getstr(tok2,3),varname1)==0);
if (err)
{
std::ostringstream ostr;
ostr << FileLine(tok) << ": Redundant condition. It is safe to deallocate a NULL pointer";
2007-05-07 19:31:35 +02:00
ReportErr(ostr.str());
}
}
2007-05-16 19:16:25 +02:00
// TODO
// if (haystack.find(needle) != haystack.end())
// haystack.remove(needle);
2007-05-07 19:31:35 +02:00
}
//---------------------------------------------------------------------------
2007-05-07 19:31:35 +02:00
2007-05-19 21:21:14 +02:00
//---------------------------------------------------------------------------
// if (condition);
//---------------------------------------------------------------------------
void WarningIf()
{
for (TOKEN *tok = tokens; tok; tok = tok->next)
{
if (strcmp(tok->str,"if")==0)
{
int parlevel = 0;
2007-05-20 07:49:51 +02:00
for (TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next)
2007-05-19 21:21:14 +02:00
{
2007-05-20 07:49:51 +02:00
if (tok2->str[0]=='(')
2007-05-19 21:21:14 +02:00
parlevel++;
2007-05-20 07:49:51 +02:00
else if (tok2->str[0]==')')
2007-05-19 21:21:14 +02:00
{
parlevel--;
if (parlevel<=0)
{
2007-05-20 07:49:51 +02:00
if (strcmp(getstr(tok2,1), ";") == 0)
2007-05-19 21:21:14 +02:00
{
std::ostringstream ostr;
ostr << FileLine(tok) << ": Found \"if (condition);\"";
ReportErr(ostr.str());
}
break;
}
}
}
}
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Dangerous functions
//---------------------------------------------------------------------------
void WarningDangerousFunctions()
{
for (TOKEN *tok = tokens; tok; tok = tok->next)
{
if (match(tok, "gets ("))
{
std::ostringstream ostr;
ostr << FileLine(tok) << ": Found 'gets'. You should use 'fgets' instead";
ReportErr(ostr.str());
}
2007-05-19 21:21:14 +02:00
else if (match(tok, "scanf (") && strcmp(getstr(tok,2),"\"%s\"") == 0)
{
std::ostringstream ostr;
ostr << FileLine(tok) << ": Found 'scanf'. You should use 'fgets' instead";
ReportErr(ostr.str());
}
}
}
2007-05-21 19:16:35 +02:00