Skip to content

Commit 99e40fa

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

File tree

2 files changed

+51
-16
lines changed

2 files changed

+51
-16
lines changed

lib/tokenize.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ namespace {
669669
return mNameToken ? mNameToken->str() : "";
670670
}
671671

672-
void replace(Token* tok) {
672+
void replace(Token* tok, const std::string &originalname) {
673673
if (tok == mNameToken)
674674
return;
675675

@@ -701,7 +701,7 @@ namespace {
701701
insertTokens(tok2, mRangeTypeQualifiers);
702702
}
703703
else { // functional-style cast
704-
tok->originalName(tok->str());
704+
tok->originalName(originalname);
705705
tok->isSimplifiedTypedef(true);
706706
tok->str("(");
707707
Token* tok2 = insertTokens(tok, mRangeType);
@@ -721,22 +721,22 @@ namespace {
721721
if (isFunctionPointer && isCast(tok->previous())) {
722722
tok->insertToken("*");
723723
Token* const tok_1 = insertTokens(tok, std::pair<Token*, Token*>(mRangeType.first, mNameToken->linkAt(1)));
724-
tok_1->originalName(tok->str());
724+
tok_1->originalName(originalname);
725725
tok->deleteThis();
726726
return;
727727
}
728728

729729
// Inherited type => skip "struct" / "class"
730730
if (Token::Match(mRangeType.first, "const| struct|class %name% {") && Token::Match(tok->previous(), "public|protected|private|<")) {
731-
tok->originalName(tok->str());
731+
tok->originalName(originalname);
732732
tok->str(mRangeType.second->strAt(-1));
733733
return;
734734
}
735735

736736
if (Token::Match(tok, "%name% ::")) {
737737
if (Token::Match(mRangeType.first, "const| struct|class|union|enum %name% %name%|{") ||
738738
Token::Match(mRangeType.first, "%name% %name% ;")) {
739-
tok->originalName(tok->str());
739+
tok->originalName(originalname);
740740
tok->str(mRangeType.second->strAt(-1));
741741
} else {
742742
mReplaceFailed = true;
@@ -790,8 +790,8 @@ namespace {
790790
Token* const tok2 = insertTokens(tok, rangeType);
791791
Token* const tok3 = insertTokens(tok2, mRangeTypeQualifiers);
792792

793-
tok2->originalName(tok->str());
794-
tok3->originalName(tok->str());
793+
tok2->originalName(originalname);
794+
tok3->originalName(originalname);
795795
Token *after = tok3;
796796
while (Token::Match(after, "%name%|*|&|&&|::"))
797797
after = after->next();
@@ -1023,12 +1023,24 @@ 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+
}*/
1038+
for (const Token* t = ts.getTypedefToken()->next(); t != ts.endToken(); t = t->next()) {
1039+
if (t != ts.nameToken())
1040+
existing_data_type += t->str() + " ";
1041+
}
10301042
if (!ts.fail())
1031-
numberOfTypedefs[ts.name()]++;
1043+
numberOfTypedefs[ts.name()].insert(existing_data_type);
10321044
continue;
10331045
}
10341046
}
@@ -1046,8 +1058,7 @@ void Tokenizer::simplifyTypedef()
10461058

10471059
if (indentlevel == 0 && tok->str() == "typedef") {
10481060
TypedefSimplifier ts(tok);
1049-
if (!ts.fail() && numberOfTypedefs[ts.name()] == 1 &&
1050-
(numberOfTypedefs.find(ts.getTypedefToken()->strAt(1)) == numberOfTypedefs.end() || ts.getTypedefToken()->strAt(2) == "(")) {
1061+
if (!ts.fail() && numberOfTypedefs[ts.name()].size() == 1) {
10511062
if (mSettings.severity.isEnabled(Severity::portability) && ts.isInvalidConstFunctionType(typedefs))
10521063
invalidConstFunctionTypeError(tok->next());
10531064
typedefs.emplace(ts.name(), ts);
@@ -1060,8 +1071,11 @@ void Tokenizer::simplifyTypedef()
10601071
auto it = typedefs.find(tok->str());
10611072
if (it != typedefs.end() && it->second.canReplace(tok)) {
10621073
std::set<std::string> r;
1074+
std::string originalname;
10631075
while (it != typedefs.end() && r.insert(tok->str()).second) {
1064-
it->second.replace(tok);
1076+
if (originalname.empty())
1077+
originalname = tok->str();
1078+
it->second.replace(tok, originalname);
10651079
it = typedefs.find(tok->str());
10661080
}
10671081
} else if (tok->str() == "enum") {

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("A", 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)