Cppcheck
Public Types | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
CppCheck Class Reference

This is the base class which will use other classes to do static code analysis for C and C++ code to find possible errors or places that could be improved. More...

#include <cppcheck.h>

Inheritance diagram for CppCheck:
ErrorLogger

Public Types

using ExecuteCmdFn = std::function< int(std::string, std::vector< std::string >, std::string, std::string &)>
 

Public Member Functions

 CppCheck (ErrorLogger &errorLogger, bool useGlobalSuppressions, ExecuteCmdFn executeCommand)
 Constructor. More...
 
 ~CppCheck () override
 Destructor. More...
 
unsigned int check (const std::string &path)
 This starts the actual checking. More...
 
unsigned int check (const FileSettings &fs)
 
unsigned int check (const std::string &path, const std::string &content)
 Check the file. More...
 
Settingssettings ()
 Get reference to current settings. More...
 
void tooManyConfigsError (const std::string &file, const int numberOfConfigurations)
 
void purgedConfigurationMessage (const std::string &file, const std::string &configuration)
 
bool analyseWholeProgram ()
 Analyse whole program, run this after all TUs has been scanned. More...
 
void analyseClangTidy (const FileSettings &fileSettings)
 Analyze all files using clang-tidy. More...
 
void analyseWholeProgram (const std::string &buildDir, const std::list< std::pair< std::string, std::size_t >> &files, const std::list< FileSettings > &fileSettings)
 analyse whole program use .analyzeinfo files More...
 
void removeCtuInfoFiles (const std::list< std::pair< std::string, std::size_t >> &files, const std::list< FileSettings > &fileSettings)
 Remove *.ctu-info files. More...
 
bool isPremiumCodingStandardId (const std::string &id) const
 
std::string getAddonMessage (const std::string &id, const std::string &text) const
 
std::string getDumpFileContentsRawTokens (const std::vector< std::string > &files, const simplecpp::TokenList &tokens1) const
 Get dumpfile <rawtokens> contents, this is only public for testing purposes. More...
 

Static Public Member Functions

static const char * version ()
 Returns current version number as a string. More...
 
static const char * extraVersion ()
 Returns extra version info as a string. More...
 
static void getErrorMessages (ErrorLogger &errorlogger)
 Call all "getErrorMessages" in all registered Check classes. More...
 
static void resetTimerResults ()
 
static void printTimerResults (SHOWTIME_MODES mode)
 

Private Types

using Location = std::pair< std::string, int >
 

Private Member Functions

void internalError (const std::string &filename, const std::string &msg)
 There has been an internal error => Report information message. More...
 
unsigned int checkFile (const std::string &filename, const std::string &cfgname, std::istream *fileStream=nullptr)
 Check a file using stream. More...
 
void checkNormalTokens (const Tokenizer &tokenizer)
 Check normal tokens. More...
 
void executeAddons (const std::vector< std::string > &files, const std::string &file0)
 Execute addons. More...
 
void executeAddons (const std::string &dumpFile, const std::string &file0)
 
void executeAddonsWholeProgram (const std::list< std::pair< std::string, std::size_t >> &files)
 Execute addons. More...
 
unsigned int checkClang (const std::string &path)
 
void reportErr (const ErrorMessage &msg) override
 Errors and warnings are directed here. More...
 
void reportOut (const std::string &outmsg, Color c=Color::Reset) override
 Information about progress is directed here. More...
 
void reportProgress (const std::string &filename, const char stage[], const std::size_t value) override
 Report progress to client. More...
 
- Private Member Functions inherited from ErrorLogger
 ErrorLogger ()=default
 
virtual ~ErrorLogger ()=default
 

Private Attributes

std::unordered_set< std::string > mErrorList
 
Settings mSettings
 
ErrorLoggermErrorLogger
 
std::string mCurrentConfig
 Current preprocessor configuration. More...
 
std::map< Location, std::set< std::string > > mLocationMacros
 
unsigned int mExitCode {}
 
