Skip to content

Commit

Permalink
Fix #13015 (CI: Test typedef-info output) (#6694)
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar authored Aug 15, 2024
1 parent 4f41804 commit 474d356
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 29 deletions.
8 changes: 5 additions & 3 deletions gui/test/resultstree/testresultstree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@

#include <QtTest>

class TestReport: public Report {
class TestReport : public Report {
public:
TestReport(QString format) : Report(QString()), format(format) {}
void writeHeader() override { output.clear(); }
void writeHeader() override {
output.clear();
}
void writeFooter() override {}
void writeError(const ErrorItem &error) override {
QString line = format;
Expand Down Expand Up @@ -125,7 +127,7 @@ void TestResultsTree::testReportType() const
TestReport report("{id},{classification},{guideline}");

int msgCount = 0;
auto createErrorItem = [&msgCount](const Severity severity, const QString& errorId) -> ErrorItem {
auto createErrorItem = [&msgCount](const Severity severity, const QString& errorId) -> ErrorItem {
++msgCount;
ErrorItem errorItem;
errorItem.errorPath << QErrorPathItem(ErrorMessage::FileLocation("file1.c", msgCount, 1));
Expand Down
60 changes: 34 additions & 26 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6158,41 +6158,49 @@ void Tokenizer::dump(std::ostream &out) const
if (list.front())
list.front()->printValueFlow(true, out);

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

outs += " name=\"";
outs += typedefInfo.name;
outs += "\"";
outs += mTemplateSimplifier->dump();

outs += " file=\"";
outs += ErrorLogger::toxml(typedefInfo.filename);
outs += "\"";
out << outs;
}

outs += " line=\"";
outs += std::to_string(typedefInfo.lineNumber);
outs += "\"";
std::string Tokenizer::dumpTypedefInfo() const
{
if (mTypedefInfo.empty())
return "";
std::string outs = " <typedef-info>";
outs += '\n';
for (const TypedefInfo &typedefInfo: mTypedefInfo) {
outs += " <info";

outs += " column=\"";
outs += std::to_string(typedefInfo.column);
outs += "\"";
outs += " name=\"";
outs += typedefInfo.name;
outs += "\"";

outs += " used=\"";
outs += std::to_string(typedefInfo.used?1:0);
outs += "\"";
outs += " file=\"";
outs += ErrorLogger::toxml(typedefInfo.filename);
outs += "\"";

outs += "/>";
outs += '\n';
}
outs += " </typedef-info>";
outs += " line=\"";
outs += std::to_string(typedefInfo.lineNumber);
outs += "\"";

outs += " column=\"";
outs += std::to_string(typedefInfo.column);
outs += "\"";

outs += " used=\"";
outs += std::to_string(typedefInfo.used?1:0);
outs += "\"";

outs += "/>";
outs += '\n';
}
outs += mTemplateSimplifier->dump();
outs += " </typedef-info>";
outs += '\n';

out << outs;
return outs;
}

void Tokenizer::simplifyHeadersAndUnusedTemplates()
Expand Down
1 change: 1 addition & 0 deletions lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ class CPPCHECKLIB Tokenizer {

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

std::string dumpTypedefInfo() const;
private:
const Token *processFunc(const Token *tok2, bool inOperator) const;
Token *processFunc(Token *tok2, bool inOperator);
Expand Down
26 changes: 26 additions & 0 deletions test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ class TestSimplifyTypedef : public TestFixture {
TEST_CASE(simplifyTypedefMacro);

TEST_CASE(simplifyTypedefOriginalName);

TEST_CASE(typedefInfo1);
}

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

std::string dumpTypedefInfo(const char code[]) {
Tokenizer tokenizer(settings1, *this);

std::istringstream istr(code);
if (!tokenizer.list.createTokens(istr, "file.c"))
return {};
tokenizer.createLinks();
tokenizer.simplifyTypedef();
try {
tokenizer.validate();
} catch (const InternalError&) {
return {};
}
return tokenizer.dumpTypedefInfo();
}

void c1() {
const char code[] = "typedef int t;\n"
"t x;";
Expand Down Expand Up @@ -4380,6 +4398,14 @@ class TestSimplifyTypedef : public TestFixture {
token = Token::findsimplematch(endOfTypeDef, "*", tokenizer.list.back());
ASSERT_EQUALS("rFunctionPointer_fp", token->originalName());
}

void typedefInfo1() {
const std::string xml = dumpTypedefInfo("typedef int A;\nA x;");
ASSERT_EQUALS(" <typedef-info>\n"
" <info name=\"A\" file=\"file.c\" line=\"1\" column=\"1\" used=\"1\"/>\n"
" </typedef-info>\n",
xml);
}
};

REGISTER_TEST(TestSimplifyTypedef)

0 comments on commit 474d356

Please sign in to comment.