diff --git a/src/libs/cplusplus/LookupContext.cpp b/src/libs/cplusplus/LookupContext.cpp
index f5a0edd66bc9c780cdf285e76e81a9c69dbc0e18..6e019ee73407f6abbcc38a70be2b3cf973ceaeeb 100644
--- a/src/libs/cplusplus/LookupContext.cpp
+++ b/src/libs/cplusplus/LookupContext.cpp
@@ -60,6 +60,17 @@ bool LookupContext::isNameCompatibleWithIdentifier(Name *name, Identifier *id)
     return false;
 }
 
+#ifndef CPLUSPLUS_WITH_NO_DEBUG
+static void printScopes(const QList<Scope *> &scopes)
+{
+    qDebug() << "===========";
+    foreach (Scope *scope, scopes) {
+        qDebug() << "scope:" << scope << scope->owner()->name() << scope->owner()->fileName()
+                << scope->owner()->line() << scope->owner()->column();
+    }
+}
+#endif
+
 /////////////////////////////////////////////////////////////////////
 // LookupContext
 /////////////////////////////////////////////////////////////////////
@@ -270,33 +281,38 @@ QList<Symbol *> LookupContext::resolve(Name *name, const QList<Scope *> &visible
     return candidates;
 }
 
+void LookupContext::buildVisibleScopes_helper(Document::Ptr doc, QList<Scope *> *scopes,
+                                              QSet<QString> *processed)
+{
+    if (doc && ! processed->contains(doc->fileName())) {
+        processed->insert(doc->fileName());
+
+        if (doc->globalSymbolCount())
+            scopes->append(doc->globalSymbols());
+
+        foreach (const Document::Include &incl, doc->includes()) {
+            buildVisibleScopes_helper(_documents.value(incl.fileName()),
+                                      scopes, processed);
+        }
+    }
+}
+
 QList<Scope *> LookupContext::buildVisibleScopes()
 {
     QList<Scope *> scopes;
 
     if (_symbol) {
         for (Scope *scope = _symbol->scope(); scope; scope = scope->enclosingScope()) {
+            if (scope == _thisDocument->globalSymbols())
+                break;
+
             scopes.append(scope);
         }
     }
 
     QSet<QString> processed;
-    processed.insert(_thisDocument->fileName());
-
-    QList<QString> todo = _thisDocument->includedFiles();
-    while (! todo.isEmpty()) {
-        QString fn = todo.last();
-        todo.removeLast();
-
-        if (processed.contains(fn))
-            continue;
-
-        processed.insert(fn);
-        if (Document::Ptr doc = document(fn)) {
-            scopes.append(doc->globalNamespace()->members());
-            todo += doc->includedFiles();
-        }
-    }
+    buildVisibleScopes_helper(_thisDocument, &scopes, &processed);
+    return scopes;
 
     while (true) {
         QList<Scope *> expandedScopes;
diff --git a/src/libs/cplusplus/LookupContext.h b/src/libs/cplusplus/LookupContext.h
index f0cbb7aded6c7fdeb28b6e4b163255149231d3ca..a75f9e013f09baeab5b214b5030b5a1e9a5d55bf 100644
--- a/src/libs/cplusplus/LookupContext.h
+++ b/src/libs/cplusplus/LookupContext.h
@@ -128,6 +128,10 @@ public:
 
 private:
     QList<Scope *> buildVisibleScopes();
+
+    void buildVisibleScopes_helper(Document::Ptr doc, QList<Scope *> *scopes,
+                                   QSet<QString> *processed);
+
     static bool isNameCompatibleWithIdentifier(Name *name, Identifier *id);
 
     static bool maybeValidSymbol(Symbol *symbol,