Skip to content

Commit 5dd90a2

Browse files
committed
removed SuppressionList::Suppression::NO_LINE
The suppressions were using `-1` as indication that no line is used but in simplecpp `0` was used for that.
1 parent 2285c1d commit 5dd90a2

12 files changed

Lines changed: 50 additions & 53 deletions

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ $(libcppdir)/cppcheck.o: lib/cppcheck.cpp externals/picojson/picojson.h external
600600
$(libcppdir)/ctu.o: lib/ctu.cpp externals/tinyxml2/tinyxml2.h lib/astutils.h lib/check.h lib/config.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h
601601
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/ctu.cpp
602602

603-
$(libcppdir)/errorlogger.o: lib/errorlogger.cpp externals/tinyxml2/tinyxml2.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/smallvector.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h
603+
$(libcppdir)/errorlogger.o: lib/errorlogger.cpp externals/tinyxml2/tinyxml2.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/smallvector.h lib/standards.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h
604604
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/errorlogger.cpp
605605

606606
$(libcppdir)/errortypes.o: lib/errortypes.cpp lib/config.h lib/errortypes.h lib/utils.h
@@ -795,7 +795,7 @@ test/testconstructors.o: test/testconstructors.cpp lib/check.h lib/checkclass.h
795795
test/testcppcheck.o: test/testcppcheck.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h test/redirect.h
796796
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testcppcheck.cpp
797797

798-
test/testerrorlogger.o: test/testerrorlogger.cpp externals/tinyxml2/tinyxml2.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/xml.h test/fixture.h test/helpers.h
798+
test/testerrorlogger.o: test/testerrorlogger.cpp externals/tinyxml2/tinyxml2.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/xml.h test/fixture.h test/helpers.h
799799
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testerrorlogger.cpp
800800

801801
test/testexceptionsafety.o: test/testexceptionsafety.cpp lib/check.h lib/checkers.h lib/checkexceptionsafety.h lib/checkimpl.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h

