diff --git a/cppcheck.cpp b/cppcheck.cpp index 9f257245c..cd32bbd63 100644 --- a/cppcheck.cpp +++ b/cppcheck.cpp @@ -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 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 code; Preprocessor preprocessor( this ); - preprocessor.preprocess(fin, code, fname); + std::map 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::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" ); diff --git a/cppcheck.h b/cppcheck.h index 972b1ce37..2188cbac3 100644 --- a/cppcheck.h +++ b/cppcheck.h @@ -23,6 +23,7 @@ #include #include #include +#include #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 _filenames; + /** Key is file name, and value is the content of the file */ + std::map _fileContents; CheckFunctionUsage _checkFunctionUsage; ErrorLogger *_errorLogger; };