[clang] [clang-format] Improve annotating star and amp (PR #194190)
Björn Schäpers via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 27 13:09:23 PDT 2026
https://github.com/HazardyKnusperkeks updated https://github.com/llvm/llvm-project/pull/194190
>From 8d502abe7b620eda822ca4228e1c0950944f2068 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <bjoern at hazardy.de>
Date: Sun, 26 Apr 2026 00:40:24 +0200
Subject: [PATCH] [clang-format] Improve annotating star and amp
Fixes #193255
---
clang/lib/Format/UnwrappedLineParser.cpp | 15 +++++++++------
clang/lib/Format/UnwrappedLineParser.h | 2 +-
clang/unittests/Format/TokenAnnotatorTest.cpp | 12 ++++++++++++
3 files changed, 22 insertions(+), 7 deletions(-)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 5629ce706ac6e..022fd62ed2bfc 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2604,11 +2604,12 @@ bool UnwrappedLineParser::parseBracedList(bool IsAngleBracket, bool IsEnum) {
}
/// Parses a pair of parentheses (and everything between them).
-/// \param AmpAmpTokenType If different than TT_Unknown sets this type for all
-/// double ampersands. This applies for all nested scopes as well.
+/// \param StarAndAmpTokenType If different than TT_Unknown sets this type for
+/// all (double) ampersands and stars. This applies for all nested scopes as
+/// well.
///
/// Returns whether there is a `=` token between the parentheses.
-bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType,
+bool UnwrappedLineParser::parseParens(TokenType StarAndAmpTokenType,
bool InMacroCall) {
assert(FormatTok->is(tok::l_paren) && "'(' expected.");
auto *LParen = FormatTok;
@@ -2623,7 +2624,7 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType,
do {
switch (FormatTok->Tok.getKind()) {
case tok::l_paren:
- if (parseParens(AmpAmpTokenType, InMacroCall))
+ if (parseParens(StarAndAmpTokenType, InMacroCall))
SeenEqual = true;
if (Style.isJava() && FormatTok->is(tok::l_brace))
parseChildBlock();
@@ -2741,9 +2742,11 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType,
case tok::kw_requires:
parseRequiresExpression();
break;
+ case tok::star:
+ case tok::amp:
case tok::ampamp:
- if (AmpAmpTokenType != TT_Unknown)
- FormatTok->setFinalizedType(AmpAmpTokenType);
+ if (StarAndAmpTokenType != TT_Unknown)
+ FormatTok->setFinalizedType(StarAndAmpTokenType);
[[fallthrough]];
default:
nextToken();
diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h
index f3c1b70147db8..8181eec1495f7 100644
--- a/clang/lib/Format/UnwrappedLineParser.h
+++ b/clang/lib/Format/UnwrappedLineParser.h
@@ -145,7 +145,7 @@ class UnwrappedLineParser {
bool *HasLabel = nullptr);
bool tryToParseBracedList();
bool parseBracedList(bool IsAngleBracket = false, bool IsEnum = false);
- bool parseParens(TokenType AmpAmpTokenType = TT_Unknown,
+ bool parseParens(TokenType StarAndAmpTokenType = TT_Unknown,
bool InMacroCall = false);
void parseSquare(bool LambdaIntroducer = false);
void keepAncestorBraces();
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 2db546b2060c0..48ae9e144cc2a 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -433,6 +433,18 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
EXPECT_TOKEN(Tokens[7], tok::r_paren, TT_Unknown); // Not TT_CastRParen
EXPECT_TOKEN(Tokens[10], tok::amp, TT_PointerOrReference);
EXPECT_TOKEN(Tokens[11], tok::identifier, TT_StartOfName);
+
+ Tokens = annotate("template <typename T>\n"
+ "concept C = requires(T t) { [](auto &c) {}(t); };");
+ ASSERT_EQ(Tokens.size(), 30u) << Tokens;
+ EXPECT_TOKEN(Tokens[18], tok::amp, TT_PointerOrReference);
+ EXPECT_TOKEN(Tokens[19], tok::identifier, TT_StartOfName);
+
+ Tokens = annotate("template <typename T>\n"
+ "concept C = requires(T t) { [](auto *c) {}(t); };");
+ ASSERT_EQ(Tokens.size(), 30u) << Tokens;
+ EXPECT_TOKEN(Tokens[18], tok::star, TT_PointerOrReference);
+ EXPECT_TOKEN(Tokens[19], tok::identifier, TT_StartOfName);
}
TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {
More information about the cfe-commits
mailing list