bool mUseGlobalSuppressions
 
bool mTooManyConfigs {}
 Are there too many configs? More...
 
std::list< Check::FileInfo * > mFileInfo
 File info used for whole program analysis. More...
 
AnalyzerInformation mAnalyzerInformation
 
ExecuteCmdFn mExecuteCommand
 Callback for executing a shell command (exe, args, output) More...
 
std::ofstream mPlistFile
 
std::unique_ptr< CheckUnusedFunctionsmUnusedFunctionsCheck
 

Additional Inherited Members

- Static Private Member Functions inherited from ErrorLogger
static std::string callStackToString (const std::list< ErrorMessage::FileLocation > &callStack)
 
static std::string toxml (const std::string &str)
 Convert XML-sensitive characters into XML entities. More...
 
static std::string plistHeader (const std::string &version, const std::vector< std::string > &files)
 
static std::string plistData (const ErrorMessage &msg)
 
static const char * plistFooter ()
 
static bool isCriticalErrorId (const std::string &id)
 

Detailed Description

This is the base class which will use other classes to do static code analysis for C and C++ code to find possible errors or places that could be improved.

Usage: See check() for more info.

Definition at line 59 of file cppcheck.h.

Member Typedef Documentation

◆ ExecuteCmdFn

using CppCheck::ExecuteCmdFn = std::function<int (std::string,std::vector<std::string>,std::string,std::string&)>

Definition at line 61 of file cppcheck.h.

◆ Location

using CppCheck::Location = std::pair<std::string, int>
private

Definition at line 235 of file cppcheck.h.

Constructor & Destructor Documentation

◆ CppCheck()

CppCheck::CppCheck ( ErrorLogger errorLogger,
bool  useGlobalSuppressions,
ExecuteCmdFn  executeCommand 
)

Constructor.

Definition at line 348 of file cppcheck.cpp.

◆ ~CppCheck()

CppCheck::~CppCheck ( )
override

Destructor.

Definition at line 356 of file cppcheck.cpp.

References mFileInfo, mPlistFile, and ErrorLogger::plistFooter().

Member Function Documentation

◆ analyseClangTidy()

void CppCheck::analyseClangTidy ( const FileSettings fileSettings)

◆ analyseWholeProgram() [1/2]

bool CppCheck::analyseWholeProgram ( )

Analyse whole program, run this after all TUs has been scanned.

This is deprecated and the plan is to remove this when .analyzeinfo is good enough. Return true if an error is reported.

Definition at line 1748 of file cppcheck.cpp.

References check(), CTU::FileInfo::functionCalls, Check::instances(), CTU::maxCtuDepth, Settings::maxCtuDepth, mExitCode, mFileInfo, mSettings, mUnusedFunctionsCheck, and CTU::FileInfo::nestedCalls.

Referenced by SingleExecutor::check(), and CheckThread::run().

◆ analyseWholeProgram() [2/2]

void CppCheck::analyseWholeProgram ( const std::string &  buildDir,
const std::list< std::pair< std::string, std::size_t >> &  files,
const std::list< FileSettings > &  fileSettings 
)

◆ check() [1/3]

unsigned int CppCheck::check ( const FileSettings fs)

◆ check() [2/3]

unsigned int CppCheck::check ( const std::string &  path)

This starts the actual checking.

Note that you must call parseFromArgs() or settings() and addFile() before calling this.

Returns
amount of errors found or 0 if none were found.

Check the file. This function checks one given file for errors.

Parameters
pathPath to the file to check.
Returns
amount of errors found or 0 if none were found.
Note
You must set settings before calling this function (by calling settings()).

Definition at line 557 of file cppcheck.cpp.

References checkClang(), checkFile(), Settings::clang, emptyString, mSettings, and Path::simplifyPath().

Referenced by analyseWholeProgram(), ProcessExecutor::check(), SingleExecutor::check(), check(), ThreadData::check(), and checkNormalTokens().

