[clang] 2ec7f63 - [clang-cl] [Sema] Do not prefer integral conversion over floating-to-integral for MS compatibility 19.28 and higher.

Marek Kurdej via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 1 23:58:27 PDT 2021


Author: Marek Kurdej
Date: 2021-04-02T08:58:22+02:00
New Revision: 2ec7f639c49fdee8956a09994d1cf99ef4282746

URL: https://github.com/llvm/llvm-project/commit/2ec7f639c49fdee8956a09994d1cf99ef4282746
DIFF: https://github.com/llvm/llvm-project/commit/2ec7f639c49fdee8956a09994d1cf99ef4282746.diff

LOG: [clang-cl] [Sema] Do not prefer integral conversion over floating-to-integral for MS compatibility 19.28 and higher.

As of MSVC 19.28 (2019 Update 8), integral conversion is no longer preferred over floating-to-integral, and so MSVC is more standard conformant and will generate a compiler error on ambiguous call.
Cf. https://godbolt.org/z/E8xsdqKsb.
Initially found during the review of D99641.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D99663

Added: 
    

Modified: 
    clang/include/clang/Basic/LangOptions.h
    clang/lib/Sema/SemaOverload.cpp
    clang/test/SemaCXX/MicrosoftCompatibility.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h
index c797167f8fd26..c898ef19a6cfd 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -124,6 +124,7 @@ class LangOptions : public LangOptionsBase {
     MSVC2017_5 = 1912,
     MSVC2017_7 = 1914,
     MSVC2019 = 1920,
+    MSVC2019_8 = 1928,
   };
 
   enum SYCLMajorVersion {

diff  --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index d7ec41bdacd9f..67a867762d69c 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -4106,7 +4106,7 @@ CompareStandardConversionSequences(Sema &S, SourceLocation Loc,
     }
   }
 
-  // In Microsoft mode, prefer an integral conversion to a
+  // In Microsoft mode (below 19.28), prefer an integral conversion to a
   // floating-to-integral conversion if the integral conversion
   // is between types of the same size.
   // For example:
@@ -4118,7 +4118,9 @@ CompareStandardConversionSequences(Sema &S, SourceLocation Loc,
   // }
   // Here, MSVC will call f(int) instead of generating a compile error
   // as clang will do in standard mode.
-  if (S.getLangOpts().MSVCCompat && SCS1.Second == ICK_Integral_Conversion &&
+  if (S.getLangOpts().MSVCCompat &&
+      !S.getLangOpts().isCompatibleWithMSVC(LangOptions::MSVC2019_8) &&
+      SCS1.Second == ICK_Integral_Conversion &&
       SCS2.Second == ICK_Floating_Integral &&
       S.Context.getTypeSize(SCS1.getFromType()) ==
           S.Context.getTypeSize(SCS1.getToType(2)))

diff  --git a/clang/test/SemaCXX/MicrosoftCompatibility.cpp b/clang/test/SemaCXX/MicrosoftCompatibility.cpp
index 8c0a000da07cb..20d478d1b5910 100644
--- a/clang/test/SemaCXX/MicrosoftCompatibility.cpp
+++ b/clang/test/SemaCXX/MicrosoftCompatibility.cpp
@@ -1,3 +1,5 @@
+// RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -std=c++11 -Wmicrosoft -verify -fms-compatibility -fexceptions -fcxx-exceptions -fms-compatibility-version=19.28
+// RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -std=c++11 -Wmicrosoft -verify -fms-compatibility -fexceptions -fcxx-exceptions -fms-compatibility-version=19.27
 // RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -std=c++11 -Wmicrosoft -verify -fms-compatibility -fexceptions -fcxx-exceptions -fms-compatibility-version=19.00
 // RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -std=c++11 -Wmicrosoft -verify -fms-compatibility -fexceptions -fcxx-exceptions -fms-compatibility-version=18.00
 
@@ -28,12 +30,20 @@ namespace ms_conversion_rules {
 
 void f(float a);
 void f(int a);
+#if _MSC_VER >= 1928
+// expected-note at -3 2 {{candidate function}}
+// expected-note at -3 2 {{candidate function}}
+#endif
 
 void test()
 {
     long a = 0;
     f((long)0);
-	f(a);
+    f(a);
+#if _MSC_VER >= 1928
+// expected-error at -3 {{call to 'f' is ambiguous}}
+// expected-error at -3 {{call to 'f' is ambiguous}}
+#endif
 }
 
 }


        


More information about the cfe-commits mailing list