Feature added: CppCheck class has now function addFile(string,string) which allows checking of unreal files, by giving just filename and file content as a

parameter.
This commit is contained in:
Reijo Tomperi 2008-11-25 20:56:11 +00:00
parent 4f4a2391e0
commit 69ba8ce7f9
2 changed files with 35 additions and 7 deletions

View File

@ -56,6 +56,12 @@ void CppCheck::addFile( const std::string &path )
_filenames.push_back( path );
}
void CppCheck::addFile( const std::string &path, const std::string &content )
{
_filenames.push_back( path );
_fileContents[ path ] = content;
}
std::string CppCheck::parseFromArgs( int argc, char* argv[] )
{
std::vector<std::string> pathnames;
@ -128,6 +134,7 @@ std::string CppCheck::parseFromArgs( int argc, char* argv[] )
return oss.str();
}
// Check function usage if "--style" and "--all" was given.
if ( _settings._showAll && _settings._checkCodingStyle )
_settings._checkFunctionUsage = true;
@ -137,10 +144,6 @@ std::string CppCheck::parseFromArgs( int argc, char* argv[] )
void CppCheck::check()
{
std::sort( _filenames.begin(), _filenames.end() );
// Check function usage if "--style" and "--all" was given.
for (unsigned int c = 0; c < _filenames.size(); c++)
{
_errout.str("");
@ -150,12 +153,25 @@ void CppCheck::check()
if ( _settings._errorsOnly == false )
_errorLogger->reportOut( std::string( "Checking " ) + fname + std::string( "..." ) );
std::ifstream fin( fname.c_str() );
std::map<std::string, std::string> code;
Preprocessor preprocessor( this );
preprocessor.preprocess(fin, code, fname);
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);
}
for ( std::map<std::string,std::string>::const_iterator it = code.begin(); it != code.end(); ++it )
{
checkFile(it->second, _filenames[c].c_str());
}
if ( _settings._errorsOnly == false && _errout.str().empty() )
_errorLogger->reportOut( "No errors found" );

View File

@ -23,6 +23,7 @@
#include <list>
#include <sstream>
#include <vector>
#include <map>
#include "settings.h"
#include "errorlogger.h"
#include "CheckFunctionUsage.h"
@ -69,6 +70,15 @@ class CppCheck : public ErrorLogger
*/
void addFile( const std::string &path );
/**
* Add new unreal file to be checked.
*
* @param path File name (used for error reporting).
* @param content If the file would be a real file, this should be
* the content of the file.
*/
void addFile( const std::string &path, const std::string &content );
/**
* Parse command line args and get settings and file lists
* from there.
@ -104,6 +114,8 @@ class CppCheck : public ErrorLogger
std::ostringstream _errout;
Settings _settings;
std::vector<std::string> _filenames;
/** Key is file name, and value is the content of the file */
std::map<std::string,std::string> _fileContents;
CheckFunctionUsage _checkFunctionUsage;
ErrorLogger *_errorLogger;
};