◆ check() [3/3]

unsigned int CppCheck::check ( const std::string &  path,
const std::string &  content 
)

Check the file.

This function checks one "virtual" file. The file is not read from the disk but the content is given in content. In errors the path is used as a filename.

Parameters
pathPath to the file to check.
contentFile content as a string.
Returns
amount of errors found or 0 if none were found.
Note
You must set settings before calling this function (by calling settings()).

Definition at line 565 of file cppcheck.cpp.

References checkFile(), emptyString, and Path::simplifyPath().

◆ checkClang()

unsigned int CppCheck::checkClang ( const std::string &  path)
private

◆ checkFile()

unsigned int CppCheck::checkFile ( const std::string &  filename,
const std::string &  cfgname,
std::istream *  fileStream = nullptr 
)
private

Check a file using stream.

Parameters
filenamefile name
cfgnamecfg name
fileStreamstream the file content can be read from
Returns
number of errors found

Definition at line 616 of file cppcheck.cpp.

References Settings::addons, AnalyzerInformation::analyzeFile(), Settings::basePaths, Settings::buildDir, TokenList::calculateHash(), Preprocessor::calculateHash(), cfg(), Settings::checkAllConfigurations, Settings::checkConfiguration, checkNormalTokens(), Settings::checks, AnalyzerInformation::close(), CPPCHECK_VERSION_STRING, Preprocessor::createDirectives(), createDumpFile(), createTokenList(), TokenList::createTokens(), Settings::debugwarnings, Preprocessor::dump(), Settings::dump, SuppressionList::dump(), Tokenizer::dump(), error, executeAddons(), FgGreen, Settings::force, ErrorMessage::fromInternalError(), Path::fromNativeSeparators(), Standards::getC(), Preprocessor::getcode(), Preprocessor::getConfigs(), Standards::getCPP(), getDumpFileContentsRawTokens(), Path::getRelativePath(), SuppressionList::getUnmatchedLocalSuppressions(), Path::identify(), Settings::includePaths, information, Preprocessor::inlineSuppressions(), internalError(), SimpleEnableGroup< T >::isEnabled(), Settings::library, Tokenizer::list, Preprocessor::loadFiles(), Preprocessor::macroChar, mAnalyzerInformation, SuppressionList::markUnmatchedInlineSuppressionsAsChecked(), Library::markupFile(), Settings::maxConfigs, mCurrentConfig, mErrorList, mErrorLogger, mExitCode, mLocationMacros, mPlistFile, mSettings, mTooManyConfigs, mUnusedFunctionsCheck, Token::next(), Suppressions::nomsg, normal, performance, Settings::platform, ErrorLogger::plistFooter(), ErrorLogger::plistHeader(), Settings::plistOutput, portability, Preprocessor::preprocess(), Settings::preprocessOnly, printTimerResults(), purgedConfigurationMessage(), Settings::quiet, Settings::relativePaths, Preprocessor::removeComments(), reportErr(), reportOut(), ErrorLogger::reportOut(), SuppressionList::reportUnmatchedSuppressions(), s_timerResults, Tokenizer::setDirectives(), TokenList::setLang(), Preprocessor::setPlatformInfo(), Tokenizer::setTimerResults(), Settings::severity, Settings::showtime, SHOWTIME_FILE, SHOWTIME_FILE_TOTAL, SHOWTIME_NONE, SHOWTIME_TOP5_FILE, Path::simplifyPath(), Preprocessor::simplifyPragmaAsm(), Tokenizer::simplifyTokens1(), split(), Settings::standards, startsWith(), Timer::stop(), style, Settings::supprs, Settings::terminated(), Tokenizer::tokens(), Path::toNativeSeparators(), tooManyConfigsError(), Platform::toString(), ErrorLogger::toxml(), unusedFunction, Settings::userDefines, Settings::userUndefs, Settings::useSingleJob(), Settings::verbose, version(), and warning.

Referenced by check().

◆ checkNormalTokens()

