Skip to content

Commit fc6e9d3

Browse files
authored
Fix #13155 (Tokenizer::simplifyTypedef does not set info correctly for typedef in function) (#7056)
1 parent 588ca87 commit fc6e9d3

File tree

2 files changed

+107
-86
lines changed

2 files changed

+107
-86
lines changed

lib/tokenize.cpp

Lines changed: 68 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,24 @@ void Tokenizer::simplifyTypedef()
11801180
simplifyTypedefCpp();
11811181
}
11821182

1183+
static Token* simplifyTypedefCopyTokens(Token* to, const Token* fromStart, const Token* toEnd, const Token* location) {
1184+
Token* ret = TokenList::copyTokens(to, fromStart, toEnd);
1185+
for (Token* tok = to->next(); tok != ret->next(); tok = tok->next()) {
1186+
tok->linenr(location->linenr());
1187+
tok->column(location->column());
1188+
tok->isSimplifiedTypedef(true);
1189+
}
1190+
return ret;
1191+
}
1192+
1193+
static Token* simplifyTypedefInsertToken(Token* tok, const std::string& str, const Token* location) {
1194+
tok = tok->insertToken(str);
1195+
tok->linenr(location->linenr());
1196+
tok->column(location->column());
1197+
tok->isSimplifiedTypedef(true);
1198+
return tok;
1199+
}
1200+
11831201
// TODO: rename - it is not C++ specific
11841202
void Tokenizer::simplifyTypedefCpp()
11851203
{
@@ -1999,14 +2017,13 @@ void Tokenizer::simplifyTypedefCpp()
19992017
const bool isPointerTypeCall = !inOperator && Token::Match(tok2, "%name% ( )") && !pointers.empty();
20002018

20012019
// start substituting at the typedef name by replacing it with the type
2002-
Token* replStart = tok2; // track first replaced token
2020+
const Token * const location = tok2;
20032021
for (Token* tok3 = typeStart; tok3 && (tok3->str() != ";"); tok3 = tok3->next())
20042022
tok3->isSimplifiedTypedef(true);
20052023
if (isPointerTypeCall) {
20062024
tok2->deleteThis();
2007-
tok2->insertToken("0");
2008-
tok2 = tok2->next();
2009-
tok2->next()->insertToken("0");
2025+
tok2 = simplifyTypedefInsertToken(tok2, "0", location);
2026+
simplifyTypedefInsertToken(tok2->next(), "0", location);
20102027
}
20112028
if (Token::Match(tok2->tokAt(-1), "class|struct|union") && tok2->strAt(-1) == typeStart->str())
20122029
tok2->deletePrevious();
@@ -2018,15 +2035,12 @@ void Tokenizer::simplifyTypedefCpp()
20182035
tok2 = tok2->previous();
20192036

20202037
if (globalScope) {
2021-
replStart = tok2->insertToken("::");
2022-
tok2 = tok2->next();
2038+
tok2 = simplifyTypedefInsertToken(tok2, "::", location);
20232039
}
20242040

20252041
for (std::size_t i = classLevel; i < spaceInfo.size(); ++i) {
2026-
tok2->insertToken(spaceInfo[i].className);
2027-
tok2 = tok2->next();
2028-
tok2->insertToken("::");
2029-
tok2 = tok2->next();
2042+
tok2 = simplifyTypedefInsertToken(tok2, spaceInfo[i].className, location);
2043+
tok2 = simplifyTypedefInsertToken(tok2, "::", location);
20302044
}
20312045
}
20322046

@@ -2043,11 +2057,11 @@ void Tokenizer::simplifyTypedefCpp()
20432057
std::string::size_type spaceIdx = 0;
20442058
std::string::size_type startIdx = 0;
20452059
while ((spaceIdx = removed1.find(' ', startIdx)) != std::string::npos) {
2046-
tok2->previous()->insertToken(removed1.substr(startIdx, spaceIdx - startIdx));
2060+
simplifyTypedefInsertToken(tok2->previous(), removed1.substr(startIdx, spaceIdx - startIdx), location);
20472061
startIdx = spaceIdx + 1;
20482062
}
2049-
tok2->previous()->insertToken(removed1.substr(startIdx));
2050-
replStart = tok2->previous()->insertToken("::");
2063+
simplifyTypedefInsertToken(tok2->previous(), removed1.substr(startIdx), location);
2064+
simplifyTypedefInsertToken(tok2->previous(), "::", location);
20512065
break;
20522066
}
20532067
idx = removed1.rfind(" ::");
@@ -2057,30 +2071,24 @@ void Tokenizer::simplifyTypedefCpp()
20572071
removed1.resize(idx);
20582072
}
20592073
}
2060-
replStart->isSimplifiedTypedef(true);
20612074
Token* constTok = Token::simpleMatch(tok2->previous(), "const") ? tok2->previous() : nullptr;
20622075
// add remainder of type
2063-
tok2 = TokenList::copyTokens(tok2, typeStart->next(), typeEnd);
2076+
tok2 = simplifyTypedefCopyTokens(tok2, typeStart->next(), typeEnd, location);
20642077

