cppcheck/main.cpp

181 lines
3.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 "CheckMemoryLeak.h"
#include "CheckBufferOverrun.h"
#include "CheckClass.h"
#include "CheckHeaders.h"
#include "CheckOther.h"
2007-05-24 07:40:45 +02:00
2007-05-07 19:31:35 +02:00
//---------------------------------------------------------------------------
bool Debug = false;
bool ShowWarnings = 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[])
{
2007-05-21 19:16:35 +02:00
const char *fname = NULL;
for (int i = 1; i < argc; i++)
{
#ifdef __linux__
if(strcasecmp(argv[i],"--debug") == 0)
#else
2007-05-21 19:16:35 +02:00
if (stricmp(argv[i],"--debug") == 0)
#endif
2007-05-21 19:16:35 +02:00
Debug = true;
#ifdef __linux__
else if (strcasecmp(argv[i],"-w") == 0)
#else
2007-05-21 19:16:35 +02:00
else if (stricmp(argv[i],"-w") == 0)
#endif
2007-05-21 19:16:35 +02:00
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;
}
//---------------------------------------------------------------------------
// CppCheck - A function that checks a specified file
//---------------------------------------------------------------------------
extern bool HasErrors;
static void CppCheck(const char FileName[])
{
HasErrors = false;
// 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();
if ( ShowWarnings )
{
// Including header which is not needed
2007-06-05 06:51:01 +02:00
// Todo: This is really slow!
WarningIncludeHeader();
}
2007-05-29 08:24:36 +02:00
SimplifyTokenList();
// Create a statement list. It's used by for example 'CheckMemoryLeak'
CreateStatementList();
2007-05-15 20:31:44 +02:00
// Memory leak
CheckMemoryLeak();
// Buffer overruns..
CheckBufferOverrun();
2007-05-15 20:31:44 +02:00
2007-05-21 19:16:35 +02:00
// Warnings
if (ShowWarnings)
{
// 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
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;
WarningRedundantCode();
2007-05-07 19:31:35 +02:00
// if (condition);
WarningIf();
2007-05-07 19:31:35 +02:00
// Dangerous usage of strtok
WarningStrTok();
}
2007-05-19 21:21:14 +02:00
// Dangerous functions, such as 'gets' and 'scanf'
WarningDangerousFunctions();
// Invalid function usage..
InvalidFunctionUsage();
// Clean up tokens..
2007-05-29 19:12:14 +02:00
DeallocateTokens();
// Todo: How should this work? Activated by a command line switch?
//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