2007-05-07 19:31:35 +02:00
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
#include "tokenize.h" // <- Tokenizer
|
2008-02-16 16:46:32 +01:00
|
|
|
#include "CommonCheck.h"
|
2007-05-24 15:07:30 +02:00
|
|
|
|
|
|
|
#include "CheckMemoryLeak.h"
|
|
|
|
#include "CheckBufferOverrun.h"
|
|
|
|
#include "CheckClass.h"
|
|
|
|
#include "CheckHeaders.h"
|
2007-05-25 08:48:56 +02:00
|
|
|
#include "CheckOther.h"
|
2007-05-24 07:40:45 +02:00
|
|
|
|
2008-02-22 08:25:31 +01:00
|
|
|
#include <algorithm>
|
2008-02-17 19:28:51 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
2008-02-18 18:11:34 +01:00
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#include <dir.h>
|
|
|
|
#endif
|
2008-02-21 20:01:24 +01:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#include <glob.h>
|
|
|
|
#endif
|
2008-02-18 18:11:34 +01:00
|
|
|
|
2007-05-07 19:31:35 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2007-05-24 15:07:30 +02:00
|
|
|
bool Debug = false;
|
2007-07-20 17:43:39 +02:00
|
|
|
bool ShowAll = false;
|
|
|
|
bool CheckCodingStyle = false;
|
2007-05-14 18:25:34 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2007-05-07 19:31:35 +02:00
|
|
|
|
2007-05-25 08:48:56 +02:00
|
|
|
static void CppCheck(const char FileName[]);
|
2007-05-19 21:21:14 +02:00
|
|
|
|
2007-05-20 19:02:24 +02:00
|
|
|
|
2008-02-21 18:56:51 +01:00
|
|
|
static void AddFiles( std::vector<std::string> &filenames, const char path[], const char pattern[] )
|
|
|
|
{
|
|
|
|
#ifndef __GNUC__
|
|
|
|
struct ffblk f;
|
|
|
|
for ( int done = findfirst(pattern, &f, 0); ! done; done = findnext(&f) )
|
|
|
|
{
|
|
|
|
std::ostringstream fname;
|
|
|
|
fname << path << f.ff_name;
|
|
|
|
filenames.push_back( fname.str() );
|
|
|
|
}
|
|
|
|
findclose(&f);
|
2008-02-21 20:01:24 +01:00
|
|
|
#else
|
|
|
|
glob_t glob_results;
|
|
|
|
glob(pattern, 0, 0, &glob_results);
|
|
|
|
for ( unsigned int i = 0; i < glob_results.gl_pathc; i++ )
|
|
|
|
{
|
|
|
|
std::ostringstream fname;
|
|
|
|
fname << path << glob_results.gl_pathv[i];
|
|
|
|
filenames.push_back( fname.str() );
|
|
|
|
}
|
|
|
|
globfree(&glob_results);
|
2008-02-21 18:56:51 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RecursiveAddFiles( std::vector<std::string> &filenames, const char path[] )
|
2008-02-21 20:01:24 +01:00
|
|
|
{
|
2008-02-21 18:56:51 +01:00
|
|
|
AddFiles( filenames, path, "*.cpp" );
|
|
|
|
AddFiles( filenames, path, "*.cc" );
|
|
|
|
AddFiles( filenames, path, "*.c" );
|
|
|
|
|
|
|
|
#ifndef __GNUC__
|
|
|
|
struct ffblk f ;
|
|
|
|
for ( int done = findfirst("*", &f, FA_DIREC); ! done; done = findnext(&f) )
|
|
|
|
{
|
|
|
|
if ( f.ff_attrib != FA_DIREC || f.ff_name[0] == '.' )
|
|
|
|
continue;
|
|
|
|
chdir( f.ff_name );
|
|
|
|
std::ostringstream curdir;
|
|
|
|
curdir << path << f.ff_name << "/";
|
|
|
|
RecursiveAddFiles( filenames, curdir.str().c_str() );
|
|
|
|
chdir( ".." );
|
|
|
|
}
|
|
|
|
findclose(&f);
|
2008-02-21 20:01:24 +01:00
|
|
|
#else
|
|
|
|
glob_t glob_results;
|
|
|
|
glob("*", GLOB_ONLYDIR, 0, &glob_results);
|
|
|
|
for ( unsigned int i = 0; i < glob_results.gl_pathc; i++ )
|
|
|
|
{
|
|
|
|
const char *dirname = glob_results.gl_pathv[i];
|
|
|
|
if ( dirname[0] == '.' )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
chdir( dirname );
|
|
|
|
std::ostringstream curdir;
|
|
|
|
curdir << path << dirname << "/";
|
|
|
|
RecursiveAddFiles( filenames, curdir.str().c_str() );
|
|
|
|
chdir( ".." );
|
|
|
|
}
|
|
|
|
globfree(&glob_results);
|
2008-02-21 18:56:51 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-05-07 19:31:35 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2007-05-25 08:48:56 +02:00
|
|
|
// Main function of cppcheck
|
|
|
|
//---------------------------------------------------------------------------
|
2007-05-09 08:50:31 +02:00
|
|
|
|
2007-05-07 19:31:35 +02:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2008-02-17 19:28:51 +01:00
|
|
|
std::vector<std::string> filenames;
|
|
|
|
|
2008-02-21 18:56:51 +01:00
|
|
|
bool Recursive = false;
|
|
|
|
|
2007-05-21 19:16:35 +02:00
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
{
|
2007-07-20 17:43:39 +02:00
|
|
|
if (strcmp(argv[i],"--debug") == 0)
|
2007-05-21 19:16:35 +02:00
|
|
|
Debug = true;
|
|
|
|
|
2007-07-20 17:43:39 +02:00
|
|
|
// Show all messages
|
|
|
|
else if (strcmp(argv[i],"--all") == 0)
|
|
|
|
ShowAll = true;
|
|
|
|
|
|
|
|
// Checking coding style.
|
|
|
|
else if (strcmp(argv[i],"--style")==0)
|
|
|
|
CheckCodingStyle = true;
|
2007-05-21 19:16:35 +02:00
|
|
|
|
2008-02-21 18:56:51 +01:00
|
|
|
else if (strcmp(argv[i],"--recursive")==0)
|
|
|
|
Recursive = true;
|
|
|
|
|
|
|
|
else if (strchr(argv[i],'*'))
|
2008-02-18 18:11:34 +01:00
|
|
|
{
|
2008-02-21 18:56:51 +01:00
|
|
|
AddFiles( filenames, "", argv[i] );
|
2008-02-18 18:11:34 +01:00
|
|
|
}
|
|
|
|
|
2007-05-21 19:16:35 +02:00
|
|
|
else
|
2008-02-21 18:56:51 +01:00
|
|
|
{
|
2008-02-17 19:28:51 +01:00
|
|
|
filenames.push_back( argv[i] );
|
2008-02-21 18:56:51 +01:00
|
|
|
}
|
2007-05-21 19:16:35 +02:00
|
|
|
}
|
2007-05-15 18:51:39 +02:00
|
|
|
|
2008-02-21 18:56:51 +01:00
|
|
|
// No filename given.. automaticly search for available files.
|
|
|
|
if ( Recursive )
|
|
|
|
RecursiveAddFiles( filenames, "" );
|
|
|
|
|
2008-02-17 19:28:51 +01:00
|
|
|
if (filenames.empty())
|
2007-05-07 19:31:35 +02:00
|
|
|
{
|
2007-07-24 08:24:12 +02:00
|
|
|
std::cout << "C/C++ code checking.\n"
|
|
|
|
"\n"
|
|
|
|
"Syntax:\n"
|
2008-02-21 18:56:51 +01:00
|
|
|
" cppcheck [--all] [--style] [--recursive] [filename1] [filename2]\n"
|
2007-07-24 08:24:12 +02:00
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
|
|
|
" --all Normally a message is only shown if cppcheck is sure\n"
|
|
|
|
" it has found a bug.\n"
|
|
|
|
" When this option is given, all messages are shown.\n"
|
|
|
|
"\n"
|
2008-02-21 18:56:51 +01:00
|
|
|
" --style Check coding style.\n"
|
2008-02-21 20:01:24 +01:00
|
|
|
" --recursive Recursively check all *.cpp, *.cc and *.c files\n";
|
2007-05-07 19:31:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-22 08:25:31 +01:00
|
|
|
std::sort( filenames.begin(), filenames.end() );
|
|
|
|
|
2008-02-17 19:28:51 +01:00
|
|
|
for (unsigned int c = 0; c < filenames.size(); c++)
|
|
|
|
{
|
|
|
|
errout.str("");
|
|
|
|
CppCheck(filenames[c].c_str());
|
|
|
|
std::cerr << errout.str();
|
|
|
|
}
|
2008-02-16 16:46:32 +01:00
|
|
|
|
2007-05-09 08:50:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-25 08:48:56 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// CppCheck - A function that checks a specified file
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-07-18 08:12:16 +02:00
|
|
|
extern bool HasErrors;
|
|
|
|
|
2007-05-09 08:50:31 +02:00
|
|
|
static void CppCheck(const char FileName[])
|
|
|
|
{
|
2007-07-18 08:12:16 +02:00
|
|
|
HasErrors = false;
|
2008-02-22 15:30:43 +01:00
|
|
|
OnlyReportUniqueErrors = true;
|
2007-07-18 08:12:16 +02:00
|
|
|
|
2007-07-24 08:24:12 +02:00
|
|
|
std::cout << "Checking " << FileName << "...\n";
|
|
|
|
|
2007-05-25 08:48:56 +02:00
|
|
|
// Tokenize the file
|
2007-05-07 19:31:35 +02:00
|
|
|
tokens = tokens_back = NULL;
|
2007-05-09 08:50:31 +02:00
|
|
|
Files.clear();
|
|
|
|
Tokenize(FileName);
|
2007-05-07 19:31:35 +02:00
|
|
|
|
2007-05-29 08:24:36 +02:00
|
|
|
|
|
|
|
// Check that the memsets are valid.
|
2007-07-18 08:12:16 +02:00
|
|
|
// The 'memset' function can do dangerous things if used wrong.
|
2007-05-29 08:24:36 +02:00
|
|
|
// Important: The checking doesn't work on simplified tokens list.
|
|
|
|
CheckMemset();
|
|
|
|
|
|
|
|
|
2008-02-20 19:20:59 +01:00
|
|
|
// Check for unwanted unsigned division
|
|
|
|
// Not accurate yet. Very important to run it before 'SimplifyTokenList'
|
|
|
|
if ( ShowAll )
|
|
|
|
CheckUnsignedDivision();
|
|
|
|
|
|
|
|
|
2008-02-22 15:30:43 +01:00
|
|
|
// Including header which is not needed (too many false positives)
|
|
|
|
//if ( CheckCodingStyle )
|
|
|
|
// WarningIncludeHeader();
|
2007-06-04 08:31:57 +02:00
|
|
|
|
|
|
|
|
2007-05-29 08:24:36 +02:00
|
|
|
SimplifyTokenList();
|
|
|
|
|
2007-05-15 20:31:44 +02:00
|
|
|
// Memory leak
|
|
|
|
CheckMemoryLeak();
|
|
|
|
|
2007-05-20 19:47:07 +02:00
|
|
|
// Buffer overruns..
|
|
|
|
CheckBufferOverrun();
|
|
|
|
|
2008-01-10 20:01:16 +01:00
|
|
|
// Check that all class constructors are ok.
|
|
|
|
CheckConstructors();
|
2007-05-15 20:31:44 +02:00
|
|
|
|
2007-07-20 17:43:39 +02:00
|
|
|
if (ShowAll)
|
|
|
|
{
|
|
|
|
// Check for "if (a=b)"
|
2007-07-24 08:24:12 +02:00
|
|
|
CheckIfAssignment();
|
|
|
|
|
2007-07-20 17:43:39 +02:00
|
|
|
// Check for case without break
|
2007-10-22 08:59:28 +02:00
|
|
|
// Disabled because it generates many false positives
|
|
|
|
// CheckCaseWithoutBreak();
|
2007-07-20 17:43:39 +02:00
|
|
|
|
|
|
|
// Dangerous usage of strtok
|
2007-10-22 08:59:28 +02:00
|
|
|
// Disabled because it generates false positives
|
|
|
|
//WarningStrTok();
|
2007-07-20 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-18 20:22:16 +02:00
|
|
|
|
2007-07-20 17:43:39 +02:00
|
|
|
// Dangerous functions, such as 'gets' and 'scanf'
|
|
|
|
WarningDangerousFunctions();
|
|
|
|
|
2008-01-10 20:01:16 +01:00
|
|
|
|
2007-07-20 17:43:39 +02:00
|
|
|
// Invalid function usage..
|
|
|
|
InvalidFunctionUsage();
|
|
|
|
|
|
|
|
|
|
|
|
if (CheckCodingStyle)
|
2007-05-21 19:16:35 +02:00
|
|
|
{
|
2007-06-08 20:01:23 +02:00
|
|
|
// Check that all private functions are called.
|
|
|
|
CheckUnusedPrivateFunctions();
|
|
|
|
|
2007-05-21 19:16:35 +02:00
|
|
|
// Found implementation in header
|
|
|
|
WarningHeaderWithImplementation();
|
|
|
|
|
|
|
|
// Warning upon c-style pointer casts
|
|
|
|
const char *ext = strrchr(FileName, '.');
|
2007-07-17 08:15:50 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
if (ext && strcasecmp(ext,".c"))
|
|
|
|
#else
|
2007-05-21 19:16:35 +02:00
|
|
|
if (ext && stricmp(ext,".c"))
|
2007-07-17 08:15:50 +02:00
|
|
|
#endif
|
2007-05-21 19:16:35 +02:00
|
|
|
WarningOldStylePointerCast();
|
|
|
|
|
|
|
|
// Use standard functions instead
|
|
|
|
WarningIsDigit();
|
2007-06-05 06:51:01 +02:00
|
|
|
WarningIsAlpha();
|
2007-05-18 20:22:16 +02:00
|
|
|
|
2007-05-21 19:16:35 +02:00
|
|
|
CheckOperatorEq1();
|
2007-05-07 19:31:35 +02:00
|
|
|
|
2007-06-08 20:01:23 +02:00
|
|
|
// if (a) delete a;
|
|
|
|
WarningRedundantCode();
|
2007-05-07 19:31:35 +02:00
|
|
|
|
2007-06-08 20:01:23 +02:00
|
|
|
// if (condition);
|
|
|
|
WarningIf();
|
|
|
|
}
|
2007-05-18 20:22:16 +02:00
|
|
|
|
2007-05-19 21:21:14 +02:00
|
|
|
|
2007-05-09 08:50:31 +02:00
|
|
|
// Clean up tokens..
|
2007-05-29 19:12:14 +02:00
|
|
|
DeallocateTokens();
|
2007-07-18 08:12:16 +02:00
|
|
|
|
|
|
|
// Todo: How should this work? Activated by a command line switch?
|
2007-07-24 08:24:12 +02:00
|
|
|
if ( ! HasErrors )
|
|
|
|
std::cout << "No errors found\n";
|
2007-05-07 19:31:35 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-10 19:25:52 +02:00
|
|
|
|
|
|
|
|
2007-05-19 21:21:14 +02:00
|
|
|
|
|
|
|
|
2007-05-20 19:02:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-21 19:16:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-07-20 17:43:39 +02:00
|
|
|
|