[clang] [clang-format][NFC] Annotate more r_braces (PR #68534)
Björn Schäpers via cfe-commits
cfe-commits at lists.llvm.org
Sun Oct 8 13:15:15 PDT 2023
https://github.com/HazardyKnusperkeks created https://github.com/llvm/llvm-project/pull/68534
In preparation of #67906.
>From 28c69947fb451db2176284f162c789b081600209 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <bjoern at hazardy.de>
Date: Sun, 8 Oct 2023 22:12:28 +0200
Subject: [PATCH] [clang-format][NFC] Annotate more r_braces
In preparation of #67906.
---
clang/lib/Format/FormatToken.h | 4 ++++
clang/lib/Format/UnwrappedLineParser.cpp | 16 ++++++++++------
clang/unittests/Format/TokenAnnotatorTest.cpp | 12 ++++++++++++
3 files changed, 26 insertions(+), 6 deletions(-)
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 5877b0a6124742a..092ad4126451bd7 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -42,6 +42,7 @@ namespace format {
TYPE(CaseLabelColon) \
TYPE(CastRParen) \
TYPE(ClassLBrace) \
+ TYPE(ClassRBrace) \
/* ternary ?: expression */ \
TYPE(ConditionalExpr) \
/* the condition in an if statement */ \
@@ -125,6 +126,7 @@ namespace format {
TYPE(PureVirtualSpecifier) \
TYPE(RangeBasedForLoopColon) \
TYPE(RecordLBrace) \
+ TYPE(RecordRBrace) \
TYPE(RegexLiteral) \
TYPE(RequiresClause) \
TYPE(RequiresClauseInARequiresExpression) \
@@ -141,6 +143,7 @@ namespace format {
* braces need to be added to split it. Not used for other languages. */ \
TYPE(StringInConcatenation) \
TYPE(StructLBrace) \
+ TYPE(StructRBrace) \
TYPE(StructuredBindingLSquare) \
TYPE(TemplateCloser) \
TYPE(TemplateOpener) \
@@ -153,6 +156,7 @@ namespace format {
TYPE(TypenameMacro) \
TYPE(UnaryOperator) \
TYPE(UnionLBrace) \
+ TYPE(UnionRBrace) \
TYPE(UntouchableMacroFunc) \
/* Like in 'assign x = 0, y = 1;' . */ \
TYPE(VerilogAssignComma) \
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index dda5fd077e590e5..1737018c6a78f8e 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3920,21 +3920,23 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
} while (!eof());
}
- auto GetBraceType = [](const FormatToken &RecordTok) {
+ auto GetBraceTypes =
+ [](const FormatToken &RecordTok) -> std::pair<TokenType, TokenType> {
switch (RecordTok.Tok.getKind()) {
case tok::kw_class:
- return TT_ClassLBrace;
+ return {TT_ClassLBrace, TT_ClassRBrace};
case tok::kw_struct:
- return TT_StructLBrace;
+ return {TT_StructLBrace, TT_StructRBrace};
case tok::kw_union:
- return TT_UnionLBrace;
+ return {TT_UnionLBrace, TT_UnionRBrace};
default:
// Useful for e.g. interface.
- return TT_RecordLBrace;
+ return {TT_RecordLBrace, TT_RecordRBrace};
}
};
if (FormatTok->is(tok::l_brace)) {
- FormatTok->setFinalizedType(GetBraceType(InitialToken));
+ auto [OpenBraceType, ClosingBraceType] = GetBraceTypes(InitialToken);
+ FormatTok->setFinalizedType(OpenBraceType);
if (ParseAsExpr) {
parseChildBlock();
} else {
@@ -3944,6 +3946,8 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
unsigned AddLevels = Style.IndentAccessModifiers ? 2u : 1u;
parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/false);
}
+ if (auto Prev = FormatTok->Previous; Prev && Prev->is(tok::r_brace))
+ Prev->setFinalizedType(ClosingBraceType);
}
// There is no addUnwrappedLine() here so that we fall through to parsing a
// structural element afterwards. Thus, in "class A {} n, m;",
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 62ec460eba7fd90..4f006ac8eb951f5 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -393,32 +393,39 @@ TEST_F(TokenAnnotatorTest, UnderstandsClasses) {
auto Tokens = annotate("class C {};");
EXPECT_EQ(Tokens.size(), 6u) << Tokens;
EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_ClassLBrace);
+ EXPECT_TOKEN(Tokens[3], tok::r_brace, TT_ClassRBrace);
Tokens = annotate("const class C {} c;");
EXPECT_EQ(Tokens.size(), 8u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::l_brace, TT_ClassLBrace);
+ EXPECT_TOKEN(Tokens[4], tok::r_brace, TT_ClassRBrace);
Tokens = annotate("const class {} c;");
EXPECT_EQ(Tokens.size(), 7u) << Tokens;
EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_ClassLBrace);
+ EXPECT_TOKEN(Tokens[3], tok::r_brace, TT_ClassRBrace);
Tokens = annotate("class [[deprecated(\"\")]] C { int i; };");
EXPECT_EQ(Tokens.size(), 17u) << Tokens;
EXPECT_TOKEN(Tokens[10], tok::l_brace, TT_ClassLBrace);
+ EXPECT_TOKEN(Tokens[14], tok::r_brace, TT_ClassRBrace);
}
TEST_F(TokenAnnotatorTest, UnderstandsStructs) {
auto Tokens = annotate("struct S {};");
EXPECT_EQ(Tokens.size(), 6u) << Tokens;
EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_StructLBrace);
+ EXPECT_TOKEN(Tokens[3], tok::r_brace, TT_StructRBrace);
Tokens = annotate("struct EXPORT_MACRO [[nodiscard]] C { int i; };");
EXPECT_EQ(Tokens.size(), 15u) << Tokens;
EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_StructLBrace);
+ EXPECT_TOKEN(Tokens[12], tok::r_brace, TT_StructRBrace);
Tokens = annotate("struct [[deprecated]] [[nodiscard]] C { int i; };");
EXPECT_EQ(Tokens.size(), 19u) << Tokens;
EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_StructLBrace);
+ EXPECT_TOKEN(Tokens[16], tok::r_brace, TT_StructRBrace);
Tokens = annotate("template <typename T> struct S<const T[N]> {};");
EXPECT_EQ(Tokens.size(), 18u) << Tokens;
@@ -426,6 +433,7 @@ TEST_F(TokenAnnotatorTest, UnderstandsStructs) {
EXPECT_TOKEN(Tokens[10], tok::l_square, TT_ArraySubscriptLSquare);
EXPECT_TOKEN(Tokens[13], tok::greater, TT_TemplateCloser);
EXPECT_TOKEN(Tokens[14], tok::l_brace, TT_StructLBrace);
+ EXPECT_TOKEN(Tokens[15], tok::r_brace, TT_StructRBrace);
Tokens = annotate("template <typename T> struct S<T const[N]> {};");
EXPECT_EQ(Tokens.size(), 18u) << Tokens;
@@ -433,6 +441,7 @@ TEST_F(TokenAnnotatorTest, UnderstandsStructs) {
EXPECT_TOKEN(Tokens[10], tok::l_square, TT_ArraySubscriptLSquare);
EXPECT_TOKEN(Tokens[13], tok::greater, TT_TemplateCloser);
EXPECT_TOKEN(Tokens[14], tok::l_brace, TT_StructLBrace);
+ EXPECT_TOKEN(Tokens[15], tok::r_brace, TT_StructRBrace);
Tokens = annotate("template <typename T, unsigned n> struct S<T const[n]> {\n"
" void f(T const (&a)[n]);\n"
@@ -445,17 +454,20 @@ TEST_F(TokenAnnotatorTest, UnderstandsStructs) {
EXPECT_TOKEN(Tokens[23], tok::l_paren, TT_FunctionTypeLParen);
EXPECT_TOKEN(Tokens[24], tok::amp, TT_UnaryOperator);
EXPECT_TOKEN(Tokens[27], tok::l_square, TT_ArraySubscriptLSquare);
+ EXPECT_TOKEN(Tokens[32], tok::r_brace, TT_StructRBrace);
}
TEST_F(TokenAnnotatorTest, UnderstandsUnions) {
auto Tokens = annotate("union U {};");
EXPECT_EQ(Tokens.size(), 6u) << Tokens;
EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_UnionLBrace);
+ EXPECT_TOKEN(Tokens[3], tok::r_brace, TT_UnionRBrace);
Tokens = annotate("union U { void f() { return; } };");
EXPECT_EQ(Tokens.size(), 14u) << Tokens;
EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_UnionLBrace);
EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_FunctionLBrace);
+ EXPECT_TOKEN(Tokens[11], tok::r_brace, TT_UnionRBrace);
}
TEST_F(TokenAnnotatorTest, UnderstandsEnums) {
More information about the cfe-commits
mailing list