Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
262 changes: 131 additions & 131 deletions Makefile

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@
#include <iostream>
#include <iterator>
#include <list>
#include <memory>
#include <set>
#include <sstream>
#include <unordered_set>
#include <utility>

#ifdef HAVE_RULES
#include "regex.h"
#include "rule.h"

// xml is used for rules
#include "xml.h"

#include <memory>
#endif

static bool addFilesToList(const std::string& fileList, std::vector<std::string>& pathNames)
Expand Down Expand Up @@ -1303,7 +1305,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
// Rule given at command line
else if (std::strncmp(argv[i], "--rule=", 7) == 0) {
#ifdef HAVE_RULES
Settings::Rule rule;
Rule rule;
rule.pattern = 7 + argv[i];

if (rule.pattern.empty()) {
Expand Down Expand Up @@ -1339,7 +1341,8 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
if (node && strcmp(node->Value(), "rules") == 0)
node = node->FirstChildElement("rule");
for (; node && strcmp(node->Value(), "rule") == 0; node = node->NextSiblingElement()) {
Settings::Rule rule;
Rule rule;
Regex::Engine regexEngine = Regex::Engine::Unknown;

for (const tinyxml2::XMLElement *subnode = node->FirstChildElement(); subnode; subnode = subnode->NextSiblingElement()) {
const char * const subname = subnode->Name();
Expand Down Expand Up @@ -1372,7 +1375,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
else if (std::strcmp(subname, "engine") == 0) {
const char * const engine = empty_if_null(subtext);
if (std::strcmp(engine, "pcre") == 0) {
rule.engine = Regex::Engine::Pcre;
regexEngine = Regex::Engine::Pcre;
}
else {
mLogger.printError(std::string("unknown regex engine '") + engine + "'.");
Expand Down Expand Up @@ -1406,7 +1409,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
}

std::string regex_err;
auto regex = Regex::create(rule.pattern, rule.engine, regex_err);
auto regex = Regex::create(rule.pattern, regexEngine, regex_err);
if (!regex) {
mLogger.printError("unable to load rule-file '" + ruleFile + "' - pattern '" + rule.pattern + "' failed to compile (" + regex_err + ").");
return Result::Fail;
Expand Down
3 changes: 2 additions & 1 deletion cli/processexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@
#include <numeric>
#include <cassert>
#include <cerrno>
#include <chrono>
#include <csignal>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <sstream>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
Expand Down
1 change: 0 additions & 1 deletion cli/singleexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "cppcheck.h"
#include "filesettings.h"
#include "settings.h"
#include "timer.h"

#include <cassert>
#include <cstddef>
Expand Down
1 change: 0 additions & 1 deletion cli/threadexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "filesettings.h"
#include "settings.h"
#include "suppressions.h"
#include "timer.h"

#include <cassert>
#include <cstdlib>
Expand Down
3 changes: 0 additions & 3 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "aboutdialog.h"
#include "analyzerinfo.h"
#include "checkers.h"
#include "checkstatistics.h"
#include "checkthread.h"
#include "common.h"
#include "cppcheck.h"
Expand Down Expand Up @@ -60,7 +59,6 @@

#include <algorithm>
#include <cstddef>
#include <fstream>
#include <iterator>
#include <list>
#include <memory>
Expand Down Expand Up @@ -101,7 +99,6 @@
#include <QSettings>
#include <QSize>
#include <QTimer>
#include <QTemporaryFile>
#include <QToolBar>
#include <QUrl>
#include <QVariant>
Expand Down
1 change: 0 additions & 1 deletion gui/projectfiledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "importproject.h"
#include "library.h"
#include "newsuppressiondialog.h"
#include "path.h"
#include "platform.h"
#include "platforms.h"
#include "projectfile.h"
Expand Down
5 changes: 5 additions & 0 deletions gui/test/projectfile/testprojectfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#include "settings.h"
#include "suppressions.h"

#ifdef HAVE_RULES
#include "rule.h"
#endif

#include <QFile>
#include <QIODevice>
#include <QList>
Expand All @@ -38,6 +42,7 @@ const char Settings::SafeChecks::XmlExternalFunctions[] = "external-functions";
const char Settings::SafeChecks::XmlInternalFunctions[] = "internal-functions";
const char Settings::SafeChecks::XmlExternalVariables[] = "external-variables";
Settings::Settings() : maxCtuDepth(10) {}
Settings::~Settings() = default;
Platform::Platform() = default;
Library::Library() = default;
Library::~Library() = default;
Expand Down
1 change: 0 additions & 1 deletion gui/test/resultstree/testresultstree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

// headers that declare mocked functions/variables
#include "applicationlist.h"
#include "common.h"
#include "filesettings.h"
#include "projectfile.h"
#include "threadhandler.h"
Expand Down
2 changes: 2 additions & 0 deletions lib/checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
#include "checkunusedvar.h"
#include "checkvaarg.h"

#include <memory>

class CheckInstancesImpl
{
private:
Expand Down
5 changes: 3 additions & 2 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

#ifdef HAVE_RULES
#include "regex.h"
#include "rule.h"
#endif

#include <algorithm>
Expand Down Expand Up @@ -1415,7 +1416,7 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer, AnalyzerInformation
#ifdef HAVE_RULES
bool CppCheck::hasRule(const std::string &tokenlist) const
{
return std::any_of(mSettings.rules.cbegin(), mSettings.rules.cend(), [&](const Settings::Rule& rule) {
return std::any_of(mSettings.rules.cbegin(), mSettings.rules.cend(), [&](const Rule& rule) {
return rule.tokenlist == tokenlist;
});
}
Expand All @@ -1433,7 +1434,7 @@ void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list)
str += tok->str();
}

for (const Settings::Rule &rule : mSettings.rules) {
for (const Rule &rule : mSettings.rules) {
if (rule.tokenlist != tokenlist)
continue;

Expand Down
2 changes: 2 additions & 0 deletions lib/cppcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
#include <string>
#include <vector>

#ifdef HAVE_RULES
class TokenList;
#endif
struct FileSettings;
class CheckUnusedFunctions;
class Tokenizer;
Expand Down
1 change: 1 addition & 0 deletions lib/cppcheck.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
<ClInclude Include="programmemory.h" />
<ClInclude Include="regex.h" />
<ClInclude Include="reverseanalyzer.h" />
<ClInclude Include="rule.h" />
<ClInclude Include="sarifreport.h" />
<ClInclude Include="settings.h" />
<ClInclude Include="smallvector.h" />
Expand Down
1 change: 1 addition & 0 deletions lib/importproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <iostream>
#include <iterator>
#include <stack>
#include <stdexcept>
#include <unordered_set>
#include <utility>
#include <vector>
Expand Down
4 changes: 4 additions & 0 deletions lib/regex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ namespace {
std::string compile();
std::string match(const std::string& str, const MatchFn& matchFn) const override;

Engine engine() const override {
return Engine::Pcre;
}

private:
std::string mPattern;
pcre* mRe{};
Expand Down
2 changes: 2 additions & 0 deletions lib/regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class CPPCHECKLIB Regex
Pcre = 1
};

virtual Engine engine() const = 0;

static std::shared_ptr<Regex> create(std::string pattern, Engine engine, std::string& err);
};

Expand Down
43 changes: 43 additions & 0 deletions lib/rule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* -*- C++ -*-
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2026 Cppcheck team.
*
* 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/>.
*/

#ifndef ruleH
#define ruleH

#ifdef HAVE_RULES

#include "errortypes.h"

#include <memory>
#include <string>

class Regex;

/** Rule */
struct Rule
{
std::string tokenlist = "normal"; // use normal tokenlist
std::string pattern;
std::string id = "rule"; // default id
std::string summary;
Severity severity = Severity::style; // default severity
std::shared_ptr<Regex> regex;
};
#endif // HAVE_RULES

#endif // ruleH
10 changes: 10 additions & 0 deletions lib/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
*/

#include "config.h"
#include "errortypes.h"
#include "settings.h"
#include "path.h"
#include "summaries.h"
#include "suppressions.h"
#include "vfvalue.h"

#ifdef HAVE_RULES
#include "rule.h"
#endif

#include <cctype>
#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -68,6 +73,11 @@ Settings::Settings()
pid = getPid();
}

Settings::~Settings() = default;

Settings::Settings(const Settings&) = default;
Settings & Settings::operator=(const Settings &) = default;

std::string Settings::loadCppcheckCfg(Settings& settings, Suppressions& suppressions, bool debug)
{
static const std::string cfgFilename = "cppcheck.cfg";
Expand Down
26 changes: 8 additions & 18 deletions lib/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#include "addoninfo.h"
#include "config.h"
#include "errortypes.h"
#include "library.h"
#include "platform.h"
#include "standards.h"
Expand All @@ -40,22 +39,20 @@
#include <unordered_set>
#include <utility>

#include "regex.h"

#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
#include <cstdio>
#endif

#ifdef HAVE_RULES
#include <memory>

class Regex;
struct Rule;
#endif

struct Suppressions;
namespace ValueFlow {
class Value;
}
enum class Severity : std::uint8_t;
enum class Certainty : std::uint8_t;
enum class Checks : std::uint8_t;

/// @addtogroup Core
/// @{
Expand Down Expand Up @@ -111,6 +108,10 @@ class CPPCHECKLIB WARN_UNUSED Settings {

public:
Settings();
~Settings();

Settings(const Settings&);
Settings& operator=(const Settings&);

static std::string loadCppcheckCfg(Settings& settings, Suppressions& suppressions, bool debug = false);

Expand Down Expand Up @@ -368,17 +369,6 @@ class CPPCHECKLIB WARN_UNUSED Settings {
int reportProgress{-1};

#ifdef HAVE_RULES
/** Rule */
struct CPPCHECKLIB Rule {
std::string tokenlist = "normal"; // use normal tokenlist
std::string pattern;
std::string id = "rule"; // default id
std::string summary;
Severity severity = Severity::style; // default severity
Regex::Engine engine = Regex::Engine::Pcre;
std::shared_ptr<Regex> regex;
};

/**
* @brief Extra rules
*/
Expand Down
1 change: 1 addition & 0 deletions lib/suppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <algorithm>
#include <cctype> // std::isdigit, std::isalnum, etc
#include <cstring>
#include <exception>
#include <functional> // std::bind, std::placeholders
#include <sstream>
#include <stdexcept>
Expand Down
1 change: 1 addition & 0 deletions lib/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <algorithm>
#include <array>
#include <cctype>
#include <cstddef>
#include <cstdint>
#include <functional>
Expand Down
1 change: 1 addition & 0 deletions lib/vf_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "vf_common.h"

#include "astutils.h"
#include "library.h"
#include "mathlib.h"
#include "path.h"
#include "platform.h"
Expand Down
Loading