curdeius updated this revision to Diff 408629.
curdeius added a comment.
Remove unused.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D119785/new/
https://reviews.llvm.org/D119785
Files:
clang/lib/Format/FormatToken.h
clang/lib/Format/TokenAnnotator.cpp
clang/lib/Format/UnwrappedLineFormatter.cpp
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTest.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Index: clang/unittests/Format/TokenAnnotatorTest.cpp
===================================================================
--- clang/unittests/Format/TokenAnnotatorTest.cpp
+++ clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -78,25 +78,38 @@
TEST_F(TokenAnnotatorTest, UnderstandsClasses) {
auto Tokens = annotate("class C {};");
EXPECT_EQ(Tokens.size(), 6u) << Tokens;
- EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_RecordLBrace);
+ EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_ClassLBrace);
+
+ Tokens = annotate("const class C {} c;");
+ EXPECT_EQ(Tokens.size(), 8u) << Tokens;
+ EXPECT_TOKEN(Tokens[3], tok::l_brace, TT_ClassLBrace);
+
+ Tokens = annotate("const class {} c;");
+ EXPECT_EQ(Tokens.size(), 7u) << Tokens;
+ EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_ClassLBrace);
}
TEST_F(TokenAnnotatorTest, UnderstandsStructs) {
auto Tokens = annotate("struct S {};");
EXPECT_EQ(Tokens.size(), 6u) << Tokens;
- EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_RecordLBrace);
+ EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_StructLBrace);
}
TEST_F(TokenAnnotatorTest, UnderstandsUnions) {
auto Tokens = annotate("union U {};");
EXPECT_EQ(Tokens.size(), 6u) << Tokens;
- EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_RecordLBrace);
+ EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_UnionLBrace);
+
+ 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);
}
TEST_F(TokenAnnotatorTest, UnderstandsEnums) {
auto Tokens = annotate("enum E {};");
EXPECT_EQ(Tokens.size(), 6u) << Tokens;
- EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_RecordLBrace);
+ EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_EnumLBrace);
}
TEST_F(TokenAnnotatorTest, UnderstandsLBracesInMacroDefinition) {
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -3665,6 +3665,21 @@
"void f() { f(); }\n"
"}",
LLVMWithNoNamespaceFix);
+ verifyFormat("namespace some_namespace {\n"
+ "class {\n"
+ "} anonymous;\n"
+ "}",
+ LLVMWithNoNamespaceFix);
+ verifyFormat("namespace some_namespace {\n"
+ "const class {\n"
+ "} anonymous;\n"
+ "}",
+ LLVMWithNoNamespaceFix);
+ verifyFormat("namespace some_namespace {\n"
+ "constexpr class C {\n"
+ "} c;\n"
+ "}",
+ LLVMWithNoNamespaceFix);
verifyFormat("namespace N::inline D {\n"
"class A {};\n"
"void f() { f(); }\n"
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -2992,7 +2992,7 @@
// Just a declaration or something is wrong.
if (FormatTok->isNot(tok::l_brace))
return true;
- FormatTok->setType(TT_RecordLBrace);
+ FormatTok->setType(TT_EnumLBrace);
FormatTok->setBlockKind(BK_Block);
if (Style.Language == FormatStyle::LK_Java) {
@@ -3229,8 +3229,22 @@
nextToken();
}
}
+
+ auto GetBraceType = [](const FormatToken &RecordTok) {
+ switch (RecordTok.Tok.getKind()) {
+ case tok::kw_class:
+ return TT_ClassLBrace;
+ case tok::kw_struct:
+ return TT_StructLBrace;
+ case tok::kw_union:
+ return TT_UnionLBrace;
+ default:
+ // Useful for e.g. interface.
+ return TT_RecordLBrace;
+ }
+ };
if (FormatTok->Tok.is(tok::l_brace)) {
- FormatTok->setType(TT_RecordLBrace);
+ FormatTok->setType(GetBraceType(InitialToken));
if (ParseAsExpr) {
parseChildBlock();
} else {
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -26,6 +26,11 @@
NextNext && NextNext->is(tok::l_brace);
}
+bool isRecordLBrace(const FormatToken &Tok) {
+ return Tok.isOneOf(TT_ClassLBrace, TT_EnumLBrace, TT_RecordLBrace,
+ TT_StructLBrace, TT_UnionLBrace);
+}
+
/// Tracks the indent level of \c AnnotatedLines across levels.
///
/// \c nextLine must be called for each \c AnnotatedLine, after which \c
@@ -310,7 +315,7 @@
for (const FormatToken *RecordTok = (*J)->Last; RecordTok;
RecordTok = RecordTok->Previous)
if (RecordTok->is(tok::l_brace))
- return RecordTok->is(TT_RecordLBrace);
+ return isRecordLBrace(*RecordTok);
}
}
@@ -457,27 +462,30 @@
}
}
- // Try to merge a block with left brace unwrapped that wasn't yet covered.
if (TheLine->Last->is(tok::l_brace)) {
- const FormatToken *Tok = TheLine->First;
bool ShouldMerge = false;
- if (Tok->is(tok::kw_typedef)) {
- Tok = Tok->getNextNonComment();
- assert(Tok);
- }
- if (Tok->isOneOf(tok::kw_class, tok::kw_struct)) {
+ // Try to merge records.
+ if (TheLine->Last->is(TT_EnumLBrace)) {
+ ShouldMerge = Style.AllowShortEnumsOnASingleLine;
+ } else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace)) {
+ // NOTE: We use AfterClass (whereas AfterStruct exists) for both classes
+ // and structs, but it seems that wrapping is still handled correctly
+ // elsewhere.
ShouldMerge = !Style.BraceWrapping.AfterClass ||
(NextLine.First->is(tok::r_brace) &&
!Style.BraceWrapping.SplitEmptyRecord);
- } else if (Tok->is(tok::kw_enum)) {
- ShouldMerge = Style.AllowShortEnumsOnASingleLine;
} else {
+ // Try to merge a block with left brace unwrapped that wasn't yet
+ // covered.
+ assert(!TheLine->First->isOneOf(tok::kw_class, tok::kw_enum,
+ tok::kw_struct));
ShouldMerge = !Style.BraceWrapping.AfterFunction ||
(NextLine.First->is(tok::r_brace) &&
!Style.BraceWrapping.SplitEmptyFunction);
}
return ShouldMerge ? tryMergeSimpleBlock(I, E, Limit) : 0;
}
+
// Try to merge a function block with left brace wrapped.
if (NextLine.First->is(TT_FunctionLBrace) &&
Style.BraceWrapping.AfterFunction) {
@@ -715,6 +723,7 @@
const FormatToken *Next = Tok->getNextNonComment();
return !Next || Next->is(tok::semi);
};
+
if (ShouldMerge()) {
// We merge empty blocks even if the line exceeds the column limit.
Tok->SpacesRequiredBefore = Style.SpaceInEmptyBlock ? 1 : 0;
@@ -723,18 +732,7 @@
} else if (Limit != 0 && !Line.startsWithNamespace() &&
!startsExternCBlock(Line)) {
// We don't merge short records.
- FormatToken *RecordTok = Line.First;
- // Skip record modifiers.
- while (RecordTok->Next &&
- RecordTok->isOneOf(tok::kw_typedef, tok::kw_export,
- Keywords.kw_declare, Keywords.kw_abstract,
- tok::kw_default, Keywords.kw_override,
- tok::kw_public, tok::kw_private,
- tok::kw_protected, Keywords.kw_internal))
- RecordTok = RecordTok->Next;
- if (RecordTok &&
- RecordTok->isOneOf(tok::kw_class, tok::kw_union, tok::kw_struct,
- Keywords.kw_interface))
+ if (isRecordLBrace(*Line.Last))
return 0;
// Check that we still have three lines and they fit into the limit.
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -1424,11 +1424,12 @@
TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
- TT_FunctionLikeOrFreestandingMacro, TT_RecordLBrace,
- TT_RequiresClause, TT_RequiresClauseInARequiresExpression,
- TT_RequiresExpression, TT_RequiresExpressionLParen,
- TT_RequiresExpressionLBrace, TT_BinaryOperator,
- TT_CompoundRequirementLBrace, TT_BracedListLBrace))
+ TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
+ TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
+ TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
+ TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
+ TT_BinaryOperator, TT_CompoundRequirementLBrace,
+ TT_BracedListLBrace))
CurrentToken->setType(TT_Unknown);
CurrentToken->Role.reset();
CurrentToken->MatchingParen = nullptr;
Index: clang/lib/Format/FormatToken.h
===================================================================
--- clang/lib/Format/FormatToken.h
+++ clang/lib/Format/FormatToken.h
@@ -37,6 +37,7 @@
TYPE(BlockComment) \
TYPE(BracedListLBrace) \
TYPE(CastRParen) \
+ TYPE(ClassLBrace) \
TYPE(CompoundRequirementLBrace) \
TYPE(ConditionalExpr) \
TYPE(ConflictAlternative) \
@@ -47,6 +48,7 @@
TYPE(DesignatedInitializerLSquare) \
TYPE(DesignatedInitializerPeriod) \
TYPE(DictLiteral) \
+ TYPE(EnumLBrace) \
TYPE(FatArrow) \
TYPE(ForEachMacro) \
TYPE(FunctionAnnotationRParen) \
@@ -108,6 +110,7 @@
TYPE(StartOfName) \
TYPE(StatementAttributeLikeMacro) \
TYPE(StatementMacro) \
+ TYPE(StructLBrace) \
TYPE(StructuredBindingLSquare) \
TYPE(TemplateCloser) \
TYPE(TemplateOpener) \
@@ -119,6 +122,7 @@
TYPE(TypeDeclarationParen) \
TYPE(TypenameMacro) \
TYPE(UnaryOperator) \
+ TYPE(UnionLBrace) \
TYPE(UntouchableMacroFunc) \
TYPE(CSharpStringLiteral) \
TYPE(CSharpNamedArgumentColon) \
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits