36 #if defined(HAS_THREADING_MODEL_THREAD) 
   39 #if defined(HAS_THREADING_MODEL_FORK) 
   53 #include <unordered_set> 
   57 #ifdef USE_UNIX_SIGNAL_HANDLING 
   61 #ifdef USE_WINDOWS_SEH 
   69 #if !defined(WIN32) && !defined(__MINGW32__) 
   77         CmdLineLoggerStd() = 
default;
 
   84         void printError(
const std::string &message)
 override 
   89         void printRaw(
const std::string &message)
 override 
   91             std::cout << message << std::endl;
 
   98         explicit StdLogger(
const Settings& settings)
 
  101             if (!mSettings.outputFile.empty()) {
 
  102                 mErrorOutput = 
new std::ofstream(settings.
outputFile);
 
  106         ~StdLogger()
 override {
 
  110         StdLogger(
const StdLogger&) = 
delete;
 
  113         void resetLatestProgressOutputTime() {
 
  114             mLatestProgressOutputTime = std::time(
nullptr);
 
  121         void reportErr(
const std::string &errmsg);
 
  126         void writeCheckersReport();
 
  128         bool hasCriticalErrors()
 const {
 
  129             return !mCriticalErrors.empty();
 
  144         void reportProgress(
const std::string &filename, 
const char stage[], 
const std::size_t value) 
override;
 
  155         std::unordered_set<std::string> mShownErrors;
 
  160         std::time_t mLatestProgressOutputTime{};
 
  165         std::ofstream* mErrorOutput{};
 
  170         std::set<std::string> mActiveCheckers;
 
  175         std::string mCriticalErrors;
 
  181 #if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING) 
  182  FILE* CppCheckExecutor::mExceptionOutput = stdout;
 
  188     CmdLineLoggerStd logger;
 
  211 #ifdef USE_WINDOWS_SEH 
  212     if (settings.exceptionHandling)
 
  214 #elif defined(USE_UNIX_SIGNAL_HANDLING) 
  215     if (settings.exceptionHandling)
 
  216         register_signal_handler();
 
  224         return s.errorId == 
"unmatchedSuppression" && s.fileName.empty() && s.lineNumber == SuppressionList::Suppression::NO_LINE;
 
  231         assert(!(!files.empty() && !fileSettings.empty()));
 
  233         for (std::list<FileWithDetails>::const_iterator i = files.cbegin(); i != files.cend(); ++i) {
 
  238         for (std::list<FileSettings>::const_iterator i = fileSettings.cbegin(); i != fileSettings.cend(); ++i) {
 
  252     StdLogger stdLogger(settings);
 
  255         stdLogger.resetLatestProgressOutputTime();
 
  262         std::list<std::string> fileNames;
 
  263         for (std::list<FileWithDetails>::const_iterator i = 
mFiles.cbegin(); i != 
mFiles.cend(); ++i)
 
  264             fileNames.emplace_back(i->path());
 
  273     auto& suppressions = 
cppcheck.settings().supprs.nomsg;
 
  275     unsigned int returnValue = 0;
 
  279         returnValue = executor.
check();
 
  281 #if defined(HAS_THREADING_MODEL_THREAD) 
  282         if (settings.
executor == Settings::ExecutorType::Thread) {
 
  284             returnValue = executor.
check();
 
  287 #if defined(HAS_THREADING_MODEL_FORK) 
  288         if (settings.
executor == Settings::ExecutorType::Process) {
 
  290             returnValue = executor.
check();
 
  299         if (err && returnValue == 0)
 
  308         stdLogger.writeCheckersReport();
 
  314     if (settings.
safety && (stdLogger.hasCriticalErrors() || returnValue != 0))
 
  322 void StdLogger::writeCheckersReport()
 
  326     bool suppressed = 
false;
 
  328         if (s.errorId == 
"checkersReport")
 
  335         msg.
id = 
"checkersReport";
 
  337         const int activeCheckers = checkersReport.getActiveCheckersCount();
 
  338         const int totalCheckers = checkersReport.getAllCheckersCount();
 
  341         if (mCriticalErrors.empty())
 
  342             what = std::to_string(activeCheckers) + 
"/" + std::to_string(totalCheckers);
 
  344             what = 
"There was critical errors";
 
  345         msg.
setmsg(
"Active checkers: " + what + 
" (use --checkers-report=<filename> to see details)");
 
  350     if (!mSettings.checkersReportFilename.empty()) {
 
  351         std::ofstream fout(mSettings.checkersReportFilename);
 
  353             fout << checkersReport.getReport(mCriticalErrors);
 
  359 static inline std::string 
ansiToOEM(
const std::string &msg, 
bool doConvert)
 
  362         const unsigned msglength = msg.length();
 
  364         std::vector<WCHAR> wcContainer(msglength);
 
  365         std::string result(msglength, 
'\0');
 
  368         MultiByteToWideChar(CP_ACP, 0, msg.data(), msglength, wcContainer.data(), msglength);
 
  370         WideCharToMultiByte(CP_OEMCP, 0, wcContainer.data(), msglength, &result[0], msglength, 
nullptr, 
nullptr);
 
  378 #define ansiToOEM(msg, doConvert) (msg) 
  381 void StdLogger::reportErr(
const std::string &errmsg)
 
  384         *mErrorOutput << errmsg << std::endl;
 
  386         std::cerr << 
ansiToOEM(errmsg, !mSettings.xml) << std::endl;
 
  390 void StdLogger::reportOut(
const std::string &outmsg, 
Color c)
 
  393         std::cout << 
ansiToOEM(outmsg, 
true) << std::endl;
 
  399 void StdLogger::reportProgress(
const std::string &filename, 
const char stage[], 
const std::size_t value)
 
  403     if (!mLatestProgressOutputTime)
 
  407     const std::time_t currentTime = std::time(
nullptr);
 
  408     if (currentTime >= (mLatestProgressOutputTime + mSettings.reportProgress))
 
  410         mLatestProgressOutputTime = currentTime;
 
  413         std::ostringstream ostr;
 
  416              << 
' ' << value << 
'%';
 
  419         reportOut(ostr.str());
 
  427         mActiveCheckers.emplace(checker);
 
  432         if (!mCriticalErrors.empty())
 
  433             mCriticalErrors += 
", ";
 
  434         mCriticalErrors += msg.
id;
 
  436             mCriticalErrors += 
" (suppressed)";
 
  445     if (!mShownErrors.insert(msg.
toString(mSettings.verbose)).second)
 
  449         reportErr(msg.
toXML());
 
  451         reportErr(msg.
toString(mSettings.verbose, mSettings.templateFormat, mSettings.templateLocation));
 
  454 #if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING) 
  457     mExceptionOutput = exceptionOutput;
 
  458 #if defined(USE_UNIX_SIGNAL_HANDLING) 
  459     set_signal_handler_output(mExceptionOutput);
 
  466     return mExceptionOutput;
 
  481     if (exe.find(
" ") != std::string::npos)
 
  482         exe = 
"\"" + exe + 
"\"";
 
  485     std::string joinedArgs;
 
  486     for (
const std::string &arg : args) {
 
  487         if (!joinedArgs.empty())
 
  489         if (arg.find(
' ') != std::string::npos)
 
  490             joinedArgs += 
'"' + arg + 
'"';
 
  495     const std::string cmd = exe + 
" " + joinedArgs + 
" " + redirect;
 
  498     FILE* p = _popen(cmd.c_str(), 
"r");
 
  500     FILE *p = popen(cmd.c_str(), 
"r");
 
  510     while (fgets(buffer, 
sizeof(buffer), p) != 
nullptr)
 
  514     const int res = _pclose(p);
 
  516     const int res = pclose(p);
 
  524 #if !defined(WIN32) && !defined(__MINGW32__) 
  525     if (WIFEXITED(res)) {
 
  526         return WEXITSTATUS(res);
 
  528     if (WIFSIGNALED(res)) {
 
  529         return WTERMSIG(res);
 
virtual void printMessage(const std::string &message)=0
print a regular message
 
virtual void printError(const std::string &message)=0
print an error message
 
virtual void printRaw(const std::string &message)=0
print to the output
 
const std::list< FileWithDetails > & getFiles() const
Return the files user gave to command line.
 
bool fillSettingsFromArgs(int argc, const char *const argv[])
Parse command line args and fill settings and file lists from there.
 
const std::list< FileSettings > & getFileSettings() const
Return the file settings read from command line.
 
static void setExceptionOutput(FILE *exceptionOutput)
 
static FILE * getExceptionOutput()
 
static bool reportSuppressions(const Settings &settings, const SuppressionList &suppressions, bool unusedFunctionCheckEnabled, const std::list< FileWithDetails > &files, const std::list< FileSettings > &fileSettings, ErrorLogger &errorLogger)
 
std::list< FileSettings > mFileSettings
 
int check_wrapper(const Settings &settings)
Wrapper around check_internal.
 
int check(int argc, const char *const argv[])
Starts the checking.
 
static int executeCommand(std::string exe, std::vector< std::string > args, std::string redirect, std::string &output_)
Execute a shell command and read the output from it.
 
std::list< FileWithDetails > mFiles
Filename associated with size of file.
 
int check_internal(const Settings &settings) const
Starts the checking.
 
This is the base class which will use other classes to do static code analysis for C and C++ code to ...
 
This is an interface, which the class responsible of error logging should implement.
 
static bool isCriticalErrorId(const std::string &id)
 
virtual void reportErr(const ErrorMessage &msg)=0
Information about found errors and warnings is directed here.
 
virtual void reportOut(const std::string &outmsg, Color c=Color::Reset)=0
Information about progress is directed here.
 
virtual void reportProgress(const std::string &filename, const char stage[], const std::size_t value)
Report progress to client.
 
Wrapper for error messages, provided by reportErr()
 
static std::string getXMLFooter()
 
static std::string getXMLHeader(std::string productName)
 
std::string toString(bool verbose, const std::string &templateFormat=emptyString, const std::string &templateLocation=emptyString) const
Format the error message into a string.
 
const std::string & shortMessage() const
Short message (single line short message)
 
std::string toXML() const
Format the error message in XML format.
 
void setmsg(const std::string &msg)
set short and verbose messages
 
This class will take a list of filenames and settings and check then all files using threads.
 
unsigned int check() override
 
This is just a container for general settings so that we don't need to pass individual values to func...
 
int exitCode
If errors are found, this value is returned from main().
 
int reportProgress
–report-progress
 
Suppressions supprs
suppressions
 
SimpleEnableGroup< Checks > checks
 
bool xml
write XML results (–xml)
 
bool checkConfiguration
Is the 'configuration checking' wanted?
 
static bool terminated()
termination requested?
 
void setMisraRuleTexts(const ExecuteCmdFn &executeCommand)
 
bool safety
Safety certified behavior Show checkers report when Cppcheck finishes Make cppcheck checking more str...
 
std::string buildDir
–cppcheck-build-dir.
 
std::string cppcheckCfgProductName
cppcheck.cfg: Custom product name
 
std::string userDefines
defines given by the user
 
std::string outputFile
write results (–output-file=<file>)
 
bool useSingleJob() const
 
SimpleEnableGroup< Severity > severity
 
std::string checkersReportFilename
–checkers-report=<filename> : Generate report of executed checkers
 
bool isEnabled(T flag) const
 
unsigned int check() override
 
class for handling suppressions
 
std::list< Suppression > getUnmatchedGlobalSuppressions(const bool unusedFunctionChecking) const
Returns list of unmatched global (glob pattern) suppressions.
 
const std::list< Suppression > & getSuppressions() const
Returns list of all suppressions.
 
static bool reportUnmatchedSuppressions(const std::list< SuppressionList::Suppression > &unmatched, ErrorLogger &errorLogger)
Report unmatched suppressions.
 
std::list< Suppression > getUnmatchedLocalSuppressions(const std::string &file, const bool unusedFunctionChecking) const
Returns list of unmatched local (per-file) suppressions.
 
This class will take a list of filenames and settings and check then all files using threads.
 
unsigned int check() override
 
static const std::string emptyString
 
#define ansiToOEM(msg, doConvert)
 
@ information
Checking information.
 
@ internal
Internal message.
 
bool endsWith(const std::string &str, char c)