From: Aaron Ballman Date: Thu, 30 May 2013 16:20:00 +0000 (+0000) Subject: Microsoft has a language extension which allows union members to be X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=76eed42f99158082fae5b903f46dfb6355e66f95;p=clang Microsoft has a language extension which allows union members to be references. What's more, they use this language extension in their ATL header files (which come as part of MFC and the Win32 SDK). This patch implements support for the Microsoft extension, and addresses PR13737. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@182936 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 46351da12f..aff02f30fc 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1179,6 +1179,9 @@ def ext_static_data_member_in_union : ExtWarn< def warn_cxx98_compat_static_data_member_in_union : Warning< "static data member %0 in union is incompatible with C++98">, InGroup, DefaultIgnore; +def ext_union_member_of_reference_type : ExtWarn< + "union member %0 has reference type %1, which is a Microsoft extension">, + InGroup; def err_union_member_of_reference_type : Error< "union member %0 has reference type %1">; def ext_anonymous_struct_union_qualified : Extension< diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 05748f72f4..7be376e60f 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -10684,11 +10684,15 @@ FieldDecl *Sema::CheckFieldDecl(DeclarationName Name, QualType T, } // C++ [class.union]p1: If a union contains a member of reference type, - // the program is ill-formed. + // the program is ill-formed, except when compiling with MSVC extensions + // enabled. if (EltTy->isReferenceType()) { - Diag(NewFD->getLocation(), diag::err_union_member_of_reference_type) + Diag(NewFD->getLocation(), getLangOpts().MicrosoftExt ? + diag::ext_union_member_of_reference_type : + diag::err_union_member_of_reference_type) << NewFD->getDeclName() << EltTy; - NewFD->setInvalidDecl(); + if (!getLangOpts().MicrosoftExt) + NewFD->setInvalidDecl(); } } } diff --git a/test/SemaCXX/MicrosoftExtensions.cpp b/test/SemaCXX/MicrosoftExtensions.cpp index ab3ff69f27..93a6d302ef 100644 --- a/test/SemaCXX/MicrosoftExtensions.cpp +++ b/test/SemaCXX/MicrosoftExtensions.cpp @@ -333,3 +333,8 @@ void TestSP9() { c3.g(); // Overloaded incdec op operand c3.h(); // Overloaded unary op operand } + +union u { + int *i1; + int &i2; // expected-warning {{union member 'i2' has reference type 'int &', which is a Microsoft extension}} +};