Skip to content

Commit bc5ae72

Browse files
committed
Fix #14319: originalName missing from the dumpfile
1 parent a9f88d4 commit bc5ae72

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

lib/tokenize.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,12 +1023,20 @@ void Tokenizer::simplifyTypedef()
10231023
{
10241024
// Simplify global typedefs that are not redefined with the fast 1-pass simplification.
10251025
// Then use the slower old typedef simplification.
1026-
std::map<std::string, int> numberOfTypedefs;
1026+
std::map<std::string, std::set<std::string>> numberOfTypedefs;
10271027
for (Token* tok = list.front(); tok; tok = tok->next()) {
10281028
if (tok->str() == "typedef") {
10291029
TypedefSimplifier ts(tok);
1030+
if (ts.name().empty())
1031+
continue;
1032+
const Token* t = tok->next();
1033+
std::string existing_data_type;
1034+
while (t && t->str() != ts.name()) {
1035+
existing_data_type += t->str() + " ";
1036+
t = t->next();
1037+
}
10301038
if (!ts.fail())
1031-
numberOfTypedefs[ts.name()]++;
1039+
numberOfTypedefs[ts.name()].insert(existing_data_type);
10321040
continue;
10331041
}
10341042
}
@@ -1046,8 +1054,7 @@ void Tokenizer::simplifyTypedef()
10461054

10471055
if (indentlevel == 0 && tok->str() == "typedef") {
10481056
TypedefSimplifier ts(tok);
1049-
if (!ts.fail() && numberOfTypedefs[ts.name()] == 1 &&
1050-
(numberOfTypedefs.find(ts.getTypedefToken()->strAt(1)) == numberOfTypedefs.end() || ts.getTypedefToken()->strAt(2) == "(")) {
1057+
if (!ts.fail() && numberOfTypedefs[ts.name()].size() == 1) {
10511058
if (mSettings.severity.isEnabled(Severity::portability) && ts.isInvalidConstFunctionType(typedefs))
10521059
invalidConstFunctionTypeError(tok->next());
10531060
typedefs.emplace(ts.name(), ts);

test/testsimplifytypedef.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ class TestSimplifyTypedef : public TestFixture {
246246

247247
TEST_CASE(simplifyTypedefMacro);
248248

249-
TEST_CASE(simplifyTypedefOriginalName);
249+
TEST_CASE(simplifyTypedefOriginalName1);
250+
TEST_CASE(simplifyTypedefOriginalName2);
250251

251252
TEST_CASE(simplifyTypedefTokenColumn1);
252253
TEST_CASE(simplifyTypedefTokenColumn2);
@@ -1284,7 +1285,7 @@ class TestSimplifyTypedef : public TestFixture {
12841285
"LPCSTR ccp;";
12851286

12861287
const char expected[] =
1287-
"; char c ; "
1288+
"char c ; "
12881289
"char * cp ; "
12891290
"const char * ccp ;";
12901291

@@ -3679,7 +3680,7 @@ class TestSimplifyTypedef : public TestFixture {
36793680
"Y y;\n"
36803681
"Yp yp;\n"
36813682
"Ya ya;\n";
3682-
exp = "long y ; long * yp ; long ya [ 3 ] ;";
3683+
exp = "; long y ; long * yp ; long ya [ 3 ] ;";
36833684
ASSERT_EQUALS(exp, tok(code));
36843685
}
36853686

@@ -4444,7 +4445,7 @@ class TestSimplifyTypedef : public TestFixture {
44444445
simplifyTypedefP(code));
44454446
}
44464447

4447-
void simplifyTypedefOriginalName() {
4448+
void simplifyTypedefOriginalName1() {
44484449
const char code[] = "typedef unsigned char uint8_t;"
44494450
"typedef float (*rFunctionPointer_fp)(uint8_t, uint8_t);"
44504451
"typedef enum eEnumDef {"
@@ -4500,6 +4501,26 @@ class TestSimplifyTypedef : public TestFixture {
45004501
ASSERT_EQUALS("rFunctionPointer_fp", token->originalName());
45014502
}
45024503

4504+
void simplifyTypedefOriginalName2() {
4505+
const char code[] = "typedef unsigned short uint16;\n"
4506+
"typedef uint16 A;\n"
4507+
"A a;";
4508+
TokenList tokenlist{ settings1, Standards::Language::C };
4509+
ASSERT(TokenListHelper::createTokensFromString(tokenlist, code, "file.c"));
4510+
TokenizerTest tokenizer(std::move(tokenlist), *this);
4511+
tokenizer.createLinks();
4512+
tokenizer.simplifyTypedef();
4513+
4514+
try {
4515+
tokenizer.validate();
4516+
}
4517+
catch (const InternalError&) {
4518+
ASSERT_EQUALS_MSG(false, true, "Validation of Tokenizer failed");
4519+
}
4520+
const Token* token = Token::findsimplematch(tokenizer.list.front(), "short");
4521+
ASSERT_EQUALS("uint16", token->originalName());
4522+
}
4523+
45034524
void simplifyTypedefTokenColumn1() { // #13155
45044525
const char code[] = "void foo(void) {\n"
45054526
" typedef signed int MY_INT;\n"

0 commit comments

Comments
 (0)