From 4d5a1f86f59e8c1531946119f81931150a665f3d Mon Sep 17 00:00:00 2001 From: swasti16 Date: Mon, 19 Jan 2026 21:50:55 +0530 Subject: [PATCH] Add tag line and column in typedef-info in dump file --- lib/tokenize.cpp | 46 ++++++++++++++++++++++++++++-------- lib/tokenize.h | 2 ++ test/testsimplifytypedef.cpp | 8 +++---- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 96c69bb4dd9..67651c7d821 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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)); @@ -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) { - 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)); @@ -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)); @@ -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 += "\""; diff --git a/lib/tokenize.h b/lib/tokenize.h index 616290e7e2a..b880bd092fa 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -696,6 +696,8 @@ class CPPCHECKLIB Tokenizer { std::string filename; int lineNumber; int column; + int tagLineNumber; + int tagColumn; bool used; bool isFunctionPointer; std::vector typedefInfoTokens; diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 087e3759901..a4347f9fc9c 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -4584,7 +4584,7 @@ class TestSimplifyTypedef : public TestFixture { void typedefInfo1() { const std::string xml = dumpTypedefInfo("typedef int A;\nA x;"); ASSERT_EQUALS(" \n" - " \n" + " \n" " \n", xml); } @@ -4596,7 +4596,7 @@ class TestSimplifyTypedef : public TestFixture { " typedef fp16 ( *pfp16 ) ( void );\n" "}\n"); ASSERT_EQUALS(" \n" - " \n" + " \n" " \n" " \n" " \n" @@ -4608,8 +4608,8 @@ class TestSimplifyTypedef : public TestFixture { " \n" " \n" " \n" - " \n" - " \n" + " \n" + " \n" " \n" " \n" " \n"