Skip to content

Commit 851b63d

Browse files
Update tokenize.cpp
1 parent c8c25bb commit 851b63d

1 file changed

Lines changed: 21 additions & 6 deletions

File tree

lib/tokenize.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ static void skipEnumBody(T *&tok)
103103
/**
104104
* is tok the start brace { of a class, struct, union, or enum
105105
*/
106-
static bool isClassStructUnionEnumStart(const Token * tok)
106+
static const Token* isClassStructUnionEnumStart(const Token* tok)
107107
{
108108
if (!Token::Match(tok->previous(), "class|struct|union|enum|%name%|>|>> {"))
109-
return false;
109+
return nullptr;
110110
const Token * tok2 = tok->previous();
111111
while (tok2 && !Token::Match(tok2, "class|struct|union|enum|{|}|)|;"))
112112
tok2 = tok2->previous();
113-
return Token::Match(tok2, "class|struct|union|enum") && !Token::simpleMatch(tok2->tokAt(-1), "->");
113+
return (Token::Match(tok2, "class|struct|union|enum") && !Token::simpleMatch(tok2->tokAt(-1), "->")) ? tok2 : nullptr;
114114
}
115115

116116
//---------------------------------------------------------------------------
@@ -1028,6 +1028,14 @@ bool Tokenizer::isFunctionPointer(const Token* tok) {
10281028
return Token::Match(tok, "%name% ) (");
10291029
}
10301030

1031+
static bool matchCurrentType(const std::string& typeStr, const std::map<int, std::string>& types)
1032+
{
1033+
auto it = std::find_if(types.begin(), types.end(), [&](const auto& element) {
1034+
return typeStr == element.second;
1035+
});
1036+
return it != types.end();
1037+
}
1038+
10311039
void Tokenizer::simplifyTypedef()
10321040
{
10331041
// Simplify global typedefs that are not redefined with the fast 1-pass simplification.
@@ -1050,12 +1058,19 @@ void Tokenizer::simplifyTypedef()
10501058

10511059
int indentlevel = 0;
10521060
std::map<std::string, TypedefSimplifier> typedefs;
1061+
std::map<int, std::string> inType;
10531062
for (Token* tok = list.front(); tok; tok = tok->next()) {
10541063
if (!tok->isName()) {
1055-
if (tok->str()[0] == '{')
1064+
if (tok->str()[0] == '{') {
10561065
++indentlevel;
1057-
else if (tok->str()[0] == '}')
1066+
if (const Token* typeStart = isClassStructUnionEnumStart(tok)) {
1067+
inType.emplace(indentlevel, typeStart->strAt(1));
1068+
}
1069+
}
1070+
else if (tok->str()[0] == '}') {
1071+
inType.erase(indentlevel);
10581072
--indentlevel;
1073+
}
10591074
continue;
10601075
}
10611076

@@ -1072,7 +1087,7 @@ void Tokenizer::simplifyTypedef()
10721087
}
10731088

10741089
auto it = typedefs.find(tok->str());
1075-
if (it != typedefs.end() && it->second.canReplace(tok)) {
1090+
if (it != typedefs.end() && it->second.canReplace(tok) && !matchCurrentType(tok->str(), inType)) {
10761091
std::set<std::string> r;
10771092
std::string originalname;
10781093
while (it != typedefs.end() && r.insert(tok->str()).second) {

0 commit comments

Comments
 (0)