Skip to content

Commit 474d356

Browse files
authored
Fix #13015 (CI: Test typedef-info output) (danmar#6694)
1 parent 4f41804 commit 474d356

File tree

4 files changed

+66
-29
lines changed

4 files changed

+66
-29
lines changed

gui/test/resultstree/testresultstree.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@
3333

3434
#include <QtTest>
3535

36-
class TestReport: public Report {
36+
class TestReport : public Report {
3737
public:
3838
TestReport(QString format) : Report(QString()), format(format) {}
39-
void writeHeader() override { output.clear(); }
39+
void writeHeader() override {
40+
output.clear();
41+
}
4042
void writeFooter() override {}
4143
void writeError(const ErrorItem &error) override {
4244
QString line = format;
@@ -125,7 +127,7 @@ void TestResultsTree::testReportType() const
125127
TestReport report("{id},{classification},{guideline}");
126128

127129
int msgCount = 0;
128-
auto createErrorItem = [&msgCount](const Severity severity, const QString& errorId) -> ErrorItem {
130+
auto createErrorItem = [&msgCount](const Severity severity, const QString& errorId) -> ErrorItem {
129131
++msgCount;
130132
ErrorItem errorItem;
131133
errorItem.errorPath << QErrorPathItem(ErrorMessage::FileLocation("file1.c", msgCount, 1));

lib/tokenize.cpp

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6158,41 +6158,49 @@ void Tokenizer::dump(std::ostream &out) const
61586158
if (list.front())
61596159
list.front()->printValueFlow(true, out);
61606160

6161-
if (!mTypedefInfo.empty()) {
6162-
outs += " <typedef-info>";
6163-
outs += '\n';
6164-
for (const TypedefInfo &typedefInfo: mTypedefInfo) {
6165-
outs += " <info";
6161+
outs += dumpTypedefInfo();
61666162

6167-
outs += " name=\"";
6168-
outs += typedefInfo.name;
6169-
outs += "\"";
6163+
outs += mTemplateSimplifier->dump();
61706164

6171-
outs += " file=\"";
6172-
outs += ErrorLogger::toxml(typedefInfo.filename);
6173-
outs += "\"";
6165+
out << outs;
6166+
}
61746167

6175-
outs += " line=\"";
6176-
outs += std::to_string(typedefInfo.lineNumber);
6177-
outs += "\"";
6168+
std::string Tokenizer::dumpTypedefInfo() const
6169+
{
6170+
if (mTypedefInfo.empty())
6171+
return "";
6172+
std::string outs = " <typedef-info>";
6173+
outs += '\n';
6174+
for (const TypedefInfo &typedefInfo: mTypedefInfo) {
6175+
outs += " <info";
61786176

6179-
outs += " column=\"";
6180-
outs += std::to_string(typedefInfo.column);
6181-
outs += "\"";
6177+
outs += " name=\"";
6178+
outs += typedefInfo.name;
6179+
outs += "\"";
61826180

6183-
outs += " used=\"";
6184-
outs += std::to_string(typedefInfo.used?1:0);
6185-
outs += "\"";
6181+
outs += " file=\"";
6182+
outs += ErrorLogger::toxml(typedefInfo.filename);
6183+
outs += "\"";
61866184

6187-
outs += "/>";
6188-
outs += '\n';
6189-
}
6190-
outs += " </typedef-info>";
6185+
outs += " line=\"";
6186+
outs += std::to_string(typedefInfo.lineNumber);
6187+
outs += "\"";
6188+
6189+
outs += " column=\"";
6190+
outs += std::to_string(typedefInfo.column);
6191+
outs += "\"";
6192+
6193+
outs += " used=\"";
6194+
outs += std::to_string(typedefInfo.used?1:0);
6195+
outs += "\"";
6196+
6197+
outs += "/>";
61916198
outs += '\n';
61926199
}
6193-
outs += mTemplateSimplifier->dump();
6200+
outs += " </typedef-info>";
6201+
outs += '\n';
61946202

6195-
out << outs;
6203+
return outs;
61966204
}
61976205

61986206
void Tokenizer::simplifyHeadersAndUnusedTemplates()

lib/tokenize.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ class CPPCHECKLIB Tokenizer {
627627

628628
void setDirectives(std::list<Directive> directives);
629629

630+
std::string dumpTypedefInfo() const;
630631
private:
631632
const Token *processFunc(const Token *tok2, bool inOperator) const;
632633
Token *processFunc(Token *tok2, bool inOperator);

test/testsimplifytypedef.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ class TestSimplifyTypedef : public TestFixture {
241241
TEST_CASE(simplifyTypedefMacro);
242242

243243
TEST_CASE(simplifyTypedefOriginalName);
244+
245+
TEST_CASE(typedefInfo1);
244246
}
245247

246248
#define tok(...) tok_(__FILE__, __LINE__, __VA_ARGS__)
@@ -307,6 +309,22 @@ class TestSimplifyTypedef : public TestFixture {
307309
return tokenizer.tokens()->stringifyList(nullptr, false);
308310
}
309311

312+
std::string dumpTypedefInfo(const char code[]) {
313+
Tokenizer tokenizer(settings1, *this);
314+
315+
std::istringstream istr(code);
316+
if (!tokenizer.list.createTokens(istr, "file.c"))
317+
return {};
318+
tokenizer.createLinks();
319+
tokenizer.simplifyTypedef();
320+
try {
321+
tokenizer.validate();
322+
} catch (const InternalError&) {
323+
return {};
324+
}
325+
return tokenizer.dumpTypedefInfo();
326+
}
327+
310328
void c1() {
311329
const char code[] = "typedef int t;\n"
312330
"t x;";
@@ -4380,6 +4398,14 @@ class TestSimplifyTypedef : public TestFixture {
43804398
token = Token::findsimplematch(endOfTypeDef, "*", tokenizer.list.back());
43814399
ASSERT_EQUALS("rFunctionPointer_fp", token->originalName());
43824400
}
4401+
4402+
void typedefInfo1() {
4403+
const std::string xml = dumpTypedefInfo("typedef int A;\nA x;");
4404+
ASSERT_EQUALS(" <typedef-info>\n"
4405+
" <info name=\"A\" file=\"file.c\" line=\"1\" column=\"1\" used=\"1\"/>\n"
4406+
" </typedef-info>\n",
4407+
xml);
4408+
}
43834409
};
43844410

43854411
REGISTER_TEST(TestSimplifyTypedef)

0 commit comments

Comments
 (0)