Skip to content

Commit bf80216

Browse files
authored
testrunner: start adding IDs and columns to error messages (danmar#7343)
1 parent fe8a5aa commit bf80216

22 files changed

+445
-404
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ test/testconstructors.o: test/testconstructors.cpp externals/simplecpp/simplecpp
746746
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
747747
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testcppcheck.cpp
748748

749-
test/testerrorlogger.o: test/testerrorlogger.cpp externals/tinyxml2/tinyxml2.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/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/utils.h lib/xml.h test/fixture.h
749+
test/testerrorlogger.o: test/testerrorlogger.cpp externals/tinyxml2/tinyxml2.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/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h lib/xml.h test/fixture.h
750750
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testerrorlogger.cpp
751751

752752
test/testexceptionsafety.o: test/testexceptionsafety.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/checkers.h lib/checkexceptionsafety.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h

lib/errorlogger.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -691,12 +691,12 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm
691691
return result;
692692
}
693693

694-
std::string ErrorLogger::callStackToString(const std::list<ErrorMessage::FileLocation> &callStack)
694+
std::string ErrorLogger::callStackToString(const std::list<ErrorMessage::FileLocation> &callStack, bool addcolumn)
695695
{
696696
std::string str;
697697
for (auto tok = callStack.cbegin(); tok != callStack.cend(); ++tok) {
698698
str += (tok == callStack.cbegin() ? "" : " -> ");
699-
str += tok->stringify();
699+
str += tok->stringify(addcolumn);
700700
}
701701
return str;
702702
}
@@ -729,14 +729,18 @@ void ErrorMessage::FileLocation::setfile(std::string file)
729729
mFileName = Path::simplifyPath(std::move(file));
730730
}
731731

