]> granicus.if.org Git - clang/commitdiff
For compatibility with MSVC, a friend declaration also act as a forward declaration...
authorFrancois Pichet <pichet2000@gmail.com>
Tue, 31 May 2011 11:44:00 +0000 (11:44 +0000)
committerFrancois Pichet <pichet2000@gmail.com>
Tue, 31 May 2011 11:44:00 +0000 (11:44 +0000)
Example:

class A {
  friend class B;
  B* b;
};
B* global_b;

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

include/clang/Sema/Sema.h
lib/Sema/SemaDecl.cpp
test/SemaCXX/MicrosoftExtensions.cpp

index a3dd687e3a123dc0c95f3e77311687dc5bffca82..8e14207793f59dc8e75b49aea727d95b229b23f2 100644 (file)
@@ -1084,6 +1084,11 @@ public:
                  bool &OwnedDecl, bool &IsDependent, bool ScopedEnum,
                  bool ScopedEnumUsesClassTag, TypeResult UnderlyingType);
 
+  void InjectMicrosoftFriendForwardDeclaration(unsigned TagSpec,
+                                               SourceLocation KWLoc,
+                                               IdentifierInfo *Name,
+                                               SourceLocation NameLoc);
+
   Decl *ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc,
                                 unsigned TagSpec, SourceLocation TagLoc,
                                 CXXScopeSpec &SS,
index 5a3f6328cac95d5ab7d0441173c4e2da6c855ccf..878c21776d8b38d63096993cf5c3b450caa4b4fb 100644 (file)
@@ -6631,6 +6631,40 @@ bool Sema::isAcceptableTagRedeclaration(const TagDecl *Previous,
   return false;
 }
 
+/// InjectMicrosoftFriendForwardDeclaration - For compatibility with MSVC,
+/// a friend declaration also act as a forward declaration if the tag name
+/// is not already declared. The tag name is declared in the next outermost
+/// non record scope.
+/// Example:
+///
+/// class A {
+///   friend class B;
+///   B* b;
+///  };
+///  B* global_b;
+void Sema::InjectMicrosoftFriendForwardDeclaration(unsigned TagSpec,
+                                                   SourceLocation KWLoc,
+                                                   IdentifierInfo *Name,
+                                                   SourceLocation NameLoc) {
+  NamedDecl *N = LookupSingleName(getCurScope(), Name, NameLoc,
+                                  Sema::LookupTagName);
+  if (!N) {
+    DeclContext *ContextToAdd = CurContext;
+    while (ContextToAdd->isRecord())
+      ContextToAdd = ContextToAdd->getParent();
+
+    TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
+    TagDecl *New = CXXRecordDecl::Create(Context, Kind, ContextToAdd, KWLoc,
+                                         NameLoc, Name, 0);
+    if (getCurScope()->getFnParent())
+      PushOnScopeChains(New, getScopeForContext(ContextToAdd), true);
+    else
+      ContextToAdd->addDecl(New);
+  } 
+}
+
+
 /// ActOnTag - This is invoked when we see 'struct foo' or 'struct {'.  In the
 /// former case, Name will be non-null.  In the later case, Name will be null.
 /// TagSpec indicates what kind of tag this is. TUK indicates whether this is a
@@ -6648,6 +6682,9 @@ Decl *Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
          "Nameless record must be a definition!");
   assert(TemplateParameterLists.size() == 0 || TUK != TUK_Reference);
 
+  if (getLangOptions().Microsoft && TUK == Sema::TUK_Friend)
+    InjectMicrosoftFriendForwardDeclaration(TagSpec, KWLoc, Name, NameLoc);
+
   OwnedDecl = false;
   TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
 
index 22a06673b951689d8bc99caf6ba59e57e65581b8..6d058f20311418b25bff1335ff9153517e8fa4b8 100644 (file)
@@ -226,4 +226,29 @@ private:
   using B::f; // expected-warning {{using declaration refers to inaccessible member 'ms_using_declaration_bug::B::f', which refers to accessible member 'ms_using_declaration_bug::A::f', accepted for Microsoft compatibility}}
 };
 
+}
+
+
+
+namespace friend_as_a_forward_decl {
+
+class A {
+  class Nested {
+    friend class B;
+    B* b;
+  };
+  B* b;
+};
+B* global_b;
+
+
+void f()
+{
+  class Local {
+    friend class Z;
+    Z* b;
+  };
+  Z* b;
+}
+
 }
\ No newline at end of file