]> granicus.if.org Git - clang/commitdiff
Put friend decls in the correct context.
authorRafael Espindola <rafael.espindola@gmail.com>
Thu, 25 Apr 2013 20:12:36 +0000 (20:12 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Thu, 25 Apr 2013 20:12:36 +0000 (20:12 +0000)
When we find a friend declaration we have to skip transparent contexts for doing
lookups, but we should not skip them when inserting the new decl if the lookup
found nothing.

Fixes PR15841.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@180571 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDeclCXX.cpp
test/SemaCXX/linkage2.cpp

index d18b9d3c43526b07638c812ddfe5f1c2e74f58ba..b5fb85856c8103fac0c5075f4a5785b4fdb21ec4 100644 (file)
@@ -11131,29 +11131,39 @@ NamedDecl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D,
 
     // Find the appropriate context according to the above.
     DC = CurContext;
+
+    // Skip class contexts.  If someone can cite chapter and verse
+    // for this behavior, that would be nice --- it's what GCC and
+    // EDG do, and it seems like a reasonable intent, but the spec
+    // really only says that checks for unqualified existing
+    // declarations should stop at the nearest enclosing namespace,
+    // not that they should only consider the nearest enclosing
+    // namespace.
+    while (DC->isRecord())
+      DC = DC->getParent();
+
+    DeclContext *LookupDC = DC;
+    while (LookupDC->isTransparentContext())
+      LookupDC = LookupDC->getParent();
+
     while (true) {
-      // Skip class contexts.  If someone can cite chapter and verse
-      // for this behavior, that would be nice --- it's what GCC and
-      // EDG do, and it seems like a reasonable intent, but the spec
-      // really only says that checks for unqualified existing
-      // declarations should stop at the nearest enclosing namespace,
-      // not that they should only consider the nearest enclosing
-      // namespace.
-      while (DC->isRecord() || DC->isTransparentContext()) 
-        DC = DC->getParent();
-
-      LookupQualifiedName(Previous, DC);
+      LookupQualifiedName(Previous, LookupDC);
 
       // TODO: decide what we think about using declarations.
-      if (isLocal || !Previous.empty())
+      if (isLocal)
         break;
 
+      if (!Previous.empty()) {
+        DC = LookupDC;
+        break;
+      }
+
       if (isTemplateId) {
-        if (isa<TranslationUnitDecl>(DC)) break;
+        if (isa<TranslationUnitDecl>(LookupDC)) break;
       } else {
-        if (DC->isFileContext()) break;
+        if (LookupDC->isFileContext()) break;
       }
-      DC = DC->getParent();
+      LookupDC = LookupDC->getParent();
     }
 
     DCScope = getScopeForDeclContext(S, DC);
index ddf4064215d783af8affe1df7d90dc2585d3854a..3cfa98138babb3e4e0875cbadfd4617524d05b6d 100644 (file)
@@ -152,3 +152,15 @@ namespace test15 {
   const int a = 5; // expected-note {{previous definition is here}}
   static const int a; // expected-error {{redefinition of 'a'}}
 }
+
+namespace test16 {
+  extern "C" {
+    class Foo {
+      int x;
+      friend int bar(Foo *y);
+    };
+    int bar(Foo *y) {
+      return y->x;
+    }
+  }
+}