20652078
if (!pointers.empty()) {
2066-
for (const std::string &p : pointers) {
2067-
tok2->insertToken(p);
2068-
tok2->isSimplifiedTypedef(true);
2069-
tok2 = tok2->next();
2070-
}
2079+
for (const std::string &p : pointers)
2080+
// cppcheck-suppress useStlAlgorithm
2081+
tok2 = simplifyTypedefInsertToken(tok2, p, location);
20712082
if (constTok) {
20722083
constTok->deleteThis();
2073-
tok2->insertToken("const");
2074-
tok2->isSimplifiedTypedef(true);
2075-
tok2 = tok2->next();
2084+
tok2 = simplifyTypedefInsertToken(tok2, "const", location);
20762085
}
20772086
}
20782087

20792088
if (funcStart && funcEnd) {
2080-
tok2->insertToken("(");
2081-
tok2 = tok2->next();
2089+
tok2 = simplifyTypedefInsertToken(tok2, "(", location);
20822090
Token *paren = tok2;
2083-
tok2 = TokenList::copyTokens(tok2, funcStart, funcEnd);
2091+
tok2 = simplifyTypedefCopyTokens(tok2, funcStart, funcEnd, location);
20842092

20852093
if (!inCast)
20862094
tok2 = processFunc(tok2, inOperator);
@@ -2091,20 +2099,17 @@ void Tokenizer::simplifyTypedefCpp()
20912099
while (Token::Match(tok2, "%name%|] ["))
20922100
tok2 = tok2->linkAt(1);
20932101

2094-
tok2->insertToken(")");
2095-
tok2 = tok2->next();
2102+
tok2 = simplifyTypedefInsertToken(tok2, ")", location);
20962103
Token::createMutualLinks(tok2, paren);
20972104

2098-
tok2 = TokenList::copyTokens(tok2, argStart, argEnd);
2105+
tok2 = simplifyTypedefCopyTokens(tok2, argStart, argEnd, location);
20992106

21002107
if (specStart) {
21012108
Token *spec = specStart;
2102-
tok2->insertToken(spec->str());
2103-
tok2 = tok2->next();
2109+
tok2 = simplifyTypedefInsertToken(tok2, spec->str(), location);
21042110
while (spec != specEnd) {
21052111
spec = spec->next();
2106-
tok2->insertToken(spec->str());
2107-
tok2 = tok2->next();
2112+
tok2 = simplifyTypedefInsertToken(tok2, spec->str(), location);
21082113
}
21092114
}
21102115
}
@@ -2116,24 +2121,20 @@ void Tokenizer::simplifyTypedefCpp()
21162121
if (!inTemplate && function && tok2->next() && tok2->strAt(1) != "*")
21172122
needParen = false;
21182123
if (needParen) {
2119-
tok2->insertToken("(");
2120-
tok2 = tok2->next();
2124+
tok2 = simplifyTypedefInsertToken(tok2, "(", location);
21212125
}
21222126
Token *tok3 = tok2;
21232127
if (namespaceStart) {
21242128
const Token *tok4 = namespaceStart;
21252129

21262130
while (tok4 != namespaceEnd) {
2127-
tok2->insertToken(tok4->str());
2128-
tok2 = tok2->next();
2131+
tok2 = simplifyTypedefInsertToken(tok2, tok4->str(), location);
21292132
tok4 = tok4->next();
21302133
}
2131-
tok2->insertToken(namespaceEnd->str());
2132-
tok2 = tok2->next();
2134+
tok2 = simplifyTypedefInsertToken(tok2, namespaceEnd->str(), location);
21332135
}
21342136
if (functionPtr) {
2135-
tok2->insertToken("*");
2136-
tok2 = tok2->next();
2137+
tok2 = simplifyTypedefInsertToken(tok2, "*", location);
21372138
}
21382139