732-
std::string ErrorMessage::FileLocation::stringify() const
732+
std::string ErrorMessage::FileLocation::stringify(bool addcolumn) const
733733
{
734734
std::string str;
735735
str += '[';
736736
str += Path::toNativeSeparators(mFileName);
737-
if (line != SuppressionList::Suppression::NO_LINE) {
737+
if (line != SuppressionList::Suppression::NO_LINE) { // TODO: should not depend on Suppression
738738
str += ':';
739739
str += std::to_string(line);
740+
if (addcolumn) {
741+
str += ':';
742+
str += std::to_string(column);
743+
}
740744
}
741745
str += ']';
742746
return str;

lib/errorlogger.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class CPPCHECKLIB ErrorMessage {
8989
/**
9090
* @return the location as a string. Format: [file:line]
9191
*/
92-
std::string stringify() const;
92+
std::string stringify(bool addcolumn = false) const;
9393

9494
unsigned int fileIndex;
9595
int line; // negative value means "no line"
@@ -257,7 +257,7 @@ class CPPCHECKLIB ErrorLogger {
257257
(void)value;
258258
}
259259

260-
static std::string callStackToString(const std::list<ErrorMessage::FileLocation> &callStack);
260+
static std::string callStackToString(const std::list<ErrorMessage::FileLocation> &callStack, bool addcolumn = false);
261261

262262
/**
263263
* Convert XML-sensitive characters into XML entities

lib/importproject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings &setti
13081308
s.fileName = empty_if_null(child->Attribute("fileName"));
13091309
if (!s.fileName.empty())
13101310
s.fileName = joinRelativePath(path, s.fileName);
1311-
s.lineNumber = child->IntAttribute("lineNumber", SuppressionList::Suppression::NO_LINE);
1311+
s.lineNumber = child->IntAttribute("lineNumber", SuppressionList::Suppression::NO_LINE); // TODO: should not depend on Suppression
13121312
s.symbolName = empty_if_null(child->Attribute("symbolName"));
13131313
s.hash = strToInt<std::size_t>(default_if_null(child->Attribute("hash"), "0"));
13141314
suppressions.push_back(std::move(s));

test/fixture.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,7 @@ void TestFixture::reportErr(const ErrorMessage &msg)
435435
}
436436
else {
437437
if (!msg.callStack.empty()) {
438-
// TODO: add column
439-
errormessage += ErrorLogger::callStackToString(msg.callStack);
438+
errormessage += ErrorLogger::callStackToString(msg.callStack, mNewTemplate);
440439
errormessage += ": ";
441440
}
442441
if (msg.severity != Severity::none) {
@@ -447,7 +446,11 @@ void TestFixture::reportErr(const ErrorMessage &msg)
447446
errormessage += ") ";
448447
}
449448
errormessage += msg.shortMessage();
450-
// TODO: add ID
449+
if (mNewTemplate) {
450+
errormessage += " [";
451+
errormessage += msg.id;
452+
errormessage += "]";
453+
}
451454
}
452455
mErrout << errormessage << std::endl;
453456
}

test/fixture.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class TestFixture : public ErrorLogger {
5959
std::string testToRun;
6060
bool quiet_tests{};
6161
bool dry_run{};
62+
bool mNewTemplate{};
6263

6364
virtual void run() = 0;
6465

test/test64bit.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Test64BitPortability : public TestFixture {
3232
const Settings settings = settingsBuilder().severity(Severity::portability).library("std.cfg").build();
3333

3434
void run() override {
35+
mNewTemplate = true;
3536
TEST_CASE(novardecl);
3637
TEST_CASE(functionpar);
3738
TEST_CASE(structmember);
@@ -83,28 +84,28 @@ class Test64BitPortability : public TestFixture {
8384
" int a = p;\n"
8485
" return a + 4;\n"
8586
"}");
86-
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning a pointer to an integer is not portable.\n", errout_str());
87+
ASSERT_EQUALS("[test.cpp:3:11]: (portability) Assigning a pointer to an integer is not portable. [AssignmentAddressToInteger]\n", errout_str());
8788

8889
check("int foo(int p[])\n"
8990
"{\n"
9091
" int a = p;\n"
9192
" return a + 4;\n"
9293
"}");
93-
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning a pointer to an integer is not portable.\n", errout_str());
94+
ASSERT_EQUALS("[test.cpp:3:11]: (portability) Assigning a pointer to an integer is not portable. [AssignmentAddressToInteger]\n", errout_str());
9495

9596
check("int foo(int p[])\n"
9697
"{\n"
9798
" int *a = p;\n"
9899
" return a;\n"
99100
"}");
100-
ASSERT_EQUALS("[test.cpp:4]: (portability) Returning an address value in a function with integer return type is not portable.\n", errout_str());
101+
ASSERT_EQUALS("[test.cpp:4:5]: (portability) Returning an address value in a function with integer return type is not portable. [CastAddressToIntegerAtReturn]\n", errout_str());
101102

102103
check("void foo(int x)\n"
103104
"{\n"
104105
" int *p = x;\n"
105106
" *p = 0;\n"
106107
"}");
107-
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning an integer to a pointer is not portable.\n", errout_str());
108+
ASSERT_EQUALS("[test.cpp:3:12]: (portability) Assigning an integer to a pointer is not portable. [AssignmentIntegerToAddress]\n", errout_str());
108109

109110
check("int f(const char *p) {\n" // #4659
110111
" return 6 + p[2] * 256;\n"
@@ -169,7 +170,7 @@ class Test64BitPortability : public TestFixture {
169170
"void f(struct Foo *foo) {\n"
170171
" int i = foo->p;\n"
171172
"}");
172-
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning a pointer to an integer is not portable.\n", errout_str());
173+
ASSERT_EQUALS("[test.cpp:3:11]: (portability) Assigning a pointer to an integer is not portable. [AssignmentAddressToInteger]\n", errout_str());
173174

174175
check("struct S {\n" // #10145
175176
" enum class E { e1, e2 };\n"
@@ -208,7 +209,7 @@ class Test64BitPortability : public TestFixture {
208209
" int x = 10;\n"
209210
" int *a = x * x;\n"
210211
"}");
211-
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning an integer to a pointer is not portable.\n", errout_str());
212+
ASSERT_EQUALS("[test.cpp:3:12]: (portability) Assigning an integer to a pointer is not portable. [AssignmentIntegerToAddress]\n", errout_str());
212213

213214
check("void foo(int *start, int *end) {\n"
214215
" int len;\n"
@@ -221,7 +222,7 @@ class Test64BitPortability : public TestFixture {
221222
check("void* foo(int i) {\n"
222223
" return i;\n"
223224
"}");
224-
ASSERT_EQUALS("[test.cpp:2]: (portability) Returning an integer in a function with pointer return type is not portable.\n", errout_str());
225+
ASSERT_EQUALS("[test.cpp:2:5]: (portability) Returning an integer in a function with pointer return type is not portable. [CastIntegerToAddressAtReturn]\n", errout_str());
225226

226227
check("void* foo(int* i) {\n"
227228
" return i;\n"
@@ -248,12 +249,12 @@ class Test64BitPortability : public TestFixture {
248249
check("int foo(char* c) {\n"
249250
" return c;\n"
250251
"}");
251-
ASSERT_EQUALS("[test.cpp:2]: (portability) Returning an address value in a function with integer return type is not portable.\n", errout_str());
252+
ASSERT_EQUALS("[test.cpp:2:5]: (portability) Returning an address value in a function with integer return type is not portable. [CastAddressToIntegerAtReturn]\n", errout_str());
252253

253254
check("int foo(char* c) {\n"
254255
" return 1+c;\n"
255256
"}");
256-
ASSERT_EQUALS("[test.cpp:2]: (portability) Returning an address value in a function with integer return type is not portable.\n", errout_str());
257+
ASSERT_EQUALS("[test.cpp:2:5]: (portability) Returning an address value in a function with integer return type is not portable. [CastAddressToIntegerAtReturn]\n", errout_str());
257258

258259
check("std::string foo(char* c) {\n"
259260
" return c;\n"

test/testassert.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class TestAssert : public TestFixture {
4444
}
4545

4646
void run() override {
47+
mNewTemplate = true;
4748
TEST_CASE(assignmentInAssert);
4849
TEST_CASE(functionCallInAssert);
4950
TEST_CASE(memberFunctionCallInAssert);
@@ -80,7 +81,7 @@ class TestAssert : public TestFixture {
8081
" return a;\n"
8182
"}\n"
8283
"assert(foo() == 3);");
83-
ASSERT_EQUALS("[test.cpp:6]: (warning) Assert statement calls a function which may have desired side effects: 'foo'.\n", errout_str());
84+
ASSERT_EQUALS("[test.cpp:6:8]: (warning) Assert statement calls a function which may have desired side effects: 'foo'. [assertWithSideEffect]\n", errout_str());
8485

8586
// Ticket #4937 "false positive: Assert calls a function which may have desired side effects"
8687
check("struct SquarePack {\n"
@@ -103,7 +104,7 @@ class TestAssert : public TestFixture {
103104
"void foo() {\n"
104105
" assert( !SquarePack::isRank1Or8(push2) );\n"
105106
"}");
106-
ASSERT_EQUALS("[test.cpp:8]: (warning) Assert statement calls a function which may have desired side effects: 'isRank1Or8'.\n", errout_str());
107+
ASSERT_EQUALS("[test.cpp:8:25]: (warning) Assert statement calls a function which may have desired side effects: 'isRank1Or8'. [assertWithSideEffect]\n", errout_str());
107108

108109
check("struct SquarePack {\n"
109110
" static bool isRank1Or8( Square *sq ) {\n"
@@ -114,7 +115,7 @@ class TestAssert : public TestFixture {
114115
"void foo() {\n"
115116
" assert( !SquarePack::isRank1Or8(push2) );\n"
116117
"}");
117-
ASSERT_EQUALS("[test.cpp:8]: (warning) Assert statement calls a function which may have desired side effects: 'isRank1Or8'.\n", errout_str());
118+
ASSERT_EQUALS("[test.cpp:8:25]: (warning) Assert statement calls a function which may have desired side effects: 'isRank1Or8'. [assertWithSideEffect]\n", errout_str());
118119

119120
check("struct SquarePack {\n"
120121
" static bool isRank1Or8( Square *sq ) {\n"
@@ -155,7 +156,7 @@ class TestAssert : public TestFixture {
155156
"void foo(SquarePack s) {\n"
156157
" assert( s.Foo() );\n"
157158
"}");
158-
ASSERT_EQUALS("[test.cpp:5]: (warning) Assert statement calls a function which may have desired side effects: 'Foo'.\n", errout_str());
159+
ASSERT_EQUALS("[test.cpp:5:14]: (warning) Assert statement calls a function which may have desired side effects: 'Foo'. [assertWithSideEffect]\n", errout_str());
159160

160161
check("struct SquarePack {\n"
161162
" int Foo() const;\n"
@@ -187,7 +188,7 @@ class TestAssert : public TestFixture {
187188
" assert(a = 2);\n"
188189
" return a;\n"
189190
"}");
190-
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout_str());
191+
ASSERT_EQUALS("[test.cpp:3:14]: (warning) Assert statement modifies 'a'. [assignmentInAssert]\n", errout_str());
191192

192193
check("void f(int a) {\n"
193194
" assert(a == 2);\n"
@@ -199,42 +200,42 @@ class TestAssert : public TestFixture {
199200
" assert(a == 2 && (b = 1));\n"
200201
" return a;\n"
201202
"}");
202-
ASSERT_EQUALS("[test.cpp:2]: (warning) Assert statement modifies 'b'.\n", errout_str());
203+
ASSERT_EQUALS("[test.cpp:2:25]: (warning) Assert statement modifies 'b'. [assignmentInAssert]\n", errout_str());
203204

204205
check("void f() {\n"
205206
" int a; a = 0;\n"
206207
" assert(a += 2);\n"
207208
" return a;\n"
208209
"}");
209-
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout_str());
210+
ASSERT_EQUALS("[test.cpp:3:14]: (warning) Assert statement modifies 'a'. [assignmentInAssert]\n", errout_str());
210211

211212
check("void f() {\n"
212213
" int a; a = 0;\n"
213214
" assert(a *= 2);\n"
214215
" return a;\n"
215216
"}");
216-
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout_str());
217+
ASSERT_EQUALS("[test.cpp:3:14]: (warning) Assert statement modifies 'a'. [assignmentInAssert]\n", errout_str());
217218

218219
check("void f() {\n"
219220
" int a; a = 0;\n"
220221
" assert(a -= 2);\n"
221222
" return a;\n"
222223
"}");
223-
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout_str());
224+
ASSERT_EQUALS("[test.cpp:3:14]: (warning) Assert statement modifies 'a'. [assignmentInAssert]\n", errout_str());
224225

225226
check("void f() {\n"
226227
" int a = 0;\n"
227228
" assert(a--);\n"
228229
" return a;\n"
229230
"}");
230-
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout_str());
231+
ASSERT_EQUALS("[test.cpp:3:13]: (warning) Assert statement modifies 'a'. [assignmentInAssert]\n", errout_str());
231232

232233
check("void f() {\n"
233234
" int a = 0;\n"
234235
" assert(--a);\n"
235236
" return a;\n"
236237
"}");
237-
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout_str());
238+
ASSERT_EQUALS("[test.cpp:3:12]: (warning) Assert statement modifies 'a'. [assignmentInAssert]\n", errout_str());
238239

239240
check("void f() {\n"
240241
" assert(std::all_of(first, last, []() {\n"

test/testcharvar.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class TestCharVar : public TestFixture {
3333
const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::portability).platform(Platform::Type::Unspecified).build();
3434

3535
void run() override {
36+
mNewTemplate = true;
3637
TEST_CASE(array_index_1);
3738
TEST_CASE(array_index_2);
3839
TEST_CASE(bitop);
@@ -65,7 +66,7 @@ class TestCharVar : public TestFixture {
6566
" char ch = 0x80;\n"
6667
" buf[ch] = 0;\n"
6768
"}");
68-
ASSERT_EQUALS("[test.cpp:5]: (portability) 'char' type used as array index.\n", errout_str());
69+
ASSERT_EQUALS("[test.cpp:5:5]: (portability) 'char' type used as array index. [unknownSignCharArrayIndex]\n", errout_str());
6970

7071
check("int buf[256];\n"
7172
"void foo()\n"
@@ -89,7 +90,7 @@ class TestCharVar : public TestFixture {
8990
" char ch = 0x80;\n"
9091
" buf[ch] = 0;\n"
9192
"}");
92-
ASSERT_EQUALS("[test.cpp:5]: (portability) 'char' type used as array index.\n", errout_str());
93+
ASSERT_EQUALS("[test.cpp:5:5]: (portability) 'char' type used as array index. [unknownSignCharArrayIndex]\n", errout_str());
9394

9495
check("int buf[256];\n"
9596
"void foo(signed char ch)\n"
@@ -110,7 +111,7 @@ class TestCharVar : public TestFixture {
110111
" char ch = 0x80;"
111112
" buf[ch] = 0;\n"
112113
"}");
113-
ASSERT_EQUALS("[test.cpp:3]: (portability) 'char' type used as array index.\n", errout_str());
114+
ASSERT_EQUALS("[test.cpp:3:24]: (portability) 'char' type used as array index. [unknownSignCharArrayIndex]\n", errout_str());
114115

115116
check("void foo(char* buf)\n"
116117
"{\n"
@@ -167,7 +168,7 @@ class TestCharVar : public TestFixture {
167168
" signed char ch = -1;\n"
168169
" *result = a | ch;\n"
169170
"}");
170-
ASSERT_EQUALS("[test.cpp:3]: (warning) When using 'char' variables in bit operations, sign extension can generate unexpected results.\n", errout_str());
171+
ASSERT_EQUALS("[test.cpp:3:17]: (warning) When using 'char' variables in bit operations, sign extension can generate unexpected results. [charBitOp]\n", errout_str());
171172

172173
check("void foo(int *result) {\n"
173174
" unsigned char ch = -1;\n"
@@ -186,7 +187,7 @@ class TestCharVar : public TestFixture {
186187
" signed char ch = -1;\n"
187188
" *result = 0x03 | ch;\n"
188189
"}");
189-
ASSERT_EQUALS("[test.cpp:3]: (warning) When using 'char' variables in bit operations, sign extension can generate unexpected results.\n", errout_str());
190+
ASSERT_EQUALS("[test.cpp:3:20]: (warning) When using 'char' variables in bit operations, sign extension can generate unexpected results. [charBitOp]\n", errout_str());
190191

191192
check("void foo(int *result) {\n"
192193
" signed char ch = -1;\n"

0 commit comments

Comments
 (0)