Skip to content

Commit 33dee83

Browse files
authored
moved suppression-specific code out of ErrorLogger (#5329)
1 parent 7f22ef4 commit 33dee83

10 files changed

Lines changed: 133 additions & 130 deletions

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ validateRules:
455455

456456
###### Build
457457

458-
$(libcppdir)/analyzerinfo.o: lib/analyzerinfo.cpp externals/tinyxml2/tinyxml2.h lib/analyzerinfo.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/path.h lib/platform.h lib/suppressions.h lib/utils.h
458+
$(libcppdir)/analyzerinfo.o: lib/analyzerinfo.cpp externals/tinyxml2/tinyxml2.h lib/analyzerinfo.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/path.h lib/platform.h lib/utils.h
459459
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/analyzerinfo.cpp
460460

461461
$(libcppdir)/astutils.o: lib/astutils.cpp lib/astutils.h lib/check.h lib/checkclass.h lib/config.h lib/errortypes.h lib/importproject.h lib/infer.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vfvalue.h
@@ -641,10 +641,10 @@ cli/cmdlineparser.o: cli/cmdlineparser.cpp cli/cmdlineparser.h cli/cppcheckexecu
641641
cli/cppcheckexecutor.o: cli/cppcheckexecutor.cpp cli/cmdlineparser.h cli/cppcheckexecutor.h cli/cppcheckexecutorseh.h cli/cppcheckexecutorsig.h cli/executor.h cli/filelister.h cli/processexecutor.h cli/singleexecutor.h cli/threadexecutor.h lib/analyzerinfo.h lib/check.h lib/checkunusedfunctions.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/pathmatch.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h
642642
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/cppcheckexecutor.cpp
643643

644-
cli/cppcheckexecutorseh.o: cli/cppcheckexecutorseh.cpp cli/cppcheckexecutor.h cli/cppcheckexecutorseh.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h lib/utils.h
644+
cli/cppcheckexecutorseh.o: cli/cppcheckexecutorseh.cpp cli/cppcheckexecutor.h cli/cppcheckexecutorseh.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/utils.h
645645
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/cppcheckexecutorseh.cpp
646646

647-
cli/cppcheckexecutorsig.o: cli/cppcheckexecutorsig.cpp cli/cppcheckexecutor.h cli/cppcheckexecutorsig.h cli/stacktrace.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h
647+
cli/cppcheckexecutorsig.o: cli/cppcheckexecutorsig.cpp cli/cppcheckexecutor.h cli/cppcheckexecutorsig.h cli/stacktrace.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h
648648
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/cppcheckexecutorsig.cpp
649649

650650
cli/executor.o: cli/executor.cpp cli/executor.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h
@@ -653,7 +653,7 @@ cli/executor.o: cli/executor.cpp cli/executor.h lib/color.h lib/config.h lib/err
653653
cli/filelister.o: cli/filelister.cpp cli/filelister.h lib/config.h lib/path.h lib/pathmatch.h lib/utils.h
654654
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/filelister.cpp
655655

656-
cli/main.o: cli/main.cpp cli/cppcheckexecutor.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h
656+
cli/main.o: cli/main.cpp cli/cppcheckexecutor.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h
657657
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/main.cpp
658658

659659
cli/processexecutor.o: cli/processexecutor.cpp cli/cppcheckexecutor.h cli/executor.h cli/processexecutor.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h

cli/cppcheckexecutor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,11 @@ bool CppCheckExecutor::reportSuppressions(const Settings &settings, bool unusedF
261261
bool err = false;
262262
if (settings.useSingleJob()) {
263263
for (std::map<std::string, std::size_t>::const_iterator i = files.cbegin(); i != files.cend(); ++i) {
264-
err |= errorLogger.reportUnmatchedSuppressions(
265-
settings.nomsg.getUnmatchedLocalSuppressions(i->first, unusedFunctionCheckEnabled));
264+
err |= Suppressions::reportUnmatchedSuppressions(
265+
settings.nomsg.getUnmatchedLocalSuppressions(i->first, unusedFunctionCheckEnabled), errorLogger);
266266
}
267267
}
268-
err |= errorLogger.reportUnmatchedSuppressions(settings.nomsg.getUnmatchedGlobalSuppressions(unusedFunctionCheckEnabled));
268+
err |= Suppressions::reportUnmatchedSuppressions(settings.nomsg.getUnmatchedGlobalSuppressions(unusedFunctionCheckEnabled), errorLogger);
269269
return err;
270270
}
271271

cli/cppcheckexecutor.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class Settings;
4343
*/
4444
class CppCheckExecutor : public ErrorLogger {
4545
public:
46+
friend class TestSuppressions;
47+
4648
/**
4749
* Constructor
4850
*/
@@ -101,8 +103,6 @@ class CppCheckExecutor : public ErrorLogger {
101103
*/
102104
static bool executeCommand(std::string exe, std::vector<std::string> args, std::string redirect, std::string &output_);
103105

104-
static bool reportSuppressions(const Settings &settings, bool unusedFunctionCheckEnabled, const std::map<std::string, std::size_t> &files, ErrorLogger& errorLogger);
105-
106106
protected:
107107

108108
/**
@@ -124,6 +124,8 @@ class CppCheckExecutor : public ErrorLogger {
124124

125125
private:
126126

127+
static bool reportSuppressions(const Settings &settings, bool unusedFunctionCheckEnabled, const std::map<std::string, std::size_t> &files, ErrorLogger& errorLogger);
128+
127129
/**
128130
* Wrapper around check_internal
129131
* - installs optional platform dependent signal handling

lib/cppcheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
10211021
// In jointSuppressionReport mode, unmatched suppressions are
10221022
// collected after all files are processed
10231023
if (!mSettings.useSingleJob() && (mSettings.severity.isEnabled(Severity::information) || mSettings.checkConfiguration)) {
1024-
reportUnmatchedSuppressions(mSettings.nomsg.getUnmatchedLocalSuppressions(filename, isUnusedFunctionCheckEnabled()));
1024+
Suppressions::reportUnmatchedSuppressions(mSettings.nomsg.getUnmatchedLocalSuppressions(filename, isUnusedFunctionCheckEnabled()), *this);
10251025
}
10261026

10271027
mErrorList.clear();
@@ -1597,7 +1597,7 @@ void CppCheck::reportErr(const ErrorMessage &msg)
15971597
mAnalyzerInformation.reportErr(msg);
15981598

15991599
// TODO: only convert if necessary
1600-
const Suppressions::ErrorMessage errorMessage = msg.toSuppressionsErrorMessage();
1600+
const auto errorMessage = Suppressions::ErrorMessage::fromErrorMessage(msg);
16011601

16021602
if (mSettings.nomsg.isSuppressed(errorMessage, mUseGlobalSuppressions)) {
16031603
return;

lib/errorlogger.cpp

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -211,22 +211,6 @@ void ErrorMessage::setmsg(const std::string &msg)
211211
}
212212
}
213213

214-
Suppressions::ErrorMessage ErrorMessage::toSuppressionsErrorMessage() const
215-
{
216-
Suppressions::ErrorMessage ret;
217-
ret.hash = hash;
218-
ret.errorId = id;
219-
if (!callStack.empty()) {
220-
ret.setFileName(callStack.back().getfile(false));
221-
ret.lineNumber = callStack.back().line;
222-
} else {
223-
ret.lineNumber = Suppressions::Suppression::NO_LINE;
224-
}
225-
ret.certainty = certainty;
226-
ret.symbolNames = mSymbolNames;
227-
return ret;
228-
}
229-
230214
static void serializeString(std::string &oss, const std::string & str)
231215
{
232216
oss += std::to_string(str.length());
@@ -681,39 +665,6 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm
681665
return result;
682666
}
683667

684-
bool ErrorLogger::reportUnmatchedSuppressions(const std::list<Suppressions::Suppression> &unmatched)
685-
{
686-
bool err = false;
687-
// Report unmatched suppressions
688-
for (const Suppressions::Suppression &s : unmatched) {
689-
// don't report "unmatchedSuppression" as unmatched
690-
if (s.errorId == "unmatchedSuppression")
691-
continue;
692-
693-
// check if this unmatched suppression is suppressed
694-
bool suppressed = false;
695-
for (const Suppressions::Suppression &s2 : unmatched) {
696-
if (s2.errorId == "unmatchedSuppression") {
697-
if ((s2.fileName.empty() || s2.fileName == "*" || s2.fileName == s.fileName) &&
698-
(s2.lineNumber == Suppressions::Suppression::NO_LINE || s2.lineNumber == s.lineNumber)) {
699-
suppressed = true;
700-
break;
701-
}
702-
}
703-
}
704-
705-
if (suppressed)
706-
continue;
707-
708-
std::list<ErrorMessage::FileLocation> callStack;
709-
if (!s.fileName.empty())
710-
callStack.emplace_back(s.fileName, s.lineNumber, 0);
711-
reportErr(ErrorMessage(callStack, emptyString, Severity::information, "Unmatched suppression: " + s.errorId, "unmatchedSuppression", Certainty::normal));
712-
err = true;
713-
}
714-
return err;
715-
}
716-
717668
std::string ErrorLogger::callStackToString(const std::list<ErrorMessage::FileLocation> &callStack)
718669
{
719670
std::string str;

lib/errorlogger.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
#include "config.h"
2525
#include "errortypes.h"
26-
#include "suppressions.h"
2726
#include "color.h"
2827

2928
#include <cstddef>
@@ -198,8 +197,6 @@ class CPPCHECKLIB ErrorMessage {
198197
return mSymbolNames;
199198
}
200199

201-
Suppressions::ErrorMessage toSuppressionsErrorMessage() const;
202-
203200
private:
204201
static std::string fixInvalidChars(const std::string& raw);
205202

@@ -250,13 +247,6 @@ class CPPCHECKLIB ErrorLogger {
250247
(void)value;
251248
}
252249

253-
/**
254-
* Report unmatched suppressions
255-
* @param unmatched list of unmatched suppressions (from Settings::Suppressions::getUnmatched(Local|Global)Suppressions)
256-
* @return true is returned if errors are reported
257-
*/
258-
bool reportUnmatchedSuppressions(const std::list<Suppressions::Suppression> &unmatched);
259-
260250
static std::string callStackToString(const std::list<ErrorMessage::FileLocation> &callStack);
261251

262252
/**

lib/suppressions.cpp

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@
3535

3636
#include <tinyxml2.h>
3737

38+
Suppressions::ErrorMessage Suppressions::ErrorMessage::fromErrorMessage(const ::ErrorMessage &msg)
39+
{
40+
Suppressions::ErrorMessage ret;
41+
ret.hash = msg.hash;
42+
ret.errorId = msg.id;
43+
if (!msg.callStack.empty()) {
44+
ret.setFileName(msg.callStack.back().getfile(false));
45+
ret.lineNumber = msg.callStack.back().line;
46+
} else {
47+
ret.lineNumber = Suppressions::Suppression::NO_LINE;
48+
}
49+
ret.certainty = msg.certainty;
50+
ret.symbolNames = msg.symbolNames();
51+
return ret;
52+
}
53+
3854
static bool isAcceptedErrorIdChar(char c)
3955
{
4056
switch (c) {
@@ -384,7 +400,7 @@ bool Suppressions::isSuppressed(const ::ErrorMessage &errmsg)
384400
{
385401
if (mSuppressions.empty())
386402
return false;
387-
return isSuppressed(errmsg.toSuppressionsErrorMessage());
403+
return isSuppressed(Suppressions::ErrorMessage::fromErrorMessage(errmsg));
388404
}
389405

390406
void Suppressions::dump(std::ostream & out) const
@@ -461,3 +477,36 @@ void Suppressions::markUnmatchedInlineSuppressionsAsChecked(const Tokenizer &tok
461477
}
462478
}
463479
}
480+
481+
bool Suppressions::reportUnmatchedSuppressions(const std::list<Suppressions::Suppression> &unmatched, ErrorLogger &errorLogger)
482+
{
483+
bool err = false;
484+
// Report unmatched suppressions
485+
for (const Suppressions::Suppression &s : unmatched) {
486+
// don't report "unmatchedSuppression" as unmatched
487+
if (s.errorId == "unmatchedSuppression")
488+
continue;
489+
490+
// check if this unmatched suppression is suppressed
491+
bool suppressed = false;
492+
for (const Suppressions::Suppression &s2 : unmatched) {
493+
if (s2.errorId == "unmatchedSuppression") {
494+
if ((s2.fileName.empty() || s2.fileName == "*" || s2.fileName == s.fileName) &&
495+
(s2.lineNumber == Suppressions::Suppression::NO_LINE || s2.lineNumber == s.lineNumber)) {
496+
suppressed = true;
497+
break;
498+
}
499+
}
500+
}
501+
502+
if (suppressed)
503+
continue;
504+
505+
std::list<::ErrorMessage::FileLocation> callStack;
506+
if (!s.fileName.empty())
507+
callStack.emplace_back(s.fileName, s.lineNumber, 0);
508+
errorLogger.reportErr(::ErrorMessage(callStack, emptyString, Severity::information, "Unmatched suppression: " + s.errorId, "unmatchedSuppression", Certainty::normal));
509+
err = true;
510+
}
511+
return err;
512+
}

lib/suppressions.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
class Tokenizer;
3636
class ErrorMessage;
37+
class ErrorLogger;
3738
enum class Certainty;
3839

3940
/** @brief class for handling suppressions */
@@ -50,6 +51,8 @@ class CPPCHECKLIB Suppressions {
5051
int lineNumber;
5152
Certainty certainty;
5253
std::string symbolNames;
54+
55+
static Suppressions::ErrorMessage fromErrorMessage(const ::ErrorMessage &msg);
5356
private:
5457
std::string mFileName;
5558
};
@@ -201,6 +204,13 @@ class CPPCHECKLIB Suppressions {
201204
*/
202205
void markUnmatchedInlineSuppressionsAsChecked(const Tokenizer &tokenizer);
203206

207+
/**
208+
* Report unmatched suppressions
209+
* @param unmatched list of unmatched suppressions (from Settings::Suppressions::getUnmatched(Local|Global)Suppressions)
210+
* @return true is returned if errors are reported
211+
*/
212+
static bool reportUnmatchedSuppressions(const std::list<Suppressions::Suppression> &unmatched, ErrorLogger &errorLogger);
213+
204214
private:
205215
/** @brief List of error which the user doesn't want to see. */
206216
std::list<Suppression> mSuppressions;

test/testerrorlogger.cpp

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class TestErrorLogger : public TestFixture {
6161
TEST_CASE(SerializeSanitize);
6262
TEST_CASE(SerializeFileLocation);
6363

64-
TEST_CASE(suppressUnmatchedSuppressions);
6564
TEST_CASE(substituteTemplateFormatStatic);
6665
TEST_CASE(substituteTemplateLocationStatic);
6766
}
@@ -425,64 +424,6 @@ class TestErrorLogger : public TestFixture {
425424
ASSERT_EQUALS("abcd:/,", msg2.callStack.front().getinfo());
426425
}
427426

428-
void suppressUnmatchedSuppressions() {
429-
std::list<Suppressions::Suppression> suppressions;
430-
431-
// No unmatched suppression
432-
errout.str("");
433-
suppressions.clear();
434-
reportUnmatchedSuppressions(suppressions);
435-
ASSERT_EQUALS("", errout.str());
436-
437-
// suppress all unmatchedSuppression
438-
errout.str("");
439-
suppressions.clear();
440-
suppressions.emplace_back("abc", "a.c", 10U);
441-
suppressions.emplace_back("unmatchedSuppression", "*", Suppressions::Suppression::NO_LINE);
442-
reportUnmatchedSuppressions(suppressions);
443-
ASSERT_EQUALS("", errout.str());
444-
445-
// suppress all unmatchedSuppression (corresponds to "--suppress=unmatchedSuppression")
446-
errout.str("");
447-
suppressions.clear();
448-
suppressions.emplace_back("abc", "a.c", 10U);
449-
suppressions.emplace_back("unmatchedSuppression", "", Suppressions::Suppression::NO_LINE);
450-
reportUnmatchedSuppressions(suppressions);
451-
ASSERT_EQUALS("", errout.str());
452-
453-
// suppress all unmatchedSuppression in a.c
454-
errout.str("");
455-
suppressions.clear();
456-
suppressions.emplace_back("abc", "a.c", 10U);
457-
suppressions.emplace_back("unmatchedSuppression", "a.c", Suppressions::Suppression::NO_LINE);
458-
reportUnmatchedSuppressions(suppressions);
459-
ASSERT_EQUALS("", errout.str());
460-
461-
// suppress unmatchedSuppression in a.c at line 10
462-
errout.str("");
463-
suppressions.clear();
464-
suppressions.emplace_back("abc", "a.c", 10U);
465-
suppressions.emplace_back("unmatchedSuppression", "a.c", 10U);
466-
reportUnmatchedSuppressions(suppressions);
467-
ASSERT_EQUALS("", errout.str());
468-
469-
// don't suppress unmatchedSuppression when file is mismatching
470-
errout.str("");
471-
suppressions.clear();
472-
suppressions.emplace_back("abc", "a.c", 10U);
473-
suppressions.emplace_back("unmatchedSuppression", "b.c", Suppressions::Suppression::NO_LINE);
474-
reportUnmatchedSuppressions(suppressions);
475-
ASSERT_EQUALS("[a.c:10]: (information) Unmatched suppression: abc\n", errout.str());
476-
477-
// don't suppress unmatchedSuppression when line is mismatching
478-
errout.str("");
479-
suppressions.clear();
480-
suppressions.emplace_back("abc", "a.c", 10U);
481-
suppressions.emplace_back("unmatchedSuppression", "a.c", 1U);
482-
reportUnmatchedSuppressions(suppressions);
483-
ASSERT_EQUALS("[a.c:10]: (information) Unmatched suppression: abc\n", errout.str());
484-
}
485-
486427
void substituteTemplateFormatStatic() const
487428
{
488429
{

0 commit comments

Comments
 (0)