cppcheck/main.cpp

360 lines
9.6 KiB
C++
Raw Normal View History

2007-05-07 19:31:35 +02:00
2008-04-11 08:58:45 +02:00
/**
* C/C++ Checking
*
* This program will check either the file(s) specified or all source files in
* all sub-directories
*
* Some checks are commented out below because they generate too many false
* positives. There are cases when the reported message is true, but it is
* what the developer wanted to do (missing break after case).
* It is safe to uncomment any of the checks.
*
* Design
* The token list is a stringlist with the same contents and structure
* as the file.
* All checks will check for errors in the token list.
*
**/
2007-05-24 07:40:45 +02:00
#include "tokenize.h" // <- Tokenizer
2008-02-16 16:46:32 +01:00
#include "CommonCheck.h"
#include "CheckMemoryLeak.h"
#include "CheckBufferOverrun.h"
#include "CheckClass.h"
#include "CheckHeaders.h"
#include "CheckOther.h"
2007-05-24 07:40:45 +02:00
#include <algorithm>
2008-02-17 19:28:51 +01:00
#include <iostream>
#include <sstream>
2008-08-30 20:29:37 +02:00
#include <cstring>
2008-02-17 19:28:51 +01:00
#ifdef __GNUC__
#include <glob.h>
#include <unistd.h>
#endif
#ifdef __BORLANDC__
#include <dir.h>
#endif
#ifdef _MSC_VER
#include <windows.h>
#endif
2008-02-18 18:11:34 +01:00
2007-05-07 19:31:35 +02:00
//---------------------------------------------------------------------------
bool Debug = false;
bool ShowAll = false;
bool CheckCodingStyle = false;
//---------------------------------------------------------------------------
2007-05-07 19:31:35 +02:00
static void CppCheck(const char FileName[], unsigned int FileId);
2007-05-19 21:21:14 +02:00
static void AddFiles( std::vector<std::string> &filenames, const char path[], const char pattern[] )
{
#ifdef __GNUC__
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);
#endif
#ifdef __BORLANDC__
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);
#endif
#ifdef _MSC_VER
WIN32_FIND_DATA ffd;
HANDLE hFind = FindFirstFile(pattern, &ffd);
if (INVALID_HANDLE_VALUE != hFind)
{
do
{
std::ostringstream fname;
fname << path << ffd.cFileName;
filenames.push_back( fname.str() );
}
while (FindNextFile(hFind, &ffd) != 0);
}
#endif
}
static void RecursiveAddFiles( std::vector<std::string> &filenames, const char path[] )
{
AddFiles( filenames, path, "*.cpp" );
AddFiles( filenames, path, "*.cc" );
AddFiles( filenames, path, "*.c" );
#ifdef __GNUC__
// gcc / cygwin..
glob_t glob_results;
glob("*", GLOB_MARK, 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;
if ( strchr(dirname, '/') == 0 )
continue;
chdir( dirname );
std::ostringstream curdir;
curdir << path << dirname;
RecursiveAddFiles( filenames, curdir.str().c_str() );
chdir( ".." );
}
globfree(&glob_results);
#endif
#ifdef __BORLANDC__
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);
#endif
#ifdef _MSC_VER
WIN32_FIND_DATA ffd;
HANDLE hFind = FindFirstFile("*", &ffd);
if (INVALID_HANDLE_VALUE != hFind)
{
do
{
if ( (ffd.cFileName[0]!='.') &&
(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
{
SetCurrentDirectory( ffd.cFileName );
std::ostringstream curdir;
curdir << path << ffd.cFileName << "/";
RecursiveAddFiles( filenames, curdir.str().c_str() );
SetCurrentDirectory( ".." );
}
}
while (FindNextFile(hFind, &ffd) != 0);
}
#endif
}
2007-05-07 19:31:35 +02:00
//---------------------------------------------------------------------------
// Main function of cppcheck
//---------------------------------------------------------------------------
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;
bool Recursive = false;
2007-05-21 19:16:35 +02:00
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i],"--debug") == 0)
2007-05-21 19:16:35 +02:00
Debug = true;
// 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
else if (strcmp(argv[i],"--recursive")==0)
Recursive = true;
else if (strchr(argv[i],'*'))
2008-02-18 18:11:34 +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-17 19:28:51 +01:00
filenames.push_back( argv[i] );
}
2007-05-21 19:16:35 +02: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"
" 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"
" --style Check coding style.\n"
" --recursive Recursively check all *.cpp, *.cc and *.c files\n";
2007-05-07 19:31:35 +02:00
return 0;
}
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(), c);
2008-02-17 19:28:51 +01:00
std::cerr << errout.str();
}
2008-02-16 16:46:32 +01:00
if ( CheckCodingStyle && filenames.size() > 1 )
{
errout.str("");
std::cout << "Checking usage of global functions..\n";
CheckGlobalFunctionUsage(filenames);
if ( ! errout.str().empty() )
{
std::cerr << "\n";
std::cerr << errout.str();
}
}
return 0;
}
//---------------------------------------------------------------------------
// CppCheck - A function that checks a specified file
//---------------------------------------------------------------------------
static void CppCheck(const char FileName[], unsigned int FileId)
{
OnlyReportUniqueErrors = true;
2007-07-24 08:24:12 +02:00
std::cout << "Checking " << FileName << "...\n";
// Tokenize the file
2007-05-07 19:31:35 +02:00
tokens = tokens_back = NULL;
Files.clear();
Tokenize(FileName);
2007-05-07 19:31:35 +02:00
FillFunctionList(FileId);
2007-05-29 08:24:36 +02:00
// Check that the memsets are valid.
// 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();
// Check for unsigned divisions where one operand is signed
// Very important to run it before 'SimplifyTokenList'
CheckUnsignedDivision();
2008-08-28 08:36:30 +02:00
// Give warning when using char variable as array index
// Doesn't work on simplified token list ('unsigned')
if ( ShowAll )
CheckCharVariable();
// Including header which is not needed (too many false positives)
//if ( CheckCodingStyle )
// WarningIncludeHeader();
2007-05-29 08:24:36 +02:00
SimplifyTokenList();
2007-05-15 20:31:44 +02:00
// Memory leak
CheckMemoryLeak();
// Buffer overruns..
CheckBufferOverrun();
// Check that all class constructors are ok.
CheckConstructors();
2007-05-15 20:31:44 +02:00
if (ShowAll)
{
// Check for "if (a=b)"
2007-07-24 08:24:12 +02:00
CheckIfAssignment();
// Check for case without break
// Disabled because it generates many false positives
// CheckCaseWithoutBreak();
// Dangerous usage of strtok
// Disabled because it generates false positives
//WarningStrTok();
}
// Dangerous functions, such as 'gets' and 'scanf'
WarningDangerousFunctions();
// Invalid function usage..
InvalidFunctionUsage();
if (CheckCodingStyle)
2007-05-21 19:16:35 +02:00
{
// Check that all private functions are called.
CheckUnusedPrivateFunctions();
2007-05-21 19:16:35 +02:00
// Warning upon c-style pointer casts
const char *ext = strrchr(FileName, '.');
if (ext && strcmp(ext,".cpp")==0)
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-21 19:16:35 +02:00
CheckOperatorEq1();
2007-05-07 19:31:35 +02:00
// if (a) delete a;
WarningRedundantCode();
2007-05-07 19:31:35 +02:00
// if (condition);
WarningIf();
2008-04-11 08:58:45 +02:00
// Variable scope (check if the scope could be limited)
//CheckVariableScope();
// Check if a constant function parameter is passed by value
2008-08-28 08:36:30 +02:00
CheckConstantFunctionParameter();
2008-08-28 08:36:30 +02:00
// Unused struct members..
CheckStructMemberUsage();
// Check for various types of incomplete statements that could for example
// mean that an ';' has been added by accident
CheckIncompleteStatement();
}
2007-05-19 21:21:14 +02:00
// Clean up tokens..
2007-05-29 19:12:14 +02:00
DeallocateTokens();
if ( errout.str().empty() )
2007-07-24 08:24:12 +02:00
std::cout << "No errors found\n";
2007-05-07 19:31:35 +02:00
}
//---------------------------------------------------------------------------