2008-12-18 22:28:57 +01:00
|
|
|
/*
|
2009-01-21 21:04:20 +01:00
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2008-12-18 22:28:57 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-12-18 22:28:57 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cppcheckexecutor.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2016-12-08 22:46:44 +01:00
|
|
|
#include "analyzerinfo.h"
|
2023-08-31 18:28:47 +02:00
|
|
|
#include "checkersreport.h"
|
2023-10-09 10:07:20 +02:00
|
|
|
#include "cmdlinelogger.h"
|
2013-09-04 06:18:22 +02:00
|
|
|
#include "cmdlineparser.h"
|
2021-07-08 21:21:35 +02:00
|
|
|
#include "color.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "config.h"
|
2008-12-18 22:28:57 +01:00
|
|
|
#include "cppcheck.h"
|
2023-11-16 10:35:43 +01:00
|
|
|
#include "errorlogger.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "errortypes.h"
|
2023-11-02 17:42:41 +01:00
|
|
|
#include "filesettings.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "settings.h"
|
2023-04-08 18:06:38 +02:00
|
|
|
#include "singleexecutor.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "suppressions.h"
|
2015-11-14 19:59:22 +01:00
|
|
|
#include "utils.h"
|
2023-04-08 18:06:38 +02:00
|
|
|
|
2018-02-10 15:34:49 +01:00
|
|
|
#include "checkunusedfunctions.h"
|
2014-06-28 14:05:18 +02:00
|
|
|
|
2022-07-08 16:42:57 +02:00
|
|
|
#if defined(THREADING_MODEL_THREAD)
|
|
|
|
#include "threadexecutor.h"
|
|
|
|
#elif defined(THREADING_MODEL_FORK)
|
|
|
|
#include "processexecutor.h"
|
|
|
|
#endif
|
|
|
|
|
2023-03-02 21:50:14 +01:00
|
|
|
#include <algorithm>
|
2017-09-07 13:02:44 +02:00
|
|
|
#include <cstdio>
|
2009-03-06 01:03:31 +01:00
|
|
|
#include <cstdlib> // EXIT_SUCCESS and EXIT_FAILURE
|
2023-11-16 10:35:43 +01:00
|
|
|
#include <ctime>
|
2014-06-28 14:05:18 +02:00
|
|
|
#include <iostream>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <list>
|
2023-11-16 10:35:43 +01:00
|
|
|
#include <set>
|
2022-09-16 07:15:49 +02:00
|
|
|
#include <sstream> // IWYU pragma: keep
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2011-10-09 20:14:44 +02:00
|
|
|
|
2022-08-16 22:03:44 +02:00
|
|
|
#ifdef USE_UNIX_SIGNAL_HANDLING
|
|
|
|
#include "cppcheckexecutorsig.h"
|
2014-03-16 12:04:13 +01:00
|
|
|
#endif
|
|
|
|
|
2022-08-16 22:03:44 +02:00
|
|
|
#ifdef USE_WINDOWS_SEH
|
|
|
|
#include "cppcheckexecutorseh.h"
|
2014-06-28 14:05:18 +02:00
|
|
|
#endif
|
|
|
|
|
2022-08-16 22:03:44 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
2014-03-16 12:04:13 +01:00
|
|
|
#endif
|
|
|
|
|
2023-11-19 19:45:10 +01:00
|
|
|
class CmdLineLoggerStd : public CmdLineLogger
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CmdLineLoggerStd() = default;
|
2023-10-09 22:22:39 +02:00
|
|
|
|
2023-11-19 19:45:10 +01:00
|
|
|
void printMessage(const std::string &message) override
|
2023-10-09 22:22:39 +02:00
|
|
|
{
|
2023-11-19 19:45:10 +01:00
|
|
|
printRaw("cppcheck: " + message);
|
|
|
|
}
|
2023-11-16 15:49:41 +01:00
|
|
|
|
2023-11-19 19:45:10 +01:00
|
|
|
void printError(const std::string &message) override
|
|
|
|
{
|
|
|
|
printMessage("error: " + message);
|
|
|
|
}
|
2023-11-16 15:49:41 +01:00
|
|
|
|
2023-11-19 19:45:10 +01:00
|
|
|
void printRaw(const std::string &message) override
|
|
|
|
{
|
|
|
|
std::cout << message << std::endl;
|
|
|
|
}
|
|
|
|
};
|
2023-09-25 13:43:18 +02:00
|
|
|
|
2023-11-16 10:35:43 +01:00
|
|
|
class CppCheckExecutor::StdLogger : public ErrorLogger
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit StdLogger(const Settings& settings)
|
|
|
|
: mSettings(settings)
|
|
|
|
{
|
|
|
|
if (!mSettings.outputFile.empty()) {
|
|
|
|
mErrorOutput = new std::ofstream(settings.outputFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~StdLogger() override {
|
|
|
|
delete mErrorOutput;
|
|
|
|
}
|
|
|
|
|
|
|
|
StdLogger(const StdLogger&) = delete;
|
|
|
|
StdLogger& operator=(const SingleExecutor &) = delete;
|
|
|
|
|
|
|
|
void resetLatestProgressOutputTime() {
|
|
|
|
mLatestProgressOutputTime = std::time(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to print out errors. Appends a line change.
|
|
|
|
* @param errmsg String printed to error stream
|
|
|
|
*/
|
|
|
|
void reportErr(const std::string &errmsg);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Write the checkers report
|
|
|
|
*/
|
|
|
|
void writeCheckersReport() const;
|
|
|
|
|
2023-12-18 18:26:23 +01:00
|
|
|
bool hasCriticalErrors() const {
|
|
|
|
return !mCriticalErrors.empty();
|
|
|
|
}
|
|
|
|
|
2023-11-16 10:35:43 +01:00
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Information about progress is directed here. This should be
|
|
|
|
* called by the CppCheck class only.
|
|
|
|
*
|
|
|
|
* @param outmsg Progress message e.g. "Checking main.cpp..."
|
|
|
|
*/
|
|
|
|
void reportOut(const std::string &outmsg, Color c = Color::Reset) override;
|
|
|
|
|
|
|
|
/** xml output of errors */
|
|
|
|
void reportErr(const ErrorMessage &msg) override;
|
|
|
|
|
|
|
|
void reportProgress(const std::string &filename, const char stage[], const std::size_t value) override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pointer to current settings; set while check() is running for reportError().
|
|
|
|
*/
|
|
|
|
const Settings& mSettings;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to filter out duplicate error messages.
|
|
|
|
*/
|
aligned and optimized unique error handling (#5280)
The handling in `CppCheck::reportErr()` and `Executor::hasToLog()` was
slightly different. I hope this can somehow be shared after the executor
reworking.
We were also using a very inappropriate container for the error list
which caused a lot of overhead.
`-D__GNUC__ --debug-warnings --template=daca2 --check-library -j2
../test/testsymboldatabase.cpp`
Clang 15
main process `284,218,587` -> `175,691,241`
worker process `9,123,697,183` -> `8,951,903,360`
2023-12-17 21:59:06 +01:00
|
|
|
// TODO: store hashes instead of the full messages
|
|
|
|
std::unordered_set<std::string> mShownErrors;
|
2023-11-16 10:35:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Report progress time
|
|
|
|
*/
|
|
|
|
std::time_t mLatestProgressOutputTime{};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error output
|
|
|
|
*/
|
|
|
|
std::ofstream* mErrorOutput{};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checkers that has been executed
|
|
|
|
*/
|
|
|
|
std::set<std::string> mActiveCheckers;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if there are critical errors
|
|
|
|
*/
|
|
|
|
std::string mCriticalErrors;
|
|
|
|
};
|
|
|
|
|
2023-08-08 22:58:02 +02:00
|
|
|
// TODO: do not directly write to stdout
|
2015-11-21 16:50:57 +01:00
|
|
|
|
2023-10-21 09:12:59 +02:00
|
|
|
#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
|
2019-07-15 18:11:11 +02:00
|
|
|
/*static*/ FILE* CppCheckExecutor::mExceptionOutput = stdout;
|
2023-10-21 09:12:59 +02:00
|
|
|
#endif
|
2015-11-21 16:50:57 +01:00
|
|
|
|
2009-03-06 01:03:31 +01:00
|
|
|
int CppCheckExecutor::check(int argc, const char* const argv[])
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2018-02-10 15:34:49 +01:00
|
|
|
CheckUnusedFunctions::clear();
|
|
|
|
|
2023-04-16 13:54:21 +02:00
|
|
|
Settings settings;
|
2023-11-19 19:45:10 +01:00
|
|
|
CmdLineLoggerStd logger;
|
|
|
|
CmdLineParser parser(logger, settings, settings.nomsg, settings.nofail);
|
|
|
|
if (!parser.fillSettingsFromArgs(argc, argv)) {
|
2012-04-08 10:05:44 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2019-09-20 21:54:30 +02:00
|
|
|
if (Settings::terminated()) {
|
2014-06-05 09:28:04 +02:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2020-05-09 14:45:47 +02:00
|
|
|
|
2023-11-25 22:07:49 +01:00
|
|
|
settings.loadSummaries();
|
|
|
|
|
2023-11-19 19:45:10 +01:00
|
|
|
mFiles = parser.getFiles();
|
|
|
|
mFileSettings = parser.getFileSettings();
|
|
|
|
|
2023-11-16 10:35:43 +01:00
|
|
|
mStdLogger = new StdLogger(settings);
|
2023-11-25 22:07:49 +01:00
|
|
|
|
2023-11-16 10:35:43 +01:00
|
|
|
CppCheck cppCheck(*mStdLogger, true, executeCommand);
|
2023-04-16 13:54:21 +02:00
|
|
|
cppCheck.settings() = settings;
|
2020-05-09 14:45:47 +02:00
|
|
|
|
2023-10-21 09:12:59 +02:00
|
|
|
const int ret = check_wrapper(cppCheck);
|
2020-05-09 14:45:47 +02:00
|
|
|
|
2023-11-16 10:35:43 +01:00
|
|
|
delete mStdLogger;
|
|
|
|
mStdLogger = nullptr;
|
2020-05-09 14:45:47 +02:00
|
|
|
return ret;
|
2014-03-16 12:04:13 +01:00
|
|
|
}
|
|
|
|
|
2022-03-21 18:35:53 +01:00
|
|
|
int CppCheckExecutor::check_wrapper(CppCheck& cppcheck)
|
2014-03-16 12:04:13 +01:00
|
|
|
{
|
2014-03-17 19:11:00 +01:00
|
|
|
#ifdef USE_WINDOWS_SEH
|
2023-10-21 09:12:59 +02:00
|
|
|
if (cppcheck.settings().exceptionHandling)
|
|
|
|
return check_wrapper_seh(*this, &CppCheckExecutor::check_internal, cppcheck);
|
2014-06-28 14:05:18 +02:00
|
|
|
#elif defined(USE_UNIX_SIGNAL_HANDLING)
|
2023-10-21 09:12:59 +02:00
|
|
|
if (cppcheck.settings().exceptionHandling)
|
|
|
|
return check_wrapper_sig(*this, &CppCheckExecutor::check_internal, cppcheck);
|
2014-03-16 12:04:13 +01:00
|
|
|
#endif
|
2023-10-21 09:12:59 +02:00
|
|
|
return check_internal(cppcheck);
|
2014-03-16 12:04:13 +01:00
|
|
|
}
|
|
|
|
|
2023-11-07 21:21:24 +01:00
|
|
|
bool CppCheckExecutor::reportSuppressions(const Settings &settings, bool unusedFunctionCheckEnabled, const std::list<std::pair<std::string, std::size_t>> &files, ErrorLogger& errorLogger) {
|
2023-01-17 20:48:26 +01:00
|
|
|
const auto& suppressions = settings.nomsg.getSuppressions();
|
|
|
|
if (std::any_of(suppressions.begin(), suppressions.end(), [](const Suppressions::Suppression& s) {
|
|
|
|
return s.errorId == "unmatchedSuppression" && s.fileName.empty() && s.lineNumber == Suppressions::Suppression::NO_LINE;
|
|
|
|
}))
|
|
|
|
return false;
|
2022-07-12 22:51:26 +02:00
|
|
|
|
2022-07-12 17:36:36 +02:00
|
|
|
bool err = false;
|
2023-04-08 18:06:38 +02:00
|
|
|
if (settings.useSingleJob()) {
|
2023-11-07 21:21:24 +01:00
|
|
|
for (std::list<std::pair<std::string, std::size_t>>::const_iterator i = files.cbegin(); i != files.cend(); ++i) {
|
2023-08-18 11:55:23 +02:00
|
|
|
err |= Suppressions::reportUnmatchedSuppressions(
|
|
|
|
settings.nomsg.getUnmatchedLocalSuppressions(i->first, unusedFunctionCheckEnabled), errorLogger);
|
2022-07-12 17:36:36 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-18 11:55:23 +02:00
|
|
|
err |= Suppressions::reportUnmatchedSuppressions(settings.nomsg.getUnmatchedGlobalSuppressions(unusedFunctionCheckEnabled), errorLogger);
|
2022-07-12 17:36:36 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2014-03-18 21:14:09 +01:00
|
|
|
/*
|
|
|
|
* That is a method which gets called from check_wrapper
|
|
|
|
* */
|
2023-11-25 22:07:49 +01:00
|
|
|
int CppCheckExecutor::check_internal(CppCheck& cppcheck) const
|
2014-03-16 12:04:13 +01:00
|
|
|
{
|
2023-11-25 22:07:49 +01:00
|
|
|
const auto& settings = cppcheck.settings();
|
|
|
|
auto& suppressions = cppcheck.settings().nomsg;
|
2019-03-15 06:59:37 +01:00
|
|
|
|
2023-08-23 11:20:20 +02:00
|
|
|
if (settings.reportProgress >= 0)
|
2023-11-16 10:35:43 +01:00
|
|
|
mStdLogger->resetLatestProgressOutputTime();
|
2017-05-30 15:04:28 +02:00
|
|
|
|
2016-01-03 16:18:17 +01:00
|
|
|
if (settings.xml) {
|
2023-11-16 10:35:43 +01:00
|
|
|
mStdLogger->reportErr(ErrorMessage::getXMLHeader(settings.cppcheckCfgProductName));
|
2009-10-17 08:19:37 +02:00
|
|
|
}
|
2009-02-09 21:51:04 +01:00
|
|
|
|
2016-11-14 20:50:08 +01:00
|
|
|
if (!settings.buildDir.empty()) {
|
2016-12-08 22:46:44 +01:00
|
|
|
std::list<std::string> fileNames;
|
2023-11-07 21:21:24 +01:00
|
|
|
for (std::list<std::pair<std::string, std::size_t>>::const_iterator i = mFiles.cbegin(); i != mFiles.cend(); ++i)
|
2020-05-11 13:48:54 +02:00
|
|
|
fileNames.emplace_back(i->first);
|
2023-11-03 23:24:04 +01:00
|
|
|
AnalyzerInformation::writeFilesTxt(settings.buildDir, fileNames, settings.userDefines, mFileSettings);
|
2016-11-14 20:50:08 +01:00
|
|
|
}
|
|
|
|
|
2023-08-29 12:00:52 +02:00
|
|
|
if (!settings.checkersReportFilename.empty())
|
|
|
|
std::remove(settings.checkersReportFilename.c_str());
|
|
|
|
|
2023-11-16 10:35:43 +01:00
|
|
|
unsigned int returnValue;
|
2023-04-08 18:06:38 +02:00
|
|
|
if (settings.useSingleJob()) {
|
2009-10-17 08:19:37 +02:00
|
|
|
// Single process
|
2023-11-25 22:07:49 +01:00
|
|
|
SingleExecutor executor(cppcheck, mFiles, mFileSettings, settings, suppressions, *mStdLogger);
|
2023-04-08 18:06:38 +02:00
|
|
|
returnValue = executor.check();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2022-07-08 16:42:57 +02:00
|
|
|
#if defined(THREADING_MODEL_THREAD)
|
2023-11-25 22:07:49 +01:00
|
|
|
ThreadExecutor executor(mFiles, mFileSettings, settings, suppressions, *mStdLogger, CppCheckExecutor::executeCommand);
|
2022-07-08 16:42:57 +02:00
|
|
|
#elif defined(THREADING_MODEL_FORK)
|
2023-11-25 22:07:49 +01:00
|
|
|
ProcessExecutor executor(mFiles, mFileSettings, settings, suppressions, *mStdLogger, CppCheckExecutor::executeCommand);
|
2022-07-08 16:42:57 +02:00
|
|
|
#endif
|
2009-10-17 08:19:37 +02:00
|
|
|
returnValue = executor.check();
|
2009-01-08 22:30:25 +01:00
|
|
|
}
|
2009-10-17 08:19:37 +02:00
|
|
|
|
2023-11-03 23:24:04 +01:00
|
|
|
cppcheck.analyseWholeProgram(settings.buildDir, mFiles, mFileSettings);
|
2016-11-05 21:26:56 +01:00
|
|
|
|
2021-02-24 22:00:06 +01:00
|
|
|
if (settings.severity.isEnabled(Severity::information) || settings.checkConfiguration) {
|
2023-11-16 10:35:43 +01:00
|
|
|
const bool err = reportSuppressions(settings, cppcheck.isUnusedFunctionCheckEnabled(), mFiles, *mStdLogger);
|
2019-01-21 20:33:22 +01:00
|
|
|
if (err && returnValue == 0)
|
|
|
|
returnValue = settings.exitCode;
|
2015-01-07 19:26:16 +01:00
|
|
|
}
|
2011-05-02 14:58:16 +02:00
|
|
|
|
2013-05-31 15:20:58 +02:00
|
|
|
if (!settings.checkConfiguration) {
|
2022-07-10 10:57:29 +02:00
|
|
|
cppcheck.tooManyConfigsError(emptyString,0U);
|
2011-05-02 14:58:16 +02:00
|
|
|
}
|
2011-02-16 02:12:15 +01:00
|
|
|
|
2016-01-03 16:18:17 +01:00
|
|
|
if (settings.xml) {
|
2023-11-16 10:35:43 +01:00
|
|
|
mStdLogger->reportErr(ErrorMessage::getXMLFooter());
|
2009-10-17 08:19:37 +02:00
|
|
|
}
|
|
|
|
|
2023-11-16 10:35:43 +01:00
|
|
|
mStdLogger->writeCheckersReport();
|
2023-08-29 12:00:52 +02:00
|
|
|
|
2023-12-18 18:26:23 +01:00
|
|
|
if (settings.safety && mStdLogger->hasCriticalErrors())
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (returnValue)
|
2016-01-03 16:18:17 +01:00
|
|
|
return settings.exitCode;
|
2023-12-18 18:26:23 +01:00
|
|
|
return EXIT_SUCCESS;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2023-11-16 10:35:43 +01:00
|
|
|
void CppCheckExecutor::StdLogger::writeCheckersReport() const
|
2023-08-29 12:00:52 +02:00
|
|
|
{
|
2023-11-16 10:35:43 +01:00
|
|
|
CheckersReport checkersReport(mSettings, mActiveCheckers);
|
2023-08-31 18:28:47 +02:00
|
|
|
|
2023-11-16 10:35:43 +01:00
|
|
|
if (!mSettings.quiet) {
|
2023-08-31 18:28:47 +02:00
|
|
|
const int activeCheckers = checkersReport.getActiveCheckersCount();
|
|
|
|
const int totalCheckers = checkersReport.getAllCheckersCount();
|
2023-08-29 12:00:52 +02:00
|
|
|
|
2023-11-16 10:35:43 +01:00
|
|
|
const std::string extra = mSettings.verbose ? " (use --checkers-report=<filename> to see details)" : "";
|
2023-08-29 12:00:52 +02:00
|
|
|
if (mCriticalErrors.empty())
|
|
|
|
std::cout << "Active checkers: " << activeCheckers << "/" << totalCheckers << extra << std::endl;
|
|
|
|
else
|
|
|
|
std::cout << "Active checkers: There was critical errors" << extra << std::endl;
|
|
|
|
}
|
|
|
|
|
2023-11-16 10:35:43 +01:00
|
|
|
if (mSettings.checkersReportFilename.empty())
|
2023-08-29 12:00:52 +02:00
|
|
|
return;
|
|
|
|
|
2023-11-16 10:35:43 +01:00
|
|
|
std::ofstream fout(mSettings.checkersReportFilename);
|
2023-08-31 18:28:47 +02:00
|
|
|
if (fout.is_open())
|
|
|
|
fout << checkersReport.getReport(mCriticalErrors);
|
2023-08-29 12:00:52 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-21 12:52:15 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
// fix trac ticket #439 'Cppcheck reports wrong filename for filenames containing 8-bit ASCII'
|
2017-10-03 18:24:18 +02:00
|
|
|
static inline std::string ansiToOEM(const std::string &msg, bool doConvert)
|
2017-08-21 12:52:15 +02:00
|
|
|
{
|
|
|
|
if (doConvert) {
|
2017-08-22 13:15:11 +02:00
|
|
|
const unsigned msglength = msg.length();
|
2017-08-21 12:52:15 +02:00
|
|
|
// convert ANSI strings to OEM strings in two steps
|
2017-08-22 13:15:11 +02:00
|
|
|
std::vector<WCHAR> wcContainer(msglength);
|
|
|
|
std::string result(msglength, '\0');
|
2017-08-21 12:52:15 +02:00
|
|
|
|
|
|
|
// ansi code page characters to wide characters
|
2017-08-22 13:15:11 +02:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, msg.data(), msglength, wcContainer.data(), msglength);
|
2017-08-21 12:52:15 +02:00
|
|
|
// wide characters to oem codepage characters
|
2023-09-28 18:20:43 +02:00
|
|
|
WideCharToMultiByte(CP_OEMCP, 0, wcContainer.data(), msglength, &result[0], msglength, nullptr, nullptr);
|
2017-08-21 12:52:15 +02:00
|
|
|
|
2017-08-22 13:15:11 +02:00
|
|
|
return result; // hope for return value optimization
|
2017-08-21 12:52:15 +02:00
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// no performance regression on non-windows systems
|
2017-08-29 20:23:45 +02:00
|
|
|
#define ansiToOEM(msg, doConvert) (msg)
|
2017-08-21 12:52:15 +02:00
|
|
|
#endif
|
|
|
|
|
2023-11-16 10:35:43 +01:00
|
|
|
void CppCheckExecutor::StdLogger::reportErr(const std::string &errmsg)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2019-07-15 18:16:07 +02:00
|
|
|
if (mErrorOutput)
|
|
|
|
*mErrorOutput << errmsg << std::endl;
|
2017-08-30 17:42:04 +02:00
|
|
|
else {
|
2023-11-16 10:35:43 +01:00
|
|
|
std::cerr << ansiToOEM(errmsg, !mSettings.xml) << std::endl;
|
2017-08-29 16:51:56 +02:00
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2023-11-16 10:35:43 +01:00
|
|
|
void CppCheckExecutor::StdLogger::reportOut(const std::string &outmsg, Color c)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2023-04-08 18:06:38 +02:00
|
|
|
if (c == Color::Reset)
|
|
|
|
std::cout << ansiToOEM(outmsg, true) << std::endl;
|
|
|
|
else
|
2023-04-08 22:19:52 +02:00
|
|
|
std::cout << c << ansiToOEM(outmsg, true) << Color::Reset << std::endl;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
2009-02-01 19:00:47 +01:00
|
|
|
|
2023-08-23 11:20:20 +02:00
|
|
|
// TODO: remove filename parameter?
|
2023-11-16 10:35:43 +01:00
|
|
|
void CppCheckExecutor::StdLogger::reportProgress(const std::string &filename, const char stage[], const std::size_t value)
|
2010-08-08 08:45:37 +02:00
|
|
|
{
|
|
|
|
(void)filename;
|
|
|
|
|
2019-07-15 20:58:07 +02:00
|
|
|
if (!mLatestProgressOutputTime)
|
2010-08-12 21:03:33 +02:00
|
|
|
return;
|
|
|
|
|
2023-08-23 11:20:20 +02:00
|
|
|
// Report progress messages every x seconds
|
2017-10-14 22:05:58 +02:00
|
|
|
const std::time_t currentTime = std::time(nullptr);
|
2023-11-16 10:35:43 +01:00
|
|
|
if (currentTime >= (mLatestProgressOutputTime + mSettings.reportProgress))
|
2023-08-23 11:20:20 +02:00
|
|
|
{
|
2019-07-15 20:58:07 +02:00
|
|
|
mLatestProgressOutputTime = currentTime;
|
2010-08-08 08:45:37 +02:00
|
|
|
|
|
|
|
// format a progress message
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << "progress: "
|
|
|
|
<< stage
|
2012-07-06 13:17:08 +02:00
|
|
|
<< ' ' << value << '%';
|
2010-08-08 08:45:37 +02:00
|
|
|
|
|
|
|
// Report progress message
|
|
|
|
reportOut(ostr.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 10:35:43 +01:00
|
|
|
void CppCheckExecutor::StdLogger::reportErr(const ErrorMessage &msg)
|
2009-02-01 19:00:47 +01:00
|
|
|
{
|
2023-12-18 18:26:23 +01:00
|
|
|
if (msg.severity == Severity::internal && (msg.id == "logChecker" || endsWith(msg.id, "-logChecker"))) {
|
2023-08-29 12:00:52 +02:00
|
|
|
const std::string& checker = msg.shortMessage();
|
|
|
|
mActiveCheckers.emplace(checker);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ErrorLogger::isCriticalErrorId(msg.id) && mCriticalErrors.find(msg.id) == std::string::npos) {
|
|
|
|
if (!mCriticalErrors.empty())
|
|
|
|
mCriticalErrors += ", ";
|
|
|
|
mCriticalErrors += msg.id;
|
2023-12-18 18:26:23 +01:00
|
|
|
if (msg.severity == Severity::internal)
|
|
|
|
mCriticalErrors += " (suppressed)";
|
2023-08-29 12:00:52 +02:00
|
|
|
}
|
|
|
|
|
2023-12-18 18:26:23 +01:00
|
|
|
if (msg.severity == Severity::internal)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// TODO: we generate a different message here then we log below
|
|
|
|
// TODO: there should be no need for verbose and default messages here
|
|
|
|
// Alert only about unique errors
|
|
|
|
if (!mShownErrors.insert(msg.toString(mSettings.verbose)).second)
|
|
|
|
return;
|
|
|
|
|
2023-11-16 10:35:43 +01:00
|
|
|
if (mSettings.xml)
|
2017-07-29 18:56:22 +02:00
|
|
|
reportErr(msg.toXML());
|
2021-04-05 20:09:45 +02:00
|
|
|
else
|
2023-11-16 10:35:43 +01:00
|
|
|
reportErr(msg.toString(mSettings.verbose, mSettings.templateFormat, mSettings.templateLocation));
|
2009-02-01 19:00:47 +01:00
|
|
|
}
|
2014-05-25 08:47:37 +02:00
|
|
|
|
2023-10-21 09:12:59 +02:00
|
|
|
#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
|
2019-07-15 18:11:11 +02:00
|
|
|
void CppCheckExecutor::setExceptionOutput(FILE* exceptionOutput)
|
2014-05-25 08:47:37 +02:00
|
|
|
{
|
2019-07-15 18:11:11 +02:00
|
|
|
mExceptionOutput = exceptionOutput;
|
2014-05-25 08:47:37 +02:00
|
|
|
}
|
2014-05-25 15:53:26 +02:00
|
|
|
|
2015-11-25 22:37:38 +01:00
|
|
|
FILE* CppCheckExecutor::getExceptionOutput()
|
2014-05-25 08:47:37 +02:00
|
|
|
{
|
2019-07-15 18:11:11 +02:00
|
|
|
return mExceptionOutput;
|
2014-05-25 08:47:37 +02:00
|
|
|
}
|
2023-10-21 09:12:59 +02:00
|
|
|
#endif
|
2014-05-25 15:53:26 +02:00
|
|
|
|
2020-05-19 16:04:25 +02:00
|
|
|
/**
|
|
|
|
* Execute a shell command and read the output from it. Returns true if command terminated successfully.
|
|
|
|
*/
|
2023-10-31 12:06:31 +01:00
|
|
|
// cppcheck-suppress passedByValueCallback - used as callback so we need to preserve the signature
|
2022-09-16 18:58:59 +02:00
|
|
|
// NOLINTNEXTLINE(performance-unnecessary-value-param) - used as callback so we need to preserve the signature
|
2023-09-20 10:40:57 +02:00
|
|
|
int CppCheckExecutor::executeCommand(std::string exe, std::vector<std::string> args, std::string redirect, std::string &output_)
|
2020-05-19 16:04:25 +02:00
|
|
|
{
|
2023-03-02 22:05:41 +01:00
|
|
|
output_.clear();
|
2020-05-19 16:04:25 +02:00
|
|
|
|
2023-08-31 13:33:29 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
// Extra quoutes are needed in windows if filename has space
|
|
|
|
if (exe.find(" ") != std::string::npos)
|
|
|
|
exe = "\"" + exe + "\"";
|
|
|
|
#endif
|
|
|
|
|
2020-05-19 16:04:25 +02:00
|
|
|
std::string joinedArgs;
|
2020-08-23 20:53:44 +02:00
|
|
|
for (const std::string &arg : args) {
|
2020-05-19 16:04:25 +02:00
|
|
|
if (!joinedArgs.empty())
|
|
|
|
joinedArgs += " ";
|
2023-02-08 21:01:51 +01:00
|
|
|
if (arg.find(' ') != std::string::npos)
|
2020-08-30 20:02:18 +02:00
|
|
|
joinedArgs += '"' + arg + '"';
|
|
|
|
else
|
|
|
|
joinedArgs += arg;
|
2020-05-19 16:04:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string cmd = exe + " " + joinedArgs + " " + redirect;
|
2023-08-31 13:33:29 +02:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
FILE* p = _popen(cmd.c_str(), "r");
|
2020-05-19 16:04:25 +02:00
|
|
|
#else
|
2023-08-31 13:33:29 +02:00
|
|
|
FILE *p = popen(cmd.c_str(), "r");
|
2020-05-19 16:04:25 +02:00
|
|
|
#endif
|
2023-09-20 10:40:57 +02:00
|
|
|
//std::cout << "invoking command '" << cmd << "'" << std::endl;
|
2023-08-31 13:33:29 +02:00
|
|
|
if (!p) {
|
2023-09-20 10:40:57 +02:00
|
|
|
// TODO: how to provide to caller?
|
|
|
|
//const int err = errno;
|
|
|
|
//std::cout << "popen() errno " << std::to_string(err) << std::endl;
|
|
|
|
return -1;
|
2023-08-31 13:33:29 +02:00
|
|
|
}
|
2020-05-19 16:04:25 +02:00
|
|
|
char buffer[1024];
|
2023-08-31 13:33:29 +02:00
|
|
|
while (fgets(buffer, sizeof(buffer), p) != nullptr)
|
2023-03-02 22:05:41 +01:00
|
|
|
output_ += buffer;
|
2023-08-31 13:33:29 +02:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
const int res = _pclose(p);
|
|
|
|
#else
|
|
|
|
const int res = pclose(p);
|
|
|
|
#endif
|
2023-10-17 18:32:07 +02:00
|
|
|
if (res == -1) { // error occurred
|
2023-09-20 10:40:57 +02:00
|
|
|
// TODO: how to provide to caller?
|
|
|
|
//const int err = errno;
|
|
|
|
//std::cout << "pclose() errno " << std::to_string(err) << std::endl;
|
|
|
|
return res;
|
2023-08-31 13:33:29 +02:00
|
|
|
}
|
2023-09-20 10:40:57 +02:00
|
|
|
#if !defined(WIN32) && !defined(__MINGW32__)
|
|
|
|
if (WIFEXITED(res)) {
|
|
|
|
return WEXITSTATUS(res);
|
2023-08-31 13:33:29 +02:00
|
|
|
}
|
2023-09-20 10:40:57 +02:00
|
|
|
if (WIFSIGNALED(res)) {
|
|
|
|
return WTERMSIG(res);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return res;
|
2020-05-19 16:04:25 +02:00
|
|
|
}
|
|
|
|
|