From 54022952450beff428a30ef5adfb82874063603d Mon Sep 17 00:00:00 2001
From: Douglas Gregor <dgregor@apple.com>
Date: Fri, 30 Apr 2010 07:08:38 +0000
Subject: [PATCH] Fix ADL for types declared in transparent decls, from Alp
 Toker!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@102695 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/Sema/SemaLookup.cpp                       | 34 +++++++++----------
 .../basic.lookup/basic.lookup.argdep/p2.cpp   | 15 ++++++++
 2 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/lib/Sema/SemaLookup.cpp b/lib/Sema/SemaLookup.cpp
index 1b2401a80cb..bdbbc119c61 100644
--- a/lib/Sema/SemaLookup.cpp
+++ b/lib/Sema/SemaLookup.cpp
@@ -1386,10 +1386,18 @@ addAssociatedClassesAndNamespaces(QualType T,
                           Sema::AssociatedNamespaceSet &AssociatedNamespaces,
                                   Sema::AssociatedClassSet &AssociatedClasses);
 
-static void CollectNamespace(Sema::AssociatedNamespaceSet &Namespaces,
-                             DeclContext *Ctx) {
+static void CollectEnclosingNamespace(Sema::AssociatedNamespaceSet &Namespaces,
+                                      DeclContext *Ctx) {
+  // Add the associated namespace for this class.
+
+  // We don't use DeclContext::getEnclosingNamespaceContext() as this may
+  // be a locally scoped record.
+
+  while (Ctx->isRecord() || Ctx->isTransparentContext())
+    Ctx = Ctx->getParent();
+
   if (Ctx->isFileContext())
-    Namespaces.insert(Ctx);
+    Namespaces.insert(Ctx->getPrimaryContext());
 }
 
 // \brief Add the associated classes and namespaces for argument-dependent
@@ -1425,9 +1433,7 @@ addAssociatedClassesAndNamespaces(const TemplateArgument &Arg,
         if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
           AssociatedClasses.insert(EnclosingClass);
         // Add the associated namespace for this class.
-        while (Ctx->isRecord())
-          Ctx = Ctx->getParent();
-        CollectNamespace(AssociatedNamespaces, Ctx);
+        CollectEnclosingNamespace(AssociatedNamespaces, Ctx);
       }
       break;
     }
@@ -1471,9 +1477,7 @@ addAssociatedClassesAndNamespaces(CXXRecordDecl *Class,
   if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
     AssociatedClasses.insert(EnclosingClass);
   // Add the associated namespace for this class.
-  while (Ctx->isRecord())
-    Ctx = Ctx->getParent();
-  CollectNamespace(AssociatedNamespaces, Ctx);
+  CollectEnclosingNamespace(AssociatedNamespaces, Ctx);
 
   // Add the class itself. If we've already seen this class, we don't
   // need to visit base classes.
@@ -1495,9 +1499,7 @@ addAssociatedClassesAndNamespaces(CXXRecordDecl *Class,
     if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
       AssociatedClasses.insert(EnclosingClass);
     // Add the associated namespace for this class.
-    while (Ctx->isRecord())
-      Ctx = Ctx->getParent();
-    CollectNamespace(AssociatedNamespaces, Ctx);
+    CollectEnclosingNamespace(AssociatedNamespaces, Ctx);
 
     const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
     for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
@@ -1538,9 +1540,7 @@ addAssociatedClassesAndNamespaces(CXXRecordDecl *Class,
       if (AssociatedClasses.insert(BaseDecl)) {
         // Find the associated namespace for this base class.
         DeclContext *BaseCtx = BaseDecl->getDeclContext();
-        while (BaseCtx->isRecord())
-          BaseCtx = BaseCtx->getParent();
-        CollectNamespace(AssociatedNamespaces, BaseCtx);
+        CollectEnclosingNamespace(AssociatedNamespaces, BaseCtx);
 
         // Make sure we visit the bases of this base class.
         if (BaseDecl->bases_begin() != BaseDecl->bases_end())
@@ -1615,9 +1615,7 @@ addAssociatedClassesAndNamespaces(QualType T,
       AssociatedClasses.insert(EnclosingClass);
 
     // Add the associated namespace for this class.
-    while (Ctx->isRecord())
-      Ctx = Ctx->getParent();
-    CollectNamespace(AssociatedNamespaces, Ctx);
+    CollectEnclosingNamespace(AssociatedNamespaces, Ctx);
 
     return;
   }
diff --git a/test/CXX/basic/basic.lookup/basic.lookup.argdep/p2.cpp b/test/CXX/basic/basic.lookup/basic.lookup.argdep/p2.cpp
index ee01416c7b1..0e262f3eb1d 100644
--- a/test/CXX/basic/basic.lookup/basic.lookup.argdep/p2.cpp
+++ b/test/CXX/basic/basic.lookup/basic.lookup.argdep/p2.cpp
@@ -71,3 +71,18 @@ namespace O {
   }
 }
 
+extern "C" {
+  struct L { };
+}
+
+void h(L); // expected-note{{candidate function}}
+
+namespace P {
+  void h(L); // expected-note{{candidate function}}
+  void test_transparent_context_adl(L l) {
+    {
+      h(l); // expected-error {{call to 'h' is ambiguous}}
+    }
+  }
+}
+
-- 
GitLab