Skip to content

Commit 32efb51

Browse files
committed
added Regex::engine() / removed unnecessary Rule::engine
1 parent b5fbcca commit 32efb51

8 files changed

Lines changed: 19 additions & 10 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ $(libcppdir)/reverseanalyzer.o: lib/reverseanalyzer.cpp lib/addoninfo.h lib/anal
657657
$(libcppdir)/sarifreport.o: lib/sarifreport.cpp externals/picojson/picojson.h lib/addoninfo.h lib/check.h lib/checkers.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/json.h lib/library.h lib/mathlib.h lib/platform.h lib/sarifreport.h lib/settings.h lib/standards.h lib/utils.h
658658
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/sarifreport.cpp
659659

660-
$(libcppdir)/settings.o: lib/settings.cpp externals/picojson/picojson.h lib/addoninfo.h lib/checkers.h lib/config.h lib/errortypes.h lib/json.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/regex.h lib/rule.h lib/settings.h lib/standards.h lib/summaries.h lib/suppressions.h lib/utils.h lib/vfvalue.h
660+
$(libcppdir)/settings.o: lib/settings.cpp externals/picojson/picojson.h lib/addoninfo.h lib/checkers.h lib/config.h lib/errortypes.h lib/json.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/rule.h lib/settings.h lib/standards.h lib/summaries.h lib/suppressions.h lib/utils.h lib/vfvalue.h
661661
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/settings.cpp
662662

663663
$(libcppdir)/standards.o: lib/standards.cpp externals/simplecpp/simplecpp.h lib/config.h lib/standards.h lib/utils.h

