Cppcheck
|
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>
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... | |
Settings & | settings () |
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... | |
unsigned int | analyseWholeProgram (const std::string &buildDir, const std::list< FileWithDetails > &files, const std::list< FileSettings > &fileSettings) |
analyse whole program use .analyzeinfo files More... | |
void | removeCtuInfoFiles (const std::list< FileWithDetails > &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< FileWithDetails > &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 |
ErrorLogger & | mErrorLogger |
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< CheckUnusedFunctions > | mUnusedFunctionsCheck |
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) |
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 60 of file cppcheck.h.
using CppCheck::ExecuteCmdFn = std::function<int (std::string,std::vector<std::string>,std::string,std::string&)> |
Definition at line 62 of file cppcheck.h.
|
private |
Definition at line 236 of file cppcheck.h.
CppCheck::CppCheck | ( | ErrorLogger & | errorLogger, |
bool | useGlobalSuppressions, | ||
ExecuteCmdFn | executeCommand | ||
) |
Constructor.
Definition at line 333 of file cppcheck.cpp.
|
override |
Destructor.
Definition at line 341 of file cppcheck.cpp.
References mFileInfo, mPlistFile, and ErrorLogger::plistFooter().
void CppCheck::analyseClangTidy | ( | const FileSettings & | fileSettings | ) |
Analyze all files using clang-tidy.
Definition at line 1656 of file cppcheck.cpp.
References Settings::buildDir, ErrorMessage::callStack, FileSettings::defines, emptyString, ErrorMessage::file0, FileSettings::filename(), AnalyzerInformation::getAnalyzerInfoFile(), getDefinesFlags(), ErrorMessage::id, FileSettings::includePaths, mExecuteCommand, mSettings, performance, portability, reportErr(), ErrorMessage::setmsg(), ErrorMessage::severity, Path::simplifyPath(), split(), style, Path::toNativeSeparators(), and warning.
Referenced by ProcessExecutor::check(), SingleExecutor::check(), and ThreadData::check().
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 1733 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().
unsigned int CppCheck::analyseWholeProgram | ( | const std::string & | buildDir, |
const std::list< FileWithDetails > & | files, | ||
const std::list< FileSettings > & | fileSettings | ||
) |
analyse whole program use .analyzeinfo files
Definition at line 1758 of file cppcheck.cpp.
References CheckUnusedFunctions::analyseWholeProgram(), check(), Settings::checks, error, executeAddonsWholeProgram(), Check::instances(), SimpleEnableGroup< T >::isEnabled(), CTU::FileInfo::loadFromXml(), CTU::maxCtuDepth, Settings::maxCtuDepth, mExitCode, mSettings, mUnusedFunctionsCheck, removeCtuInfoFiles(), and unusedFunction.
unsigned int CppCheck::check | ( | const FileSettings & | fs | ) |
Definition at line 556 of file cppcheck.cpp.
References SuppressionList::addSuppressions(), FileSettings::cfg, check(), checkFile(), Settings::checks, Settings::clang, FileSettings::cppcheckDefines(), FileSettings::defines, FileSettings::filename(), SuppressionList::getSuppressions(), FileSettings::includePaths, Settings::includePaths, SimpleEnableGroup< T >::isEnabled(), mErrorLogger, mExecuteCommand, mSettings, mUnusedFunctionsCheck, mUseGlobalSuppressions, Suppressions::nomsg, Settings::platform, FileSettings::platformType, Platform::set(), Standards::setC(), Standards::setCPP(), Path::simplifyPath(), FileSettings::standard, Settings::standards, Settings::supprs, FileSettings::systemIncludePaths, FileSettings::undefs, unusedFunction, Settings::userDefines, and Settings::userUndefs.
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.
Check the file. This function checks one given file for errors.
path | Path to the file to check. |
Definition at line 542 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().
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.
path | Path to the file to check. |
content | File content as a string. |
Definition at line 550 of file cppcheck.cpp.
References checkFile(), emptyString, and Path::simplifyPath().
|
private |
Definition at line 414 of file cppcheck.cpp.
References TokenList::appendFileIfNew(), Settings::buildDir, checkNormalTokens(), Settings::checks, Settings::clangExecutable, createDumpFile(), Settings::debugnormal, Tokenizer::dump(), emptyString, executeAddons(), FgGreen, ErrorMessage::fromInternalError(), Path::fromNativeSeparators(), AnalyzerInformation::getAnalyzerInfoFile(), Standards::getC(), Standards::getCPP(), getDefinesFlags(), Tokenizer::getSymbolDatabase(), Path::identify(), Settings::includePaths, internalError(), SimpleEnableGroup< T >::isEnabled(), Tokenizer::list, mErrorLogger, mExecuteCommand, mExitCode, mSettings, mUnusedFunctionsCheck, clangimport::parseClangAstDump(), Tokenizer::printDebugOutput(), Settings::quiet, reportClangErrors(), reportErr(), ErrorLogger::reportOut(), s_timerResults, ValueFlow::setValues(), split(), Settings::standards, Standards::stdValue, ErrorLogger::toxml(), unusedFunction, Settings::userDefines, and Settings::verbose.
Referenced by check().
|
private |
Check a file using stream.
filename | file name |
cfgname | cfg name |
fileStream | stream the file content can be read from |
Definition at line 601 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().
|
private |
Check normal tokens.
tokenizer | tokenizer instance |
Definition at line 1027 of file cppcheck.cpp.
References CheckUnusedFunctions::analyzerInfo(), Settings::buildDir, check(), Settings::checks, Settings::checksMaxTime, Settings::clang, debug, Settings::debugwarnings, emptyString, CTU::getFileInfo(), TokenList::getFiles(), Check::instances(), SimpleEnableGroup< T >::isEnabled(), Tokenizer::list, mAnalyzerInformation, mFileInfo, mSettings, mUnusedFunctionsCheck, normal, CheckUnusedFunctions::parseTokens(), reportErr(), s_timerResults, AnalyzerInformation::setFileInfo(), Settings::showtime, Settings::terminated(), unusedFunction, and Settings::useSingleJob().
Referenced by checkClang(), and checkFile().
|
private |
Definition at line 1379 of file cppcheck.cpp.
References executeAddons().
|
private |
Execute addons.
Definition at line 1387 of file cppcheck.cpp.
References Settings::addonInfos, Settings::addonPython, Settings::addons, ErrorMessage::callStack, AddonInfo::ctu, endsWith(), executeAddon(), FILELIST, Settings::getMisraRuleText(), Path::getPathFromFilename(), internal, SimpleEnableGroup< T >::isEnabled(), isPremiumCodingStandardId(), mExecuteCommand, mSettings, AddonInfo::name, none, Settings::pid, Settings::premiumArgs, reportErr(), Settings::severity, severityFromString(), and startsWith().
Referenced by checkClang(), checkFile(), executeAddons(), and executeAddonsWholeProgram().
|
private |
Execute addons.
Definition at line 1463 of file cppcheck.cpp.
References Settings::addons, Settings::buildDir, executeAddons(), ErrorMessage::fromInternalError(), getCtuInfoFileName(), getDumpFileName(), mSettings, and reportErr().
Referenced by analyseWholeProgram().
|
static |
Returns extra version info as a string.
This is for returning extra version info, like Git commit id, build time/date etc.
Definition at line 359 of file cppcheck.cpp.
References ExtraVersion.
Referenced by MainWindow::about(), MainWindow::formatAndSetTitle(), CmdLineParser::getVersion(), and ShowVersion().
std::string CppCheck::getAddonMessage | ( | const std::string & | id, |
const std::string & | text | ||
) | const |
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 1867 of file cppcheck.cpp.
References Settings::basePaths, Path::getRelativePath(), mSettings, and ErrorLogger::toxml().
Referenced by checkFile().
|
static |
Call all "getErrorMessages" in all registered Check classes.
Also print out XML header and footer.
Definition at line 1633 of file cppcheck.cpp.
References emptyString, SimpleEnableGroup< T >::enable(), CheckUnusedFunctions::getErrorMessages(), Preprocessor::getErrorMessages(), information, Check::instances(), performance, portability, Settings::severity, style, and warning.
Referenced by NewSuppressionDialog::NewSuppressionDialog(), and CmdLineParser::parseFromArgs().
|
private |
There has been an internal error => Report information message.
Definition at line 1007 of file cppcheck.cpp.
References emptyString, error, mErrorLogger, normal, and ErrorLogger::reportErr().
Referenced by checkClang(), and checkFile().
bool CppCheck::isPremiumCodingStandardId | ( | const std::string & | id | ) | const |
Definition at line 1855 of file cppcheck.cpp.
References mSettings, Settings::premiumArgs, and startsWith().
Referenced by executeAddons().
|
static |
Definition at line 1850 of file cppcheck.cpp.
References s_timerResults, and TimerResults::showResults().
Referenced by ProcessExecutor::check(), SingleExecutor::check(), ThreadExecutor::check(), and checkFile().
void CppCheck::purgedConfigurationMessage | ( | const std::string & | file, |
const std::string & | configuration | ||
) |
Definition at line 1531 of file cppcheck.cpp.
References emptyString, information, SimpleEnableGroup< T >::isEnabled(), mSettings, mTooManyConfigs, normal, reportErr(), and Settings::severity.
Referenced by checkFile().
void CppCheck::removeCtuInfoFiles | ( | const std::list< FileWithDetails > & | files, |
const std::list< FileSettings > & | fileSettings | ||
) |
Remove *.ctu-info files.
Definition at line 1828 of file cppcheck.cpp.
References Settings::buildDir, getCtuInfoFileName(), getDumpFileName(), and mSettings.
Referenced by analyseWholeProgram().
|
overrideprivatevirtual |
Errors and warnings are directed here.
msg | Errors messages are normally in format "[filepath:line number] Message", e.g. "[main.cpp:4] Uninitialized member variable" |
Implements ErrorLogger.
Definition at line 1556 of file cppcheck.cpp.
References Settings::buildDir, ErrorMessage::callStack, ErrorMessage::file0, SuppressionList::ErrorMessage::fromErrorMessage(), ErrorMessage::id, internal, ErrorLogger::isCriticalErrorId(), SuppressionList::isSuppressed(), SuppressionList::isSuppressedExplicitly(), Settings::library, mAnalyzerInformation, mErrorList, mErrorLogger, mExitCode, mLocationMacros, mPlistFile, mSettings, mUseGlobalSuppressions, Suppressions::nofail, Suppressions::nomsg, ErrorLogger::plistData(), Settings::plistOutput, AnalyzerInformation::reportErr(), ErrorLogger::reportErr(), Library::reportErrors(), Settings::safety, ErrorMessage::severity, Settings::supprs, ErrorMessage::toString(), and Settings::verbose.
Referenced by analyseClangTidy(), checkClang(), checkFile(), checkNormalTokens(), executeAddons(), executeAddonsWholeProgram(), purgedConfigurationMessage(), and tooManyConfigsError().
|
overrideprivatevirtual |
Information about progress is directed here.
outmsg | Message to show, e.g. "Checking main.cpp..." |
Implements ErrorLogger.
Definition at line 1623 of file cppcheck.cpp.
References mErrorLogger, and ErrorLogger::reportOut().
Referenced by checkFile().
|
overrideprivatevirtual |
Report progress to client.
filename | main file that is checked |
stage | for example preprocess / tokenize / simplify / check |
value | progress value (0-100) |
Reimplemented from ErrorLogger.
Definition at line 1628 of file cppcheck.cpp.
References mErrorLogger, and ErrorLogger::reportProgress().
|
static |
Definition at line 1845 of file cppcheck.cpp.
References TimerResults::reset(), and s_timerResults.
Settings & CppCheck::settings | ( | ) |
Get reference to current settings.
Definition at line 1487 of file cppcheck.cpp.
References mSettings.
Referenced by ProcessExecutor::check(), CheckThread::check(), ThreadData::check(), CheckThread::run(), and CheckThread::runAddonsAndTools().
void CppCheck::tooManyConfigsError | ( | const std::string & | file, |
const int | numberOfConfigurations | ||
) |
Definition at line 1492 of file cppcheck.cpp.
References CWE398, emptyString, information, SimpleEnableGroup< T >::isEnabled(), Settings::maxConfigs, mSettings, mTooManyConfigs, normal, reportErr(), and Settings::severity.
Referenced by checkFile().
|
static |
Returns current version number as a string.
Definition at line 354 of file cppcheck.cpp.
References Version.
Referenced by MainWindow::about(), checkFile(), MainWindow::formatAndSetTitle(), CmdLineParser::getVersion(), ErrorMessage::getXMLHeader(), MainWindow::replyFinished(), ShowVersion(), and XmlReportV2::writeHeader().
|
private |
Definition at line 249 of file cppcheck.h.
Referenced by checkFile(), checkNormalTokens(), and reportErr().
|
private |
Current preprocessor configuration.
Definition at line 234 of file cppcheck.h.
Referenced by checkFile().
|
private |
Definition at line 226 of file cppcheck.h.
Referenced by checkFile(), and reportErr().
|
private |
Definition at line 231 of file cppcheck.h.
Referenced by check(), checkClang(), checkFile(), internalError(), reportErr(), reportOut(), and reportProgress().
|
private |
Callback for executing a shell command (exe, args, output)
Definition at line 252 of file cppcheck.h.
Referenced by analyseClangTidy(), check(), checkClang(), and executeAddons().
|
private |
Definition at line 239 of file cppcheck.h.
Referenced by analyseWholeProgram(), checkClang(), checkFile(), and reportErr().
|
private |
File info used for whole program analysis.
Definition at line 247 of file cppcheck.h.
Referenced by analyseWholeProgram(), checkNormalTokens(), and ~CppCheck().
|
private |
Definition at line 237 of file cppcheck.h.
Referenced by checkFile(), and reportErr().
|
private |
Definition at line 254 of file cppcheck.h.
Referenced by checkFile(), reportErr(), and ~CppCheck().
|
private |
Definition at line 227 of file cppcheck.h.
Referenced by analyseClangTidy(), analyseWholeProgram(), check(), checkClang(), checkFile(), checkNormalTokens(), executeAddons(), executeAddonsWholeProgram(), getDumpFileContentsRawTokens(), isPremiumCodingStandardId(), purgedConfigurationMessage(), removeCtuInfoFiles(), reportErr(), settings(), and tooManyConfigsError().
|
private |
Are there too many configs?
Definition at line 244 of file cppcheck.h.
Referenced by checkFile(), purgedConfigurationMessage(), and tooManyConfigsError().
|
private |
Definition at line 256 of file cppcheck.h.
Referenced by analyseWholeProgram(), check(), checkClang(), checkFile(), and checkNormalTokens().
|
private |
Definition at line 241 of file cppcheck.h.
Referenced by check(), and reportErr().