@@ -1180,6 +1180,24 @@ void Tokenizer::simplifyTypedef()
1180
1180
simplifyTypedefCpp ();
1181
1181
}
1182
1182
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
+
1183
1201
// TODO: rename - it is not C++ specific
1184
1202
void Tokenizer::simplifyTypedefCpp ()
1185
1203
{
@@ -1999,14 +2017,13 @@ void Tokenizer::simplifyTypedefCpp()
1999
2017
const bool isPointerTypeCall = !inOperator && Token::Match (tok2, " %name% ( )" ) && !pointers.empty ();
2000
2018
2001
2019
// 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;
2003
2021
for (Token* tok3 = typeStart; tok3 && (tok3->str () != " ;" ); tok3 = tok3->next ())
2004
2022
tok3->isSimplifiedTypedef (true );
2005
2023
if (isPointerTypeCall) {
2006
2024
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);
2010
2027
}
2011
2028
if (Token::Match (tok2->tokAt (-1 ), " class|struct|union" ) && tok2->strAt (-1 ) == typeStart->str ())
2012
2029
tok2->deletePrevious ();
@@ -2018,15 +2035,12 @@ void Tokenizer::simplifyTypedefCpp()
2018
2035
tok2 = tok2->previous ();
2019
2036
2020
2037
if (globalScope) {
2021
- replStart = tok2->insertToken (" ::" );
2022
- tok2 = tok2->next ();
2038
+ tok2 = simplifyTypedefInsertToken (tok2, " ::" , location);
2023
2039
}
2024
2040
2025
2041
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);
2030
2044
}
2031
2045
}
2032
2046
@@ -2043,11 +2057,11 @@ void Tokenizer::simplifyTypedefCpp()
2043
2057
std::string::size_type spaceIdx = 0 ;
2044
2058
std::string::size_type startIdx = 0 ;
2045
2059
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 );
2047
2061
startIdx = spaceIdx + 1 ;
2048
2062
}
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 );
2051
2065
break ;
2052
2066
}
2053
2067
idx = removed1.rfind (" ::" );
@@ -2057,30 +2071,24 @@ void Tokenizer::simplifyTypedefCpp()
2057
2071
removed1.resize (idx);
2058
2072
}
2059
2073
}
2060
- replStart->isSimplifiedTypedef (true );
2061
2074
Token* constTok = Token::simpleMatch (tok2->previous (), " const" ) ? tok2->previous () : nullptr ;
2062
2075
// add remainder of type
2063
- tok2 = TokenList::copyTokens (tok2, typeStart->next (), typeEnd);
2076
+ tok2 = simplifyTypedefCopyTokens (tok2, typeStart->next (), typeEnd, location );
2064
2077
2065
2078
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);
2071
2082
if (constTok) {
2072
2083
constTok->deleteThis ();
2073
- tok2->insertToken (" const" );
2074
- tok2->isSimplifiedTypedef (true );
2075
- tok2 = tok2->next ();
2084
+ tok2 = simplifyTypedefInsertToken (tok2, " const" , location);
2076
2085
}
2077
2086
}
2078
2087
2079
2088
if (funcStart && funcEnd) {
2080
- tok2->insertToken (" (" );
2081
- tok2 = tok2->next ();
2089
+ tok2 = simplifyTypedefInsertToken (tok2, " (" , location);
2082
2090
Token *paren = tok2;
2083
- tok2 = TokenList::copyTokens (tok2, funcStart, funcEnd);
2091
+ tok2 = simplifyTypedefCopyTokens (tok2, funcStart, funcEnd, location );
2084
2092
2085
2093
if (!inCast)
2086
2094
tok2 = processFunc (tok2, inOperator);
@@ -2091,20 +2099,17 @@ void Tokenizer::simplifyTypedefCpp()
2091
2099
while (Token::Match (tok2, " %name%|] [" ))
2092
2100
tok2 = tok2->linkAt (1 );
2093
2101
2094
- tok2->insertToken (" )" );
2095
- tok2 = tok2->next ();
2102
+ tok2 = simplifyTypedefInsertToken (tok2, " )" , location);
2096
2103
Token::createMutualLinks (tok2, paren);
2097
2104
2098
- tok2 = TokenList::copyTokens (tok2, argStart, argEnd);
2105
+ tok2 = simplifyTypedefCopyTokens (tok2, argStart, argEnd, location );
2099
2106
2100
2107
if (specStart) {
2101
2108
Token *spec = specStart;
2102
- tok2->insertToken (spec->str ());
2103
- tok2 = tok2->next ();
2109
+ tok2 = simplifyTypedefInsertToken (tok2, spec->str (), location);
2104
2110
while (spec != specEnd) {
2105
2111
spec = spec->next ();
2106
- tok2->insertToken (spec->str ());
2107
- tok2 = tok2->next ();
2112
+ tok2 = simplifyTypedefInsertToken (tok2, spec->str (), location);
2108
2113
}
2109
2114
}
2110
2115
}
@@ -2116,24 +2121,20 @@ void Tokenizer::simplifyTypedefCpp()
2116
2121
if (!inTemplate && function && tok2->next () && tok2->strAt (1 ) != " *" )
2117
2122
needParen = false ;
2118
2123
if (needParen) {
2119
- tok2->insertToken (" (" );
2120
- tok2 = tok2->next ();
2124
+ tok2 = simplifyTypedefInsertToken (tok2, " (" , location);
2121
2125
}
2122
2126
Token *tok3 = tok2;
2123
2127
if (namespaceStart) {
2124
2128
const Token *tok4 = namespaceStart;
2125
2129
2126
2130
while (tok4 != namespaceEnd) {
2127
- tok2->insertToken (tok4->str ());
2128
- tok2 = tok2->next ();
2131
+ tok2 = simplifyTypedefInsertToken (tok2, tok4->str (), location);
2129
2132
tok4 = tok4->next ();
2130
2133
}
2131
- tok2->insertToken (namespaceEnd->str ());
2132
- tok2 = tok2->next ();
2134
+ tok2 = simplifyTypedefInsertToken (tok2, namespaceEnd->str (), location);
2133
2135
}
2134
2136
if (functionPtr) {
2135
- tok2->insertToken (" *" );
2136
- tok2 = tok2->next ();
2137
+ tok2 = simplifyTypedefInsertToken (tok2, " *" , location);
2137
2138
}
2138
2139
2139
2140
if (!inCast)
@@ -2143,42 +2144,35 @@ void Tokenizer::simplifyTypedefCpp()
2143
2144
if (!tok2)
2144
2145
syntaxError (nullptr );
2145
2146
2146
- tok2->insertToken (" )" );
2147
- tok2 = tok2->next ();
2147
+ tok2 = simplifyTypedefInsertToken (tok2, " )" , location);
2148
2148
Token::createMutualLinks (tok2, tok3);
2149
2149
}
2150
2150
if (!tok2)
2151
2151
syntaxError (nullptr );
2152
2152
2153
- tok2 = TokenList::copyTokens (tok2, argStart, argEnd);
2153
+ tok2 = simplifyTypedefCopyTokens (tok2, argStart, argEnd, location );
2154
2154
if (inTemplate) {
2155
2155
tok2 = tok2->next ();
2156
2156
}
2157
2157
2158
2158
if (specStart) {
2159
2159
Token *spec = specStart;
2160
- tok2->insertToken (spec->str ());
2161
- tok2 = tok2->next ();
2160
+ tok2 = simplifyTypedefInsertToken (tok2, spec->str (), location);
2162
2161
while (spec != specEnd) {
2163
2162
spec = spec->next ();
2164
- tok2->insertToken (spec->str ());
2165
- tok2 = tok2->next ();
2163
+ tok2 = simplifyTypedefInsertToken (tok2, spec->str (), location);
2166
2164
}
2167
2165
}
2168
2166
} else if (functionRetFuncPtr || functionPtrRetFuncPtr) {
2169
- tok2->insertToken (" (" );
2170
- tok2 = tok2->next ();
2167
+ tok2 = simplifyTypedefInsertToken (tok2, " (" , location);
2171
2168
Token *tok3 = tok2;
2172
- tok2->insertToken (" *" );
2173
- tok2 = tok2->next ();
2169
+ tok2 = simplifyTypedefInsertToken (tok2, " *" , location);
2174
2170
2175
2171
Token * tok4 = nullptr ;
2176
2172
if (functionPtrRetFuncPtr) {
2177
- tok2->insertToken (" (" );
2178
- tok2 = tok2->next ();
2173
+ tok2 = simplifyTypedefInsertToken (tok2, " (" , location);
2179
2174
tok4 = tok2;
2180
- tok2->insertToken (" *" );
2181
- tok2 = tok2->next ();
2175
+ tok2 = simplifyTypedefInsertToken (tok2, " *" , location);
2182
2176
}
2183
2177
2184
2178
// skip over variable name if there
@@ -2191,28 +2185,24 @@ void Tokenizer::simplifyTypedefCpp()
2191
2185
}
2192
2186
2193
2187
if (tok4 && functionPtrRetFuncPtr) {
2194
- tok2->insertToken (" )" );
2195
- tok2 = tok2->next ();
2188
+ tok2 = simplifyTypedefInsertToken (tok2," )" , location);
2196
2189
Token::createMutualLinks (tok2, tok4);
2197
2190
}
2198
2191
2199
- tok2 = TokenList::copyTokens (tok2, argStart, argEnd);
2192
+ tok2 = simplifyTypedefCopyTokens (tok2, argStart, argEnd, location );
2200
2193
2201
- tok2->insertToken (" )" );
2202
- tok2 = tok2->next ();
2194
+ tok2 = simplifyTypedefInsertToken (tok2, " )" , location);
2203
2195
Token::createMutualLinks (tok2, tok3);
2204
2196
2205
- tok2 = TokenList::copyTokens (tok2, argFuncRetStart, argFuncRetEnd);
2197
+ tok2 = simplifyTypedefCopyTokens (tok2, argFuncRetStart, argFuncRetEnd, location );
2206
2198
} else if (ptrToArray || refToArray) {
2207
- tok2->insertToken (" (" );
2208
- tok2 = tok2->next ();
2199
+ tok2 = simplifyTypedefInsertToken (tok2, " (" , location);
2209
2200
Token *tok3 = tok2;
2210
2201
2211
2202
if (ptrToArray)
2212
- tok2-> insertToken ( " *" );
2203
+ tok2 = simplifyTypedefInsertToken (tok2, " *" , location );
2213
2204
else
2214
- tok2->insertToken (" &" );
2215
- tok2 = tok2->next ();
2205
+ tok2 = simplifyTypedefInsertToken (tok2, " &" , location);
2216
2206
2217
2207
bool hasName = false ;
2218
2208
// skip over name
@@ -2231,15 +2221,14 @@ void Tokenizer::simplifyTypedefCpp()
2231
2221
tok2 = tok2->linkAt (1 );
2232
2222
}
2233
2223
2234
- tok2-> insertToken ( " )" );
2224
+ simplifyTypedefInsertToken (tok2, " )" , location );
2235
2225
Token::createMutualLinks (tok2->next (), tok3);
2236
2226
2237
2227
if (!hasName)
2238
2228
tok2 = tok2->next ();
2239
2229
} else if (ptrMember) {
2240
2230
if (Token::simpleMatch (tok2, " * (" )) {
2241
- tok2->insertToken (" *" );
2242
- tok2 = tok2->next ();
2231
+ tok2 = simplifyTypedefInsertToken (tok2, " *" , location);
2243
2232
} else {
2244
2233
// This is the case of casting operator.
2245
2234
// Name is not available, and () should not be
@@ -2248,38 +2237,33 @@ void Tokenizer::simplifyTypedefCpp()
2248
2237
Token *openParenthesis = nullptr ;
2249
2238
2250
2239
if (!castOperator) {
2251
- tok2->insertToken (" (" );
2252
- tok2 = tok2->next ();
2240
+ tok2 = simplifyTypedefInsertToken (tok2, " (" , location);
2253
2241
2254
2242
openParenthesis = tok2;
2255
2243
}
2256
2244
2257
2245
const Token *tok4 = namespaceStart;
2258
2246
2259
2247
while (tok4 != namespaceEnd) {
2260
- tok2->insertToken (tok4->str ());
2261
- tok2 = tok2->next ();
2248
+ tok2 = simplifyTypedefInsertToken (tok2, tok4->str (), location);
2262
2249
tok4 = tok4->next ();
2263
2250
}
2264
- tok2->insertToken (namespaceEnd->str ());
2265
- tok2 = tok2->next ();
2251
+ tok2 = simplifyTypedefInsertToken (tok2, namespaceEnd->str (), location);
2266
2252
2267
- tok2->insertToken (" *" );
2268
- tok2 = tok2->next ();
2253
+ tok2 = simplifyTypedefInsertToken (tok2, " *" , location);
2269
2254
2270
2255
if (openParenthesis) {
2271
2256
// Skip over name, if any
2272
2257
if (Token::Match (tok2->next (), " %name%" ))
2273
2258
tok2 = tok2->next ();
2274
2259
2275
- tok2->insertToken (" )" );
2276
- tok2 = tok2->next ();
2260
+ tok2 = simplifyTypedefInsertToken (tok2, " )" , location);
2277
2261
2278
2262
Token::createMutualLinks (tok2, openParenthesis);
2279
2263
}
2280
2264
}
2281
2265
} else if (typeOf) {
2282
- tok2 = TokenList::copyTokens (tok2, argStart, argEnd);
2266
+ tok2 = simplifyTypedefCopyTokens (tok2, argStart, argEnd, location );
2283
2267
} else if (Token::Match (tok2, " %name% [" )) {
2284
2268
while (Token::Match (tok2, " %name%|] [" )) {
2285
2269
tok2 = tok2->linkAt (1 );
@@ -2301,8 +2285,7 @@ void Tokenizer::simplifyTypedefCpp()
2301
2285
// reference or pointer to array?
2302
2286
if (Token::Match (tok2, " &|*|&&" )) {
2303
2287
tok2 = tok2->previous ();
2304
- tok2->insertToken (" (" );
2305
- Token *tok3 = tok2->next ();
2288
+ Token *tok3 = simplifyTypedefInsertToken (tok2, " (" , location);
2306
2289
2307
2290
// handle missing variable name
2308
2291
if (Token::Match (tok3, " ( *|&|&& *|&|&& %name%" ))
@@ -2333,8 +2316,7 @@ void Tokenizer::simplifyTypedefCpp()
2333
2316
tok2 = tok2->tokAt (3 );
2334
2317
}
2335
2318
2336
- tok2->insertToken (" )" );
2337
- tok2 = tok2->next ();
2319
+ tok2 = simplifyTypedefInsertToken (tok2, " )" , location);
2338
2320
Token::createMutualLinks (tok2, tok3);
2339
2321
}
2340
2322
@@ -2345,7 +2327,7 @@ void Tokenizer::simplifyTypedefCpp()
2345
2327
while (tok2->strAt (1 ) == " [" )
2346
2328
tok2 = tok2->linkAt (1 );
2347
2329
2348
- tok2 = TokenList::copyTokens (tok2, arrayStart, arrayEnd);
2330
+ tok2 = simplifyTypedefCopyTokens (tok2, arrayStart, arrayEnd, location );
2349
2331
if (!tok2->next ())
2350
2332
syntaxError (tok2);
2351
2333
0 commit comments