cli/cmdlineparser.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
13421342
node = node->FirstChildElement("rule");
13431343
for (; node && strcmp(node->Value(), "rule") == 0; node = node->NextSiblingElement()) {
13441344
Rule rule;
1345+
Regex::Engine regexEngine = Regex::Engine::Unknown;
13451346

13461347
for (const tinyxml2::XMLElement *subnode = node->FirstChildElement(); subnode; subnode = subnode->NextSiblingElement()) {
13471348
const char * const subname = subnode->Name();
@@ -1374,7 +1375,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
13741375
else if (std::strcmp(subname, "engine") == 0) {
13751376
const char * const engine = empty_if_null(subtext);
13761377
if (std::strcmp(engine, "pcre") == 0) {
1377-
rule.engine = Regex::Engine::Pcre;
1378+
regexEngine = Regex::Engine::Pcre;
13781379
}
13791380
else {
13801381
mLogger.printError(std::string("unknown regex engine '") + engine + "'.");
@@ -1408,7 +1409,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
14081409
}
14091410

14101411
std::string regex_err;
1411-
auto regex = Regex::create(rule.pattern, rule.engine, regex_err);
1412+
auto regex = Regex::create(rule.pattern, regexEngine, regex_err);
14121413
if (!regex) {
14131414
mLogger.printError("unable to load rule-file '" + ruleFile + "' - pattern '" + rule.pattern + "' failed to compile (" + regex_err + ").");
14141415
return Result::Fail;

lib/regex.cpp

Lines changed: 4 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{};

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222
#ifdef HAVE_RULES
2323

2424
#include "errortypes.h"
25-
#include "regex.h"
2625

2726
#include <memory>
2827
#include <string>
2928

29+
class Regex;
30+
3031
/** Rule */
3132
struct Rule
3233
{
@@ -35,7 +36,6 @@ struct Rule
3536
std::string id = "rule"; // default id
3637
std::string summary;
3738
Severity severity = Severity::style; // default severity
38-
Regex::Engine engine = Regex::Engine::Pcre;
3939
std::shared_ptr<Regex> regex;
4040
};
4141
#endif // HAVE_RULES

oss-fuzz/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ $(libcppdir)/reverseanalyzer.o: ../lib/reverseanalyzer.cpp ../lib/addoninfo.h ..
327327
$(libcppdir)/sarifreport.o: ../lib/sarifreport.cpp ../externals/picojson/picojson.h ../lib/addoninfo.h ../lib/check.h ../lib/checkers.h ../lib/config.h ../lib/cppcheck.h ../lib/errorlogger.h ../lib/errortypes.h ../lib/json.h ../lib/library.h ../lib/mathlib.h ../lib/platform.h ../lib/sarifreport.h ../lib/settings.h ../lib/standards.h ../lib/utils.h
328328
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/sarifreport.cpp
329329

330-
$(libcppdir)/settings.o: ../lib/settings.cpp ../externals/picojson/picojson.h ../lib/addoninfo.h ../lib/checkers.h ../lib/config.h ../lib/errortypes.h ../lib/json.h ../lib/library.h ../lib/mathlib.h ../lib/path.h ../lib/platform.h ../lib/regex.h ../lib/rule.h ../lib/settings.h ../lib/standards.h ../lib/summaries.h ../lib/suppressions.h ../lib/utils.h ../lib/vfvalue.h
330+
$(libcppdir)/settings.o: ../lib/settings.cpp ../externals/picojson/picojson.h ../lib/addoninfo.h ../lib/checkers.h ../lib/config.h ../lib/errortypes.h ../lib/json.h ../lib/library.h ../lib/mathlib.h ../lib/path.h ../lib/platform.h ../lib/rule.h ../lib/settings.h ../lib/standards.h ../lib/summaries.h ../lib/suppressions.h ../lib/utils.h ../lib/vfvalue.h
331331
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/settings.cpp
332332

333333
$(libcppdir)/standards.o: ../lib/standards.cpp ../externals/simplecpp/simplecpp.h ../lib/config.h ../lib/standards.h ../lib/utils.h

test/testcmdlineparser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,14 +2733,14 @@ class TestCmdlineParser : public TestFixture {
27332733
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
27342734
ASSERT_EQUALS(2, settings->rules.size());
27352735
auto it = settings->rules.cbegin();
2736-
ASSERT_EQUALS_ENUM(Regex::Engine::Pcre, it->engine);
2736+
ASSERT_EQUALS_ENUM(Regex::Engine::Pcre, it->regex->engine());
27372737
ASSERT_EQUALS("raw", it->tokenlist);
27382738
ASSERT_EQUALS(".+", it->pattern);
27392739
ASSERT_EQUALS_ENUM(Severity::error, it->severity);
27402740
ASSERT_EQUALS("ruleId1", it->id);
27412741
ASSERT_EQUALS("ruleSummary1", it->summary);
27422742
++it;
2743-
ASSERT_EQUALS_ENUM(Regex::Engine::Pcre, it->engine);
2743+
ASSERT_EQUALS_ENUM(Regex::Engine::Pcre, it->regex->engine());
27442744
ASSERT_EQUALS("define", it->tokenlist);
27452745
ASSERT_EQUALS(".*", it->pattern);
27462746
ASSERT_EQUALS_ENUM(Severity::warning, it->severity);
@@ -2764,7 +2764,7 @@ class TestCmdlineParser : public TestFixture {
27642764
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
27652765
ASSERT_EQUALS(1, settings->rules.size());
27662766
auto it = settings->rules.cbegin();
2767-
ASSERT_EQUALS_ENUM(Regex::Engine::Pcre, it->engine);
2767+
ASSERT_EQUALS_ENUM(Regex::Engine::Pcre, it->regex->engine());
27682768
ASSERT_EQUALS("define", it->tokenlist);
27692769
ASSERT_EQUALS(".+", it->pattern);
27702770
ASSERT_EQUALS_ENUM(Severity::error, it->severity);

test/testregex.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ class TestRegExBase : public TestFixture {
4747
std::shared_ptr<Regex> assertRegex_(const char* file, int line, std::string pattern, const std::string& exp_err = "") const {
4848
std::string regex_err;
4949
auto r = Regex::create(std::move(pattern), mEngine, regex_err);
50-
if (exp_err.empty())
50+
if (exp_err.empty()) {
5151
ASSERT_LOC(!!r.get(), file, line);
52+
ASSERT_EQUALS_ENUM_LOC(mEngine, r->engine(), file, line);
53+
}
5254
else
5355
ASSERT_LOC(!r.get(), file, line); // only not set if we encountered an error
5456
ASSERT_EQUALS_LOC(exp_err, regex_err, file, line);

0 commit comments

Comments
 (0)