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 "settings.h"
|
2021-10-13 20:02:48 +02:00
|
|
|
#include "path.h"
|
2020-12-20 19:53:58 +01:00
|
|
|
#include "summaries.h"
|
2023-01-26 22:23:22 +01:00
|
|
|
#include "vfvalue.h"
|
2009-03-06 07:22:07 +01:00
|
|
|
|
2021-10-13 20:02:48 +02:00
|
|
|
#include <fstream>
|
|
|
|
|
2023-08-16 19:35:53 +02:00
|
|
|
#include "json.h"
|
2021-10-13 20:02:48 +02:00
|
|
|
|
2019-12-19 18:37:51 +01:00
|
|
|
std::atomic<bool> Settings::mTerminated;
|
2016-08-21 15:57:19 +02:00
|
|
|
|
2019-07-23 11:54:38 +02:00
|
|
|
const char Settings::SafeChecks::XmlRootName[] = "safe-checks";
|
|
|
|
const char Settings::SafeChecks::XmlClasses[] = "class-public";
|
|
|
|
const char Settings::SafeChecks::XmlExternalFunctions[] = "external-functions";
|
|
|
|
const char Settings::SafeChecks::XmlInternalFunctions[] = "internal-functions";
|
|
|
|
const char Settings::SafeChecks::XmlExternalVariables[] = "external-variables";
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
Settings::Settings()
|
|
|
|
{
|
2021-02-24 22:00:06 +01:00
|
|
|
severity.setEnabled(Severity::error, true);
|
|
|
|
certainty.setEnabled(Certainty::normal, true);
|
2023-04-09 13:48:13 +02:00
|
|
|
setCheckLevelNormal();
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
2009-03-06 07:22:07 +01:00
|
|
|
|
2023-12-01 16:34:14 +01:00
|
|
|
std::string Settings::loadCppcheckCfg()
|
2021-10-13 20:02:48 +02:00
|
|
|
{
|
2023-12-01 16:34:14 +01:00
|
|
|
static const std::string cfgFilename = "cppcheck.cfg";
|
|
|
|
std::string fileName;
|
2021-11-05 20:37:48 +01:00
|
|
|
#ifdef FILESDIR
|
2023-12-01 16:34:14 +01:00
|
|
|
if (Path::isFile(Path::join(FILESDIR, cfgFilename)))
|
|
|
|
fileName = Path::join(FILESDIR, cfgFilename);
|
2021-11-05 20:37:48 +01:00
|
|
|
#endif
|
2023-12-01 16:34:14 +01:00
|
|
|
// cppcheck-suppress knownConditionTrueFalse
|
|
|
|
if (fileName.empty()) {
|
|
|
|
fileName = Path::getPathFromFilename(exename) + cfgFilename;
|
|
|
|
if (!Path::isFile(fileName))
|
|
|
|
return "";
|
|
|
|
}
|
2021-11-05 20:37:48 +01:00
|
|
|
|
|
|
|
std::ifstream fin(fileName);
|
2021-10-13 20:02:48 +02:00
|
|
|
if (!fin.is_open())
|
2023-12-01 16:34:14 +01:00
|
|
|
return "could not open file";
|
2021-10-13 20:02:48 +02:00
|
|
|
picojson::value json;
|
|
|
|
fin >> json;
|
2023-12-01 16:34:14 +01:00
|
|
|
{
|
|
|
|
const std::string& lastErr = picojson::get_last_error();
|
|
|
|
if (!lastErr.empty())
|
|
|
|
return "not a valid JSON - " + lastErr;
|
|
|
|
}
|
|
|
|
const picojson::object& obj = json.get<picojson::object>();
|
|
|
|
{
|
|
|
|
const picojson::object::const_iterator it = obj.find("productName");
|
|
|
|
if (it != obj.cend()) {
|
|
|
|
const auto& v = it->second;
|
|
|
|
if (!v.is<std::string>())
|
|
|
|
return "'productName' is not a string";
|
|
|
|
cppcheckCfgProductName = v.get<std::string>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const picojson::object::const_iterator it = obj.find("about");
|
|
|
|
if (it != obj.cend()) {
|
|
|
|
const auto& v = it->second;
|
|
|
|
if (!v.is<std::string>())
|
|
|
|
return "'about' is not a string";
|
|
|
|
cppcheckCfgAbout = v.get<std::string>();
|
2021-10-13 20:02:48 +02:00
|
|
|
}
|
|
|
|
}
|
2023-12-01 16:34:14 +01:00
|
|
|
{
|
|
|
|
const picojson::object::const_iterator it = obj.find("addons");
|
|
|
|
if (it != obj.cend()) {
|
|
|
|
const auto& entry = it->second;
|
|
|
|
if (!entry.is<picojson::array>())
|
|
|
|
return "'addons' is not an array";
|
|
|
|
for (const picojson::value &v : entry.get<picojson::array>())
|
|
|
|
{
|
|
|
|
if (!v.is<std::string>())
|
|
|
|
return "'addons' array entry is not a string";
|
|
|
|
const std::string &s = v.get<std::string>();
|
|
|
|
if (!Path::isAbsolute(s))
|
|
|
|
addons.emplace(Path::join(Path::getPathFromFilename(fileName), s));
|
|
|
|
else
|
|
|
|
addons.emplace(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const picojson::object::const_iterator it = obj.find("suppressions");
|
|
|
|
if (it != obj.cend()) {
|
|
|
|
const auto& entry = it->second;
|
|
|
|
if (!entry.is<picojson::array>())
|
|
|
|
return "'suppressions' is not an array";
|
|
|
|
for (const picojson::value &v : entry.get<picojson::array>())
|
|
|
|
{
|
|
|
|
if (!v.is<std::string>())
|
|
|
|
return "'suppressions' array entry is not a string";
|
|
|
|
const std::string &s = v.get<std::string>();
|
|
|
|
const std::string err = nomsg.addSuppressionLine(s);
|
|
|
|
if (!err.empty())
|
|
|
|
return "could not parse suppression '" + s + "' - " + err;
|
|
|
|
}
|
|
|
|
}
|
2021-10-13 20:02:48 +02:00
|
|
|
}
|
2023-12-18 18:26:23 +01:00
|
|
|
{
|
|
|
|
const picojson::object::const_iterator it = obj.find("safety");
|
|
|
|
if (it != obj.cend()) {
|
|
|
|
const auto& v = it->second;
|
|
|
|
if (!v.is<bool>())
|
|
|
|
return "'safety' is not a bool";
|
|
|
|
safety = v.get<bool>();
|
|
|
|
}
|
|
|
|
}
|
2023-12-01 16:34:14 +01:00
|
|
|
|
|
|
|
return "";
|
2021-10-13 20:02:48 +02:00
|
|
|
}
|
|
|
|
|
2023-10-12 11:58:39 +02:00
|
|
|
std::string Settings::parseEnabled(const std::string &str, std::tuple<SimpleEnableGroup<Severity>, SimpleEnableGroup<Checks>> &groups)
|
2009-11-30 22:48:58 +01:00
|
|
|
{
|
2009-12-04 21:31:14 +01:00
|
|
|
// Enable parameters may be comma separated...
|
2015-08-15 20:24:26 +02:00
|
|
|
if (str.find(',') != std::string::npos) {
|
2009-12-04 21:31:14 +01:00
|
|
|
std::string::size_type prevPos = 0;
|
|
|
|
std::string::size_type pos = 0;
|
2015-08-15 20:24:26 +02:00
|
|
|
while ((pos = str.find(',', pos)) != std::string::npos) {
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos == prevPos)
|
2021-10-30 13:30:48 +02:00
|
|
|
return std::string("--enable parameter is empty");
|
2023-01-21 10:39:44 +01:00
|
|
|
std::string errmsg(parseEnabled(str.substr(prevPos, pos - prevPos), groups));
|
2010-07-23 13:29:16 +02:00
|
|
|
if (!errmsg.empty())
|
|
|
|
return errmsg;
|
2009-12-04 21:31:14 +01:00
|
|
|
++pos;
|
|
|
|
prevPos = pos;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (prevPos >= str.length())
|
2021-10-30 13:30:48 +02:00
|
|
|
return std::string("--enable parameter is empty");
|
2023-01-21 10:39:44 +01:00
|
|
|
return parseEnabled(str.substr(prevPos), groups);
|
2009-12-04 21:31:14 +01:00
|
|
|
}
|
|
|
|
|
2023-01-21 10:39:44 +01:00
|
|
|
auto& severity = std::get<0>(groups);
|
|
|
|
auto& checks = std::get<1>(groups);
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (str == "all") {
|
2023-01-21 10:39:44 +01:00
|
|
|
// "error" is always enabled and cannot be controlled - so exclude it from "all"
|
2023-10-12 11:58:39 +02:00
|
|
|
SimpleEnableGroup<Severity> newSeverity;
|
2023-01-21 10:39:44 +01:00
|
|
|
newSeverity.fill();
|
2023-10-12 11:58:39 +02:00
|
|
|
newSeverity.disable(Severity::error);
|
2023-01-21 10:39:44 +01:00
|
|
|
severity.enable(newSeverity);
|
2021-02-24 22:00:06 +01:00
|
|
|
checks.enable(Checks::missingInclude);
|
|
|
|
checks.enable(Checks::unusedFunction);
|
2017-04-11 11:49:09 +02:00
|
|
|
} else if (str == "warning") {
|
2021-02-24 22:00:06 +01:00
|
|
|
severity.enable(Severity::warning);
|
2017-04-11 11:49:09 +02:00
|
|
|
} else if (str == "style") {
|
2021-02-24 22:00:06 +01:00
|
|
|
severity.enable(Severity::style);
|
2017-04-11 11:49:09 +02:00
|
|
|
} else if (str == "performance") {
|
2021-02-24 22:00:06 +01:00
|
|
|
severity.enable(Severity::performance);
|
2017-04-11 11:49:09 +02:00
|
|
|
} else if (str == "portability") {
|
2021-02-24 22:00:06 +01:00
|
|
|
severity.enable(Severity::portability);
|
2017-04-11 11:49:09 +02:00
|
|
|
} else if (str == "information") {
|
2021-02-24 22:00:06 +01:00
|
|
|
severity.enable(Severity::information);
|
2017-04-11 11:49:09 +02:00
|
|
|
} else if (str == "unusedFunction") {
|
2021-02-24 22:00:06 +01:00
|
|
|
checks.enable(Checks::unusedFunction);
|
2017-04-11 11:49:09 +02:00
|
|
|
} else if (str == "missingInclude") {
|
2021-02-24 22:00:06 +01:00
|
|
|
checks.enable(Checks::missingInclude);
|
2017-04-11 11:49:09 +02:00
|
|
|
}
|
|
|
|
#ifdef CHECK_INTERNAL
|
|
|
|
else if (str == "internal") {
|
2021-02-24 22:00:06 +01:00
|
|
|
checks.enable(Checks::internalCheck);
|
2017-04-11 11:49:09 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else {
|
2023-01-21 10:39:44 +01:00
|
|
|
// the actual option is prepending in the applyEnabled() call
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str.empty())
|
2023-01-21 10:39:44 +01:00
|
|
|
return " parameter is empty";
|
2023-06-20 18:43:21 +02:00
|
|
|
return " parameter with the unknown name '" + str + "'";
|
2009-11-30 22:48:58 +01:00
|
|
|
}
|
2010-07-23 13:29:16 +02:00
|
|
|
|
2023-01-21 10:39:44 +01:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Settings::addEnabled(const std::string &str)
|
|
|
|
{
|
|
|
|
return applyEnabled(str, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Settings::removeEnabled(const std::string &str)
|
|
|
|
{
|
|
|
|
return applyEnabled(str, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Settings::applyEnabled(const std::string &str, bool enable)
|
|
|
|
{
|
2023-10-12 11:58:39 +02:00
|
|
|
std::tuple<SimpleEnableGroup<Severity>, SimpleEnableGroup<Checks>> groups;
|
2023-01-21 10:39:44 +01:00
|
|
|
std::string errmsg = parseEnabled(str, groups);
|
|
|
|
if (!errmsg.empty())
|
|
|
|
return (enable ? "--enable" : "--disable") + errmsg;
|
|
|
|
|
|
|
|
const auto s = std::get<0>(groups);
|
|
|
|
const auto c = std::get<1>(groups);
|
|
|
|
if (enable) {
|
|
|
|
severity.enable(s);
|
|
|
|
checks.enable(c);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
severity.disable(s);
|
|
|
|
checks.disable(c);
|
|
|
|
}
|
|
|
|
// FIXME: hack to make sure "error" is always enabled
|
2023-10-12 11:58:39 +02:00
|
|
|
severity.enable(Severity::error);
|
2023-01-21 10:39:44 +01:00
|
|
|
return errmsg;
|
2009-11-30 22:48:58 +01:00
|
|
|
}
|
|
|
|
|
2017-05-15 20:05:11 +02:00
|
|
|
bool Settings::isEnabled(const ValueFlow::Value *value, bool inconclusiveCheck) const
|
|
|
|
{
|
2021-02-24 22:00:06 +01:00
|
|
|
if (!severity.isEnabled(Severity::warning) && (value->condition || value->defaultArg))
|
2017-05-15 20:05:11 +02:00
|
|
|
return false;
|
2021-02-24 22:00:06 +01:00
|
|
|
if (!certainty.isEnabled(Certainty::inconclusive) && (inconclusiveCheck || value->isInconclusive()))
|
2017-05-15 20:05:11 +02:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2020-12-19 19:02:42 +01:00
|
|
|
|
|
|
|
void Settings::loadSummaries()
|
|
|
|
{
|
2020-12-20 19:53:58 +01:00
|
|
|
Summaries::loadReturn(buildDir, summaryReturn);
|
2020-12-19 19:02:42 +01:00
|
|
|
}
|
2023-04-09 13:48:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
void Settings::setCheckLevelExhaustive()
|
|
|
|
{
|
|
|
|
// Checking can take a little while. ~ 10 times slower than normal analysis is OK.
|
|
|
|
performanceValueFlowMaxIfCount = -1;
|
2023-04-12 22:09:48 +02:00
|
|
|
performanceValueFlowMaxSubFunctionArgs = 256;
|
2023-04-09 13:48:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Settings::setCheckLevelNormal()
|
|
|
|
{
|
|
|
|
// Checking should finish in reasonable time.
|
2023-04-12 22:09:48 +02:00
|
|
|
performanceValueFlowMaxSubFunctionArgs = 8;
|
2023-04-09 13:48:13 +02:00
|
|
|
performanceValueFlowMaxIfCount = 100;
|
|
|
|
}
|