Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,10 @@ void Tokenizer::simplifyTypedef()
typedefInfo.filename = list.file(typedefToken);
typedefInfo.lineNumber = typedefToken->linenr();
typedefInfo.column = typedefToken->column();
if (Token::Match(typedefToken->next(), "struct|enum|class|union %name% {") && typedefToken->strAt(2) == typedefInfo.name)
typedefInfo.tagToken = typedefToken->tokAt(2);
else
typedefInfo.tagToken = nullptr;
typedefInfo.used = t.second.isUsed();
typedefInfo.isFunctionPointer = isFunctionPointer(t.second.nameToken());
if (typedefInfo.isFunctionPointer) {
Expand Down Expand Up @@ -1638,8 +1642,12 @@ void Tokenizer::simplifyTypedefCpp()
TypedefInfo typedefInfo;
typedefInfo.name = typeName->str();
typedefInfo.filename = list.file(typeName);
typedefInfo.lineNumber = typeName->linenr();
typedefInfo.column = typeName->column();
typedefInfo.lineNumber = typeDef->linenr();
typedefInfo.column = typeDef->column();
if (Token::Match(typeDef->next(), "struct|enum|class|union %name% {") && typeDef->strAt(2) == typedefInfo.name)
typedefInfo.tagToken = typeDef->tokAt(2);
else
typedefInfo.tagToken = nullptr;
typedefInfo.used = false;
typedefInfo.isFunctionPointer = isFunctionPointer(typeName);
if (typedefInfo.isFunctionPointer) {
Copy link
Owner

@danmar danmar Jan 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we talked about it but I feel skeptic about removing this condition.

Maybe you can search for the tagname like this:

    if (Token::Match(typeDef->next(), "struct|enum|class|union %name% {") && typeDef->strAt(2) == typeName->str())
           typedefInfo.tagToken = typeDef->tokAt(2);
    else
           typedefInfo.tagToken = nullptr;

There might be some alternative syntax we want to handle later but we could start with this.

Expand Down Expand Up @@ -3170,6 +3178,7 @@ bool Tokenizer::simplifyUsing()
usingInfo.filename = list.file(nameToken);
usingInfo.lineNumber = nameToken->linenr();
usingInfo.column = nameToken->column();
usingInfo.tagToken = nullptr;
usingInfo.used = true;
usingInfo.isFunctionPointer = false;
mTypedefInfo.push_back(std::move(usingInfo));
Expand Down Expand Up @@ -6326,7 +6335,15 @@ std::string Tokenizer::dumpTypedefInfo() const
outs += " column=\"";
outs += std::to_string(typedefInfo.column);
outs += "\"";
if (typedefInfo.tagToken) {
outs += " tagline=\"";
outs += std::to_string(typedefInfo.tagToken->linenr());
outs += "\"";

outs += " tagcolumn=\"";
outs += std::to_string(typedefInfo.tagToken->column());
outs += "\"";
}
outs += " used=\"";
outs += std::to_string(typedefInfo.used?1:0);
outs += "\"";
Expand Down
1 change: 1 addition & 0 deletions lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ class CPPCHECKLIB Tokenizer {
std::string filename;
int lineNumber;
int column;
const Token* tagToken;
bool used;
bool isFunctionPointer;
std::vector<TypedefToken> typedefInfoTokens;
Expand Down
14 changes: 13 additions & 1 deletion test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ class TestSimplifyTypedef : public TestFixture {
TEST_CASE(typedefInfo1);
TEST_CASE(typedefInfo2);
TEST_CASE(typedefInfo3);
TEST_CASE(typedefInfo4);
}

class TokenizerTest : public Tokenizer
Expand Down Expand Up @@ -4609,7 +4610,7 @@ class TestSimplifyTypedef : public TestFixture {
" <token line=\"2\" column=\"35\" str=\")\"/>\n"
" </info>\n"
" <info name=\"int16_t\" file=\"file.c\" line=\"1\" column=\"1\" used=\"1\" isFunctionPointer=\"0\"/>\n"
" <info name=\"pfp16\" file=\"file.c\" line=\"4\" column=\"20\" used=\"0\" isFunctionPointer=\"1\">\n"
" <info name=\"pfp16\" file=\"file.c\" line=\"4\" column=\"4\" used=\"0\" isFunctionPointer=\"1\">\n"
" <token line=\"4\" column=\"4\" str=\"typedef\"/>\n"
" <token line=\"4\" column=\"12\" str=\"void\"/>\n"
" <token line=\"4\" column=\"12\" str=\"(\"/>\n"
Expand Down Expand Up @@ -4638,6 +4639,17 @@ class TestSimplifyTypedef : public TestFixture {
"}\n");
ASSERT_EQUALS("",xml);
}

void typedefInfo4() {
const std::string xml = dumpTypedefInfo("typedef struct coord {\n"
" uint16_t x;\n"
" uint16_t y;\n"
"} coord;\n"
"coord c;");
ASSERT_EQUALS(" <typedef-info>\n"
" <info name=\"coord\" file=\"file.c\" line=\"1\" column=\"1\" tagline=\"1\" tagcolumn=\"16\" used=\"1\" isFunctionPointer=\"0\"/>\n"
" </typedef-info>\n", xml);
}
};

REGISTER_TEST(TestSimplifyTypedef)
Loading