Skip to content

Commit ba7a3b3

Browse files
author
Daniel Marjamäki
committed
Fixed danmar#2167 (Drop linefeeds from error messages)
1 parent 0328d26 commit ba7a3b3

File tree

11 files changed

+87
-50
lines changed

11 files changed

+87
-50
lines changed

cli/cppcheckexecutor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ void CppCheckExecutor::reportErr(const ErrorLogger::ErrorMessage &msg)
184184
{
185185
if (_settings._xml)
186186
{
187-
reportErr(msg.toXML());
187+
reportErr(msg.toXML(_settings._verbose));
188188
}
189189
else
190190
{
191-
reportErr(msg.toString(_settings._outputFormat));
191+
reportErr(msg.toString(_settings._verbose, _settings._outputFormat));
192192
}
193193
}

cli/threadexecutor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ int ThreadExecutor::handleRead(unsigned int &result)
9393
msg.deserialize(buf);
9494

9595
// Alert only about unique errors
96-
std::string errmsg = msg.toString();
96+
std::string errmsg = msg.toString(_settings._verbose);
9797
if (std::find(_errorList.begin(), _errorList.end(), errmsg) == _errorList.end())
9898
{
9999
_errorList.push_back(errmsg);

gui/threadresult.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg)
6969
item.files = files;
7070
item.id = QString(msg._id.c_str());
7171
item.lines = lines;
72-
item.msg = QString(msg._msg.c_str());
72+
item.msg = QString::fromStdString(msg.verboseMessage());
7373
item.severity = QString::fromStdString(Severity::toString(msg._severity));
7474

7575
if (msg._severity != Severity::debug)

lib/check.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class Check
110110
*/
111111
static void reportError(const ErrorLogger::ErrorMessage &errmsg)
112112
{
113-
std::cout << errmsg.toXML() << std::endl;
113+
std::cout << errmsg.toXML(true) << std::endl;
114114
}
115115

