@@ -173,6 +173,30 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
173
173
174
174
const bool doProgress = (mSettings .reportProgress != -1 );
175
175
176
+ std::map<Scope *, std::set<std::string>> forwardDecls;
177
+
178
+ const std::function<Scope *(const Token *, Scope *)> findForwardDeclScope = [&](const Token *tok, Scope *startScope) {
179
+ if (tok->str () == " ::" )
180
+ return findForwardDeclScope (tok->next (), &scopeList.front ());
181
+
182
+ if (Token::Match (tok, " %name% :: %name%" )) {
183
+ auto it = std::find_if (startScope->nestedList .cbegin (), startScope->nestedList .cend (), [&](const Scope *scope) {
184
+ return scope->className == tok->str ();
185
+ });
186
+
187
+ if (it == startScope->nestedList .cend ())
188
+ return static_cast <Scope *>(nullptr );
189
+
190
+ return findForwardDeclScope (tok->tokAt (2 ), *it);
191
+ }
192
+
193
+ auto it = forwardDecls.find (startScope);
194
+ if (it == forwardDecls.cend ())
195
+ return static_cast <Scope *>(nullptr );
196
+
197
+ return it->second .count (tok->str ()) > 0 ? startScope : nullptr ;
198
+ };
199
+
176
200
// find all scopes
177
201
for (const Token *tok = mTokenizer .tokens (); tok; tok = tok ? tok->next () : nullptr ) {
178
202
// #5593 suggested to add here:
@@ -291,7 +315,11 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
291
315
scope = new_scope;
292
316
tok = tok2;
293
317
} else {
294
- scopeList.emplace_back (*this , tok, scope);
318
+
319
+ const Scope *forwardDeclScope = findForwardDeclScope (tok->next (), scope);
320
+ const Scope *nestedIn = forwardDeclScope ? forwardDeclScope : scope;
321
+
322
+ scopeList.emplace_back (*this , tok, nestedIn);
295
323
new_scope = &scopeList.back ();
296
324
297
325
if (tok->str () == " class" )
@@ -303,7 +331,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
303
331
if (new_scope->isClassOrStructOrUnion () || new_scope->type == ScopeType::eEnum) {
304
332
Type* new_type = findType (name, scope);
305
333
if (!new_type) {
306
- typeList.emplace_back (new_scope->classDef , new_scope, scope );
334
+ typeList.emplace_back (new_scope->classDef , new_scope, nestedIn );
307
335
new_type = &typeList.back ();
308
336
scope->definedTypesMap [new_type->name ()] = new_type;
309
337
} else
@@ -386,6 +414,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
386
414
typeList.emplace_back (tok, nullptr , scope);
387
415
Type* new_type = &typeList.back ();
388
416
scope->definedTypesMap [new_type->name ()] = new_type;
417
+ forwardDecls[scope].insert (tok->strAt (1 ));
389
418
}
390
419
tok = tok->tokAt (2 );
391
420
}
0 commit comments