Skip to content

Commit d0524c3

Browse files
authored
Fix #14413 : Add tag line and column in typedef-info in dump file (danmar#8134)
1 parent 0c2b64b commit d0524c3

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

lib/tokenize.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,10 @@ void Tokenizer::simplifyTypedef()
11041104
typedefInfo.filename = list.file(typedefToken);
11051105
typedefInfo.lineNumber = typedefToken->linenr();
11061106
typedefInfo.column = typedefToken->column();
1107+
if (Token::Match(typedefToken->next(), "struct|enum|class|union %name% {") && typedefToken->strAt(2) == typedefInfo.name) {
1108+
typedefInfo.tagLine = typedefToken->tokAt(2)->linenr();
1109+
typedefInfo.tagColumn = typedefToken->tokAt(2)->column();
1110+
}
11071111
typedefInfo.used = t.second.isUsed();
11081112
typedefInfo.isFunctionPointer = isFunctionPointer(t.second.nameToken());
11091113
if (typedefInfo.isFunctionPointer) {
@@ -1638,8 +1642,12 @@ void Tokenizer::simplifyTypedefCpp()
16381642
TypedefInfo typedefInfo;
16391643
typedefInfo.name = typeName->str();
16401644
typedefInfo.filename = list.file(typeName);
1641-
typedefInfo.lineNumber = typeName->linenr();
1642-
typedefInfo.column = typeName->column();
1645+
typedefInfo.lineNumber = typeDef->linenr();
1646+
typedefInfo.column = typeDef->column();
1647+
if (Token::Match(typeDef->next(), "struct|enum|class|union %name% {") && typeDef->strAt(2) == typedefInfo.name) {
1648+
typedefInfo.tagLine = typeDef->tokAt(2)->linenr();
1649+
typedefInfo.tagColumn = typeDef->tokAt(2)->column();
1650+
}
16431651
typedefInfo.used = false;
16441652
typedefInfo.isFunctionPointer = isFunctionPointer(typeName);
16451653
if (typedefInfo.isFunctionPointer) {
@@ -6361,7 +6369,15 @@ std::string Tokenizer::dumpTypedefInfo() const
63616369
outs += " column=\"";
63626370
outs += std::to_string(typedefInfo.column);
63636371
outs += "\"";
6372+
if (typedefInfo.tagLine > -1 && typedefInfo.tagColumn > -1) {
6373+
outs += " tagline=\"";
6374+
outs += std::to_string(typedefInfo.tagLine);
6375+
outs += "\"";
63646376

6377+
outs += " tagcolumn=\"";
6378+
outs += std::to_string(typedefInfo.tagColumn);
6379+
outs += "\"";
6380+
}
63656381
outs += " used=\"";
63666382
outs += std::to_string(typedefInfo.used?1:0);
63676383
outs += "\"";

lib/tokenize.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,8 @@ class CPPCHECKLIB Tokenizer {
696696
std::string filename;
697697
int lineNumber;
698698
int column;
699+
int tagLine{-1};
700+
int tagColumn{-1};
699701
bool used;
700702
bool isFunctionPointer;
701703
std::vector<TypedefToken> typedefInfoTokens;

test/testsimplifytypedef.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ class TestSimplifyTypedef : public TestFixture {
259259
TEST_CASE(typedefInfo1);
260260
TEST_CASE(typedefInfo2);
261261
TEST_CASE(typedefInfo3);
262+
TEST_CASE(typedefInfo4);
262263
}
263264

264265
class TokenizerTest final : public Tokenizer
@@ -4609,7 +4610,7 @@ class TestSimplifyTypedef : public TestFixture {
46094610
" <token line=\"2\" column=\"35\" str=\")\"/>\n"
46104611
" </info>\n"
46114612
" <info name=\"int16_t\" file=\"file.c\" line=\"1\" column=\"1\" used=\"1\" isFunctionPointer=\"0\"/>\n"
4612-
" <info name=\"pfp16\" file=\"file.c\" line=\"4\" column=\"20\" used=\"0\" isFunctionPointer=\"1\">\n"
4613+
" <info name=\"pfp16\" file=\"file.c\" line=\"4\" column=\"4\" used=\"0\" isFunctionPointer=\"1\">\n"
46134614
" <token line=\"4\" column=\"4\" str=\"typedef\"/>\n"
46144615
" <token line=\"4\" column=\"12\" str=\"void\"/>\n"
46154616
" <token line=\"4\" column=\"12\" str=\"(\"/>\n"
@@ -4638,6 +4639,17 @@ class TestSimplifyTypedef : public TestFixture {
46384639
"}\n");
46394640
ASSERT_EQUALS("",xml);
46404641
}
4642+
4643+
void typedefInfo4() {
4644+
const std::string xml = dumpTypedefInfo("typedef struct coord {\n"
4645+
" uint16_t x;\n"
4646+
" uint16_t y;\n"
4647+
"} coord;\n"
4648+
"coord c;");
4649+
ASSERT_EQUALS(" <typedef-info>\n"
4650+
" <info name=\"coord\" file=\"file.c\" line=\"1\" column=\"1\" tagline=\"1\" tagcolumn=\"16\" used=\"1\" isFunctionPointer=\"0\"/>\n"
4651+
" </typedef-info>\n", xml);
4652+
}
46414653
};
46424654

46434655
REGISTER_TEST(TestSimplifyTypedef)

0 commit comments

Comments
 (0)