cppcheck/main.cpp

216 lines
5.0 KiB
C++
Raw Normal View History

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"
#include "CheckMemoryLeak.h"
#include "CheckBufferOverrun.h"
#include "CheckClass.h"
#include "CheckHeaders.h"
#include "CheckOther.h"
2007-05-24 07:40:45 +02:00
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
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[]);
2007-05-19 21:21:14 +02:00
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;
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
2008-02-18 18:11:34 +01:00
// Filenames
else if ( strchr(argv[i], '*') )
{
#ifndef __GNUC__
struct ffblk f;
int done = findfirst(argv[i], &f, 0);
while ( ! done )
{
filenames.push_back( f.ff_name );
done = findnext(&f);
}
findclose(&f);
#endif
}
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
}
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-17 19:28:51 +01:00
" cppcheck [--all] [--style] 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";
2007-05-07 19:31:35 +02:00
return 0;
}
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
return 0;
}
//---------------------------------------------------------------------------
// CppCheck - A function that checks a specified file
//---------------------------------------------------------------------------
extern bool HasErrors;
static void CppCheck(const char FileName[])
{
HasErrors = false;
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
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();
// Including header which is not needed
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
// Found implementation in header
WarningHeaderWithImplementation();
// Warning upon c-style pointer casts
const char *ext = strrchr(FileName, '.');
#ifdef __linux__
if (ext && strcasecmp(ext,".c"))
#else
2007-05-21 19:16:35 +02:00
if (ext && stricmp(ext,".c"))
#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-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();
}
2007-05-19 21:21:14 +02:00
// Clean up tokens..
2007-05-29 19:12:14 +02:00
DeallocateTokens();
// 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-19 21:21:14 +02:00
2007-05-21 19:16:35 +02:00