116116
protected:
@@ -130,14 +130,6 @@ class Check
130130
/** report an error */
131131
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, std::string msg)
132132
{
133-
// If the verbose flag hasn't been given, don't show verbose information
134-
if (!_settings || !_settings->_verbose)
135-
{
136-
std::string::size_type pos = msg.find("\n");
137-
if (pos != std::string::npos)
138-
msg.erase(pos);
139-
}
140-
141133
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
142134
for (std::list<const Token *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it)
143135
{

lib/checkother.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,8 @@ void CheckOther::invalidScanfError(const Token *tok)
398398
{
399399
reportError(tok, Severity::warning,
400400
"invalidscanf", "scanf without field width limits can crash with huge input data\n"
401-
"To fix this error message add a field width specifier:\n"
401+
"scanf without field width limits can crash with huge input data. To fix this error "
402+
"message add a field width specifier:\n"
402403
" %s => %20s\n"
403404
" %i => %3i\n"
404405
"\n"
@@ -2429,8 +2430,9 @@ void CheckOther::variableScopeError(const Token *tok, const std::string &varname
24292430
Severity::style,
24302431
"variableScope",
24312432
"The scope of the variable " + varname + " can be reduced\n"
2432-
"Warning: It can be unsafe to fix this message. Be careful. Especially when there are inner loops.\n"
2433-
"Here is an example where cppcheck will write that the scope for 'i' can be reduced:\n"
2433+
"The scope of the variable " + varname + " can be reduced. Warning: It can be unsafe "
2434+
"to fix this message. Be careful. Especially when there are inner loops. Here is an "
2435+
"example where cppcheck will write that the scope for 'i' can be reduced:\n"
24342436
"void f(int x)\n"
24352437
"{\n"
24362438
" int i = 0;\n"
@@ -2442,7 +2444,6 @@ void CheckOther::variableScopeError(const Token *tok, const std::string &varname
24422444
" }\n"
24432445
" }\n"
24442446
"}\n"
2445-
"\n"
24462447
"When you see this message it is always safe to reduce the variable scope 1 level.");
24472448
}
24482449

lib/cppcheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ Settings CppCheck::settings() const
308308

309309
void CppCheck::reportErr(const ErrorLogger::ErrorMessage &msg)
310310
{
311-
std::string errmsg = msg.toString();
311+
std::string errmsg = msg.toString(_settings._verbose);
312312

313313
// Alert only about unique errors
314314
if (std::find(_errorList.begin(), _errorList.end(), errmsg) != _errorList.end())

lib/errorlogger.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,31 @@ ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack
3232
{
3333
_callStack = callStack;
3434
_severity = severity;
35-
_msg = msg;
35+
setmsg(msg);
3636
_id = id;
3737
}
3838

39+
void ErrorLogger::ErrorMessage::setmsg(const std::string &msg)
40+
{
41+
const std::string::size_type pos = msg.find("\n");
42+
if (pos == std::string::npos)
43+
{
44+
_shortMessage = msg;
45+
_verboseMessage = msg;
46+
}
47+
else
48+
{
49+
_shortMessage = msg.substr(0, pos - 1);
50+
_verboseMessage = msg.substr(pos + 1);
51+
}
52+
}
53+
3954
std::string ErrorLogger::ErrorMessage::serialize() const
4055
{
4156
std::ostringstream oss;
4257
oss << _id.length() << " " << _id;
4358
oss << Severity::toString(_severity).length() << " " << Severity::toString(_severity);
44-
oss << _msg.length() << " " << _msg;
59+
oss << _shortMessage.length() << " " << _shortMessage;
4560
oss << _callStack.size() << " ";
4661

4762
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = _callStack.begin(); tok != _callStack.end(); ++tok)
@@ -79,7 +94,7 @@ bool ErrorLogger::ErrorMessage::deserialize(const std::string &data)
7994

8095
_id = results[0];
8196
_severity = Severity::fromString(results[1]);
82-
_msg = results[2];
97+
_shortMessage = results[2];
8398

8499
unsigned int stackSize = 0;
85100
if (!(iss >> stackSize))
@@ -146,7 +161,7 @@ static std::string stringToXml(std::string s)
146161
return s;
147162
}
148163

149-
std::string ErrorLogger::ErrorMessage::toXML() const
164+
std::string ErrorLogger::ErrorMessage::toXML(bool verbose) const
150165
{
151166
std::ostringstream xml;
152167
xml << "<error";
@@ -157,7 +172,7 @@ std::string ErrorLogger::ErrorMessage::toXML() const
157172
}
158173
xml << " id=\"" << _id << "\"";
159174
xml << " severity=\"" << (_severity == Severity::error ? "error" : "style") << "\"";
160-
xml << " msg=\"" << stringToXml(_msg) << "\"";
175+
xml << " msg=\"" << stringToXml(verbose ? _verboseMessage : _shortMessage) << "\"";
161176
xml << "/>";
162177
return xml.str();
163178
}
@@ -172,7 +187,7 @@ void ErrorLogger::ErrorMessage::findAndReplace(std::string &source, const std::s
172187
}
173188
}
174189

175-
std::string ErrorLogger::ErrorMessage::toString(const std::string &outputFormat) const
190+
std::string ErrorLogger::ErrorMessage::toString(bool verbose, const std::string &outputFormat) const
176191
{
177192
if (outputFormat.length() == 0)
178193
{
@@ -181,15 +196,15 @@ std::string ErrorLogger::ErrorMessage::toString(const std::string &outputFormat)
181196
text << callStackToString(_callStack) << ": ";
182197
if (_severity != Severity::none)
183198
text << "(" << Severity::toString(_severity) << ") ";
184-
text << _msg;
199+
text << (verbose ? _verboseMessage : _shortMessage);
185200
return text.str();
186201
}
187202
else
188203
{
189204
std::string result = outputFormat;
190205
findAndReplace(result, "{id}", _id);
191206
findAndReplace(result, "{severity}", Severity::toString(_severity));
192-
findAndReplace(result, "{message}", _msg);
207+
findAndReplace(result, "{message}", verbose ? _verboseMessage : _shortMessage);
193208

194209
if (!_callStack.empty())
195210
{

lib/errorlogger.h

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,50 @@ class ErrorLogger
120120

121121
ErrorMessage(const std::list<FileLocation> &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id);
122122
ErrorMessage();
123-
std::string toXML() const;
123+
124+
/**
125+
* Format the error message in XML format
126+
* @param verbose use verbose message
127+
*/
128+
std::string toXML(bool verbose) const;
124129

125130
static std::string getXMLHeader();
126131
static std::string getXMLFooter();
127132

128133
/**
129134
* Format the error message into a string.
135+
* @param verbose use verbose message
130136
* @param outputFormat Empty string to use default output format
131137
* or template to be used. E.g. "{file}:{line},{severity},{id},{message}"
132138
*/
133-
std::string toString(const std::string &outputFormat = "") const;
139+
std::string toString(bool verbose, const std::string &outputFormat = "") const;
140+
141+
std::string serialize() const;
142+
bool deserialize(const std::string &data);
143+
144+
std::list<FileLocation> _callStack;
145+
Severity::SeverityType _severity;
146+
std::string _id;
147+
148+
/** source file (not header) */
149+
std::string file0;
134150

151+
/** set short and verbose messages */
152+
void setmsg(const std::string &msg);
153+
154+
/** Short message (single line short message) */
155+
const std::string &shortMessage() const
156+
{
157+
return _shortMessage;
158+
}
159+
160+
/** Verbose message (may be the same as the short message) */
161+
const std::string &verboseMessage() const
162+
{
163+
return _verboseMessage;
164+
}
165+
166+
private:
135167
/**
136168
* Replace all occurances of searchFor with replaceWith in the
137169
* given source.
@@ -140,15 +172,12 @@ class ErrorLogger
140172
* @param replaceWith What will replace the found item
141173
*/
142174
static void findAndReplace(std::string &source, const std::string &searchFor, const std::string &replaceWith);
143-
std::string serialize() const;
144-
bool deserialize(const std::string &data);
145-
std::list<FileLocation> _callStack;
146-
Severity::SeverityType _severity;
147-
std::string _msg;
148-
std::string _id;
149175

150-
/** source file (not header) */
151-
std::string file0;
176+
/** Short message */
177+
std::string _shortMessage;
178+
179+
/** Verbose message */
180+
std::string _verboseMessage;
152181
};
153182

154183
ErrorLogger() { }

lib/preprocessor.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
845845
loc.line = linenr;
846846
errmsg._callStack.push_back(loc);
847847
errmsg._severity = Severity::fromString("error");
848-
errmsg._msg = "mismatching number of '(' and ')' in this line: " + def;
848+
errmsg.setmsg("mismatching number of '(' and ')' in this line: " + def);
849849
errmsg._id = "preprocessor" + lineStream.str();
850850
_errorLogger->reportErr(errmsg);
851851
ret.clear();
@@ -993,8 +993,8 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
993993
loc.setfile(filename);
994994
loc.line = 1;
995995
errmsg._callStack.push_back(loc);
996-
errmsg._severity = Severity::fromString("error");
997-
errmsg._msg = "Error parsing this: " + s;
996+
errmsg._severity = Severity::error;
997+
errmsg.setmsg("Error parsing this: " + s);
998998
errmsg._id = "preprocessor" + lineStream.str();
999999
_errorLogger->reportErr(errmsg);
10001000
}
@@ -2402,11 +2402,11 @@ void Preprocessor::getErrorMessages(std::ostream &ostr)
24022402
Severity::style,
24032403
"Include file: \"\" not found.",
24042404
"missingInclude");
2405-
ostr << errmsg.toXML() << std::endl;
2405+
ostr << errmsg.toXML(false) << std::endl;
24062406

24072407
const ErrorLogger::ErrorMessage errmsg2(locationList,
24082408
Severity::error,
24092409
"#error ...",
24102410
"preprocessorErrorDirective");
2411-
ostr << errmsg2.toXML() << std::endl;
2411+
ostr << errmsg2.toXML(false) << std::endl;
24122412
}

test/testcppcheck.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,8 @@ class TestCppcheck : public TestFixture
388388
{
389389
// Test the errorlogger..
390390
ErrorLogger::ErrorMessage errorMessage;
391-
errorMessage._msg = "ab<cd>ef";
392-
ASSERT_EQUALS("<error id=\"\" severity=\"style\" msg=\"ab&lt;cd&gt;ef\"/>", errorMessage.toXML());
391+
errorMessage.setmsg("ab<cd>ef");
392+
ASSERT_EQUALS("<error id=\"\" severity=\"style\" msg=\"ab&lt;cd&gt;ef\"/>", errorMessage.toXML(false));
393393
}
394394

395395

@@ -400,8 +400,8 @@ class TestCppcheck : public TestFixture
400400
loc.setfile("ab/cd/../ef.h");
401401
errorMessage._callStack.push_back(loc);
402402
const std::string fname(Path::toNativeSeparators("ab/ef.h"));
403-
ASSERT_EQUALS("<error file=\"" + fname + "\" line=\"0\" id=\"\" severity=\"style\" msg=\"\"/>", errorMessage.toXML());
404-
ASSERT_EQUALS("[" + fname + ":0]: ", errorMessage.toString());
403+
ASSERT_EQUALS("<error file=\"" + fname + "\" line=\"0\" id=\"\" severity=\"style\" msg=\"\"/>", errorMessage.toXML(false));
404+
ASSERT_EQUALS("[" + fname + ":0]: ", errorMessage.toString(false));
405405
}
406406

