Skip to content

Commit 233c016

Browse files
authored
Fix #14224: dumpfile: Add tokens to function pointer typedef (#7910)
1 parent 92fffee commit 233c016

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

addons/cppcheckdata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ def iterconfigurations(self):
13411341
# Parse tokens
13421342
elif node.tag == 'tokenlist' and event == 'start':
13431343
continue
1344-
elif node.tag == 'token' and event == 'start' and not iter_directive:
1344+
elif node.tag == 'token' and event == 'start' and not iter_directive and not iter_typedef_info:
13451345
cfg.tokenlist.append(Token(node))
13461346

13471347
# Parse scopes

lib/tokenize.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,17 @@ void Tokenizer::simplifyTypedef()
10891089
typedefInfo.column = typedefToken->column();
10901090
typedefInfo.used = t.second.isUsed();
10911091
typedefInfo.isFunctionPointer = Token::Match(t.second.nameToken(), "%name% ) (");
1092+
if (typedefInfo.isFunctionPointer) {
1093+
const Token* tok = typedefToken;
1094+
while (tok != t.second.endToken()) {
1095+
TypedefToken ttok;
1096+
ttok.name = tok->str();
1097+
ttok.lineNumber = tok->linenr();
1098+
ttok.column = tok->column();
1099+
typedefInfo.typedefInfoTokens.emplace_back(ttok);
1100+
tok = tok->next();
1101+
}
1102+
}
10921103
mTypedefInfo.push_back(std::move(typedefInfo));
10931104

10941105
t.second.removeDeclaration();
@@ -1612,6 +1623,17 @@ void Tokenizer::simplifyTypedefCpp()
16121623
typedefInfo.column = typeName->column();
16131624
typedefInfo.used = false;
16141625
typedefInfo.isFunctionPointer = Token::Match(typeName, "%name% ) (");
1626+
if (typedefInfo.isFunctionPointer) {
1627+
const Token* t = typeDef;
1628+
while (t != tok) {
1629+
TypedefToken ttok;
1630+
ttok.name = t->str();
1631+
ttok.lineNumber = t->linenr();
1632+
ttok.column = t->column();
1633+
typedefInfo.typedefInfoTokens.emplace_back(ttok);
1634+
t = t->next();
1635+
}
1636+
}
16151637
mTypedefInfo.push_back(std::move(typedefInfo));
16161638

16171639
while (!done) {
@@ -6291,6 +6313,16 @@ std::string Tokenizer::dumpTypedefInfo() const
62916313

62926314
outs += "/>";
62936315
outs += '\n';
6316+
for (const auto& t : typedefInfo.typedefInfoTokens) {
6317+
outs += " <token ";
6318+
outs += "column=\"";
6319+
outs += std::to_string(t.column);
6320+
outs += "\" ";
6321+
outs += "str=\"";
6322+
outs += ErrorLogger::toxml(t.name);
6323+
outs += "\"/>";
6324+
outs += '\n';
6325+
}
62946326
}
62956327
outs += " </typedef-info>";
62966328
outs += '\n';

lib/tokenize.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,13 +655,19 @@ class CPPCHECKLIB Tokenizer {
655655
/** sizeof information for known types */
656656
std::map<std::string, int> mTypeSize;
657657

658+
struct TypedefToken {
659+
std::string name;
660+
int lineNumber;
661+
int column;
662+
};
658663
struct TypedefInfo {
659664
std::string name;
660665
std::string filename;
661666
int lineNumber;
662667
int column;
663668
bool used;
664669
bool isFunctionPointer;
670+
std::vector<TypedefToken> typedefInfoTokens;
665671
};
666672
std::vector<TypedefInfo> mTypedefInfo;
667673

test/testsimplifytypedef.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4557,8 +4557,35 @@ class TestSimplifyTypedef : public TestFixture {
45574557
"}\n");
45584558
ASSERT_EQUALS(" <typedef-info>\n"
45594559
" <info name=\"fp16\" file=\"file.c\" line=\"2\" column=\"1\" used=\"1\" isFunctionPointer=\"1\"/>\n"
4560+
" <token column=\"1\" str=\"typedef\"/>\n"
4561+
" <token column=\"9\" str=\"void\"/>\n"
4562+
" <token column=\"14\" str=\"(\"/>\n"
4563+
" <token column=\"16\" str=\"*\"/>\n"
4564+
" <token column=\"17\" str=\"fp16\"/>\n"
4565+
" <token column=\"22\" str=\")\"/>\n"
4566+
" <token column=\"23\" str=\"(\"/>\n"
4567+
" <token column=\"25\" str=\"int16_t\"/>\n"
4568+
" <token column=\"33\" str=\"n\"/>\n"
4569+
" <token column=\"35\" str=\")\"/>\n"
45604570
" <info name=\"int16_t\" file=\"file.c\" line=\"1\" column=\"1\" used=\"1\" isFunctionPointer=\"0\"/>\n"
45614571
" <info name=\"pfp16\" file=\"file.c\" line=\"4\" column=\"20\" used=\"0\" isFunctionPointer=\"1\"/>\n"
4572+
" <token column=\"4\" str=\"typedef\"/>\n"
4573+
" <token column=\"12\" str=\"void\"/>\n"
4574+
" <token column=\"12\" str=\"(\"/>\n"
4575+
" <token column=\"12\" str=\"*\"/>\n"
4576+
" <token column=\"17\" str=\"(\"/>\n"
4577+
" <token column=\"19\" str=\"*\"/>\n"
4578+
" <token column=\"20\" str=\"pfp16\"/>\n"
4579+
" <token column=\"26\" str=\")\"/>\n"
4580+
" <token column=\"28\" str=\"(\"/>\n"
4581+
" <token column=\"30\" str=\"void\"/>\n"
4582+
" <token column=\"35\" str=\")\"/>\n"
4583+
" <token column=\"35\" str=\")\"/>\n"
4584+
" <token column=\"35\" str=\"(\"/>\n"
4585+
" <token column=\"35\" str=\"signed\"/>\n"
4586+
" <token column=\"35\" str=\"short\"/>\n"
4587+
" <token column=\"35\" str=\"n\"/>\n"
4588+
" <token column=\"35\" str=\")\"/>\n"
45624589
" </typedef-info>\n",xml);
45634590
}
45644591

0 commit comments

Comments
 (0)