Skip to content

Commit bb4f6c4

Browse files
committed
[clang-format] Treat NTTP default values as expressions
clang-format already has logic to threat the right-hand side of an equals sign. This patch applies that logic to template defaults, which are likely to be non-template type parameters in which case the default value should be annotated as an expression. This should mostly only ever apply to bool and &&. Fixes #61664 Reviewed By: MyDeveloperDay, owenpan Differential Revision: https://reviews.llvm.org/D146760
1 parent a8d2bff commit bb4f6c4

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,11 @@ class AnnotatingParser {
17221722
return false;
17231723
}
17241724

1725+
// This is the default value of a non-template type parameter, so treat
1726+
// it as an expression.
1727+
if (Contexts.back().ContextKind == tok::less)
1728+
return true;
1729+
17251730
Tok = Tok->MatchingParen;
17261731
if (!Tok)
17271732
return false;

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
253253
"};");
254254
ASSERT_EQ(Tokens.size(), 30u) << Tokens;
255255
EXPECT_TOKEN(Tokens[14], tok::ampamp, TT_BinaryOperator);
256+
257+
Tokens = annotate("template <bool B = C && D> struct S {};");
258+
ASSERT_EQ(Tokens.size(), 15u) << Tokens;
259+
EXPECT_TOKEN(Tokens[6], tok::ampamp, TT_BinaryOperator);
260+
261+
Tokens = annotate("template <typename T, bool B = C && D> struct S {};");
262+
ASSERT_EQ(Tokens.size(), 18u) << Tokens;
263+
EXPECT_TOKEN(Tokens[9], tok::ampamp, TT_BinaryOperator);
256264
}
257265

258266
TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {

0 commit comments

Comments
 (0)