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
46 changes: 36 additions & 10 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1104,18 +1104,26 @@ void Tokenizer::simplifyTypedef()
typedefInfo.filename = list.file(typedefToken);
typedefInfo.lineNumber = typedefToken->linenr();
typedefInfo.column = typedefToken->column();
typedefInfo.tagLineNumber = typedefInfo.lineNumber;
typedefInfo.tagColumn = typedefInfo.column;
typedefInfo.used = t.second.isUsed();
typedefInfo.isFunctionPointer = isFunctionPointer(t.second.nameToken());
if (typedefInfo.isFunctionPointer) {
const Token* tok = typedefToken;
while (tok != t.second.endToken()) {
const Token* tok = typedefToken;
while (tok != t.second.endToken()) {
if (tok->str() == typedefInfo.name) {
typedefInfo.tagLineNumber = tok->linenr();
typedefInfo.tagColumn = tok->column();
if (!typedefInfo.isFunctionPointer)
break;
}
if (typedefInfo.isFunctionPointer) {
TypedefToken ttok;
ttok.name = tok->str();
ttok.lineNumber = tok->linenr();
ttok.column = tok->column();
typedefInfo.typedefInfoTokens.emplace_back(ttok);
tok = tok->next();
}
tok = tok->next();
}
mTypedefInfo.push_back(std::move(typedefInfo));

Expand Down Expand Up @@ -1638,20 +1646,28 @@ 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();
typedefInfo.tagLineNumber = typedefInfo.lineNumber;
typedefInfo.tagColumn = typedefInfo.column;
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.

const Token* t = typeDef;
while (t != tok) {
const Token* t = typeDef;
while (t != tok) {
if (t->str() == typedefInfo.name) {
typedefInfo.tagLineNumber = t->linenr();
typedefInfo.tagColumn = t->column();
if (!typedefInfo.isFunctionPointer)
break;
}
if (typedefInfo.isFunctionPointer) {
TypedefToken ttok;
ttok.name = t->str();
ttok.lineNumber = t->linenr();
ttok.column = t->column();
typedefInfo.typedefInfoTokens.emplace_back(ttok);
t = t->next();
}
t = t->next();
}
mTypedefInfo.push_back(std::move(typedefInfo));

Expand Down Expand Up @@ -3170,6 +3186,8 @@ bool Tokenizer::simplifyUsing()
usingInfo.filename = list.file(nameToken);
usingInfo.lineNumber = nameToken->linenr();
usingInfo.column = nameToken->column();
usingInfo.tagLineNumber = usingInfo.lineNumber;
usingInfo.tagColumn = usingInfo.column;
usingInfo.used = true;
usingInfo.isFunctionPointer = false;
mTypedefInfo.push_back(std::move(usingInfo));
Expand Down Expand Up @@ -6327,6 +6345,14 @@ std::string Tokenizer::dumpTypedefInfo() const
outs += std::to_string(typedefInfo.column);
outs += "\"";

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

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

outs += " used=\"";
outs += std::to_string(typedefInfo.used?1:0);
outs += "\"";
Expand Down
2 changes: 2 additions & 0 deletions lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,8 @@ class CPPCHECKLIB Tokenizer {
std::string filename;
int lineNumber;
int column;
int tagLineNumber;
int tagColumn;
bool used;
bool isFunctionPointer;
std::vector<TypedefToken> typedefInfoTokens;
Expand Down
8 changes: 4 additions & 4 deletions test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4584,7 +4584,7 @@ class TestSimplifyTypedef : public TestFixture {
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\" isFunctionPointer=\"0\"/>\n"
" <info name=\"A\" file=\"file.c\" line=\"1\" column=\"1\" tagline=\"1\" tagcolumn=\"13\" used=\"1\" isFunctionPointer=\"0\"/>\n"
" </typedef-info>\n",
xml);
}
Expand All @@ -4596,7 +4596,7 @@ class TestSimplifyTypedef : public TestFixture {
" typedef fp16 ( *pfp16 ) ( void );\n"
"}\n");
ASSERT_EQUALS(" <typedef-info>\n"
" <info name=\"fp16\" file=\"file.c\" line=\"2\" column=\"1\" used=\"1\" isFunctionPointer=\"1\">\n"
" <info name=\"fp16\" file=\"file.c\" line=\"2\" column=\"1\" tagline=\"2\" tagcolumn=\"17\" used=\"1\" isFunctionPointer=\"1\">\n"
Copy link
Owner

Choose a reason for hiding this comment

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

as far as I see there is no tagname in any typedef in this example. I think a tagname always follows after a struct/enum keyword.

" <token line=\"2\" column=\"1\" str=\"typedef\"/>\n"
" <token line=\"2\" column=\"9\" str=\"void\"/>\n"
" <token line=\"2\" column=\"14\" str=\"(\"/>\n"
Expand All @@ -4608,8 +4608,8 @@ class TestSimplifyTypedef : public TestFixture {
" <token line=\"2\" column=\"33\" str=\"n\"/>\n"
" <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=\"int16_t\" file=\"file.c\" line=\"1\" column=\"1\" tagline=\"1\" tagcolumn=\"22\" used=\"1\" isFunctionPointer=\"0\"/>\n"
" <info name=\"pfp16\" file=\"file.c\" line=\"4\" column=\"4\" tagline=\"4\" tagcolumn=\"20\" 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
Loading