Skip to content

Commit bd025a8

Browse files
committed
output remarks in xml and add {remark} template option
1 parent b090895 commit bd025a8

5 files changed

Lines changed: 44 additions & 17 deletions

File tree

lib/cppcheck.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
706706
}
707707

708708
// Parse comments and then remove them
709+
mRemarkComments = preprocessor.getRemarkComments(tokens1);
709710
preprocessor.inlineSuppressions(tokens1, mSettings.supprs.nomsg);
710711
if (mSettings.dump || !mSettings.addons.empty()) {
711712
std::ostringstream oss;
@@ -1613,7 +1614,26 @@ void CppCheck::reportErr(const ErrorMessage &msg)
16131614
mExitCode = 1;
16141615
}
16151616

1616-
mErrorLogger.reportErr(msg);
1617+
std::string remark;
1618+
if (!msg.callStack.empty()) {
1619+
for (const auto& r: mRemarkComments) {
1620+
if (r.file != msg.callStack.back().getfile(false))
1621+
continue;
1622+
if (r.lineNumber != msg.callStack.back().line)
1623+
continue;
1624+
remark = r.str;
1625+
break;
1626+
}
1627+
}
1628+
1629+
if (!remark.empty()) {
1630+
ErrorMessage msg2(msg);
1631+
msg2.remark = remark;
1632+
mErrorLogger.reportErr(msg2);
1633+
} else {
1634+
mErrorLogger.reportErr(msg);
1635+
}
1636+
16171637
// check if plistOutput should be populated and the current output file is open and the error is not suppressed
16181638
if (!mSettings.plistOutput.empty() && mPlistFile.is_open() && !mSettings.supprs.nomsg.isSuppressed(errorMessage)) {
16191639
// add error to plist output file

lib/cppcheck.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct FileSettings;
4545
class CheckUnusedFunctions;
4646
class Tokenizer;
4747
class FileWithDetails;
48+
class RemarkComment;
4849

4950
namespace simplecpp { class TokenList; }
5051

@@ -254,6 +255,8 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
254255
std::ofstream mPlistFile;
255256

256257
std::unique_ptr<CheckUnusedFunctions> mUnusedFunctionsCheck;
258+
259+
std::vector<RemarkComment> mRemarkComments;
257260
};
258261

259262
/// @}

lib/errorlogger.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,9 @@ std::string ErrorMessage::serialize() const
274274
serializeString(oss, severityToString(severity));
275275
serializeString(oss, std::to_string(cwe.id));
276276
serializeString(oss, std::to_string(hash));
277+
serializeString(oss, fixInvalidChars(remark));
277278
serializeString(oss, file0);
278-
if (certainty == Certainty::inconclusive) {
279-
const std::string text("inconclusive");
280-
serializeString(oss, text);
281-
}
279+
serializeString(oss, (certainty == Certainty::inconclusive) ? "1" : "0");
282280

283281
const std::string saneShortMessage = fixInvalidChars(mShortMessage);
284282
const std::string saneVerboseMessage = fixInvalidChars(mVerboseMessage);
@@ -312,9 +310,9 @@ void ErrorMessage::deserialize(const std::string &data)
312310
callStack.clear();
313311

314312
std::istringstream iss(data);
315-
std::array<std::string, 7> results;
313+
std::array<std::string, 9> results;
316314
std::size_t elem = 0;
317-
while (iss.good() && elem < 7) {
315+
while (iss.good() && elem < 9) {
318316
unsigned int len = 0;
319317
if (!(iss >> len))
320318
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - invalid length");
@@ -332,11 +330,6 @@ void ErrorMessage::deserialize(const std::string &data)
332330

333331
if (!iss.good())
334332
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - premature end of data");
335-
336-
if (temp == "inconclusive") {
337-
certainty = Certainty::inconclusive;
338-
continue;
339-
}
340333
}
341334

342335
results[elem++] = std::move(temp);
@@ -345,7 +338,7 @@ void ErrorMessage::deserialize(const std::string &data)
345338
if (!iss.good())
346339
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - premature end of data");
347340

348-
if (elem != 7)
341+
if (elem != 9)
349342
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - insufficient elements");
350343

351344
id = std::move(results[0]);
@@ -362,9 +355,12 @@ void ErrorMessage::deserialize(const std::string &data)
362355
if (!strToInt(results[3], hash, &err))
363356
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - invalid hash - " + err);
364357
}
365-
file0 = std::move(results[4]);
366-
mShortMessage = std::move(results[5]);
367-
mVerboseMessage = std::move(results[6]);
358+
remark = std::move(results[4]);
359+
file0 = std::move(results[5]);
360+
if (results[6] == "1")
361+
certainty = Certainty::inconclusive;
362+
mShortMessage = std::move(results[7]);
363+
mVerboseMessage = std::move(results[8]);
368364

369365
unsigned int stackSize = 0;
370366
if (!(iss >> stackSize))
@@ -496,6 +492,9 @@ std::string ErrorMessage::toXML() const
496492
if (!file0.empty())
497493
printer.PushAttribute("file0", file0.c_str());
498494

495+
if (!remark.empty())
496+
printer.PushAttribute("remark", fixInvalidChars(remark).c_str());
497+
499498
for (std::list<FileLocation>::const_reverse_iterator it = callStack.crbegin(); it != callStack.crend(); ++it) {
500499
printer.OpenElement("location", false);
501500
printer.PushAttribute("file", it->getfile().c_str());
@@ -641,6 +640,7 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm
641640
findAndReplace(result, "{severity}", severityToString(severity));
642641
findAndReplace(result, "{cwe}", std::to_string(cwe.id));
643642
findAndReplace(result, "{message}", verbose ? mVerboseMessage : mShortMessage);
643+
findAndReplace(result, "{remark}", remark);
644644
if (!callStack.empty()) {
645645
if (result.find("{callstack}") != std::string::npos)
646646
findAndReplace(result, "{callstack}", ErrorLogger::callStackToString(callStack));

lib/errorlogger.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ class CPPCHECKLIB ErrorMessage {
171171
CWE cwe;
172172
Certainty certainty;
173173

174+
/** remark from REMARK comment */
175+
std::string remark;
176+
174177
/** Warning hash */
175178
std::size_t hash;
176179

lib/preprocessor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ struct CPPCHECKLIB Directive {
6060
Directive(std::string _file, const int _linenr, const std::string &_str);
6161
};
6262

63-
struct CPPCHECKLIB RemarkComment {
63+
class CPPCHECKLIB RemarkComment {
64+
public:
6465
RemarkComment(const std::string &file, const unsigned int lineNumber, const std::string &str)
6566
: file(file)
6667
, lineNumber(lineNumber)

0 commit comments

Comments
 (0)