cppcheck/lib/settings.cpp

224 lines
7.3 KiB
C++
Raw Normal View History

2008-12-18 22:28:57 +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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2008-12-18 22:28:57 +01:00
*/
#include "settings.h"
#include "path.h"
2020-12-20 19:53:58 +01:00
#include "summaries.h"
2017-05-27 04:33:47 +02:00
#include "valueflow.h"
#include <fstream>
#define PICOJSON_USE_INT64
#include <picojson.h>
std::atomic<bool> Settings::mTerminated;
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()
: checkAllConfigurations(true),
2021-08-07 20:51:18 +02:00
checkConfiguration(false),
checkHeaders(true),
checkLibrary(false),
checksMaxTime(0),
2021-08-07 20:51:18 +02:00
checkUnusedTemplates(true),
clang(false),
clangExecutable("clang"),
clangTidy(false),
daca(false),
debugnormal(false),
debugSimplified(false),
debugtemplate(false),
debugwarnings(false),
dump(false),
2022-11-20 12:29:56 +01:00
enforcedLang(Language::None),
2021-08-07 20:51:18 +02:00
exceptionHandling(false),
exitCode(0),
force(false),
inlineSuppressions(false),
jobs(1),
jointSuppressionReport(false),
loadAverage(0),
maxConfigs(12),
maxCtuDepth(2),
maxTemplateRecursion(100),
performanceValueFlowMaxTime(-1),
2021-08-07 20:51:18 +02:00
preprocessOnly(false),
quiet(false),
relativePaths(false),
reportProgress(false),
showtime(SHOWTIME_MODES::SHOWTIME_NONE),
templateMaxTime(0),
typedefMaxTime(0),
valueFlowMaxIterations(4),
2021-08-07 20:51:18 +02:00
verbose(false),
xml(false),
xml_version(2)
2008-12-18 22:28:57 +01:00
{
severity.setEnabled(Severity::error, true);
certainty.setEnabled(Certainty::normal, true);
2008-12-18 22:28:57 +01:00
}
void Settings::loadCppcheckCfg()
{
std::string fileName = Path::getPathFromFilename(exename) + "cppcheck.cfg";
#ifdef FILESDIR
if (Path::fileExists(FILESDIR "/cppcheck.cfg"))
fileName = FILESDIR "/cppcheck.cfg";
#endif
std::ifstream fin(fileName);
if (!fin.is_open())
return;
picojson::value json;
fin >> json;
if (!picojson::get_last_error().empty())
return;
picojson::object obj = json.get<picojson::object>();
if (obj.count("productName") && obj["productName"].is<std::string>())
cppcheckCfgProductName = obj["productName"].get<std::string>();
if (obj.count("about") && obj["about"].is<std::string>())
cppcheckCfgAbout = obj["about"].get<std::string>();
if (obj.count("addons") && obj["addons"].is<picojson::array>()) {
for (const picojson::value &v : obj["addons"].get<picojson::array>()) {
const std::string &s = v.get<std::string>();
if (!Path::isAbsolute(s))
addons.emplace(Path::getPathFromFilename(fileName) + s);
else
addons.emplace(s);
}
}
if (obj.count("suppressions") && obj["suppressions"].is<picojson::array>()) {
for (const picojson::value &v : obj["suppressions"].get<picojson::array>())
nomsg.addSuppressionLine(v.get<std::string>());
}
}
std::string Settings::parseEnabled(const std::string &str, std::tuple<SimpleEnableGroup<Severity::SeverityType>, SimpleEnableGroup<Checks>> &groups)
{
// Enable parameters may be comma separated...
if (str.find(',') != std::string::npos) {
std::string::size_type prevPos = 0;
std::string::size_type pos = 0;
while ((pos = str.find(',', pos)) != std::string::npos) {
if (pos == prevPos)
return std::string("--enable parameter is empty");
std::string errmsg(parseEnabled(str.substr(prevPos, pos - prevPos), groups));
if (!errmsg.empty())
return errmsg;
++pos;
prevPos = pos;
}
if (prevPos >= str.length())
return std::string("--enable parameter is empty");
return parseEnabled(str.substr(prevPos), groups);
}
auto& severity = std::get<0>(groups);
auto& checks = std::get<1>(groups);
2011-10-13 20:53:06 +02:00
if (str == "all") {
// "error" is always enabled and cannot be controlled - so exclude it from "all"
SimpleEnableGroup<Severity::SeverityType> newSeverity;
newSeverity.fill();
newSeverity.disable(Severity::SeverityType::error);
severity.enable(newSeverity);
checks.enable(Checks::missingInclude);
checks.enable(Checks::unusedFunction);
} else if (str == "warning") {
severity.enable(Severity::warning);
} else if (str == "style") {
severity.enable(Severity::style);
} else if (str == "performance") {
severity.enable(Severity::performance);
} else if (str == "portability") {
severity.enable(Severity::portability);
} else if (str == "information") {
severity.enable(Severity::information);
checks.enable(Checks::missingInclude);
} else if (str == "unusedFunction") {
checks.enable(Checks::unusedFunction);
} else if (str == "missingInclude") {
checks.enable(Checks::missingInclude);
}
#ifdef CHECK_INTERNAL
else if (str == "internal") {
checks.enable(Checks::internalCheck);
}
#endif
else {
// the actual option is prepending in the applyEnabled() call
if (str.empty())
return " parameter is empty";
else
return " parameter with the unknown name '" + str + "'";
}
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)
{
std::tuple<SimpleEnableGroup<Severity::SeverityType>, SimpleEnableGroup<Checks>> groups;
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
severity.enable(Severity::SeverityType::error);
return errmsg;
}
bool Settings::isEnabled(const ValueFlow::Value *value, bool inconclusiveCheck) const
{
if (!severity.isEnabled(Severity::warning) && (value->condition || value->defaultArg))
return false;
if (!certainty.isEnabled(Certainty::inconclusive) && (inconclusiveCheck || value->isInconclusive()))
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
}