r179067 - <rdar://problem/13540899> Collect using directives from all of the semantic contexts not represented by scopes.

Douglas Gregor dgregor at apple.com
Mon Apr 8 16:11:25 PDT 2013


Author: dgregor
Date: Mon Apr  8 18:11:25 2013
New Revision: 179067

URL: http://llvm.org/viewvc/llvm-project?rev=179067&view=rev
Log:
<rdar://problem/13540899> Collect using directives from all of the semantic contexts not represented by scopes.

This fixes a regression I introduced in r178136, where we would not
consider the using directives from the semantic declaration contexts
that aren't represented by the lexical scopes (Scope) when performing
unqualified name lookup. This lead to horribly funny diagnostics like
"no identifier named 'foo'; did you mean 'foo'?".


Modified:
    cfe/trunk/lib/Sema/SemaLookup.cpp
    cfe/trunk/test/CXX/basic/basic.lookup/basic.lookup.unqual/p14.cpp

Modified: cfe/trunk/lib/Sema/SemaLookup.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaLookup.cpp?rev=179067&r1=179066&r2=179067&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaLookup.cpp (original)
+++ cfe/trunk/lib/Sema/SemaLookup.cpp Mon Apr  8 18:11:25 2013
@@ -884,6 +884,8 @@ bool Sema::CppLookupName(LookupResult &R
   //   }
   // }
   //
+  UnqualUsingDirectiveSet UDirs;
+  bool VisitedUsingDirectives = false;
   DeclContext *OutsideOfTemplateParamDC = 0;
   for (; S && !isNamespaceOrTranslationUnitScope(S); S = S->getParent()) {
     DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity());
@@ -957,9 +959,24 @@ bool Sema::CppLookupName(LookupResult &R
         // If this is a file context, we need to perform unqualified name
         // lookup considering using directives.
         if (Ctx->isFileContext()) {
-          UnqualUsingDirectiveSet UDirs;
-          UDirs.visit(Ctx, Ctx);
-          UDirs.done();
+          // If we haven't handled using directives yet, do so now.
+          if (!VisitedUsingDirectives) {
+            // Add using directives from this context up to the top level.
+            for (DeclContext *UCtx = Ctx; UCtx; UCtx = UCtx->getParent())
+              UDirs.visit(UCtx, UCtx);
+
+            // Find the innermost file scope, so we can add using directives
+            // from local scopes.
+            Scope *InnermostFileScope = S;
+            while (InnermostFileScope &&
+                   !isNamespaceOrTranslationUnitScope(InnermostFileScope))
+              InnermostFileScope = InnermostFileScope->getParent();
+            UDirs.visitScopeChain(Initial, InnermostFileScope);
+
+            UDirs.done();
+
+            VisitedUsingDirectives = true;
+          }
 
           if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) {
             R.resolveKind();
@@ -994,11 +1011,11 @@ bool Sema::CppLookupName(LookupResult &R
   //
   // FIXME: Cache this sorted list in Scope structure, and DeclContext, so we
   // don't build it for each lookup!
-
-  UnqualUsingDirectiveSet UDirs;
-  UDirs.visitScopeChain(Initial, S);
-  UDirs.done();
-
+  if (!VisitedUsingDirectives) {
+    UDirs.visitScopeChain(Initial, S);
+    UDirs.done();
+  }
+  
   // Lookup namespace scope, and global scope.
   // Unqualified name lookup in C++ requires looking into scopes
   // that aren't strictly lexical, and therefore we walk through the

Modified: cfe/trunk/test/CXX/basic/basic.lookup/basic.lookup.unqual/p14.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/basic/basic.lookup/basic.lookup.unqual/p14.cpp?rev=179067&r1=179066&r2=179067&view=diff
==============================================================================
--- cfe/trunk/test/CXX/basic/basic.lookup/basic.lookup.unqual/p14.cpp (original)
+++ cfe/trunk/test/CXX/basic/basic.lookup/basic.lookup.unqual/p14.cpp Mon Apr  8 18:11:25 2013
@@ -47,3 +47,22 @@ class Other {
 void Other::foo(YFloat a, YFloat b) {
   YFloat c = a - b;
 }
+
+// <rdar://problem/13540899>
+namespace Other {
+  void other_foo();
+}
+
+namespace M2 {
+  using namespace Other;
+
+  namespace MInner {
+    class Bar { 
+      void bar();
+    };
+  }
+}
+
+void M2::MInner::Bar::bar() {
+  other_foo();
+}





More information about the cfe-commits mailing list