From 08d2fa0a782163cba3078b3a873b477a9ecbb3f8 Mon Sep 17 00:00:00 2001 From: Francois Pichet Date: Sun, 18 Sep 2011 21:37:37 +0000 Subject: [PATCH] In Microsoft mode(-fms-compatibility), prefer an integral conversion to a floating-to-integral conversion if the integral conversion is between types of the same size. For example: void f(float); void f(int); int main { long a; f(a); } Here, MSVC will call f(int) instead of generating a compile error as clang will do in standard mode. This fixes a few errors when parsing MFC code with clang. llvm-svn: 140007 --- clang/lib/Sema/SemaOverload.cpp | 19 +++++++++++++++++++ clang/test/SemaCXX/MicrosoftCompatibility.cpp | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 clang/test/SemaCXX/MicrosoftCompatibility.cpp diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index a2ef403618fb..c084f76a50ba 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -2866,6 +2866,25 @@ CompareStandardConversionSequences(Sema &S, } } + // In Microsoft mode, prefer an integral conversion to a + // floating-to-integral conversion if the integral conversion + // is between types of the same size. + // For example: + // void f(float); + // void f(int); + // int main { + // long a; + // f(a); + // } + // Here, MSVC will call f(int) instead of generating a compile error + // as clang will do in standard mode. + if (S.getLangOptions().MicrosoftMode && + SCS1.Second == ICK_Integral_Conversion && + SCS2.Second == ICK_Floating_Integral && + S.Context.getTypeSize(SCS1.getFromType()) == + S.Context.getTypeSize(SCS1.getToType(2))) + return ImplicitConversionSequence::Better; + return ImplicitConversionSequence::Indistinguishable; } diff --git a/clang/test/SemaCXX/MicrosoftCompatibility.cpp b/clang/test/SemaCXX/MicrosoftCompatibility.cpp new file mode 100644 index 000000000000..fa4ed3ebefff --- /dev/null +++ b/clang/test/SemaCXX/MicrosoftCompatibility.cpp @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -verify -fms-compatibility + + + +namespace ms_conversion_rules { + +void f(float a); +void f(int a); + +void test() +{ + long a = 0; + f((long)0); + f(a); +} + +} +