21392140
if (!inCast)
@@ -2143,42 +2144,35 @@ void Tokenizer::simplifyTypedefCpp()
21432144
if (!tok2)
21442145
syntaxError(nullptr);
21452146

2146-
tok2->insertToken(")");
2147-
tok2 = tok2->next();
2147+
tok2 = simplifyTypedefInsertToken(tok2, ")", location);
21482148
Token::createMutualLinks(tok2, tok3);
21492149
}
21502150
if (!tok2)
21512151
syntaxError(nullptr);
21522152

2153-
tok2 = TokenList::copyTokens(tok2, argStart, argEnd);
2153+
tok2 = simplifyTypedefCopyTokens(tok2, argStart, argEnd, location);
21542154
if (inTemplate) {
21552155
tok2 = tok2->next();
21562156
}
21572157

21582158
if (specStart) {
21592159
Token *spec = specStart;
2160-
tok2->insertToken(spec->str());
2161-
tok2 = tok2->next();
2160+
tok2 = simplifyTypedefInsertToken(tok2, spec->str(), location);
21622161
while (spec != specEnd) {
21632162
spec = spec->next();
2164-
tok2->insertToken(spec->str());
2165-
tok2 = tok2->next();
2163+
tok2 = simplifyTypedefInsertToken(tok2, spec->str(), location);
21662164
}
21672165
}
21682166
} else if (functionRetFuncPtr || functionPtrRetFuncPtr) {
2169-
tok2->insertToken("(");
2170-
tok2 = tok2->next();
2167+
tok2 = simplifyTypedefInsertToken(tok2, "(", location);
21712168
Token *tok3 = tok2;
2172-
tok2->insertToken("*");
2173-
tok2 = tok2->next();
2169+
tok2 = simplifyTypedefInsertToken(tok2, "*", location);
21742170

21752171
Token * tok4 = nullptr;
21762172
if (functionPtrRetFuncPtr) {
2177-
tok2->insertToken("(");
2178-
tok2 = tok2->next();
2173+
tok2 = simplifyTypedefInsertToken(tok2, "(", location);
21792174
tok4 = tok2;
2180-
tok2->insertToken("*");
2181-
tok2 = tok2->next();
2175+
tok2 = simplifyTypedefInsertToken(tok2, "*", location);
21822176
}
21832177

21842178
// skip over variable name if there
@@ -2191,28 +2185,24 @@ void Tokenizer::simplifyTypedefCpp()
21912185
}
21922186

21932187
if (tok4 && functionPtrRetFuncPtr) {
2194-
tok2->insertToken(")");
2195-
tok2 = tok2->next();
2188+
tok2 = simplifyTypedefInsertToken(tok2,")", location);
21962189
Token::createMutualLinks(tok2, tok4);
21972190
}
21982191

2199-
tok2 = TokenList::copyTokens(tok2, argStart, argEnd);
2192+
tok2 = simplifyTypedefCopyTokens(tok2, argStart, argEnd, location);
22002193

2201-
tok2->insertToken(")");
2202-
tok2 = tok2->next();
2194+
tok2 = simplifyTypedefInsertToken(tok2, ")", location);
22032195
Token::createMutualLinks(tok2, tok3);
22042196

2205-
tok2 = TokenList::copyTokens(tok2, argFuncRetStart, argFuncRetEnd);
2197+
tok2 = simplifyTypedefCopyTokens(tok2, argFuncRetStart, argFuncRetEnd, location);
22062198
} else if (ptrToArray || refToArray) {
2207-
tok2->insertToken("(");
2208-
tok2 = tok2->next();
2199+
tok2 = simplifyTypedefInsertToken(tok2, "(", location);
22092200
Token *tok3 = tok2;
22102201

22112202
if (ptrToArray)
2212-
tok2->insertToken("*");
2203+
tok2 = simplifyTypedefInsertToken(tok2, "*", location);
22132204
else
2214-
tok2->insertToken("&");
2215-
tok2 = tok2->next();
2205+
tok2 = simplifyTypedefInsertToken(tok2, "&", location);
22162206

22172207
bool hasName = false;
22182208
// skip over name
@@ -2231,15 +2221,14 @@ void Tokenizer::simplifyTypedefCpp()
22312221
tok2 = tok2->linkAt(1);
22322222
}
22332223