cli/cppcheckexecutor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ static std::vector<ErrorMessage> getUnmatchedSuppressions(const std::list<Suppre
312312
for (const SuppressionList::Suppression &s2 : unmatched) {
313313
if (s2.errorId == "unmatchedSuppression") { // TODO: handle unmatchedPolyspaceSuppression?
314314
if ((s2.fileName.empty() || s2.fileName == "*" || s2.fileName == s.fileName) &&
315-
(s2.lineNumber == SuppressionList::Suppression::NO_LINE || s2.lineNumber == s.lineNumber)) {
315+
(s2.lineNumber == 0 || s2.lineNumber == s.lineNumber)) {
316316
suppressed = true;
317317
break;
318318
}
@@ -330,7 +330,7 @@ static std::vector<ErrorMessage> getUnmatchedSuppressions(const std::list<Suppre
330330

331331
std::list<ErrorMessage::FileLocation> callStack;
332332
if (!s.fileName.empty()) {
333-
callStack.emplace_back(s.fileName, s.lineNumber == -1 ? 0 : s.lineNumber, s.column); // TODO: get rid of s.lineNumber == -1 hack
333+
callStack.emplace_back(s.fileName, s.lineNumber, s.column);
334334
}
335335
const std::string unmatchedSuppressionId = s.isPolyspace ? "unmatchedPolyspaceSuppression" : "unmatchedSuppression";
336336
errors.emplace_back(std::move(callStack), "", Severity::information, "Unmatched suppression: " + s.errorId, unmatchedSuppressionId, Certainty::normal);
@@ -347,7 +347,7 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con
347347
auto suppr = suppressions.getSuppressions();
348348
if (std::any_of(suppr.cbegin(), suppr.cend(), [](const SuppressionList::Suppression& s) {
349349
// TODO: handle unmatchedPolyspaceSuppression?
350-
return s.errorId == "unmatchedSuppression" && (s.fileName.empty() || s.fileName == "*") && s.lineNumber == SuppressionList::Suppression::NO_LINE;
350+
return s.errorId == "unmatchedSuppression" && (s.fileName.empty() || s.fileName == "*") && s.lineNumber == 0;
351351
}))
352352
return false;
353353

gui/projectfile.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ void ProjectFile::readSuppressions(QXmlStreamReader &reader)
624624
if (reader.attributes().hasAttribute(QString(),"fileName"))
625625
suppression.fileName = reader.attributes().value(QString(),"fileName").toString().toStdString();
626626
if (reader.attributes().hasAttribute(QString(),"lineNumber"))
627-
suppression.lineNumber = reader.attributes().value(QString(),"lineNumber").toInt();
627+
suppression.lineNumber = reader.attributes().value(QString(),"lineNumber").toUInt();
628628
if (reader.attributes().hasAttribute(QString(),"symbolName"))
629629
suppression.symbolName = reader.attributes().value(QString(),"symbolName").toString().toStdString();
630630
if (reader.attributes().hasAttribute(QString(),"hash"))
@@ -951,6 +951,7 @@ bool ProjectFile::write(const QString &filename)
951951
CppcheckXml::LibraryElementName);
952952

953953
if (!mSuppressions.isEmpty()) {
954+
// TODO: merge/align with SuppressionList::dump()?
954955
xmlWriter.writeStartElement(CppcheckXml::SuppressionsElementName);
955956
for (const SuppressionList::Suppression &suppression : utils::as_const(mSuppressions)) {
956957
xmlWriter.writeStartElement(CppcheckXml::SuppressionElementName);

gui/projectfiledialog.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,17 @@ static const std::array<Platform::Type, 6> builtinPlatforms = {
9090
Platform::Type::Unix64
9191
};
9292

93+
// TODO: use SuppressionList::Suppression::toString() instead?
9394
static std::string suppressionAsText(const SuppressionList::Suppression& s)
9495
{
9596
std::string ret;
9697
if (!s.errorId.empty())
9798
ret = s.errorId;
9899
if (!s.fileName.empty())
99100
ret += " fileName=" + s.fileName;
100-
if (s.lineNumber != SuppressionList::Suppression::NO_LINE)
101+
if (s.lineNumber != 0)
101102
ret += " lineNumber=" + std::to_string(s.lineNumber);
103+
// do not include column as suppressions are only line-based
102104
if (!s.symbolName.empty())
103105
ret += " symbolName=" + s.symbolName;
104106
if (s.hash > 0)

lib/errorlogger.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "cppcheck.h"
2323
#include "path.h"
2424
#include "settings.h"
25-
#include "suppressions.h"
2625
#include "token.h"
2726
#include "tokenlist.h"
2827
#include "utils.h"
@@ -766,13 +765,11 @@ std::string ErrorMessage::FileLocation::stringify(bool addcolumn) const
766765
std::string str;
767766
str += '[';
768767
str += Path::toNativeSeparators(mFileName);
769-
if (line != SuppressionList::Suppression::NO_LINE) { // TODO: should not depend on Suppression
768+
str += ':';
769+
str += std::to_string(line);
770+
if (addcolumn) {
770771
str += ':';
771-
str += std::to_string(line);
772-
if (addcolumn) {
773-
str += ':';
774-
str += std::to_string(column);
775-
}
772+
str += std::to_string(column);
776773
}
777774
str += ']';
778775
return str;

lib/errorlogger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class CPPCHECKLIB ErrorMessage {
8888
std::string stringify(bool addcolumn = false) const;
8989

9090
unsigned int fileIndex;
91-
int line; // negative value means "no line"
91+
int line; // TODO: should be unsigned
9292
unsigned int column;
9393

9494
const std::string& getinfo() const {

lib/importproject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1515,7 +1515,7 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings &setti
15151515
s.fileName = empty_if_null(child->Attribute("fileName"));
15161516
if (!s.fileName.empty())
15171517
s.fileName = joinRelativePath(path, s.fileName);
1518-
s.lineNumber = child->IntAttribute("lineNumber", SuppressionList::Suppression::NO_LINE); // TODO: should not depend on Suppression
1518+
s.lineNumber = child->UnsignedAttribute("lineNumber", 0);
15191519
s.symbolName = empty_if_null(child->Attribute("symbolName"));
15201520
s.hash = strToInt<std::size_t>(default_if_null(child->Attribute("hash"), "0"));
15211521
suppressions.push_back(std::move(s));

lib/suppressions.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ SuppressionList::ErrorMessage SuppressionList::ErrorMessage::fromErrorMessage(co
5151
ret.lineNumber = msg.callStack.back().line;
5252
} else {
5353
ret.setFileName(msg.file0);
54-
ret.lineNumber = SuppressionList::Suppression::NO_LINE;
54+
ret.lineNumber = 0;
5555
}
5656
ret.certainty = msg.certainty;
5757
ret.symbolNames = msg.symbolNames();
@@ -133,7 +133,7 @@ std::string SuppressionList::parseXmlFile(const char *filename)
133133
else if (std::strcmp(name, "fileName") == 0)
134134
s.fileName = Path::simplifyPath(text);
135135
else if (std::strcmp(name, "lineNumber") == 0)
136-
s.lineNumber = strToInt<int>(text);
136+
s.lineNumber = strToInt<unsigned int>(text);
137137
else if (std::strcmp(name, "symbolName") == 0)
138138
s.symbolName = text;
139139
else if (*text && std::strcmp(name, "hash") == 0)
@@ -248,7 +248,7 @@ SuppressionList::Suppression SuppressionList::parseLine(std::string line)
248248
throw std::runtime_error("filename is missing");
249249

250250
try {
251-
suppression.lineNumber = strToInt<int>(line_s);
251+
suppression.lineNumber = strToInt<unsigned int>(line_s);
252252
} catch (const std::runtime_error& e) {
253253
throw std::runtime_error(std::string("invalid line number (") + e.what() + ")");
254254
}
@@ -416,7 +416,7 @@ SuppressionList::Suppression::Result SuppressionList::Suppression::isSuppressed(
416416
if (!errorId.empty() && !matchglob(errorId, errmsg.errorId))
417417
return Result::Checked;
418418
} else {
419-
if ((SuppressionList::Type::unique == type) && (lineNumber != NO_LINE) && (lineNumber != errmsg.lineNumber)) {
419+
if ((SuppressionList::Type::unique == type) && (lineNumber != 0) && (lineNumber != errmsg.lineNumber)) {
420420
if (!thisAndNextLine || lineNumber + 1 != errmsg.lineNumber)
421421
return Result::None;
422422
}
@@ -525,15 +525,15 @@ void SuppressionList::dump(std::ostream & out, const std::string& filePath) cons
525525
out << " errorId=\"" << ErrorLogger::toxml(suppression.errorId) << '"';
526526
if (!suppression.fileName.empty())
527527
out << " fileName=\"" << ErrorLogger::toxml(suppression.fileName) << '"';
528-
if (suppression.lineNumber != Suppression::NO_LINE)
528+
if (suppression.lineNumber != 0)
529529
out << " lineNumber=\"" << suppression.lineNumber << '"';
530530
if (!suppression.symbolName.empty())
531531
out << " symbolName=\"" << ErrorLogger::toxml(suppression.symbolName) << '\"';
532532
if (suppression.hash > 0)
533533
out << " hash=\"" << suppression.hash << '\"';
534-
if (suppression.lineBegin != Suppression::NO_LINE)
534+
if (suppression.lineBegin != 0)
535535
out << " lineBegin=\"" << suppression.lineBegin << '"';
536-
if (suppression.lineEnd != Suppression::NO_LINE)
536+
if (suppression.lineEnd != 0)
537537
out << " lineEnd=\"" << suppression.lineEnd << '"';
538538
if (suppression.type == SuppressionList::Type::file)
539539
out << " type=\"file\"";
@@ -566,7 +566,7 @@ std::list<SuppressionList::Suppression> SuppressionList::getUnmatchedLocalSuppre
566566
continue;
567567
if (s.matched)
568568
continue;
569-
if ((s.lineNumber != Suppression::NO_LINE) && !s.checked)
569+
if ((s.lineNumber != 0) && !s.checked)
570570
continue;
571571
if (s.type == SuppressionList::Type::macro)
572572
continue;
@@ -632,7 +632,7 @@ std::list<SuppressionList::Suppression> SuppressionList::getSuppressions() const
632632
void SuppressionList::markUnmatchedInlineSuppressionsAsChecked(const TokenList &tokenlist) {
633633
std::lock_guard<std::mutex> lg(mSuppressionsSync);
634634

635-
int currLineNr = -1;
635+
unsigned int currLineNr = 0;
636636
int currFileIdx = -1;
637637
for (const Token *tok = tokenlist.front(); tok; tok = tok->next()) {
638638
if (currFileIdx != tok->fileIndex() || currLineNr != tok->linenr()) {
@@ -662,7 +662,7 @@ std::string SuppressionList::Suppression::toString() const
662662
if (!fileName.empty()) {
663663
s += ':';
664664
s += fileName;
665-
if (lineNumber != -1) {
665+
if (lineNumber != 0) {
666666
s += ':';
667667
s += std::to_string(lineNumber);
668668
}
@@ -747,7 +747,7 @@ polyspace::CommentKind polyspace::Parser::parseKind(const std::string& comment,
747747
}
748748

749749

750-
std::list<SuppressionList::Suppression> polyspace::Parser::parse(const std::string &comment, int line, const std::string &filename) const
750+
std::list<SuppressionList::Suppression> polyspace::Parser::parse(const std::string &comment, unsigned int line, const std::string &filename) const
751751
{
752752
// Syntax for a polyspace suppression:
753753
// https://se.mathworks.com/help/bugfinder/ug/annotate-hide-known-acceptable-polyspace-results-web-browser.html

lib/suppressions.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class CPPCHECKLIB SuppressionList {
6969

7070
struct CPPCHECKLIB Suppression {
7171
Suppression() = default;
72-
Suppression(std::string id, std::string file, int line=NO_LINE) : errorId(std::move(id)), fileName(std::move(file)), lineNumber(line) {}
72+
Suppression(std::string id, std::string file, unsigned int line=0) : errorId(std::move(id)), fileName(std::move(file)), lineNumber(line) {}
7373

7474
bool operator<(const Suppression &other) const {
7575
if (errorId != other.errorId)
@@ -153,9 +153,9 @@ class CPPCHECKLIB SuppressionList {
153153
std::string extraComment;
154154
// TODO: use simplecpp::Location?
155155
int fileIndex{};
156-
int lineNumber = NO_LINE; // TODO: needs to be unsigned
157-
int lineBegin = NO_LINE;
158-
int lineEnd = NO_LINE;
156+
unsigned int lineNumber{};
157+
unsigned int lineBegin{};
158+
unsigned int lineEnd {};
159159
int column{};
160160
Type type = Type::unique;
161161
std::string symbolName;
@@ -166,8 +166,6 @@ class CPPCHECKLIB SuppressionList {
166166
bool checked{}; /** This suppression applied to code which was being analyzed but did not match the error in an isSuppressed() call */
167167
bool isInline{};
168168
bool isPolyspace{};
169-
170-
enum : std::int8_t { NO_LINE = -1 };
171169
};
172170

173171
/**
@@ -312,7 +310,7 @@ namespace polyspace {
312310
Parser() = delete;
313311
explicit Parser(const Settings &settings);
314312

315-
std::list<SuppressionList::Suppression> parse(const std::string &comment, int line, const std::string &filename) const;
313+
std::list<SuppressionList::Suppression> parse(const std::string &comment, unsigned int line, const std::string &filename) const;
316314

317315
static int parseRange(const std::string& comment, std::string::size_type& pos);
318316
static std::vector<std::pair<std::string, std::string>> parseFamilyRules(const std::string& comment, std::string::size_type& pos);

oss-fuzz/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ $(libcppdir)/cppcheck.o: ../lib/cppcheck.cpp ../externals/picojson/picojson.h ..
270270
$(libcppdir)/ctu.o: ../lib/ctu.cpp ../externals/tinyxml2/tinyxml2.h ../lib/astutils.h ../lib/check.h ../lib/config.h ../lib/ctu.h ../lib/errorlogger.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/path.h ../lib/smallvector.h ../lib/sourcelocation.h ../lib/standards.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenize.h ../lib/tokenlist.h ../lib/utils.h ../lib/vfvalue.h ../lib/xml.h
271271
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/ctu.cpp
272272

273-
$(libcppdir)/errorlogger.o: ../lib/errorlogger.cpp ../externals/tinyxml2/tinyxml2.h ../lib/check.h ../lib/checkers.h ../lib/color.h ../lib/config.h ../lib/cppcheck.h ../lib/errorlogger.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/path.h ../lib/platform.h ../lib/settings.h ../lib/smallvector.h ../lib/standards.h ../lib/suppressions.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenlist.h ../lib/utils.h ../lib/vfvalue.h ../lib/xml.h
273+
$(libcppdir)/errorlogger.o: ../lib/errorlogger.cpp ../externals/tinyxml2/tinyxml2.h ../lib/check.h ../lib/checkers.h ../lib/color.h ../lib/config.h ../lib/cppcheck.h ../lib/errorlogger.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/path.h ../lib/platform.h ../lib/settings.h ../lib/smallvector.h ../lib/standards.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenlist.h ../lib/utils.h ../lib/vfvalue.h ../lib/xml.h
274274
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/errorlogger.cpp
275275

276276
$(libcppdir)/errortypes.o: ../lib/errortypes.cpp ../lib/config.h ../lib/errortypes.h ../lib/utils.h

0 commit comments

Comments
 (0)