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
20 changes: 18 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.tagLine = typedefToken->tokAt(2)->linenr();
typedefInfo.tagColumn = typedefToken->tokAt(2)->column();
}
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.tagLine = typeDef->tokAt(2)->linenr();
typedefInfo.tagColumn = typeDef->tokAt(2)->column();
}
typedefInfo.used = false;
typedefInfo.isFunctionPointer = isFunctionPointer(typeName);
if (typedefInfo.isFunctionPointer) {
Expand Down Expand Up @@ -6326,7 +6334,15 @@ std::string Tokenizer::dumpTypedefInfo() const
outs += " column=\"";
outs += std::to_string(typedefInfo.column);
outs += "\"";
if (typedefInfo.tagLine > -1 && typedefInfo.tagColumn > -1) {
outs += " tagline=\"";
outs += std::to_string(typedefInfo.tagLine);
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 tagLine{-1};
int tagColumn{-1};
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