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
|
2022-02-05 11:45:17 +01:00
|
|
|
* Copyright (C) 2007-2022 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"
|
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"
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "errortypes.h"
|
2013-09-04 06:18:22 +02:00
|
|
|
#include "filelister.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "importproject.h"
|
|
|
|
#include "library.h"
|
2013-09-04 06:18:22 +02:00
|
|
|
#include "path.h"
|
|
|
|
#include "pathmatch.h"
|
|
|
|
#include "preprocessor.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "settings.h"
|
|
|
|
#include "suppressions.h"
|
2015-11-14 19:59:22 +01:00
|
|
|
#include "utils.h"
|
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
|
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <atomic>
|
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
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <functional>
|
2014-06-28 14:05:18 +02:00
|
|
|
#include <iostream>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <list>
|
2020-05-23 07:16:49 +02:00
|
|
|
#include <memory>
|
2022-08-19 10:16:23 +02:00
|
|
|
#include <sstream>
|
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
|
|
|
|
|
2015-11-21 16:50:57 +01:00
|
|
|
|
2019-07-15 18:11:11 +02:00
|
|
|
/*static*/ FILE* CppCheckExecutor::mExceptionOutput = stdout;
|
2015-11-21 16:50:57 +01:00
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
CppCheckExecutor::CppCheckExecutor()
|
2022-04-11 07:30:55 +02:00
|
|
|
: mSettings(nullptr), mLatestProgressOutputTime(0), mErrorOutput(nullptr), mShowAllErrors(false)
|
2021-08-07 20:51:18 +02:00
|
|
|
{}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
CppCheckExecutor::~CppCheckExecutor()
|
|
|
|
{
|
2019-07-15 18:16:07 +02:00
|
|
|
delete mErrorOutput;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2010-08-31 20:32:26 +02:00
|
|
|
bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* const argv[])
|
|
|
|
{
|
2012-04-06 14:19:26 +02:00
|
|
|
Settings& settings = cppcheck->settings();
|
|
|
|
CmdLineParser parser(&settings);
|
2018-04-23 12:39:47 +02:00
|
|
|
const bool success = parser.parseFromArgs(argc, argv);
|
2010-08-31 20:32:26 +02:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (success) {
|
2018-04-23 12:39:47 +02:00
|
|
|
if (parser.getShowVersion() && !parser.getShowErrorMessages()) {
|
2022-03-24 22:44:47 +01:00
|
|
|
if (!settings.cppcheckCfgProductName.empty()) {
|
|
|
|
std::cout << settings.cppcheckCfgProductName << std::endl;
|
|
|
|
} else {
|
|
|
|
const char * const extraVersion = CppCheck::extraVersion();
|
|
|
|
if (*extraVersion != 0)
|
|
|
|
std::cout << "Cppcheck " << CppCheck::version() << " ("
|
|
|
|
<< extraVersion << ')' << std::endl;
|
|
|
|
else
|
|
|
|
std::cout << "Cppcheck " << CppCheck::version() << std::endl;
|
|
|
|
}
|
2010-08-31 20:32:26 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 12:39:47 +02:00
|
|
|
if (parser.getShowErrorMessages()) {
|
2019-07-15 18:31:12 +02:00
|
|
|
mShowAllErrors = true;
|
2022-07-07 17:35:13 +02:00
|
|
|
std::cout << ErrorMessage::getXMLHeader(settings.cppcheckCfgProductName);
|
2010-08-31 20:32:26 +02:00
|
|
|
cppcheck->getErrorMessages();
|
2020-05-23 07:16:49 +02:00
|
|
|
std::cout << ErrorMessage::getXMLFooter() << std::endl;
|
2010-08-31 20:32:26 +02:00
|
|
|
}
|
2011-01-27 09:30:53 +01:00
|
|
|
|
2018-04-23 12:39:47 +02:00
|
|
|
if (parser.exitAfterPrinting()) {
|
2019-09-20 21:54:30 +02:00
|
|
|
Settings::terminate();
|
2014-06-05 09:28:04 +02:00
|
|
|
return true;
|
|
|
|
}
|
2011-11-05 18:15:03 +01:00
|
|
|
} else {
|
2014-06-05 09:28:04 +02:00
|
|
|
return false;
|
2010-08-31 20:32:26 +02:00
|
|
|
}
|
|
|
|
|
2011-01-09 09:29:38 +01:00
|
|
|
// Check that all include paths exist
|
|
|
|
{
|
2018-04-04 21:11:23 +02:00
|
|
|
for (std::list<std::string>::iterator iter = settings.includePaths.begin();
|
2016-01-03 16:18:17 +01:00
|
|
|
iter != settings.includePaths.end();
|
2021-08-07 20:51:18 +02:00
|
|
|
) {
|
2011-01-11 20:03:18 +01:00
|
|
|
const std::string path(Path::toNativeSeparators(*iter));
|
2011-04-16 11:19:56 +02:00
|
|
|
if (FileLister::isDirectory(path))
|
|
|
|
++iter;
|
2011-10-13 20:53:06 +02:00
|
|
|
else {
|
2015-07-25 17:39:44 +02:00
|
|
|
// If the include path is not found, warn user and remove the non-existing path from the list.
|
2021-02-24 22:00:06 +01:00
|
|
|
if (settings.severity.isEnabled(Severity::information))
|
2016-05-20 21:32:59 +02:00
|
|
|
std::cout << "(information) Couldn't find path given by -I '" << path << '\'' << std::endl;
|
2016-01-03 16:18:17 +01:00
|
|
|
iter = settings.includePaths.erase(iter);
|
2011-01-09 09:29:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-23 14:01:33 +02:00
|
|
|
// Output a warning for the user if he tries to exclude headers
|
|
|
|
bool warn = false;
|
2018-04-23 12:39:47 +02:00
|
|
|
const std::vector<std::string>& ignored = parser.getIgnoredPaths();
|
|
|
|
for (const std::string &i : ignored) {
|
|
|
|
if (Path::isHeader(i)) {
|
2015-07-23 14:01:33 +02:00
|
|
|
warn = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (warn) {
|
|
|
|
std::cout << "cppcheck: filename exclusion does not apply to header (.h and .hpp) files." << std::endl;
|
|
|
|
std::cout << "cppcheck: Please use --suppress for ignoring results from the header files." << std::endl;
|
2010-08-31 20:32:26 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 12:39:47 +02:00
|
|
|
const std::vector<std::string>& pathnames = parser.getPathNames();
|
2011-08-06 11:29:51 +02:00
|
|
|
|
2011-10-02 11:02:07 +02:00
|
|
|
#if defined(_WIN32)
|
2015-07-23 14:01:33 +02:00
|
|
|
// For Windows we want case-insensitive path matching
|
|
|
|
const bool caseSensitive = false;
|
2011-10-02 11:02:07 +02:00
|
|
|
#else
|
2015-07-23 14:01:33 +02:00
|
|
|
const bool caseSensitive = true;
|
2011-10-02 11:02:07 +02:00
|
|
|
#endif
|
2021-11-23 22:51:45 +01:00
|
|
|
if (!mSettings->project.fileSettings.empty() && !mSettings->fileFilters.empty()) {
|
2020-01-10 08:57:37 +01:00
|
|
|
// filter only for the selected filenames from all project files
|
|
|
|
std::list<ImportProject::FileSettings> newList;
|
|
|
|
|
|
|
|
for (const ImportProject::FileSettings &fsetting : settings.project.fileSettings) {
|
2021-11-23 22:51:45 +01:00
|
|
|
if (matchglobs(mSettings->fileFilters, fsetting.filename)) {
|
2020-05-11 13:48:54 +02:00
|
|
|
newList.emplace_back(fsetting);
|
2020-01-10 08:57:37 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-10 11:31:12 +01:00
|
|
|
if (!newList.empty())
|
2020-01-10 08:57:37 +01:00
|
|
|
settings.project.fileSettings = newList;
|
|
|
|
else {
|
|
|
|
std::cout << "cppcheck: error: could not find any files matching the filter." << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-10 11:31:12 +01:00
|
|
|
} else if (!pathnames.empty()) {
|
2015-07-23 14:01:33 +02:00
|
|
|
// Execute recursiveAddFiles() to each given file parameter
|
2018-04-04 21:02:13 +02:00
|
|
|
const PathMatch matcher(ignored, caseSensitive);
|
2021-04-18 21:52:14 +02:00
|
|
|
for (const std::string &pathname : pathnames) {
|
|
|
|
std::string err = FileLister::recursiveAddFiles(mFiles, Path::toNativeSeparators(pathname), mSettings->library.markupExtensions(), matcher);
|
|
|
|
if (!err.empty()) {
|
|
|
|
std::cout << "cppcheck: " << err << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2010-10-22 17:09:50 +02:00
|
|
|
}
|
2011-01-31 14:25:51 +01:00
|
|
|
|
2019-07-15 18:21:52 +02:00
|
|
|
if (mFiles.empty() && settings.project.fileSettings.empty()) {
|
2015-07-23 14:01:33 +02:00
|
|
|
std::cout << "cppcheck: error: could not find or open any of the paths given." << std::endl;
|
|
|
|
if (!ignored.empty())
|
|
|
|
std::cout << "cppcheck: Maybe all paths were ignored?" << std::endl;
|
2011-01-31 14:25:51 +01:00
|
|
|
return false;
|
2021-11-23 22:51:45 +01:00
|
|
|
} else if (!mSettings->fileFilters.empty() && settings.project.fileSettings.empty()) {
|
2020-01-10 08:57:37 +01:00
|
|
|
std::map<std::string, std::size_t> newMap;
|
|
|
|
for (std::map<std::string, std::size_t>::const_iterator i = mFiles.begin(); i != mFiles.end(); ++i)
|
2021-11-23 22:51:45 +01:00
|
|
|
if (matchglobs(mSettings->fileFilters, i->first)) {
|
2020-01-10 08:57:37 +01:00
|
|
|
newMap[i->first] = i->second;
|
|
|
|
}
|
|
|
|
mFiles = newMap;
|
|
|
|
if (mFiles.empty()) {
|
|
|
|
std::cout << "cppcheck: error: could not find any files matching the filter." << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-01-10 11:31:12 +01:00
|
|
|
|
2014-09-02 16:10:51 +02:00
|
|
|
return true;
|
2010-08-31 20:32:26 +02: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
|
|
|
{
|
2011-05-02 14:58:16 +02:00
|
|
|
Preprocessor::missingIncludeFlag = false;
|
2014-05-03 19:31:15 +02:00
|
|
|
Preprocessor::missingSystemIncludeFlag = false;
|
2011-05-02 14:58:16 +02:00
|
|
|
|
2018-02-10 15:34:49 +01:00
|
|
|
CheckUnusedFunctions::clear();
|
|
|
|
|
2020-05-19 16:04:25 +02:00
|
|
|
CppCheck cppCheck(*this, true, executeCommand);
|
2009-02-19 23:21:18 +01:00
|
|
|
|
2015-11-14 19:59:22 +01:00
|
|
|
const Settings& settings = cppCheck.settings();
|
2019-07-15 18:24:27 +02:00
|
|
|
mSettings = &settings;
|
2012-04-06 14:19:26 +02:00
|
|
|
|
2012-04-08 10:05:44 +02:00
|
|
|
if (!parseFromArgs(&cppCheck, argc, argv)) {
|
2020-05-09 14:45:47 +02:00
|
|
|
mSettings = nullptr;
|
2012-04-08 10:05:44 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2019-09-20 21:54:30 +02:00
|
|
|
if (Settings::terminated()) {
|
2020-05-09 14:45:47 +02:00
|
|
|
mSettings = nullptr;
|
2014-06-05 09:28:04 +02:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2020-05-09 14:45:47 +02:00
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (cppCheck.settings().exceptionHandling)
|
2022-03-21 18:35:53 +01:00
|
|
|
ret = check_wrapper(cppCheck);
|
2020-05-09 14:45:47 +02:00
|
|
|
else
|
2022-03-21 18:35:53 +01:00
|
|
|
ret = check_internal(cppCheck);
|
2020-05-09 14:45:47 +02:00
|
|
|
|
|
|
|
mSettings = nullptr;
|
|
|
|
return ret;
|
2014-03-16 12:04:13 +01:00
|
|
|
}
|
|
|
|
|
2017-08-21 12:52:15 +02:00
|
|
|
void CppCheckExecutor::setSettings(const Settings &settings)
|
|
|
|
{
|
2019-07-15 18:24:27 +02:00
|
|
|
mSettings = &settings;
|
2017-08-21 12:52:15 +02: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
|
2022-08-16 22:03:44 +02:00
|
|
|
return check_wrapper_seh(*this, &CppCheckExecutor::check_internal, cppcheck);
|
2014-06-28 14:05:18 +02:00
|
|
|
#elif defined(USE_UNIX_SIGNAL_HANDLING)
|
2022-08-16 22:03:44 +02:00
|
|
|
return check_wrapper_sig(*this, &CppCheckExecutor::check_internal, cppcheck);
|
2014-03-16 12:04:13 +01:00
|
|
|
#else
|
2022-03-21 18:35:53 +01:00
|
|
|
return check_internal(cppcheck);
|
2014-03-16 12:04:13 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-12 17:36:36 +02:00
|
|
|
bool CppCheckExecutor::reportSuppressions(const Settings &settings, bool unusedFunctionCheckEnabled, const std::map<std::string, std::size_t> &files, ErrorLogger& errorLogger) {
|
2022-07-12 22:51:26 +02:00
|
|
|
for (const Suppressions::Suppression& suppression: settings.nomsg.getSuppressions()) {
|
|
|
|
if (suppression.errorId == "unmatchedSuppression" && suppression.fileName.empty() && suppression.lineNumber == Suppressions::Suppression::NO_LINE)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-12 17:36:36 +02:00
|
|
|
bool err = false;
|
|
|
|
if (settings.jointSuppressionReport) {
|
|
|
|
for (std::map<std::string, std::size_t>::const_iterator i = files.begin(); i != files.end(); ++i) {
|
|
|
|
err |= errorLogger.reportUnmatchedSuppressions(
|
|
|
|
settings.nomsg.getUnmatchedLocalSuppressions(i->first, unusedFunctionCheckEnabled));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err |= errorLogger.reportUnmatchedSuppressions(settings.nomsg.getUnmatchedGlobalSuppressions(unusedFunctionCheckEnabled));
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2014-03-18 21:14:09 +01:00
|
|
|
/*
|
|
|
|
* That is a method which gets called from check_wrapper
|
|
|
|
* */
|
2022-03-21 18:35:53 +01:00
|
|
|
int CppCheckExecutor::check_internal(CppCheck& cppcheck)
|
2014-03-16 12:04:13 +01:00
|
|
|
{
|
2014-04-07 20:39:19 +02:00
|
|
|
Settings& settings = cppcheck.settings();
|
2019-07-15 18:24:27 +02:00
|
|
|
mSettings = &settings;
|
2022-03-21 18:35:53 +01:00
|
|
|
const bool std = tryLoadLibrary(settings.library, settings.exename, "std.cfg");
|
2019-03-15 06:59:37 +01:00
|
|
|
|
|
|
|
for (const std::string &lib : settings.libraries) {
|
2022-03-21 18:35:53 +01:00
|
|
|
if (!tryLoadLibrary(settings.library, settings.exename, lib.c_str())) {
|
2019-03-15 06:59:37 +01:00
|
|
|
const std::string msg("Failed to load the library " + lib);
|
2020-05-23 07:16:49 +02:00
|
|
|
const std::list<ErrorMessage::FileLocation> callstack;
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage errmsg(callstack, emptyString, Severity::information, msg, "failedToLoadCfg", Certainty::normal);
|
2019-03-15 06:59:37 +01:00
|
|
|
reportErr(errmsg);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 18:35:53 +01:00
|
|
|
if (!std) {
|
2020-05-23 07:16:49 +02:00
|
|
|
const std::list<ErrorMessage::FileLocation> callstack;
|
2022-03-21 18:35:53 +01:00
|
|
|
const std::string msg("Failed to load std.cfg. Your Cppcheck installation is broken, please re-install.");
|
2019-08-17 10:53:07 +02:00
|
|
|
#ifdef FILESDIR
|
|
|
|
const std::string details("The Cppcheck binary was compiled with FILESDIR set to \""
|
|
|
|
FILESDIR "\" and will therefore search for "
|
|
|
|
"std.cfg in " FILESDIR "/cfg.");
|
2014-01-03 21:59:50 +01:00
|
|
|
#else
|
2022-03-21 18:35:53 +01:00
|
|
|
const std::string cfgfolder(Path::fromNativeSeparators(Path::getPathFromFilename(settings.exename)) + "cfg");
|
2019-08-17 10:53:07 +02:00
|
|
|
const std::string details("The Cppcheck binary was compiled without FILESDIR set. Either the "
|
|
|
|
"std.cfg should be available in " + cfgfolder + " or the FILESDIR "
|
2014-01-03 21:59:50 +01:00
|
|
|
"should be configured.");
|
|
|
|
#endif
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage errmsg(callstack, emptyString, Severity::information, msg+" "+details, "failedToLoadCfg", Certainty::normal);
|
2013-12-26 11:15:28 +01:00
|
|
|
reportErr(errmsg);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2013-12-23 10:06:45 +01:00
|
|
|
|
2012-04-06 14:19:26 +02:00
|
|
|
if (settings.reportProgress)
|
2019-07-15 20:58:07 +02:00
|
|
|
mLatestProgressOutputTime = std::time(nullptr);
|
2010-08-12 21:03:33 +02:00
|
|
|
|
2017-05-30 15:04:28 +02:00
|
|
|
if (!settings.outputFile.empty()) {
|
2019-07-15 18:16:07 +02:00
|
|
|
mErrorOutput = new std::ofstream(settings.outputFile);
|
2017-05-30 15:04:28 +02:00
|
|
|
}
|
|
|
|
|
2016-01-03 16:18:17 +01:00
|
|
|
if (settings.xml) {
|
2022-07-07 17:35:13 +02:00
|
|
|
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()) {
|
2020-12-19 19:02:42 +01:00
|
|
|
settings.loadSummaries();
|
|
|
|
|
2016-12-08 22:46:44 +01:00
|
|
|
std::list<std::string> fileNames;
|
2019-07-15 18:21:52 +02:00
|
|
|
for (std::map<std::string, std::size_t>::const_iterator i = mFiles.begin(); i != mFiles.end(); ++i)
|
2020-05-11 13:48:54 +02:00
|
|
|
fileNames.emplace_back(i->first);
|
2020-12-19 20:45:51 +01:00
|
|
|
AnalyzerInformation::writeFilesTxt(settings.buildDir, fileNames, settings.userDefines, settings.project.fileSettings);
|
2016-11-14 20:50:08 +01:00
|
|
|
}
|
|
|
|
|
2009-10-17 08:19:37 +02:00
|
|
|
unsigned int returnValue = 0;
|
2016-01-03 16:18:17 +01:00
|
|
|
if (settings.jobs == 1) {
|
2009-10-17 08:19:37 +02:00
|
|
|
// Single process
|
2015-01-07 19:26:16 +01:00
|
|
|
settings.jointSuppressionReport = true;
|
2011-04-19 13:52:32 +02:00
|
|
|
|
2012-07-08 23:39:46 +02:00
|
|
|
std::size_t totalfilesize = 0;
|
2019-07-15 18:21:52 +02:00
|
|
|
for (std::map<std::string, std::size_t>::const_iterator i = mFiles.begin(); i != mFiles.end(); ++i) {
|
2011-04-19 13:52:32 +02:00
|
|
|
totalfilesize += i->second;
|
|
|
|
}
|
|
|
|
|
2012-07-08 23:39:46 +02:00
|
|
|
std::size_t processedsize = 0;
|
2012-02-19 17:22:59 +01:00
|
|
|
unsigned int c = 0;
|
2019-04-13 20:01:40 +02:00
|
|
|
if (settings.project.fileSettings.empty()) {
|
2019-07-15 18:21:52 +02:00
|
|
|
for (std::map<std::string, std::size_t>::const_iterator i = mFiles.begin(); i != mFiles.end(); ++i) {
|
2019-07-15 18:24:27 +02:00
|
|
|
if (!mSettings->library.markupFile(i->first)
|
|
|
|
|| !mSettings->library.processMarkupAfterCode(i->first)) {
|
2019-04-13 20:01:40 +02:00
|
|
|
returnValue += cppcheck.check(i->first);
|
|
|
|
processedsize += i->second;
|
|
|
|
if (!settings.quiet)
|
2019-07-15 18:21:52 +02:00
|
|
|
reportStatus(c + 1, mFiles.size(), processedsize, totalfilesize);
|
2019-04-13 20:01:40 +02:00
|
|
|
c++;
|
|
|
|
}
|
2013-10-20 14:09:10 +02:00
|
|
|
}
|
2019-04-13 20:01:40 +02:00
|
|
|
} else {
|
|
|
|
// filesettings
|
2020-01-10 08:57:37 +01:00
|
|
|
// check all files of the project
|
2019-04-13 20:01:40 +02:00
|
|
|
for (const ImportProject::FileSettings &fs : settings.project.fileSettings) {
|
|
|
|
returnValue += cppcheck.check(fs);
|
|
|
|
++c;
|
|
|
|
if (!settings.quiet)
|
|
|
|
reportStatus(c, settings.project.fileSettings.size(), c, settings.project.fileSettings.size());
|
2020-01-31 14:13:52 +01:00
|
|
|
if (settings.clangTidy)
|
|
|
|
cppcheck.analyseClangTidy(fs);
|
2019-04-13 20:01:40 +02:00
|
|
|
}
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
|
|
|
|
2013-10-31 19:09:01 +01:00
|
|
|
// second loop to parse all markup files which may not work until all
|
2013-10-20 14:09:10 +02:00
|
|
|
// c/cpp files have been parsed and checked
|
2019-07-15 18:21:52 +02:00
|
|
|
for (std::map<std::string, std::size_t>::const_iterator i = mFiles.begin(); i != mFiles.end(); ++i) {
|
2019-07-15 18:24:27 +02:00
|
|
|
if (mSettings->library.markupFile(i->first) && mSettings->library.processMarkupAfterCode(i->first)) {
|
2014-04-07 20:39:19 +02:00
|
|
|
returnValue += cppcheck.check(i->first);
|
2013-10-20 14:09:10 +02:00
|
|
|
processedsize += i->second;
|
2015-07-25 17:39:44 +02:00
|
|
|
if (!settings.quiet)
|
2019-07-15 18:21:52 +02:00
|
|
|
reportStatus(c + 1, mFiles.size(), processedsize, totalfilesize);
|
2013-10-20 14:09:10 +02:00
|
|
|
c++;
|
|
|
|
}
|
2011-04-23 14:20:55 +02:00
|
|
|
}
|
2018-01-12 08:24:01 +01:00
|
|
|
if (cppcheck.analyseWholeProgram())
|
|
|
|
returnValue++;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2022-07-08 16:42:57 +02:00
|
|
|
#if defined(THREADING_MODEL_THREAD)
|
2019-07-15 18:21:52 +02:00
|
|
|
ThreadExecutor executor(mFiles, settings, *this);
|
2022-07-08 16:42:57 +02:00
|
|
|
#elif defined(THREADING_MODEL_FORK)
|
|
|
|
ProcessExecutor executor(mFiles, settings, *this);
|
|
|
|
#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
|
|
|
|
2019-07-15 18:24:27 +02:00
|
|
|
cppcheck.analyseWholeProgram(mSettings->buildDir, mFiles);
|
2016-11-05 21:26:56 +01:00
|
|
|
|
2021-02-24 22:00:06 +01:00
|
|
|
if (settings.severity.isEnabled(Severity::information) || settings.checkConfiguration) {
|
2022-07-12 17:36:36 +02:00
|
|
|
const bool err = reportSuppressions(settings, cppcheck.isUnusedFunctionCheckEnabled(), mFiles, *this);
|
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);
|
2012-12-26 18:35:49 +01:00
|
|
|
|
2021-02-24 22:00:06 +01:00
|
|
|
if (settings.checks.isEnabled(Checks::missingInclude) && (Preprocessor::missingIncludeFlag || Preprocessor::missingSystemIncludeFlag)) {
|
2020-05-23 07:16:49 +02:00
|
|
|
const std::list<ErrorMessage::FileLocation> callStack;
|
|
|
|
ErrorMessage msg(callStack,
|
|
|
|
emptyString,
|
|
|
|
Severity::information,
|
|
|
|
"Cppcheck cannot find all the include files (use --check-config for details)\n"
|
|
|
|
"Cppcheck cannot find all the include files. Cppcheck can check the code without the "
|
|
|
|
"include files found. But the results will probably be more accurate if all the include "
|
|
|
|
"files are found. Please check your project's include directories and add all of them "
|
|
|
|
"as include directories for Cppcheck. To see what files Cppcheck cannot find use "
|
|
|
|
"--check-config.",
|
2022-07-09 23:21:11 +02:00
|
|
|
"",
|
2021-02-24 22:00:06 +01:00
|
|
|
Certainty::normal);
|
2022-07-09 23:21:11 +02:00
|
|
|
if (Preprocessor::missingIncludeFlag) {
|
|
|
|
msg.id = "missingInclude";
|
|
|
|
reportInfo(msg);
|
|
|
|
}
|
|
|
|
if (Preprocessor::missingSystemIncludeFlag) {
|
|
|
|
msg.id = "missingIncludeSystem";
|
|
|
|
reportInfo(msg);
|
|
|
|
}
|
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) {
|
2020-05-23 07:16:49 +02:00
|
|
|
reportErr(ErrorMessage::getXMLFooter());
|
2009-10-17 08:19:37 +02:00
|
|
|
}
|
|
|
|
|
2019-07-15 18:24:27 +02:00
|
|
|
mSettings = nullptr;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (returnValue)
|
2016-01-03 16:18:17 +01:00
|
|
|
return settings.exitCode;
|
2018-04-04 21:11:23 +02:00
|
|
|
return 0;
|
2008-12-18 22:28:57 +01: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
|
2018-04-05 08:12:15 +02:00
|
|
|
WideCharToMultiByte(CP_OEMCP, 0, wcContainer.data(), msglength, const_cast<char *>(result.data()), 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
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
void CppCheckExecutor::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 {
|
2019-07-15 18:24:27 +02:00
|
|
|
std::cerr << ansiToOEM(errmsg, (mSettings == nullptr) ? true : !mSettings->xml) << std::endl;
|
2017-08-29 16:51:56 +02:00
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2021-07-08 21:21:35 +02:00
|
|
|
void CppCheckExecutor::reportOut(const std::string &outmsg, Color c)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2021-07-08 21:21:35 +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
|
|
|
|
2012-07-08 23:39:46 +02:00
|
|
|
void CppCheckExecutor::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;
|
|
|
|
|
2010-08-08 08:45:37 +02:00
|
|
|
// Report progress messages every 10 seconds
|
2017-10-14 22:05:58 +02:00
|
|
|
const std::time_t currentTime = std::time(nullptr);
|
2019-07-15 20:58:07 +02:00
|
|
|
if (currentTime >= (mLatestProgressOutputTime + 10)) {
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
void CppCheckExecutor::reportInfo(const ErrorMessage &msg)
|
2012-06-18 23:15:48 +02:00
|
|
|
{
|
2012-07-04 18:21:56 +02:00
|
|
|
reportErr(msg);
|
2012-06-18 23:15:48 +02:00
|
|
|
}
|
|
|
|
|
2012-07-08 23:39:46 +02:00
|
|
|
void CppCheckExecutor::reportStatus(std::size_t fileindex, std::size_t filecount, std::size_t sizedone, std::size_t sizetotal)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (filecount > 1) {
|
2009-02-19 23:21:18 +01:00
|
|
|
std::ostringstream oss;
|
2017-08-29 16:51:56 +02:00
|
|
|
const long percentDone = (sizetotal > 0) ? static_cast<long>(static_cast<long double>(sizedone) / sizetotal * 100) : 0;
|
2012-02-18 11:55:05 +01:00
|
|
|
oss << fileindex << '/' << filecount
|
2017-08-29 16:51:56 +02:00
|
|
|
<< " files checked " << percentDone
|
2010-04-15 20:08:51 +02:00
|
|
|
<< "% done";
|
2021-07-08 21:21:35 +02:00
|
|
|
std::cout << Color::FgBlue << oss.str() << Color::Reset << std::endl;
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
void CppCheckExecutor::reportErr(const ErrorMessage &msg)
|
2009-02-01 19:00:47 +01:00
|
|
|
{
|
2019-07-15 18:31:12 +02:00
|
|
|
if (mShowAllErrors) {
|
2017-07-29 18:56:22 +02:00
|
|
|
reportOut(msg.toXML());
|
2021-04-05 20:09:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alert only about unique errors
|
|
|
|
if (!mShownErrors.insert(msg.toString(mSettings->verbose)).second)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mSettings->xml)
|
2017-07-29 18:56:22 +02:00
|
|
|
reportErr(msg.toXML());
|
2021-04-05 20:09:45 +02:00
|
|
|
else
|
2019-07-15 18:24:27 +02: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
|
|
|
|
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
|
|
|
}
|
2014-05-25 15:53:26 +02:00
|
|
|
|
2022-03-21 18:35:53 +01:00
|
|
|
bool CppCheckExecutor::tryLoadLibrary(Library& destination, const std::string& basepath, const char* filename)
|
2015-01-10 22:18:57 +01:00
|
|
|
{
|
2022-03-21 18:35:53 +01:00
|
|
|
const Library::Error err = destination.load(basepath.c_str(), filename);
|
2015-01-10 22:18:57 +01:00
|
|
|
|
2021-01-08 10:29:01 +01:00
|
|
|
if (err.errorcode == Library::ErrorCode::UNKNOWN_ELEMENT)
|
2015-01-10 22:18:57 +01:00
|
|
|
std::cout << "cppcheck: Found unknown elements in configuration file '" << filename << "': " << err.reason << std::endl;
|
2021-01-08 10:29:01 +01:00
|
|
|
else if (err.errorcode != Library::ErrorCode::OK) {
|
2020-05-13 19:05:15 +02:00
|
|
|
std::cout << "cppcheck: Failed to load library configuration file '" << filename << "'. ";
|
2015-01-10 22:18:57 +01:00
|
|
|
switch (err.errorcode) {
|
2021-01-08 10:29:01 +01:00
|
|
|
case Library::ErrorCode::OK:
|
2015-01-10 22:18:57 +01:00
|
|
|
break;
|
2021-01-08 10:29:01 +01:00
|
|
|
case Library::ErrorCode::FILE_NOT_FOUND:
|
2020-05-13 19:05:15 +02:00
|
|
|
std::cout << "File not found";
|
2015-01-10 22:18:57 +01:00
|
|
|
break;
|
2021-01-08 10:29:01 +01:00
|
|
|
case Library::ErrorCode::BAD_XML:
|
2020-05-13 19:05:15 +02:00
|
|
|
std::cout << "Bad XML";
|
2015-01-10 22:18:57 +01:00
|
|
|
break;
|
2021-01-08 10:29:01 +01:00
|
|
|
case Library::ErrorCode::UNKNOWN_ELEMENT:
|
2020-05-13 19:05:15 +02:00
|
|
|
std::cout << "Unexpected element";
|
2015-01-10 22:18:57 +01:00
|
|
|
break;
|
2021-01-08 10:29:01 +01:00
|
|
|
case Library::ErrorCode::MISSING_ATTRIBUTE:
|
2020-05-13 19:05:15 +02:00
|
|
|
std::cout << "Missing attribute";
|
2015-01-10 22:18:57 +01:00
|
|
|
break;
|
2021-01-08 10:29:01 +01:00
|
|
|
case Library::ErrorCode::BAD_ATTRIBUTE_VALUE:
|
2020-05-13 19:05:15 +02:00
|
|
|
std::cout << "Bad attribute value";
|
2015-01-10 22:18:57 +01:00
|
|
|
break;
|
2021-01-08 10:29:01 +01:00
|
|
|
case Library::ErrorCode::UNSUPPORTED_FORMAT:
|
2020-05-13 19:05:15 +02:00
|
|
|
std::cout << "File is of unsupported format version";
|
2015-01-10 22:18:57 +01:00
|
|
|
break;
|
2021-01-08 10:29:01 +01:00
|
|
|
case Library::ErrorCode::DUPLICATE_PLATFORM_TYPE:
|
2020-05-13 19:05:15 +02:00
|
|
|
std::cout << "Duplicate platform type";
|
2015-01-10 22:18:57 +01:00
|
|
|
break;
|
2021-01-08 10:29:01 +01:00
|
|
|
case Library::ErrorCode::PLATFORM_TYPE_REDEFINED:
|
2020-05-13 19:05:15 +02:00
|
|
|
std::cout << "Platform type redefined";
|
2015-01-10 22:18:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!err.reason.empty())
|
2020-05-13 19:05:15 +02:00
|
|
|
std::cout << " '" + err.reason + "'";
|
|
|
|
std::cout << std::endl;
|
2015-01-10 22:18:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-19 16:04:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a shell command and read the output from it. Returns true if command terminated successfully.
|
|
|
|
*/
|
2021-01-09 20:32:38 +01:00
|
|
|
// cppcheck-suppress passedByValue - "exe" copy needed in _WIN32 code
|
2021-07-16 21:55:12 +02:00
|
|
|
bool CppCheckExecutor::executeCommand(std::string exe, std::vector<std::string> args, const std::string &redirect, std::string *output_)
|
2020-05-19 16:04:25 +02:00
|
|
|
{
|
2021-07-16 21:55:12 +02:00
|
|
|
output_->clear();
|
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 += " ";
|
2020-08-30 20:02:18 +02:00
|
|
|
if (arg.find(" ") != std::string::npos)
|
|
|
|
joinedArgs += '"' + arg + '"';
|
|
|
|
else
|
|
|
|
joinedArgs += arg;
|
2020-05-19 16:04:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
// Extra quoutes are needed in windows if filename has space
|
|
|
|
if (exe.find(" ") != std::string::npos)
|
|
|
|
exe = "\"" + exe + "\"";
|
|
|
|
const std::string cmd = exe + " " + joinedArgs + " " + redirect;
|
|
|
|
std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd.c_str(), "r"), _pclose);
|
|
|
|
#else
|
|
|
|
const std::string cmd = exe + " " + joinedArgs + " " + redirect;
|
|
|
|
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
|
|
|
|
#endif
|
|
|
|
if (!pipe)
|
|
|
|
return false;
|
|
|
|
char buffer[1024];
|
|
|
|
while (fgets(buffer, sizeof(buffer), pipe.get()) != nullptr)
|
2021-07-16 21:55:12 +02:00
|
|
|
*output_ += buffer;
|
2020-05-19 16:04:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|