Index: include/clang/Basic/DiagnosticASTKinds.td =================================================================== --- include/clang/Basic/DiagnosticASTKinds.td +++ include/clang/Basic/DiagnosticASTKinds.td @@ -206,7 +206,9 @@ def note_odr_tag_kind_here: Note< "%0 is a %select{struct|interface|union|class|enum}1 here">; def note_odr_field : Note<"field %0 has type %1 here">; +def note_odr_friend : Note<"friend declared here">; def note_odr_missing_field : Note<"no corresponding field here">; +def note_odr_missing_friend : Note<"no corresponding friend here">; def note_odr_bit_field : Note<"bit-field %0 with type %1 and length %2 here">; def note_odr_not_bit_field : Note<"field %0 is not a bit-field">; def note_odr_base : Note<"class has base type %0">; Index: lib/AST/ASTImporter.cpp =================================================================== --- lib/AST/ASTImporter.cpp +++ lib/AST/ASTImporter.cpp @@ -2214,8 +2214,12 @@ // Not found. Create it. FriendDecl::FriendUnion ToFU; - if (NamedDecl *FriendD = D->getFriendDecl()) - ToFU = cast_or_null(Importer.Import(FriendD)); + if (NamedDecl *FriendD = D->getFriendDecl()) { + auto ToFriendD = cast_or_null(Importer.Import(FriendD)); + if (ToFriendD && FriendD->getFriendObjectKind()) + ToFriendD->setObjectOfFriendDecl(); + ToFU = ToFriendD; + } else ToFU = Importer.Import(D->getFriendType()); if (!ToFU) @@ -2237,7 +2241,6 @@ ToTPLists); Importer.Imported(D, FrD); - RD->pushFriendDecl(FrD); FrD->setAccess(D->getAccess()); FrD->setLexicalDeclContext(LexicalDC); Index: lib/AST/ASTStructuralEquivalence.cpp =================================================================== --- lib/AST/ASTStructuralEquivalence.cpp +++ lib/AST/ASTStructuralEquivalence.cpp @@ -900,6 +900,37 @@ return false; } } + + // Check the friends for consistency. + CXXRecordDecl::friend_iterator Friend2 = D2CXX->friend_begin(), + Friend2End = D2CXX->friend_end(); + for (CXXRecordDecl::friend_iterator Friend1 = D1CXX->friend_begin(), + Friend1End = D1CXX->friend_end(); + Friend1 != Friend1End; ++Friend1, ++Friend2) { + if (Friend2 == Friend2End) { + if (Context.Complain) { + Context.Diag2(D2->getLocation(), + diag::warn_odr_tag_type_inconsistent) + << Context.ToCtx.getTypeDeclType(D2CXX); + Context.Diag1((*Friend1)->getFriendLoc(), diag::note_odr_friend); + Context.Diag2(D2->getLocation(), diag::note_odr_missing_friend); + } + return false; + } + + if (!IsStructurallyEquivalent(Context, *Friend1, *Friend2)) + return false; + } + + if (Friend2 != Friend2End) { + if (Context.Complain) { + Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) + << Context.ToCtx.getTypeDeclType(D2); + Context.Diag2((*Friend2)->getFriendLoc(), diag::note_odr_friend); + Context.Diag1(D1->getLocation(), diag::note_odr_missing_friend); + } + return false; + } } else if (D1CXX->getNumBases() > 0) { if (Context.Complain) { Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) @@ -1107,6 +1138,30 @@ D2->getTemplatedDecl()); } +static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, + FriendDecl *D1, FriendDecl *D2) { + if (D1->getFriendType() && D2->getFriendType()) { + if (!::IsStructurallyEquivalent(Context, + D1->getFriendType()->getType(), + D2->getFriendType()->getType())) + return false; + } else if (D1->getFriendDecl() && D2->getFriendDecl()) { + if (!::IsStructurallyEquivalent(Context, D1->getFriendDecl(), + D2->getFriendDecl())) + return false; + } + + return true; +} + +static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, + FunctionDecl *D1, FunctionDecl *D2) { + if (!::IsStructurallyEquivalent(Context, D1->getType(), D2->getType())) + return false; + + return true; +} + /// Determine structural equivalence of two declarations. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, Decl *D1, Decl *D2) { @@ -1301,6 +1356,27 @@ // Kind mismatch. Equivalent = false; } + } else if (FunctionDecl *FD1 = dyn_cast(D1)) { + if (FunctionDecl *FD2 = dyn_cast(D2)) { + if (!::IsStructurallyEquivalent(FD1->getIdentifier(), + FD2->getIdentifier())) + Equivalent = false; + if (!::IsStructurallyEquivalent(*this, FD1, FD2)) + Equivalent = false; + } else { + // Kind mismatch. + Equivalent = false; + } + } else if (FriendDecl *FrD1 = dyn_cast(D1)) { + if (FriendDecl *FrD2 = dyn_cast(D2)) { + if (FrD1->getFriendType() && FrD2->getFriendType()) { + if (!::IsStructurallyEquivalent(*this, FrD1, FrD2)) + Equivalent = false; + } else { + // Kind mismatch. + Equivalent = false; + } + } } if (!Equivalent) { Index: test/ASTMerge/class/Inputs/class1.cpp =================================================================== --- test/ASTMerge/class/Inputs/class1.cpp +++ test/ASTMerge/class/Inputs/class1.cpp @@ -18,3 +18,31 @@ enum E { b = 1 }; + +//Friend import tests +void f(); +int g(int a); +struct X; +struct Y; + +struct F1 { +public: + int x; + friend struct X; + friend int g(int); + friend void f(); +}; + +struct F2 { +public: + int x; + friend struct X; + friend void f(); +}; + +struct F3 { +public: + int x; + friend int g(int); + friend void f(); +}; Index: test/ASTMerge/class/Inputs/class2.cpp =================================================================== --- test/ASTMerge/class/Inputs/class2.cpp +++ test/ASTMerge/class/Inputs/class2.cpp @@ -12,3 +12,29 @@ a = 0, b = 1 }; + +//Friend import tests +void f(); +int g(int a); +struct X; +struct Y; + +struct F1 { +public: + int x; + friend struct X; + friend int g(int); + friend void f(); +}; + +struct F2 { +public: + int x; + friend struct X; +}; + +struct F3 { +public: + int x; + friend void f(); +}; Index: test/ASTMerge/class/test.cpp =================================================================== --- test/ASTMerge/class/test.cpp +++ test/ASTMerge/class/test.cpp @@ -12,3 +12,13 @@ // CHECK: class1.cpp:18:6: warning: type 'E' has incompatible definitions in different translation units // CHECK: class1.cpp:19:3: note: enumerator 'b' with value 1 here // CHECK: class2.cpp:12:3: note: enumerator 'a' with value 0 here + +// CHECK: class1.cpp:36:8: warning: type 'F2' has incompatible definitions in different translation units +// CHECK: class1.cpp:39:3: note: friend declared here +// CHECK: class2.cpp:30:8: note: no corresponding friend here + +// CHECK: class1.cpp:43:8: warning: type 'F3' has incompatible definitions in different translation units +// CHECK: class1.cpp:46:3: note: friend declared here +// CHECK: class2.cpp:36:8: note: no corresponding friend here + +// CHECK: 4 warnings generated.