[clang] 7558262 - [clang-format] Improve annotating star and amp (#194190)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 28 11:49:22 PDT 2026
Author: Björn Schäpers
Date: 2026-04-28T20:49:17+02:00
New Revision: 7558262ea2a696745596540ed96fed97193865d2
URL: https://github.com/llvm/llvm-project/commit/7558262ea2a696745596540ed96fed97193865d2
DIFF: https://github.com/llvm/llvm-project/commit/7558262ea2a696745596540ed96fed97193865d2.diff
LOG: [clang-format] Improve annotating star and amp (#194190)
Use the context better.
Fixes #193255
Added:
Modified:
clang/lib/Format/UnwrappedLineParser.cpp
clang/lib/Format/UnwrappedLineParser.h
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
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
diff erent than TT_Unknown sets this type for all
-/// double ampersands. This applies for all nested scopes as well.
+/// \param StarAndAmpTokenType If
diff erent 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