void CppCheck::checkNormalTokens ( const Tokenizer tokenizer)
private

◆ executeAddons() [1/2]

void CppCheck::executeAddons ( const std::string &  dumpFile,
const std::string &  file0 
)
private

Definition at line 1394 of file cppcheck.cpp.

References executeAddons().

◆ executeAddons() [2/2]

void CppCheck::executeAddons ( const std::vector< std::string > &  files,
const std::string &  file0 
)
private

◆ executeAddonsWholeProgram()

void CppCheck::executeAddonsWholeProgram ( const std::list< std::pair< std::string, std::size_t >> &  files)
private

◆ extraVersion()

const char * CppCheck::extraVersion ( )
static

Returns extra version info as a string.

This is for returning extra version info, like Git commit id, build time/date etc.

Returns
extra version info, e.g. "04d42151" (Git commit id).

Definition at line 374 of file cppcheck.cpp.

References ExtraVersion.

Referenced by MainWindow::about(), MainWindow::formatAndSetTitle(), CmdLineParser::getVersion(), and ShowVersion().

◆ getAddonMessage()

std::string CppCheck::getAddonMessage ( const std::string &  id,
const std::string &  text 
) const

◆ getDumpFileContentsRawTokens()

std::string CppCheck::getDumpFileContentsRawTokens ( const std::vector< std::string > &  files,
const simplecpp::TokenList &  tokens1 
) const

Get dumpfile <rawtokens> contents, this is only public for testing purposes.

Definition at line 1880 of file cppcheck.cpp.

References Settings::basePaths, Path::getRelativePath(), mSettings, and ErrorLogger::toxml().

Referenced by checkFile().

◆ getErrorMessages()

void CppCheck::getErrorMessages ( ErrorLogger errorlogger)
static

◆ internalError()

void CppCheck::internalError ( const std::string &  filename,
const std::string &  msg 
)
private

There has been an internal error => Report information message.

Definition at line 1022 of file cppcheck.cpp.

References emptyString, error, mErrorLogger, normal, and ErrorLogger::reportErr().

Referenced by checkClang(), and checkFile().

◆ isPremiumCodingStandardId()

bool CppCheck::isPremiumCodingStandardId ( const std::string &  id) const

Definition at line 1868 of file cppcheck.cpp.

References mSettings, Settings::premiumArgs, and startsWith().

Referenced by executeAddons().

◆ printTimerResults()

void CppCheck::printTimerResults ( SHOWTIME_MODES  mode)
static

◆ purgedConfigurationMessage()

void CppCheck::purgedConfigurationMessage ( const std::string &  file,
const std::string &  configuration 
)

◆ removeCtuInfoFiles()

void CppCheck::removeCtuInfoFiles ( const std::list< std::pair< std::string, std::size_t >> &  files,
const std::list< FileSettings > &  fileSettings 
)

Remove *.ctu-info files.

Definition at line 1841 of file cppcheck.cpp.

References Settings::buildDir, getCtuInfoFileName(), getDumpFileName(), and mSettings.

Referenced by analyseWholeProgram().

◆ reportErr()

void CppCheck::reportErr ( const ErrorMessage msg)
overrideprivatevirtual

◆ reportOut()

void CppCheck::reportOut ( const std::string &  outmsg,
Color  c = Color::Reset 
)
overrideprivatevirtual

Information about progress is directed here.

Parameters
outmsgMessage to show, e.g. "Checking main.cpp..."

Implements ErrorLogger.

Definition at line 1638 of file cppcheck.cpp.

References mErrorLogger, and ErrorLogger::reportOut().

Referenced by checkFile().

◆ reportProgress()

void CppCheck::reportProgress ( const std::string &  filename,
const char  stage[],
const std::size_t  value 
)
overrideprivatevirtual

Report progress to client.

Parameters
filenamemain file that is checked
stagefor example preprocess / tokenize / simplify / check
valueprogress value (0-100)

Reimplemented from ErrorLogger.

