From f712c48ce80763f3c0bbc2e1b0afe6ed3b5b88cb Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Sun, 2 Sep 2012 00:13:48 +0000 Subject: [PATCH] Added a diagnostic for mismatched MS inheritance attributes. Also fixed the incomplete type member pointer size calculation under the MS ABI. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@163078 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 3 ++ lib/AST/MicrosoftCXXABI.cpp | 10 ++++ lib/Sema/SemaDeclAttr.cpp | 54 +++++++++++++++++----- lib/Sema/SemaType.cpp | 8 ---- test/SemaCXX/member-pointer-ms.cpp | 12 +++-- 5 files changed, 62 insertions(+), 25 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index e0dac62403..1e532e7b1f 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1672,6 +1672,9 @@ def warn_objc_redundant_literal_use : Warning< def err_attr_tlsmodel_arg : Error<"tls_model must be \"global-dynamic\", " "\"local-dynamic\", \"initial-exec\" or \"local-exec\"">; +def warn_ms_inheritance_already_declared : Warning< + "ignored since inheritance model was already declared as '" + "%select{single|multiple|virtual}0'">; def err_only_annotate_after_access_spec : Error< "access specifier can only have annotation attributes">; diff --git a/lib/AST/MicrosoftCXXABI.cpp b/lib/AST/MicrosoftCXXABI.cpp index 51308ea0c0..575272d357 100644 --- a/lib/AST/MicrosoftCXXABI.cpp +++ b/lib/AST/MicrosoftCXXABI.cpp @@ -55,6 +55,16 @@ public: unsigned MicrosoftCXXABI::getMemberPointerSize(const MemberPointerType *MPT) const { QualType Pointee = MPT->getPointeeType(); CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); + + if (RD->getTypeForDecl()->isIncompleteType()) { + if (RD->hasAttr()) + return 1; + else if (RD->hasAttr()) + return 2; + else + return 3; + } + if (RD->getNumVBases() > 0) { if (Pointee->isFunctionType()) return 3; diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 50d1117599..bf1c14338d 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -4149,20 +4149,50 @@ static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid"; } +static bool hasOtherInheritanceAttr(Decl *D, AttributeList::Kind Kind, + int& Existing) { + if (Kind != AttributeList::AT_SingleInheritance && + D->hasAttr()) { + Existing = 0; + return true; + } + else if (Kind != AttributeList::AT_MultipleInheritance && + D->hasAttr()) { + Existing = 1; + return true; + } + else if (Kind != AttributeList::AT_VirtualInheritance && + D->hasAttr()) { + Existing = 2; + return true; + } + return false; +} + static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { - if (S.LangOpts.MicrosoftExt) { - AttributeList::Kind Kind = Attr.getKind(); - if (Kind == AttributeList::AT_SingleInheritance) - D->addAttr( - ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context)); - else if (Kind == AttributeList::AT_MultipleInheritance) - D->addAttr( - ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context)); - else if (Kind == AttributeList::AT_VirtualInheritance) - D->addAttr( - ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context)); - } else + if (!S.LangOpts.MicrosoftExt) { S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); + return; + } + + AttributeList::Kind Kind = Attr.getKind(); + + int Existing; + if (hasOtherInheritanceAttr(D->getCanonicalDecl(), Kind, Existing)) { + S.Diag(Attr.getLoc(), diag::warn_ms_inheritance_already_declared) << Existing; + return; + } + + if (Kind == AttributeList::AT_SingleInheritance) { + D->addAttr( + ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context)); + } else if (Kind == AttributeList::AT_MultipleInheritance) { + D->addAttr( + ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context)); + } else if (Kind == AttributeList::AT_VirtualInheritance) { + D->addAttr( + ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context)); + } } static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { diff --git a/lib/Sema/SemaType.cpp b/lib/Sema/SemaType.cpp index 82dccfa5f4..68cd5cdcf8 100644 --- a/lib/Sema/SemaType.cpp +++ b/lib/Sema/SemaType.cpp @@ -1583,14 +1583,6 @@ QualType Sema::BuildMemberPointerType(QualType T, QualType Class, return QualType(); } - // In the Microsoft ABI, the class is allowed to be an incomplete - // type. In such cases, the compiler makes a worst-case assumption. - // We make no such assumption right now, so emit an error if the - // class isn't a complete type. - if (Context.getTargetInfo().getCXXABI() == CXXABI_Microsoft && - RequireCompleteType(Loc, Class, diag::err_incomplete_type)) - return QualType(); - return Context.getMemberPointerType(T, Class.getTypePtr()); } diff --git a/test/SemaCXX/member-pointer-ms.cpp b/test/SemaCXX/member-pointer-ms.cpp index 3b2d0fceb9..90618bc7dc 100644 --- a/test/SemaCXX/member-pointer-ms.cpp +++ b/test/SemaCXX/member-pointer-ms.cpp @@ -1,8 +1,4 @@ -// RUN: %clang_cc1 -cxx-abi microsoft -fsyntax-only -verify %s - -// Test that we reject pointers to members of incomplete classes (for now) -struct A; //expected-note{{forward declaration of 'A'}} -int A::*pai1; //expected-error{{incomplete type 'A'}} +// RUN: %clang_cc1 -cxx-abi microsoft -fms-compatibility -fsyntax-only -verify %s // Test that we don't allow reinterpret_casts from pointers of one size to // pointers of a different size. @@ -12,3 +8,9 @@ struct C: A, B {}; void (A::*paf)(); void (C::*pcf)() = reinterpret_cast(paf); //expected-error{{cannot reinterpret_cast from member pointer type}} + +class __single_inheritance D; +class __multiple_inheritance D; // expected-warning {{ignored since inheritance model was already declared as 'single'}} + +class __virtual_inheritance E; +class __virtual_inheritance E; // no warning expected since same attribute -- 2.40.0