[clang] [clang-format] Handle function decls with MS calling conventions (PR #143083)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 5 23:51:50 PDT 2025
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/143083
None
>From 6f319be22826718c82b027c605885d6b95ed5689 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Thu, 5 Jun 2025 23:48:47 -0700
Subject: [PATCH] [clang-format] Handle function decls with MS calling
conventions
---
clang/lib/Format/TokenAnnotator.cpp | 10 +++++++++-
clang/unittests/Format/TokenAnnotatorTest.cpp | 5 +++++
2 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 37ab40ca97bff..aed1672afac66 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1769,7 +1769,15 @@ class AnnotatingParser {
Keywords.kw___has_include_next)) {
parseHasInclude();
}
- if (Style.isCSharp()) {
+ if (IsCpp) {
+ if (Next && Next->is(tok::l_paren) && Prev &&
+ Prev->isOneOf(tok::kw___cdecl, tok::kw___stdcall,
+ tok::kw___fastcall, tok::kw___thiscall,
+ tok::kw___regcall, tok::kw___vectorcall)) {
+ Tok->setFinalizedType(TT_FunctionDeclarationName);
+ Next->setFinalizedType(TT_FunctionDeclarationLParen);
+ }
+ } else if (Style.isCSharp()) {
if (Tok->is(Keywords.kw_where) && Next && Next->isNot(tok::l_paren)) {
Tok->setType(TT_CSharpGenericTypeConstraint);
parseCSharpGenericTypeConstraint();
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 9d62ff8d39a77..d64e34de1fcf4 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2273,6 +2273,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) {
EXPECT_TOKEN(Tokens[16], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[18], tok::arrow, TT_TrailingReturnArrow);
+ Tokens = annotate("void __stdcall f();");
+ ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+ EXPECT_TOKEN(Tokens[2], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_FunctionDeclarationLParen);
+
Tokens = annotate("int iso_time(time_t);");
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
More information about the cfe-commits
mailing list