Skip to content

Commit 01e2254

Browse files
authored
extracted Rule from settings.h to rule.h / added Regex::engine() / removed unnecessary Rule::engine (#8580)
1 parent 8c2ed4e commit 01e2254

14 files changed

Lines changed: 277 additions & 209 deletions

File tree

Makefile

Lines changed: 131 additions & 131 deletions
Large diffs are not rendered by default.

cli/cmdlineparser.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
#ifdef HAVE_RULES
5959
#include "regex.h"
60+
#include "rule.h"
6061

6162
// xml is used for rules
6263
#include "xml.h"
@@ -1304,7 +1305,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
13041305
// Rule given at command line
13051306
else if (std::strncmp(argv[i], "--rule=", 7) == 0) {
13061307
#ifdef HAVE_RULES
1307-
Settings::Rule rule;
1308+
Rule rule;
13081309
rule.pattern = 7 + argv[i];
13091310

13101311
if (rule.pattern.empty()) {
@@ -1340,7 +1341,8 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
13401341
if (node && strcmp(node->Value(), "rules") == 0)
13411342
node = node->FirstChildElement("rule");
13421343
for (; node && strcmp(node->Value(), "rule") == 0; node = node->NextSiblingElement()) {
1343-
Settings::Rule rule;
1344+
Rule rule;
1345+
Regex::Engine regexEngine = Regex::Engine::Pcre;
13441346

13451347
for (const tinyxml2::XMLElement *subnode = node->FirstChildElement(); subnode; subnode = subnode->NextSiblingElement()) {
13461348
const char * const subname = subnode->Name();
@@ -1373,7 +1375,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
13731375
else if (std::strcmp(subname, "engine") == 0) {
13741376
const char * const engine = empty_if_null(subtext);
13751377
if (std::strcmp(engine, "pcre") == 0) {
1376-
rule.engine = Regex::Engine::Pcre;
1378+
regexEngine = Regex::Engine::Pcre;
13771379
}
13781380
else {
13791381
mLogger.printError(std::string("unknown regex engine '") + engine + "'.");
@@ -1407,7 +1409,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
14071409
}
14081410

14091411
std::string regex_err;
1410-
auto regex = Regex::create(rule.pattern, rule.engine, regex_err);
1412+
auto regex = Regex::create(rule.pattern, regexEngine, regex_err);
14111413
if (!regex) {
14121414
mLogger.printError("unable to load rule-file '" + ruleFile + "' - pattern '" + rule.pattern + "' failed to compile (" + regex_err + ").");
14131415
return Result::Fail;

gui/test/projectfile/testprojectfile.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#include "settings.h"
2525
#include "suppressions.h"
2626

27+
#ifdef HAVE_RULES
28+
#include "rule.h"
29+
#endif
30+
2731
#include <QFile>
2832
#include <QIODevice>
2933
#include <QList>
@@ -38,6 +42,7 @@ const char Settings::SafeChecks::XmlExternalFunctions[] = "external-functions";
3842
const char Settings::SafeChecks::XmlInternalFunctions[] = "internal-functions";
3943
const char Settings::SafeChecks::XmlExternalVariables[] = "external-variables";
4044
Settings::Settings() : maxCtuDepth(10) {}
45+
Settings::~Settings() = default;
4146
Platform::Platform() = default;
4247
Library::Library() = default;
4348
Library::~Library() = default;

lib/cppcheck.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
#ifdef HAVE_RULES
4949
#include "regex.h"
50+
#include "rule.h"
5051
#endif
5152

5253
#include <algorithm>
@@ -1415,7 +1416,7 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer, AnalyzerInformation
14151416
#ifdef HAVE_RULES
14161417
bool CppCheck::hasRule(const std::string &tokenlist) const
14171418
{
1418-
return std::any_of(mSettings.rules.cbegin(), mSettings.rules.cend(), [&](const Settings::Rule& rule) {
1419+
return std::any_of(mSettings.rules.cbegin(), mSettings.rules.cend(), [&](const Rule& rule) {
14191420
return rule.tokenlist == tokenlist;
14201421
});
14211422
}
@@ -1433,7 +1434,7 @@ void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list)
14331434
str += tok->str();
14341435
}
14351436

1436-
for (const Settings::Rule &rule : mSettings.rules) {
1437+
for (const Rule &rule : mSettings.rules) {
14371438
if (rule.tokenlist != tokenlist)
14381439
continue;
14391440

lib/cppcheck.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
<ClInclude Include="programmemory.h" />
169169
<ClInclude Include="regex.h" />
170170
<ClInclude Include="reverseanalyzer.h" />
171+
<ClInclude Include="rule.h" />
171172
<ClInclude Include="sarifreport.h" />
172173
<ClInclude Include="settings.h" />
173174
<ClInclude Include="smallvector.h" />

lib/regex.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ namespace {
179179
std::string compile();
180180
std::string match(const std::string& str, const MatchFn& matchFn) const override;
181181

182+
Engine engine() const override {
183+
return Engine::Pcre;
184+
}
185+
182186
private:
183187
std::string mPattern;
184188
pcre* mRe{};
@@ -254,6 +258,7 @@ static T* createAndCompileRegex(std::string pattern, std::string& err)
254258
return regex;
255259
}
256260

261+
// cppcheck-suppress shadowFunction - FP see #14802
257262
std::shared_ptr<Regex> Regex::create(std::string pattern, Engine engine, std::string& err)
258263
{
259264
Regex* regex = nullptr;

lib/regex.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class CPPCHECKLIB Regex
4444
Pcre = 1
4545
};
4646

47+
virtual Engine engine() const = 0;
48+
4749
static std::shared_ptr<Regex> create(std::string pattern, Engine engine, std::string& err);
4850
};
4951

lib/rule.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* -*- C++ -*-
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2026 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef ruleH
20+
#define ruleH
21+
22+
#ifdef HAVE_RULES
23+
24+
#include "errortypes.h"
25+
26+
#include <memory>
27+
#include <string>
28+
29+
class Regex;
30+
31+
/** Rule */
32+
struct Rule
33+
{
34+
std::string tokenlist = "normal"; // use normal tokenlist
35+
std::string pattern;
36+
std::string id = "rule"; // default id
37+
std::string summary;
38+
Severity severity = Severity::style; // default severity
39+
std::shared_ptr<Regex> regex;
40+
};
41+
#endif // HAVE_RULES
42+
43+
#endif // ruleH

lib/settings.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#include "suppressions.h"
2525
#include "vfvalue.h"
2626

27+
#ifdef HAVE_RULES
28+
#include "rule.h"
29+
#endif
30+
2731
#include <cctype>
2832
#include <cstdlib>
2933
#include <cstring>
@@ -69,6 +73,14 @@ Settings::Settings()
6973
pid = getPid();
7074
}
7175

76+
Settings::~Settings() = default;
77+
78+
Settings::Settings(const Settings&) = default;
79+
Settings & Settings::operator=(const Settings &) = default;
80+
81+
Settings::Settings(Settings&&) noexcept = default;
82+
Settings & Settings::operator=(Settings &&) noexcept = default;
83+
7284
std::string Settings::loadCppcheckCfg(Settings& settings, Suppressions& suppressions, bool debug)
7385
{
7486
static const std::string cfgFilename = "cppcheck.cfg";

lib/settings.h

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,13 @@
4444
#endif
4545

4646
#ifdef HAVE_RULES
47-
#include "errortypes.h"
48-
#include "regex.h"
49-
50-
#include <memory>
51-
52-
class Regex;
53-
#else
54-
enum class Severity : std::uint8_t;
47+
struct Rule;
5548
#endif
56-
5749
struct Suppressions;
5850
namespace ValueFlow {
5951
class Value;
6052
}
53+
enum class Severity : std::uint8_t;
6154
enum class Certainty : std::uint8_t;
6255
enum class Checks : std::uint8_t;
6356

@@ -115,6 +108,13 @@ class CPPCHECKLIB WARN_UNUSED Settings {
115108

116109
public:
117110
Settings();
111+
~Settings();
112+
113+
Settings(const Settings&);
114+
Settings& operator=(const Settings&);
115+
116+
Settings(Settings&&) noexcept;
117+
Settings& operator=(Settings&&) noexcept;
118118

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

@@ -372,17 +372,6 @@ class CPPCHECKLIB WARN_UNUSED Settings {
372372
int reportProgress{-1};
373373

374374
#ifdef HAVE_RULES
375-
/** Rule */
376-
struct CPPCHECKLIB Rule {
377-
std::string tokenlist = "normal"; // use normal tokenlist
378-
std::string pattern;
379-
std::string id = "rule"; // default id
380-
std::string summary;
381-
Severity severity = Severity::style; // default severity
382-
Regex::Engine engine = Regex::Engine::Pcre;
383-
std::shared_ptr<Regex> regex;
384-
};
385-
386375
/**
387376
* @brief Extra rules
388377
*/

0 commit comments

Comments
 (0)