Changeset View
Changeset View
Standalone View
Standalone View
clang/lib/Format/UnwrappedLineFormatter.cpp
Show First 20 Lines • Show All 600 Lines • ▼ Show 20 Lines | if (Line.Last->is(tok::l_brace)) { | ||||
if (!nextTwoLinesFitInto(I, Limit)) | if (!nextTwoLinesFitInto(I, Limit)) | ||||
return 0; | return 0; | ||||
// Second, check that the next line does not contain any braces - if it | // Second, check that the next line does not contain any braces - if it | ||||
// does, readability declines when putting it into a single line. | // does, readability declines when putting it into a single line. | ||||
if (I[1]->Last->is(TT_LineComment)) | if (I[1]->Last->is(TT_LineComment)) | ||||
return 0; | return 0; | ||||
do { | do { | ||||
if (Tok->is(tok::l_brace) && Tok->BlockKind != BK_BracedInit) | if (Tok->is(tok::l_brace) && Tok->isNot(BK_BracedInit)) | ||||
return 0; | return 0; | ||||
Tok = Tok->Next; | Tok = Tok->Next; | ||||
} while (Tok); | } while (Tok); | ||||
// Last, check that the third line starts with a closing brace. | // Last, check that the third line starts with a closing brace. | ||||
Tok = I[2]->First; | Tok = I[2]->First; | ||||
if (Tok->isNot(tok::r_brace)) | if (Tok->isNot(tok::r_brace)) | ||||
return 0; | return 0; | ||||
▲ Show 20 Lines • Show All 144 Lines • ▼ Show 20 Lines | protected: | ||||
/// | /// | ||||
/// This enables us to keep the simple structure of the | /// This enables us to keep the simple structure of the | ||||
/// \c UnwrappedLineFormatter, where we only have two options for each token: | /// \c UnwrappedLineFormatter, where we only have two options for each token: | ||||
/// break or don't break. | /// break or don't break. | ||||
bool formatChildren(LineState &State, bool NewLine, bool DryRun, | bool formatChildren(LineState &State, bool NewLine, bool DryRun, | ||||
unsigned &Penalty) { | unsigned &Penalty) { | ||||
const FormatToken *LBrace = State.NextToken->getPreviousNonComment(); | const FormatToken *LBrace = State.NextToken->getPreviousNonComment(); | ||||
FormatToken &Previous = *State.NextToken->Previous; | FormatToken &Previous = *State.NextToken->Previous; | ||||
if (!LBrace || LBrace->isNot(tok::l_brace) || | if (!LBrace || LBrace->isNot(tok::l_brace) || LBrace->isNot(BK_Block) || | ||||
LBrace->BlockKind != BK_Block || Previous.Children.size() == 0) | Previous.Children.size() == 0) | ||||
// The previous token does not open a block. Nothing to do. We don't | // 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. | // assert so that we can simply call this function for all tokens. | ||||
return true; | return true; | ||||
if (NewLine) { | if (NewLine) { | ||||
int AdditionalIndent = State.Stack.back().Indent - | int AdditionalIndent = State.Stack.back().Indent - | ||||
Previous.Children[0]->Level * Style.IndentWidth; | Previous.Children[0]->Level * Style.IndentWidth; | ||||
▲ Show 20 Lines • Show All 194 Lines • ▼ Show 20 Lines | while (!Queue.empty()) { | ||||
// complex. See description of IgnoreStackForComparison. | // complex. See description of IgnoreStackForComparison. | ||||
if (Count > 50000) | if (Count > 50000) | ||||
Node->State.IgnoreStackForComparison = true; | Node->State.IgnoreStackForComparison = true; | ||||
if (!Seen.insert(&Node->State).second) | if (!Seen.insert(&Node->State).second) | ||||
// State already examined with lower penalty. | // State already examined with lower penalty. | ||||
continue; | continue; | ||||
FormatDecision LastFormat = Node->State.NextToken->Decision; | FormatDecision LastFormat = Node->State.NextToken->getDecision(); | ||||
if (LastFormat == FD_Unformatted || LastFormat == FD_Continue) | if (LastFormat == FD_Unformatted || LastFormat == FD_Continue) | ||||
addNextStateToQueue(Penalty, Node, /*NewLine=*/false, &Count, &Queue); | addNextStateToQueue(Penalty, Node, /*NewLine=*/false, &Count, &Queue); | ||||
if (LastFormat == FD_Unformatted || LastFormat == FD_Break) | if (LastFormat == FD_Unformatted || LastFormat == FD_Break) | ||||
addNextStateToQueue(Penalty, Node, /*NewLine=*/true, &Count, &Queue); | addNextStateToQueue(Penalty, Node, /*NewLine=*/true, &Count, &Queue); | ||||
} | } | ||||
if (Queue.empty()) { | if (Queue.empty()) { | ||||
// We were unable to find a solution, do nothing. | // We were unable to find a solution, do nothing. | ||||
▲ Show 20 Lines • Show All 278 Lines • Show Last 20 Lines |