Definition at line 1643 of file cppcheck.cpp.

References mErrorLogger, and ErrorLogger::reportProgress().

◆ resetTimerResults()

void CppCheck::resetTimerResults ( )
static

Definition at line 1858 of file cppcheck.cpp.

References TimerResults::reset(), and s_timerResults.

◆ settings()

Settings & CppCheck::settings ( )

Get reference to current settings.

Returns
a reference to current settings

Definition at line 1502 of file cppcheck.cpp.

References mSettings.

Referenced by ProcessExecutor::check(), CheckThread::check(), ThreadData::check(), CheckThread::run(), and CheckThread::runAddonsAndTools().

◆ tooManyConfigsError()

void CppCheck::tooManyConfigsError ( const std::string &  file,
const int  numberOfConfigurations 
)

◆ version()

const char * CppCheck::version ( )
static

Returns current version number as a string.

Returns
version, e.g. "1.38"

Definition at line 369 of file cppcheck.cpp.

References Version.

Referenced by MainWindow::about(), checkFile(), MainWindow::formatAndSetTitle(), CmdLineParser::getVersion(), ErrorMessage::getXMLHeader(), MainWindow::replyFinished(), ShowVersion(), and XmlReportV2::writeHeader().

Member Data Documentation

◆ mAnalyzerInformation

AnalyzerInformation CppCheck::mAnalyzerInformation
private

Definition at line 248 of file cppcheck.h.

Referenced by checkFile(), checkNormalTokens(), and reportErr().

◆ mCurrentConfig

std::string CppCheck::mCurrentConfig
private

Current preprocessor configuration.

Definition at line 233 of file cppcheck.h.

Referenced by checkFile().

◆ mErrorList

std::unordered_set<std::string> CppCheck::mErrorList
private

Definition at line 225 of file cppcheck.h.

Referenced by checkFile(), and reportErr().

◆ mErrorLogger

ErrorLogger& CppCheck::mErrorLogger
private

◆ mExecuteCommand

ExecuteCmdFn CppCheck::mExecuteCommand
private

Callback for executing a shell command (exe, args, output)

Definition at line 251 of file cppcheck.h.

Referenced by analyseClangTidy(), check(), checkClang(), and executeAddons().

◆ mExitCode

unsigned int CppCheck::mExitCode {}
private

Definition at line 238 of file cppcheck.h.

Referenced by analyseWholeProgram(), checkClang(), checkFile(), and reportErr().

◆ mFileInfo

std::list<Check::FileInfo*> CppCheck::mFileInfo
private

File info used for whole program analysis.

Definition at line 246 of file cppcheck.h.

Referenced by analyseWholeProgram(), checkNormalTokens(), and ~CppCheck().

◆ mLocationMacros

std::map<Location, std::set<std::string> > CppCheck::mLocationMacros
private

Definition at line 236 of file cppcheck.h.

Referenced by checkFile(), and reportErr().

◆ mPlistFile

std::ofstream CppCheck::mPlistFile
private

Definition at line 253 of file cppcheck.h.

Referenced by checkFile(), reportErr(), and ~CppCheck().

◆ mSettings

Settings CppCheck::mSettings
private

◆ mTooManyConfigs

bool CppCheck::mTooManyConfigs {}
private

Are there too many configs?

Definition at line 243 of file cppcheck.h.

Referenced by checkFile(), purgedConfigurationMessage(), and tooManyConfigsError().

◆ mUnusedFunctionsCheck

std::unique_ptr<CheckUnusedFunctions> CppCheck::mUnusedFunctionsCheck
private

Definition at line 255 of file cppcheck.h.

Referenced by analyseWholeProgram(), check(), checkClang(), checkFile(), and checkNormalTokens().

◆ mUseGlobalSuppressions

bool CppCheck::mUseGlobalSuppressions
private

Definition at line 240 of file cppcheck.h.

Referenced by check(), and reportErr().


The documentation for this class was generated from the following files: