[clang] [clang-format] Fix Microsoft calling convensions preventing function names from being marked TT_StartOfName (PR #143047)

Ben Dunkin via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 5 16:38:36 PDT 2025


https://github.com/bdunkin created https://github.com/llvm/llvm-project/pull/143047

This fixes the `SpaceBeforeParensOptions.AfterFunctionDeclarationName` and `SpaceBeforeParensOptions.AfterFunctionDefinitionName` options not adding spaces when the function has an explicit Microsoft calling convention.

Attribution Note - I have been authorized to contribute this change on behalf of my company: ArenaNet LLC

>From 7c3f0f8fb12eae866bc3812f103384a6cacb0dcf Mon Sep 17 00:00:00 2001
From: Ben Dunkin <bdunkin at arena.net>
Date: Thu, 5 Jun 2025 16:12:49 -0700
Subject: [PATCH] Fix Microsoft calling convensions preventing function names
 from being marked TT_StartOfName

---
 clang/lib/Format/FormatToken.h        |  1 +
 clang/lib/Format/TokenAnnotator.cpp   | 15 +++++++++++++++
 clang/unittests/Format/FormatTest.cpp | 12 ++++++++++++
 3 files changed, 28 insertions(+)

diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 94014aee3221f..ef1c129b29b0c 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -114,6 +114,7 @@ namespace format {
   TYPE(LineComment)                                                            \
   TYPE(MacroBlockBegin)                                                        \
   TYPE(MacroBlockEnd)                                                          \
+  TYPE(MicrosoftCallingConvention)                                             \
   TYPE(ModulePartitionColon)                                                   \
   TYPE(NamespaceLBrace)                                                        \
   TYPE(NamespaceMacro)                                                         \
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index da279d07b5918..4e23478058499 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1802,6 +1802,14 @@ class AnnotatingParser {
       if (Style.isTableGen() && !parseTableGenValue())
         return false;
       break;
+    case tok::kw___cdecl:
+    case tok::kw___stdcall:
+    case tok::kw___fastcall:
+    case tok::kw___thiscall:
+    case tok::kw___regcall:
+    case tok::kw___vectorcall:
+      Tok->setType(TT_MicrosoftCallingConvention);
+      break;
     default:
       break;
     }
@@ -2611,6 +2619,13 @@ class AnnotatingParser {
     // Skip "const" as it does not have an influence on whether this is a name.
     FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
 
+    // Skip Microsoft calling conventions, as they come before the function
+    // name, but after the return type
+    while (PreviousNotConst &&
+           PreviousNotConst->is(TT_MicrosoftCallingConvention)) {
+      PreviousNotConst = PreviousNotConst->getPreviousNonComment();
+    }
+
     // For javascript const can be like "let" or "var"
     if (!Style.isJavaScript())
       while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index c0633ba3c29b3..0f96b0f92cdb2 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -17504,6 +17504,12 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
   verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
   verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
   verifyFormat("void __attribute__((asdf)) f ();", SpaceFuncDecl);
+  verifyFormat("void __stdcall f ();", SpaceFuncDecl);
+  verifyFormat("void __cdecl f ();", SpaceFuncDecl);
+  verifyFormat("void __fastcall f ();", SpaceFuncDecl);
+  verifyFormat("void __stdcall f() {}", SpaceFuncDecl);
+  verifyFormat("void __cdecl f() {}", SpaceFuncDecl);
+  verifyFormat("void __fastcall f() {}", SpaceFuncDecl);
   verifyFormat("#define A(x) x", SpaceFuncDecl);
   verifyFormat("#define A (x) x", SpaceFuncDecl);
   verifyFormat("#if defined(x)\n"
@@ -17540,6 +17546,12 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
   verifyFormat("A::A () : a(1) {}", SpaceFuncDef);
   verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
   verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef);
+  verifyFormat("void __stdcall f();", SpaceFuncDef);
+  verifyFormat("void __cdecl f();", SpaceFuncDef);
+  verifyFormat("void __fastcall f();", SpaceFuncDef);
+  verifyFormat("void __stdcall f () {}", SpaceFuncDef);
+  verifyFormat("void __cdecl f () {}", SpaceFuncDef);
+  verifyFormat("void __fastcall f () {}", SpaceFuncDef);
   verifyFormat("#define A(x) x", SpaceFuncDef);
   verifyFormat("#define A (x) x", SpaceFuncDef);
   verifyFormat("#if defined(x)\n"



More information about the cfe-commits mailing list