2234-
tok2->insertToken(")");
2224+
simplifyTypedefInsertToken(tok2, ")", location);
22352225
Token::createMutualLinks(tok2->next(), tok3);
22362226

22372227
if (!hasName)
22382228
tok2 = tok2->next();
22392229
} else if (ptrMember) {
22402230
if (Token::simpleMatch(tok2, "* (")) {
2241-
tok2->insertToken("*");
2242-
tok2 = tok2->next();
2231+
tok2 = simplifyTypedefInsertToken(tok2, "*", location);
22432232
} else {
22442233
// This is the case of casting operator.
22452234
// Name is not available, and () should not be
@@ -2248,38 +2237,33 @@ void Tokenizer::simplifyTypedefCpp()
22482237
Token *openParenthesis = nullptr;
22492238

22502239
if (!castOperator) {
2251-
tok2->insertToken("(");
2252-
tok2 = tok2->next();
2240+
tok2 = simplifyTypedefInsertToken(tok2, "(", location);
22532241

22542242
openParenthesis = tok2;
22552243
}
22562244

22572245
const Token *tok4 = namespaceStart;
22582246

22592247
while (tok4 != namespaceEnd) {
2260-
tok2->insertToken(tok4->str());
2261-
tok2 = tok2->next();
2248+
tok2 = simplifyTypedefInsertToken(tok2, tok4->str(), location);
22622249
tok4 = tok4->next();
22632250
}
2264-
tok2->insertToken(namespaceEnd->str());
2265-
tok2 = tok2->next();
2251+
tok2 = simplifyTypedefInsertToken(tok2, namespaceEnd->str(), location);
22662252

2267-
tok2->insertToken("*");
2268-
tok2 = tok2->next();
2253+
tok2 = simplifyTypedefInsertToken(tok2, "*", location);
22692254

22702255
if (openParenthesis) {
22712256
// Skip over name, if any
22722257
if (Token::Match(tok2->next(), "%name%"))
22732258
tok2 = tok2->next();
22742259

2275-
tok2->insertToken(")");
2276-
tok2 = tok2->next();
2260+
tok2 = simplifyTypedefInsertToken(tok2, ")", location);
22772261

22782262
Token::createMutualLinks(tok2, openParenthesis);
22792263
}
22802264
}
22812265
} else if (typeOf) {
2282-
tok2 = TokenList::copyTokens(tok2, argStart, argEnd);
2266+
tok2 = simplifyTypedefCopyTokens(tok2, argStart, argEnd, location);
22832267
} else if (Token::Match(tok2, "%name% [")) {
22842268
while (Token::Match(tok2, "%name%|] [")) {
22852269
tok2 = tok2->linkAt(1);
@@ -2301,8 +2285,7 @@ void Tokenizer::simplifyTypedefCpp()
23012285
// reference or pointer to array?
23022286
if (Token::Match(tok2, "&|*|&&")) {
23032287
tok2 = tok2->previous();
2304-
tok2->insertToken("(");
2305-
Token *tok3 = tok2->next();
2288+
Token *tok3 = simplifyTypedefInsertToken(tok2, "(", location);
23062289

23072290
// handle missing variable name
23082291
if (Token::Match(tok3, "( *|&|&& *|&|&& %name%"))
@@ -2333,8 +2316,7 @@ void Tokenizer::simplifyTypedefCpp()
23332316
tok2 = tok2->tokAt(3);
23342317
}
23352318

2336-
tok2->insertToken(")");
2337-
tok2 = tok2->next();
2319+
tok2 = simplifyTypedefInsertToken(tok2, ")", location);
23382320
Token::createMutualLinks(tok2, tok3);
23392321
}
23402322

@@ -2345,7 +2327,7 @@ void Tokenizer::simplifyTypedefCpp()
23452327
while (tok2->strAt(1) == "[")
23462328
tok2 = tok2->linkAt(1);
23472329

2348-
tok2 = TokenList::copyTokens(tok2, arrayStart, arrayEnd);
2330+
tok2 = simplifyTypedefCopyTokens(tok2, arrayStart, arrayEnd, location);
23492331
if (!tok2->next())
23502332
syntaxError(tok2);
23512333

0 commit comments

Comments
 (0)