aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/clang/lib/Format/UnwrappedLineFormatter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/llvm-project/clang/lib/Format/UnwrappedLineFormatter.cpp')
-rw-r--r--contrib/llvm-project/clang/lib/Format/UnwrappedLineFormatter.cpp78
1 files changed, 62 insertions, 16 deletions
diff --git a/contrib/llvm-project/clang/lib/Format/UnwrappedLineFormatter.cpp b/contrib/llvm-project/clang/lib/Format/UnwrappedLineFormatter.cpp
index 22f27a668dcc..7d197310e65b 100644
--- a/contrib/llvm-project/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/contrib/llvm-project/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -248,6 +248,11 @@ private:
return !Style.BraceWrapping.SplitEmptyRecord && EmptyBlock
? tryMergeSimpleBlock(I, E, Limit)
: 0;
+
+ if (Tok && Tok->is(tok::kw_template) &&
+ Style.BraceWrapping.SplitEmptyRecord && EmptyBlock) {
+ return 0;
+ }
}
// FIXME: TheLine->Level != 0 might or might not be the right check to do.
@@ -309,7 +314,8 @@ private:
// Try to merge a control statement block with left brace wrapped
if (I[1]->First->is(tok::l_brace) &&
(TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,
- tok::kw_switch, tok::kw_try, tok::kw_do) ||
+ tok::kw_switch, tok::kw_try, tok::kw_do,
+ TT_ForEachMacro) ||
(TheLine->First->is(tok::r_brace) && TheLine->First->Next &&
TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) &&
Style.BraceWrapping.AfterControlStatement ==
@@ -354,6 +360,30 @@ private:
if (TheLine->First->is(tok::l_brace) && I != AnnotatedLines.begin() &&
I[-1]->First->isOneOf(tok::kw_case, tok::kw_default))
return 0;
+
+ // Don't merge an empty template class or struct if SplitEmptyRecords
+ // is defined.
+ if (Style.BraceWrapping.SplitEmptyRecord &&
+ TheLine->Last->is(tok::l_brace) && I != AnnotatedLines.begin() &&
+ I[-1]->Last) {
+ const FormatToken *Previous = I[-1]->Last;
+ if (Previous) {
+ if (Previous->is(tok::comment))
+ Previous = Previous->getPreviousNonComment();
+ if (Previous) {
+ if (Previous->is(tok::greater) && !I[-1]->InPPDirective)
+ return 0;
+ if (Previous->is(tok::identifier)) {
+ const FormatToken *PreviousPrevious =
+ Previous->getPreviousNonComment();
+ if (PreviousPrevious &&
+ PreviousPrevious->isOneOf(tok::kw_class, tok::kw_struct))
+ return 0;
+ }
+ }
+ }
+ }
+
// Try to merge a block with left brace wrapped that wasn't yet covered
if (TheLine->Last->is(tok::l_brace)) {
return !Style.BraceWrapping.AfterFunction ||
@@ -606,7 +636,7 @@ private:
if (I[1]->Last->is(TT_LineComment))
return 0;
do {
- if (Tok->is(tok::l_brace) && Tok->BlockKind != BK_BracedInit)
+ if (Tok->is(tok::l_brace) && Tok->isNot(BK_BracedInit))
return 0;
Tok = Tok->Next;
} while (Tok);
@@ -767,8 +797,8 @@ protected:
unsigned &Penalty) {
const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
FormatToken &Previous = *State.NextToken->Previous;
- if (!LBrace || LBrace->isNot(tok::l_brace) ||
- LBrace->BlockKind != BK_Block || Previous.Children.size() == 0)
+ if (!LBrace || LBrace->isNot(tok::l_brace) || LBrace->isNot(BK_Block) ||
+ Previous.Children.size() == 0)
// The previous token does not open a block. Nothing to do. We don't
// assert so that we can simply call this function for all tokens.
return true;
@@ -979,7 +1009,7 @@ private:
// State already examined with lower penalty.
continue;
- FormatDecision LastFormat = Node->State.NextToken->Decision;
+ FormatDecision LastFormat = Node->State.NextToken->getDecision();
if (LastFormat == FD_Unformatted || LastFormat == FD_Continue)
addNextStateToQueue(Penalty, Node, /*NewLine=*/false, &Count, &Queue);
if (LastFormat == FD_Unformatted || LastFormat == FD_Break)
@@ -1215,10 +1245,33 @@ void UnwrappedLineFormatter::formatFirstToken(
!startsExternCBlock(*PreviousLine))
Newlines = 1;
- // Insert extra new line before access specifiers.
- if (PreviousLine && PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) &&
- RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1)
- ++Newlines;
+ // Insert or remove empty line before access specifiers.
+ if (PreviousLine && RootToken.isAccessSpecifier()) {
+ switch (Style.EmptyLineBeforeAccessModifier) {
+ case FormatStyle::ELBAMS_Never:
+ if (RootToken.NewlinesBefore > 1)
+ Newlines = 1;
+ break;
+ case FormatStyle::ELBAMS_Leave:
+ Newlines = std::max(RootToken.NewlinesBefore, 1u);
+ break;
+ case FormatStyle::ELBAMS_LogicalBlock:
+ if (PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) &&
+ RootToken.NewlinesBefore <= 1)
+ Newlines = 2;
+ break;
+ case FormatStyle::ELBAMS_Always: {
+ const FormatToken *previousToken;
+ if (PreviousLine->Last->is(tok::comment))
+ previousToken = PreviousLine->Last->getPreviousNonComment();
+ else
+ previousToken = PreviousLine->Last;
+ if ((!previousToken || !previousToken->is(tok::l_brace)) &&
+ RootToken.NewlinesBefore <= 1)
+ Newlines = 2;
+ } break;
+ }
+ }
// Remove empty lines after access specifiers.
if (PreviousLine && PreviousLine->First->isAccessSpecifier() &&
@@ -1228,13 +1281,6 @@ void UnwrappedLineFormatter::formatFirstToken(
if (Newlines)
Indent = NewlineIndent;
- // If in Whitemsmiths mode, indent start and end of blocks
- if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) {
- if (RootToken.isOneOf(tok::l_brace, tok::r_brace, tok::kw_case,
- tok::kw_default))
- Indent += Style.IndentWidth;
- }
-
// Preprocessor directives get indented before the hash only if specified
if (Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
(Line.Type == LT_PreprocessorDirective ||