[clang] [clang-format] Fix Microsoft calling convensions preventing function names from being marked TT_StartOfName (PR #143047)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 5 16:39:33 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Ben Dunkin (bdunkin)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/143047.diff
3 Files Affected:
- (modified) clang/lib/Format/FormatToken.h (+1)
- (modified) clang/lib/Format/TokenAnnotator.cpp (+15)
- (modified) clang/unittests/Format/FormatTest.cpp (+12)
``````````diff
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"
``````````
</details>
https://github.com/llvm/llvm-project/pull/143047
More information about the cfe-commits
mailing list