cppcheck/main.cpp

302 lines
9.2 KiB
C++
Raw Normal View History

2008-10-30 17:46:05 +01:00
/*
2008-10-26 08:55:15 +01:00
* c++check - c/c++ syntax checking
* Copyright (C) 2007 Daniel Marjamäki
2008-04-11 08:58:45 +02:00
*
2008-10-26 08:55:15 +01:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
2008-04-11 08:58:45 +02:00
*
2008-10-26 08:55:15 +01:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
2008-04-11 08:58:45 +02:00
*
2008-10-26 08:55:15 +01:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/
*/
2008-04-11 08:58:45 +02:00
#include "preprocessor.h" // preprocessor.
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"
#include "FileLister.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>
#include <fstream>
#include <map>
2008-08-30 20:29:37 +02:00
2007-05-07 19:31:35 +02:00
//---------------------------------------------------------------------------
bool Debug = false;
bool ShowAll = false;
bool CheckCodingStyle = false;
bool ErrorsOnly = false;
//---------------------------------------------------------------------------
2007-05-07 19:31:35 +02:00
static void CppCheck(const std::string &code, const char FileName[], unsigned int FileId);
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[])
{
std::vector<std::string> pathnames;
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
// Only print something when there are errors
else if (strcmp(argv[i],"--errorsonly")==0)
ErrorsOnly = true;
else if (strcmp(argv[i],"--recursive")==0)
Recursive = true;
else
pathnames.push_back( argv[i] );
}
std::vector<std::string> filenames;
// --recursive was used
if ( Recursive )
{
if( pathnames.size() == 0 )
2008-02-18 18:11:34 +01:00
{
// Handle situation: cppcheck --recursive
FileLister::RecursiveAddFiles( filenames, "", true );
2008-02-18 18:11:34 +01:00
}
2007-05-21 19:16:35 +02:00
else
{
// Handle situation: cppcheck --recursive path1 path2
// Execute RecursiveAddFiles() to each given file parameter
std::vector<std::string>::const_iterator iter;
for(iter=pathnames.begin(); iter!=pathnames.end(); iter++)
FileLister::RecursiveAddFiles( filenames, iter->c_str(), true );
}
2007-05-21 19:16:35 +02:00
}
else
{
std::vector<std::string>::const_iterator iter;
for(iter=pathnames.begin(); iter!=pathnames.end(); iter++)
FileLister::RecursiveAddFiles( filenames, iter->c_str(), false );
}
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] [--errorsonly] [--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"
" --errorsonly Only print something when there is an error\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("");
std::string fname = filenames[c];
// If only errors are printed, print filename after the check
if (!ErrorsOnly)
std::cout << "Checking " << fname << "...\n";
std::ifstream fin( fname.c_str() );
std::map<std::string, std::string> code;
Preprocessor preprocessor;
preprocessor.preprocess(fin, code, fname);
for ( std::map<std::string,std::string>::const_iterator it = code.begin(); it != code.end(); ++it )
CppCheck(it->second, filenames[c].c_str(), c);
if (ErrorsOnly)
{
if ( !errout.str().empty() )
{
std::cout << "Errors found in " << fname << ":\n";
std::cerr << errout.str();
}
}
else
{
if ( errout.str().empty() )
std::cout << "No errors found\n";
else
std::cerr << errout.str();
}
2008-02-17 19:28:51 +01:00
}
2008-02-16 16:46:32 +01:00
// This generates false positives - especially for libraries
if ( ShowAll && CheckCodingStyle && filenames.size() > 1 )
{
errout.str("");
std::cout << "Checking usage of global functions (this may take several minutes)..\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 std::string &code, const char FileName[], unsigned int FileId)
{
Tokenizer tokenizer;
OnlyReportUniqueErrors = true;
// Tokenize the file
{
std::istringstream istr(code);
tokenizer.Tokenize(istr, 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.
CheckClass checkClass( &tokenizer );
checkClass.CheckMemset();
2007-05-29 08:24:36 +02:00
// Check for unsigned divisions where one operand is signed
// Very important to run it before 'SimplifyTokenList'
CheckOther checkOther( &tokenizer );
checkOther.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 )
checkOther.CheckCharVariable();
2008-08-28 08:36:30 +02:00
// Including header which is not needed (too many false positives)
// if ( CheckCodingStyle )
// {
// CheckHeaders checkHeaders( &tokenizer );
// checkHeaders.WarningIncludeHeader();
// }
tokenizer.SimplifyTokenList();
2007-05-29 08:24:36 +02:00
// Memory leak
CheckMemoryLeakClass checkMemoryLeak( &tokenizer );
checkMemoryLeak.CheckMemoryLeak();
2007-05-15 20:31:44 +02:00
// Buffer overruns..
CheckBufferOverrunClass checkBufferOverrun( &tokenizer );
checkBufferOverrun.CheckBufferOverrun();
// Check that all class constructors are ok.
checkClass.CheckConstructors();
2007-05-15 20:31:44 +02:00
if (ShowAll)
{
// Check for "if (a=b)"
checkOther.CheckIfAssignment();
2007-07-24 08:24:12 +02:00
// 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'
checkBufferOverrun.WarningDangerousFunctions();
// Invalid function usage..
checkOther.InvalidFunctionUsage();
if (CheckCodingStyle)
2007-05-21 19:16:35 +02:00
{
// Check that all private functions are called.
checkClass.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)
checkOther.WarningOldStylePointerCast();
2007-05-21 19:16:35 +02:00
// Use standard functions instead
checkOther.WarningIsDigit();
checkOther.WarningIsAlpha();
checkClass.CheckOperatorEq1();
2007-05-07 19:31:35 +02:00
// if (a) delete a;
checkOther.WarningRedundantCode();
2007-05-07 19:31:35 +02:00
// if (condition);
checkOther.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
checkOther.CheckConstantFunctionParameter();
2008-08-28 08:36:30 +02:00
// Unused struct members..
checkOther.CheckStructMemberUsage();
// Check for various types of incomplete statements that could for example
// mean that an ';' has been added by accident
checkOther.CheckIncompleteStatement();
}
2007-05-19 21:21:14 +02:00
// Clean up tokens..
tokenizer.DeallocateTokens();
2007-05-07 19:31:35 +02:00
}
//---------------------------------------------------------------------------