407407
void templateFormat()
@@ -413,11 +413,11 @@ class TestCppcheck : public TestFixture
413413
errorMessage._callStack.push_back(loc);
414414
errorMessage._id = "testId";
415415
errorMessage._severity = Severity::fromString("error");
416-
errorMessage._msg = "long testMessage";
416+
errorMessage.setmsg("long testMessage");
417417
const std::string fname(Path::toNativeSeparators("some/{file}file.cpp"));
418-
ASSERT_EQUALS("<error file=\"" + fname + "\" line=\"10\" id=\"testId\" severity=\"error\" msg=\"long testMessage\"/>", errorMessage.toXML());
419-
ASSERT_EQUALS("[" + fname + ":10]: (error) long testMessage", errorMessage.toString());
420-
ASSERT_EQUALS("testId-" + fname + ",error.10?{long testMessage}", errorMessage.toString("{id}-{file},{severity}.{line}?{{message}}"));
418+
ASSERT_EQUALS("<error file=\"" + fname + "\" line=\"10\" id=\"testId\" severity=\"error\" msg=\"long testMessage\"/>", errorMessage.toXML(false));
419+
ASSERT_EQUALS("[" + fname + ":10]: (error) long testMessage", errorMessage.toString(false));
420+
ASSERT_EQUALS("testId-" + fname + ",error.10?{long testMessage}", errorMessage.toString(false, "{id}-{file},{severity}.{line}?{{message}}"));
421421
}
422422

423423
void getErrorMessages()

0 commit comments

Comments
 (0)