[PATCH] Turn error about fastcall variadic function into warning in MS mode (PR12535)

Hans Wennborg hans at chromium.org
Tue Oct 8 14:22:23 PDT 2013


Hi rnk,

Previously we'd throw an error for declaring a variadic function with __fastcall calling convention.

It seems msvc accepts this though, and silently falls back to __cdecl.

For compatibility with msvc, this patch turns the error into a warning in MS mode and makes sure (we did this already, but I added a test) that we fall back to __cdecl.


I did note that Clang doesn't warn or error about declaring a variadic function with __stdcall or __thiscall. I'll look into that in a separate patch.

http://llvm-reviews.chandlerc.com/D1861

Files:
  include/clang/Basic/DiagnosticSemaKinds.td
  lib/Sema/SemaType.cpp
  test/CodeGen/microsoft-call-conv.c
  test/Sema/callingconv.c

Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -2053,6 +2053,9 @@
   "function with no prototype cannot use %0 calling convention">;
 def err_cconv_varargs : Error<
   "variadic function cannot use %0 calling convention">;
+def warn_cconv_varargs : Warning<
+  "%0 calling convention ignored on variadic function">,
+  InGroup<IgnoredAttributes>;
 def err_regparm_mismatch : Error<"function declared with regparm(%0) "
   "attribute was previously declared "
   "%plural{0:without the regparm|:with the regparm(%1)}1 attribute">;
Index: lib/Sema/SemaType.cpp
===================================================================
--- lib/Sema/SemaType.cpp
+++ lib/Sema/SemaType.cpp
@@ -4553,7 +4553,11 @@
 
     const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
     if (FnP->isVariadic()) {
-      S.Diag(attr.getLoc(), diag::err_cconv_varargs)
+      // In MS compatibility mode, this is just a warning.
+      const LangOptions &L = S.getLangOpts();
+      unsigned DiagID = L.MicrosoftMode ? diag::warn_cconv_varargs
+                                        : diag::err_cconv_varargs;
+      S.Diag(attr.getLoc(), DiagID)
         << FunctionType::getNameForCallConv(CC);
       attr.setInvalid();
       return true;
Index: test/CodeGen/microsoft-call-conv.c
===================================================================
--- test/CodeGen/microsoft-call-conv.c
+++ test/CodeGen/microsoft-call-conv.c
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -triple i386-pc-linux -emit-llvm < %s | FileCheck %s
+// RUN: %clang_cc1 -triple i386-pc-linux -emit-llvm -fms-compatibility -DWIN < %s | FileCheck --check-prefix=WIN %s
 
 void __fastcall f1(void);
 void __stdcall f2(void);
@@ -48,3 +49,11 @@
   f7(0);
   // CHECK: call x86_stdcallcc void @f7(i32 0)
 }
+
+// PR12535
+#ifdef WIN
+void __fastcall f9(int x, int y) {};
+// WIN: define x86_fastcallcc void @f9({{.*}})
+void __fastcall f10(int x, ...) {};
+// WIN: define void @f10({{.*}})
+#endif
Index: test/Sema/callingconv.c
===================================================================
--- test/Sema/callingconv.c
+++ test/Sema/callingconv.c
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 %s -fsyntax-only -triple i386-unknown-unknown -verify
+// RUN: %clang_cc1 %s -fsyntax-only -triple i386-unknown-unknown -fms-compatibility -DWIN -verify
 
 void __attribute__((fastcall)) foo(float *a) {
 }
@@ -15,8 +16,13 @@
 void __attribute__((fastcall)) test1(void) {
 }
 
+#ifdef WIN
+void __attribute__((fastcall)) test2(int a, ...) { // expected-warning {{fastcall calling convention ignored on variadic function}}
+}
+#else
 void __attribute__((fastcall)) test2(int a, ...) { // expected-error {{variadic function cannot use fastcall calling convention}}
 }
+#endif
 
 void __attribute__((cdecl)) ctest0() {}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1861.1.patch
Type: text/x-patch
Size: 2926 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131008/a80c3625/attachment.bin>


More information about the cfe-commits mailing list