[clang] [Clang] [Sema] Error on reference types inside a union with msvc 1900+ (PR #102851)
Max Winkler via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 13 18:51:06 PDT 2024
https://github.com/MaxEW707 updated https://github.com/llvm/llvm-project/pull/102851
>From e6b925894066656a2773278a093dbf709ba66319 Mon Sep 17 00:00:00 2001
From: MaxEW707 <max.enrico.winkler at gmail.com>
Date: Sun, 11 Aug 2024 22:37:55 -0700
Subject: [PATCH 1/3] Error on reference inside a union with msvc 1910+
---
clang/lib/Sema/SemaDecl.cpp | 14 +++++++++-----
clang/test/SemaCXX/MicrosoftExtensions.cpp | 21 ++++++++++++++++-----
2 files changed, 25 insertions(+), 10 deletions(-)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 694a754646f274..0386560e337618 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -18467,11 +18467,15 @@ FieldDecl *Sema::CheckFieldDecl(DeclarationName Name, QualType T,
// the program is ill-formed, except when compiling with MSVC extensions
// enabled.
if (EltTy->isReferenceType()) {
- Diag(NewFD->getLocation(), getLangOpts().MicrosoftExt ?
- diag::ext_union_member_of_reference_type :
- diag::err_union_member_of_reference_type)
- << NewFD->getDeclName() << EltTy;
- if (!getLangOpts().MicrosoftExt)
+ const bool HaveMSExt =
+ getLangOpts().MicrosoftExt &&
+ !getLangOpts().isCompatibleWithMSVC(LangOptions::MSVC2017);
+
+ Diag(NewFD->getLocation(),
+ HaveMSExt ? diag::ext_union_member_of_reference_type
+ : diag::err_union_member_of_reference_type)
+ << NewFD->getDeclName() << EltTy;
+ if (!HaveMSExt)
NewFD->setInvalidDecl();
}
}
diff --git a/clang/test/SemaCXX/MicrosoftExtensions.cpp b/clang/test/SemaCXX/MicrosoftExtensions.cpp
index 98c19975095bbe..8601f32f064b33 100644
--- a/clang/test/SemaCXX/MicrosoftExtensions.cpp
+++ b/clang/test/SemaCXX/MicrosoftExtensions.cpp
@@ -3,6 +3,8 @@
// RUN: %clang_cc1 -std=c++11 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify=expected,precxx17 -fms-extensions -fexceptions -fcxx-exceptions -DTEST1
// RUN: %clang_cc1 -std=c++14 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify=expected,precxx17 -fexceptions -fcxx-exceptions -DTEST2
// RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -std=c++11 -fms-compatibility -verify -DTEST3
+// RUN: %clang_cc1 -std=c++17 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -verify -fms-extensions -fms-compatibility-version=19.00 -DTEST4
+// RUN: %clang_cc1 -std=c++17 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -verify -fms-extensions -fms-compatibility-version=19.10 -DTEST5
#if TEST1
@@ -384,11 +386,6 @@ void TestSP9() {
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}}
-};
-
// Property getter using reference.
struct SP11 {
__declspec(property(get=GetV)) int V;
@@ -619,6 +616,20 @@ template<typename T> struct A {};
template<typename T> struct B : A<A<T>> { A<T>::C::D d; }; // expected-warning {{implicit 'typename' is a C++20 extension}}
}
+#elif TEST4
+
+union u {
+ int *i1;
+ int &i2; // expected-warning {{union member 'i2' has reference type 'int &', which is a Microsoft extension}}
+};
+
+#elif TEST5
+
+union u {
+ int *i1;
+ int &i2; // expected-error {{union member 'i2' has reference type 'int &'}}
+};
+
#else
#error Unknown test mode
>From 114a0417e0a8d975168b01f6c33477056b4588ad Mon Sep 17 00:00:00 2001
From: MaxEW707 <max.enrico.winkler at gmail.com>
Date: Mon, 12 Aug 2024 21:48:53 -0700
Subject: [PATCH 2/3] Switch to VS2015
---
clang/lib/Sema/SemaDecl.cpp | 2 +-
clang/test/SemaCXX/MicrosoftExtensions.cpp | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 0386560e337618..d90b9eca4ab0a1 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -18469,7 +18469,7 @@ FieldDecl *Sema::CheckFieldDecl(DeclarationName Name, QualType T,
if (EltTy->isReferenceType()) {
const bool HaveMSExt =
getLangOpts().MicrosoftExt &&
- !getLangOpts().isCompatibleWithMSVC(LangOptions::MSVC2017);
+ !getLangOpts().isCompatibleWithMSVC(LangOptions::MSVC2015);
Diag(NewFD->getLocation(),
HaveMSExt ? diag::ext_union_member_of_reference_type
diff --git a/clang/test/SemaCXX/MicrosoftExtensions.cpp b/clang/test/SemaCXX/MicrosoftExtensions.cpp
index 8601f32f064b33..08e3af34d45d6d 100644
--- a/clang/test/SemaCXX/MicrosoftExtensions.cpp
+++ b/clang/test/SemaCXX/MicrosoftExtensions.cpp
@@ -3,8 +3,8 @@
// RUN: %clang_cc1 -std=c++11 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify=expected,precxx17 -fms-extensions -fexceptions -fcxx-exceptions -DTEST1
// RUN: %clang_cc1 -std=c++14 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify=expected,precxx17 -fexceptions -fcxx-exceptions -DTEST2
// RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -std=c++11 -fms-compatibility -verify -DTEST3
-// RUN: %clang_cc1 -std=c++17 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -verify -fms-extensions -fms-compatibility-version=19.00 -DTEST4
-// RUN: %clang_cc1 -std=c++17 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -verify -fms-extensions -fms-compatibility-version=19.10 -DTEST5
+// RUN: %clang_cc1 -std=c++17 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -verify -fms-extensions -fms-compatibility-version=18.00 -DTEST4
+// RUN: %clang_cc1 -std=c++17 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -verify -fms-extensions -fms-compatibility-version=19.00 -DTEST5
#if TEST1
>From 033414c6d45abd989b2235c46a995d8173804fcd Mon Sep 17 00:00:00 2001
From: MaxEW707 <max.enrico.winkler at gmail.com>
Date: Tue, 13 Aug 2024 18:50:53 -0700
Subject: [PATCH 3/3] Update release notes
---
clang/docs/ReleaseNotes.rst | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3c2e0282d1c72d..9cccd1b0d948af 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -220,6 +220,12 @@ Android Support
Windows Support
^^^^^^^^^^^^^^^
+- Clang no longer allows references inside a union when emulating MSVC 1900+ even if `fms-extensions` is enabled.
+ Starting with VS2015, MSVC 1900, this Microsoft extension is no longer allowed and always results in an error.
+ Clang now follows the MSVC behavior in this scenario.
+ When `-fms-compatibility-version=18.00` or prior is set on the command line this Microsoft extension is still
+ allowed as VS2013 and prior allow it.
+
LoongArch Support
^^^^^^^^^^^^^^^^^
More information about the cfe-commits
mailing list