2008-11-14 23:26:38 +01:00
|
|
|
/*
|
|
|
|
* c++check - c/c++ syntax checking
|
|
|
|
* Copyright (C) 2007-2008 Daniel Marjamäki and Reijo Tomperi
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
#include "cppcheck.h"
|
|
|
|
|
|
|
|
#include "preprocessor.h" // preprocessor.
|
2008-11-22 20:39:12 +01:00
|
|
|
#include "tokenize.h" // <- Tokenizer
|
2008-11-15 23:41:56 +01:00
|
|
|
|
2008-11-14 23:26:38 +01:00
|
|
|
#include "CheckMemoryLeak.h"
|
|
|
|
#include "CheckBufferOverrun.h"
|
|
|
|
#include "CheckClass.h"
|
|
|
|
#include "CheckHeaders.h"
|
|
|
|
#include "CheckOther.h"
|
2008-11-23 07:21:44 +01:00
|
|
|
#include "CheckFunctionUsage.h"
|
2008-11-14 23:26:38 +01:00
|
|
|
#include "FileLister.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <sstream>
|
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-24 22:50:20 +01:00
|
|
|
CppCheck::CppCheck( ErrorLogger &errorLogger ) : _checkFunctionUsage( this )
|
2008-11-14 23:26:38 +01:00
|
|
|
{
|
2008-11-24 22:50:20 +01:00
|
|
|
_errorLogger = &errorLogger;
|
2008-11-14 23:26:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CppCheck::~CppCheck()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-11-23 23:01:09 +01:00
|
|
|
void CppCheck::settings( const Settings &settings )
|
|
|
|
{
|
|
|
|
_settings = settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppCheck::addFile( const std::string &path )
|
|
|
|
{
|
|
|
|
_filenames.push_back( path );
|
|
|
|
}
|
|
|
|
|
2008-11-25 21:56:11 +01:00
|
|
|
void CppCheck::addFile( const std::string &path, const std::string &content )
|
|
|
|
{
|
|
|
|
_filenames.push_back( path );
|
|
|
|
_fileContents[ path ] = content;
|
|
|
|
}
|
|
|
|
|
2008-12-06 10:27:28 +01:00
|
|
|
std::string CppCheck::parseFromArgs( int argc, const char* const argv[] )
|
2008-11-14 23:26:38 +01:00
|
|
|
{
|
|
|
|
std::vector<std::string> pathnames;
|
|
|
|
bool Recursive = false;
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
{
|
2008-11-30 20:32:36 +01:00
|
|
|
// Flag used for various purposes during debugging
|
2008-11-14 23:26:38 +01:00
|
|
|
if (strcmp(argv[i],"--debug") == 0)
|
|
|
|
_settings._debug = true;
|
|
|
|
|
|
|
|
// Show all messages
|
|
|
|
else if (strcmp(argv[i],"--all") == 0)
|
|
|
|
_settings._showAll = true;
|
|
|
|
|
|
|
|
// Only print something when there are errors
|
|
|
|
else if (strcmp(argv[i],"--errorsonly")==0)
|
|
|
|
_settings._errorsOnly = true;
|
|
|
|
|
2008-11-30 10:08:07 +01:00
|
|
|
// Checking coding style
|
|
|
|
else if (strcmp(argv[i],"--style")==0)
|
|
|
|
_settings._checkCodingStyle = true;
|
|
|
|
|
|
|
|
// Recursively check source files
|
2008-11-14 23:26:38 +01:00
|
|
|
else if (strcmp(argv[i],"--recursive")==0)
|
|
|
|
Recursive = true;
|
2008-11-30 10:08:07 +01:00
|
|
|
|
|
|
|
// Verbose error messages (configuration info)
|
|
|
|
else if (strcmp(argv[i],"--verbose")==0)
|
|
|
|
_settings._verbose = true;
|
|
|
|
|
2008-11-14 23:26:38 +01:00
|
|
|
else
|
|
|
|
pathnames.push_back( argv[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// --recursive was used
|
|
|
|
if ( Recursive )
|
|
|
|
{
|
|
|
|
if( pathnames.size() == 0 )
|
|
|
|
{
|
|
|
|
// Handle situation: cppcheck --recursive
|
2008-11-23 23:01:09 +01:00
|
|
|
FileLister::RecursiveAddFiles( _filenames, "", true );
|
2008-11-14 23:26:38 +01: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++)
|
2008-11-23 23:01:09 +01:00
|
|
|
FileLister::RecursiveAddFiles( _filenames, iter->c_str(), true );
|
2008-11-14 23:26:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector<std::string>::const_iterator iter;
|
|
|
|
for(iter=pathnames.begin(); iter!=pathnames.end(); iter++)
|
2008-11-23 23:01:09 +01:00
|
|
|
FileLister::RecursiveAddFiles( _filenames, iter->c_str(), false );
|
2008-11-14 23:26:38 +01:00
|
|
|
}
|
|
|
|
|
2008-11-23 23:01:09 +01:00
|
|
|
if (_filenames.empty())
|
2008-11-14 23:26:38 +01:00
|
|
|
{
|
2008-11-23 23:01:09 +01:00
|
|
|
std::ostringstream oss;
|
2008-11-29 14:58:28 +01:00
|
|
|
oss << "c++check 1.26\n"
|
|
|
|
"\n"
|
|
|
|
"C/C++ code checking\n"
|
2008-11-23 23:01:09 +01:00
|
|
|
"\n"
|
|
|
|
"Syntax:\n"
|
2008-11-30 10:08:07 +01:00
|
|
|
" cppcheck [--all] [--errorsonly] [--recursive] [--style] [--verbose] [filename1] [filename2]\n"
|
2008-11-23 23:01:09 +01:00
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
2008-11-29 14:58:28 +01:00
|
|
|
" --all Make the checking more sensitive. More bugs are detected,\n"
|
|
|
|
" but there are also more false positives\n"
|
|
|
|
" --errorsonly Only print error messages\n"
|
2008-11-30 10:08:07 +01:00
|
|
|
" --recursive Recursively check all *.cpp, *.cxx, *.cc and *.c files\n"
|
|
|
|
" --style Check coding style\n"
|
2008-11-30 11:38:58 +01:00
|
|
|
" --verbose More detailed error reports\n";
|
2008-11-23 23:01:09 +01:00
|
|
|
return oss.str();
|
2008-11-14 23:26:38 +01:00
|
|
|
}
|
|
|
|
|
2008-11-25 21:56:11 +01:00
|
|
|
// Check function usage if "--style" and "--all" was given.
|
2008-11-29 14:58:28 +01:00
|
|
|
// There will be false positives for exported library functions
|
2008-11-23 23:01:09 +01:00
|
|
|
if ( _settings._showAll && _settings._checkCodingStyle )
|
|
|
|
_settings._checkFunctionUsage = true;
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppCheck::check()
|
|
|
|
{
|
|
|
|
std::sort( _filenames.begin(), _filenames.end() );
|
|
|
|
for (unsigned int c = 0; c < _filenames.size(); c++)
|
2008-11-14 23:26:38 +01:00
|
|
|
{
|
2008-11-23 12:08:07 +01:00
|
|
|
_errout.str("");
|
2008-11-23 23:01:09 +01:00
|
|
|
std::string fname = _filenames[c];
|
2008-11-14 23:26:38 +01:00
|
|
|
|
|
|
|
// If only errors are printed, print filename after the check
|
2008-11-24 22:50:20 +01:00
|
|
|
if ( _settings._errorsOnly == false )
|
|
|
|
_errorLogger->reportOut( std::string( "Checking " ) + fname + std::string( "..." ) );
|
2008-11-14 23:26:38 +01:00
|
|
|
|
2008-11-20 23:19:26 +01:00
|
|
|
Preprocessor preprocessor( this );
|
2008-11-25 21:56:11 +01:00
|
|
|
std::map<std::string, std::string> code;
|
|
|
|
if( _fileContents.size() > 0 && _fileContents.find( _filenames[c] ) != _fileContents.end() )
|
|
|
|
{
|
|
|
|
// File content was given as a string
|
|
|
|
std::istringstream iss( _fileContents[ _filenames[c] ] );
|
|
|
|
preprocessor.preprocess(iss, code, fname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Only file name was given, read the content from file
|
|
|
|
std::ifstream fin( fname.c_str() );
|
|
|
|
preprocessor.preprocess(fin, code, fname);
|
|
|
|
}
|
|
|
|
|
2008-11-14 23:26:38 +01:00
|
|
|
for ( std::map<std::string,std::string>::const_iterator it = code.begin(); it != code.end(); ++it )
|
2008-11-25 21:56:11 +01:00
|
|
|
{
|
2008-11-30 10:08:07 +01:00
|
|
|
cfg = it->first;
|
2008-11-23 23:01:09 +01:00
|
|
|
checkFile(it->second, _filenames[c].c_str());
|
2008-11-25 21:56:11 +01:00
|
|
|
}
|
2008-11-14 23:26:38 +01:00
|
|
|
|
2008-11-24 22:50:20 +01:00
|
|
|
if ( _settings._errorsOnly == false && _errout.str().empty() )
|
|
|
|
_errorLogger->reportOut( "No errors found" );
|
2008-11-14 23:26:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// This generates false positives - especially for libraries
|
2008-11-30 10:08:07 +01:00
|
|
|
_settings._verbose = false;
|
2008-11-23 23:01:09 +01:00
|
|
|
if ( _settings._checkFunctionUsage )
|
2008-11-14 23:26:38 +01:00
|
|
|
{
|
2008-11-23 12:08:07 +01:00
|
|
|
_errout.str("");
|
2008-11-24 22:50:20 +01:00
|
|
|
if( _settings._errorsOnly == false )
|
|
|
|
_errorLogger->reportOut( "Checking usage of global functions (this may take several minutes).." );
|
|
|
|
|
2008-11-23 23:01:09 +01:00
|
|
|
_checkFunctionUsage.check();
|
2008-11-14 23:26:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// CppCheck - A function that checks a specified file
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-23 23:01:09 +01:00
|
|
|
void CppCheck::checkFile(const std::string &code, const char FileName[])
|
2008-11-14 23:26:38 +01:00
|
|
|
{
|
2008-11-22 20:39:12 +01:00
|
|
|
Tokenizer _tokenizer;
|
|
|
|
|
2008-11-14 23:26:38 +01:00
|
|
|
// Tokenize the file
|
|
|
|
{
|
|
|
|
std::istringstream istr(code);
|
2008-11-25 19:34:51 +01:00
|
|
|
_tokenizer.tokenize(istr, FileName);
|
2008-11-14 23:26:38 +01:00
|
|
|
}
|
|
|
|
|
2008-11-23 12:08:07 +01:00
|
|
|
_tokenizer.fillFunctionList();
|
2008-11-14 23:26:38 +01:00
|
|
|
|
|
|
|
// Check that the memsets are valid.
|
|
|
|
// The 'memset' function can do dangerous things if used wrong.
|
|
|
|
// Important: The checking doesn't work on simplified tokens list.
|
2008-11-20 23:19:26 +01:00
|
|
|
CheckClass checkClass( &_tokenizer, _settings, this );
|
2008-11-14 23:26:38 +01:00
|
|
|
checkClass.CheckMemset();
|
|
|
|
|
|
|
|
|
|
|
|
// Check for unsigned divisions where one operand is signed
|
|
|
|
// Very important to run it before 'SimplifyTokenList'
|
2008-11-20 23:19:26 +01:00
|
|
|
CheckOther checkOther( &_tokenizer, this );
|
2008-11-21 09:20:21 +01:00
|
|
|
if ( _settings._checkCodingStyle )
|
|
|
|
checkOther.CheckUnsignedDivision();
|
2008-11-14 23:26:38 +01:00
|
|
|
|
|
|
|
// Give warning when using char variable as array index
|
|
|
|
// Doesn't work on simplified token list ('unsigned')
|
2008-11-21 09:20:21 +01:00
|
|
|
if ( _settings._checkCodingStyle )
|
2008-11-14 23:26:38 +01:00
|
|
|
checkOther.CheckCharVariable();
|
|
|
|
|
|
|
|
|
|
|
|
// Including header which is not needed (too many false positives)
|
2008-11-16 16:18:50 +01:00
|
|
|
// if ( _settings._checkCodingStyle )
|
2008-11-14 23:26:38 +01:00
|
|
|
// {
|
|
|
|
// CheckHeaders checkHeaders( &tokenizer );
|
|
|
|
// checkHeaders.WarningIncludeHeader();
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
2008-11-25 19:34:51 +01:00
|
|
|
_tokenizer.simplifyTokenList();
|
2008-11-14 23:26:38 +01:00
|
|
|
|
2008-11-23 07:21:44 +01:00
|
|
|
|
2008-11-23 23:01:09 +01:00
|
|
|
if ( _settings._checkFunctionUsage )
|
|
|
|
_checkFunctionUsage.parseTokens(_tokenizer);
|
2008-11-23 07:21:44 +01:00
|
|
|
|
2008-11-14 23:26:38 +01:00
|
|
|
// Memory leak
|
2008-11-20 23:19:26 +01:00
|
|
|
CheckMemoryLeakClass checkMemoryLeak( &_tokenizer, _settings, this );
|
2008-11-14 23:26:38 +01:00
|
|
|
checkMemoryLeak.CheckMemoryLeak();
|
|
|
|
|
|
|
|
// Buffer overruns..
|
2008-11-27 21:29:29 +01:00
|
|
|
CheckBufferOverrunClass checkBufferOverrun( &_tokenizer, _settings, this );
|
2008-11-14 23:26:38 +01:00
|
|
|
checkBufferOverrun.CheckBufferOverrun();
|
|
|
|
|
|
|
|
// Check that all class constructors are ok.
|
|
|
|
checkClass.CheckConstructors();
|
|
|
|
|
|
|
|
if (_settings._showAll)
|
|
|
|
{
|
|
|
|
// Check for "if (a=b)"
|
|
|
|
checkOther.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'
|
|
|
|
checkBufferOverrun.WarningDangerousFunctions();
|
|
|
|
|
|
|
|
|
|
|
|
// Invalid function usage..
|
|
|
|
checkOther.InvalidFunctionUsage();
|
|
|
|
|
|
|
|
|
|
|
|
if (_settings._checkCodingStyle)
|
|
|
|
{
|
|
|
|
// Check that all private functions are called.
|
|
|
|
checkClass.CheckUnusedPrivateFunctions();
|
|
|
|
|
|
|
|
// Warning upon c-style pointer casts
|
|
|
|
const char *ext = strrchr(FileName, '.');
|
|
|
|
if (ext && strcmp(ext,".cpp")==0)
|
|
|
|
checkOther.WarningOldStylePointerCast();
|
|
|
|
|
|
|
|
// Use standard functions instead
|
|
|
|
checkOther.WarningIsDigit();
|
|
|
|
checkOther.WarningIsAlpha();
|
|
|
|
|
|
|
|
checkClass.CheckOperatorEq1();
|
|
|
|
|
|
|
|
// if (a) delete a;
|
|
|
|
checkOther.WarningRedundantCode();
|
|
|
|
|
|
|
|
// if (condition);
|
|
|
|
checkOther.WarningIf();
|
|
|
|
|
|
|
|
// Variable scope (check if the scope could be limited)
|
|
|
|
//CheckVariableScope();
|
|
|
|
|
|
|
|
// Check if a constant function parameter is passed by value
|
|
|
|
checkOther.CheckConstantFunctionParameter();
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-20 23:19:26 +01:00
|
|
|
void CppCheck::reportErr( const std::string &errmsg)
|
|
|
|
{
|
|
|
|
if ( /*OnlyReportUniqueErrors*/ true )
|
|
|
|
{
|
|
|
|
if ( std::find( _errorList.begin(), _errorList.end(), errmsg ) != _errorList.end() )
|
|
|
|
return;
|
|
|
|
_errorList.push_back( errmsg );
|
|
|
|
}
|
2008-11-24 22:50:20 +01:00
|
|
|
|
2008-11-30 10:08:07 +01:00
|
|
|
std::string errmsg2( errmsg );
|
|
|
|
if ( _settings._verbose )
|
|
|
|
{
|
|
|
|
errmsg2 += "\n Defines=\'" + cfg + "\'\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_errorLogger->reportErr( errmsg2 );
|
2008-11-24 22:50:20 +01:00
|
|
|
|
2008-11-30 10:08:07 +01:00
|
|
|
_errout << errmsg2 << std::endl;
|
2008-11-20 23:19:26 +01:00
|
|
|
}
|
2008-11-14 23:26:38 +01:00
|
|
|
|
2008-11-24 22:50:20 +01:00
|
|
|
void CppCheck::reportOut( const std::string &outmsg)
|
2008-11-20 23:19:26 +01:00
|
|
|
{
|
2008-11-24 22:50:20 +01:00
|
|
|
// This is currently never called. It is here just to comply with
|
|
|
|
// the interface.
|
2008-11-20 23:19:26 +01:00
|
|
|
}
|