https://github.com/edtice-goog created 
https://github.com/llvm/llvm-project/pull/246874

The Microsoft C++ ABI has no calling conventions on constructors. MSVC ignores 
one written on a constructor, and
Clang already does too when it is on the first declaration 
(`Sema::adjustMemberFunctionCC`). An out-of-line
definition that names a calling convention after a declaration that didn't was 
still rejected as a calling
convention mismatch:

```c++
struct S { S(); };
__cdecl S::S() {}   // error: function declared 'cdecl' here was previously 
declared without calling convention
```

MSVC accepts this, with or without `/permissive-`.

When the target uses the Microsoft C++ ABI, `Sema::MergeFunctionDecl` now 
treats such a redeclaration as
`adjustMemberFunctionCC` treats a first declaration. The previous declaration's 
calling convention is kept, and the
existing `-Wignored-attributes` warning ("%0 calling convention is not 
supported on constructor/destructor") is
emitted. As in `adjustMemberFunctionCC`, there is no warning for `__stdcall`. 
Other member functions and other C++
ABIs (MinGW included) are unchanged. Out-of-line destructor definitions are 
already accepted.

Tests: new cases in `clang/test/SemaCXX/decl-microsoft-call-conv.cpp`. They 
cover `__stdcall`, `__cdecl` and
`__fastcall` on out-of-line constructor definitions, which are accepted (with 
the warning, except `__stdcall`) for
i686-pc-win32 and are still errors for i686-pc-mingw32. A non-constructor 
member still has to match. A release note
is added under Windows Support.

🤖 Generated with [Claude Code](https://claude.com/claude-code)


>From 6e2913f43f5a8a17ce9fa7a0e12a97f592c49a25 Mon Sep 17 00:00:00 2001
From: Ed Tice <[email protected]>
Date: Thu, 26 Nov 2026 11:41:22 -0500
Subject: [PATCH] Ignore calling conventions on out-of-line constructor
 definitions

The Microsoft C++ ABI has no calling conventions on constructors. MSVC
ignores one written on a constructor, and Clang already does too when it
is on the first declaration (Sema::adjustMemberFunctionCC). An out-of-line
definition that names a calling convention after a declaration that did
not was still rejected as a calling convention mismatch:

  struct S { S(); };
  __cdecl S::S() {}  // error: function declared 'cdecl' here was
                     // previously declared without calling convention

MSVC accepts this, with or without /permissive-.

When the target uses the Microsoft C++ ABI, MergeFunctionDecl now treats
such a redeclaration as adjustMemberFunctionCC treats a first declaration:
the previous calling convention is kept and the existing
-Wignored-attributes warning is emitted, except for __stdcall, which
adjustMemberFunctionCC also accepts without a warning. Other member
functions and other C++ ABIs (MinGW included) are unchanged. Out-of-line
destructor definitions are already accepted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 clang/docs/ReleaseNotes.md                    |  5 ++++
 clang/lib/Sema/SemaDecl.cpp                   | 11 +++++++++
 .../test/SemaCXX/decl-microsoft-call-conv.cpp | 24 +++++++++++++++++++
 3 files changed, 40 insertions(+)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 6bbc910d86b5aa..6673cad4cc806a 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1505,6 +1505,11 @@ documentation](CIR/index.md) for more information 
regarding ClangIR's status and
 
 #### Windows Support
 
+- When targeting the Microsoft C++ ABI, Clang now ignores a calling convention
+  on an out-of-line constructor definition, as it already did on
+  the declaration and as MSVC does, instead of rejecting it as a calling
+  convention mismatch.
+
 - `clang-cl` now supports `/std:c++23` in accordance with the release of MSVC
   Build Tools version 14.52. `/std:c++23preview` is now an alias of 
`/std:c++23`.
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 63c581d3fb4f34..b066ab6ea85b9a 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -3962,6 +3962,17 @@ bool Sema::MergeFunctionDecl(FunctionDecl *New, 
NamedDecl *&OldD, Scope *S,
           << (int)CallingConventionIgnoredReason::BuiltinFunction;
       NewTypeInfo = NewTypeInfo.withCallingConv(OldTypeInfo.getCC());
       RequiresAdjustment = true;
+    } else if (Context.getTargetInfo().getCXXABI().isMicrosoft() &&
+               isa<CXXConstructorDecl>(New)) {
+      // MSVC ignores calling conventions on constructors, also on an
+      // out-of-line definition after a declaration without one. Do as
+      // adjustMemberFunctionCC does for the first declaration.
+      if (NewTypeInfo.getCC() != CC_X86StdCall)
+        Diag(New->getLocation(), diag::warn_cconv_unsupported)
+            << FunctionType::getNameForCallConv(NewTypeInfo.getCC())
+            << (int)CallingConventionIgnoredReason::ConstructorDestructor;
+      NewTypeInfo = NewTypeInfo.withCallingConv(OldTypeInfo.getCC());
+      RequiresAdjustment = true;
     } else {
       // Calling conventions aren't compatible, so complain.
       bool FirstCCExplicit = getCallingConvAttributedType(First->getType());
diff --git a/clang/test/SemaCXX/decl-microsoft-call-conv.cpp 
b/clang/test/SemaCXX/decl-microsoft-call-conv.cpp
index b399d8e35bd4fb..8342df15ce8112 100644
--- a/clang/test/SemaCXX/decl-microsoft-call-conv.cpp
+++ b/clang/test/SemaCXX/decl-microsoft-call-conv.cpp
@@ -111,6 +111,30 @@ void            S::member_variadic_cdecl(int x, ...) { 
(void)x; }
 void __cdecl    S::static_member_variadic_default(int x, ...) { (void)x; }
 void            S::static_member_variadic_cdecl(int x, ...) { (void)x; }
 
+// The MS ABI also ignores a calling convention on an out-of-line constructor
+// definition that follows a declaration without one.
+struct StructorRedecl {
+  StructorRedecl();        // #StructorRedecl-default
+  StructorRedecl(int);     // #StructorRedecl-int
+  StructorRedecl(char);    // #StructorRedecl-char
+  void member();           // #StructorRedecl-member
+};
+#ifdef MSABI
+__stdcall  StructorRedecl::StructorRedecl() {}
+__cdecl    StructorRedecl::StructorRedecl(int) {} // expected-warning {{cdecl 
calling convention is not supported on constructor/destructor}}
+__fastcall StructorRedecl::StructorRedecl(char) {} // expected-warning 
{{fastcall calling convention is not supported on constructor/destructor}}
+#else
+__stdcall  StructorRedecl::StructorRedecl() {}    // expected-error {{function 
declared 'stdcall' here was previously declared without calling convention}}
+                                                  // 
expected-note@#StructorRedecl-default {{previous declaration is here}}
+__cdecl    StructorRedecl::StructorRedecl(int) {} // expected-error {{function 
declared 'cdecl' here was previously declared without calling convention}}
+                                                  // 
expected-note@#StructorRedecl-int {{previous declaration is here}}
+__fastcall StructorRedecl::StructorRedecl(char) {} // expected-error 
{{function declared 'fastcall' here was previously declared without calling 
convention}}
+                                                  // 
expected-note@#StructorRedecl-char {{previous declaration is here}}
+#endif
+// Other member functions still have to match.
+__stdcall void StructorRedecl::member() {}        // expected-error {{function 
declared 'stdcall' here was previously declared without calling convention}}
+                                                  // 
expected-note@#StructorRedecl-member {{previous declaration is here}}
+
 // Declare a template using a calling convention.
 template <class CharT> inline int __cdecl mystrlen(const CharT *str) {
   int i;

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to