12 #include "llvm/Support/Debug.h"
15 #define DEBUG_TYPE "format-formatter"
22 bool startsExternCBlock(
const AnnotatedLine &
Line) {
23 const FormatToken *
Next = Line.First->getNextNonComment();
24 const FormatToken *NextNext = Next ? Next->getNextNonComment() :
nullptr;
25 return Line.startsWith(tok::kw_extern) && Next && Next->isStringLiteral() &&
26 NextNext && NextNext->is(tok::l_brace);
38 class LevelIndentTracker {
40 LevelIndentTracker(
const FormatStyle &
Style,
41 const AdditionalKeywords &
Keywords,
unsigned StartLevel,
44 for (
unsigned i = 0; i != StartLevel; ++i)
45 IndentForLevel.push_back(Style.IndentWidth * i + AdditionalIndent);
49 unsigned getIndent()
const {
return Indent; }
53 void nextLine(
const AnnotatedLine &Line) {
54 Offset = getIndentOffset(*Line.First);
59 if (Line.InPPDirective) {
71 void skipLine(
const AnnotatedLine &Line) {
81 void adjustToUnmodifiedLine(
const AnnotatedLine &Line) {
82 unsigned LevelIndent = Line.First->OriginalColumn;
83 if (static_cast<int>(LevelIndent) -
Offset >= 0)
85 if ((!Line.First->is(tok::comment) ||
IndentForLevel[Line.Level] == -1) &&
95 int getIndentOffset(
const FormatToken &RootToken) {
99 if (RootToken.isAccessSpecifier(
false) ||
100 RootToken.isObjCAccessSpecifier() ||
101 (RootToken.isOneOf(Keywords.kw_signals, Keywords.kw_qsignals) &&
102 RootToken.Next && RootToken.Next->is(tok::colon)))
103 return Style.AccessModifierOffset;
113 if (IndentForLevel[Level] != -1)
114 return IndentForLevel[
Level];
117 return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth;
137 bool isNamespaceDeclaration(
const AnnotatedLine *Line) {
138 const FormatToken *NamespaceTok = Line->First;
139 return NamespaceTok && NamespaceTok->getNamespaceToken();
142 bool isEndOfNamespace(
const AnnotatedLine *Line,
144 if (!Line->startsWith(tok::r_brace))
146 size_t StartLineIndex = Line->MatchingOpeningBlockLineIndex;
149 assert(StartLineIndex < AnnotatedLines.size());
150 return isNamespaceDeclaration(AnnotatedLines[StartLineIndex]);
155 LineJoiner(
const FormatStyle &
Style,
const AdditionalKeywords &
Keywords,
156 const SmallVectorImpl<AnnotatedLine *> &Lines)
157 : Style(Style), Keywords(Keywords),
End(Lines.end()),
Next(Lines.begin()),
158 AnnotatedLines(Lines) {}
161 const AnnotatedLine *getNextMergedLine(
bool DryRun,
162 LevelIndentTracker &IndentTracker) {
166 IndentTracker.nextLine(*Current);
167 unsigned MergedLines =
168 tryFitMultipleLinesInOne(IndentTracker,
Next,
End);
169 if (MergedLines > 0 &&
Style.ColumnLimit == 0)
172 for (
unsigned i = 0; i < MergedLines; ++i)
173 if (
Next[i + 1]->First->NewlinesBefore > 0)
176 for (
unsigned i = 0; i < MergedLines; ++i)
185 tryFitMultipleLinesInOne(LevelIndentTracker &IndentTracker,
186 SmallVectorImpl<AnnotatedLine *>::const_iterator
I,
187 SmallVectorImpl<AnnotatedLine *>::const_iterator
E) {
188 const unsigned Indent = IndentTracker.getIndent();
194 const AnnotatedLine *TheLine = *
I;
195 if (TheLine->Last->is(TT_LineComment))
197 if (I[1]->Type ==
LT_Invalid || I[1]->First->MustBreakBefore)
199 if (TheLine->InPPDirective &&
200 (!I[1]->InPPDirective || I[1]->First->HasUnescapedNewline))
203 if (
Style.ColumnLimit > 0 && Indent >
Style.ColumnLimit)
210 Limit = TheLine->Last->TotalLength > Limit
212 : Limit - TheLine->Last->TotalLength;
214 if (TheLine->Last->is(TT_FunctionLBrace) &&
215 TheLine->First == TheLine->Last &&
216 !
Style.BraceWrapping.SplitEmptyFunction &&
217 I[1]->First->is(tok::r_brace))
218 return tryMergeSimpleBlock(I, E, Limit);
221 if (TheLine->Last->is(tok::l_brace) && TheLine->First == TheLine->Last &&
222 I != AnnotatedLines.begin()) {
223 bool EmptyBlock = I[1]->First->is(tok::r_brace);
225 const FormatToken *Tok = I[-1]->First;
226 if (Tok && Tok->is(tok::comment))
227 Tok = Tok->getNextNonComment();
229 if (Tok && Tok->getNamespaceToken())
230 return !
Style.BraceWrapping.SplitEmptyNamespace && EmptyBlock
231 ? tryMergeSimpleBlock(I, E, Limit) : 0;
233 if (Tok && Tok->is(tok::kw_typedef))
234 Tok = Tok->getNextNonComment();
235 if (Tok && Tok->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union,
237 return !
Style.BraceWrapping.SplitEmptyRecord && EmptyBlock
238 ? tryMergeSimpleBlock(I, E, Limit) : 0;
243 bool MergeShortFunctions =
246 I[1]->First->is(tok::r_brace)) ||
248 TheLine->Level != 0);
250 if (
Style.CompactNamespaces) {
251 if (isNamespaceDeclaration(TheLine)) {
253 unsigned closingLine = TheLine->MatchingOpeningBlockLineIndex - 1;
254 for (; I + 1 + i != E && isNamespaceDeclaration(I[i + 1]) &&
255 closingLine == I[i + 1]->MatchingOpeningBlockLineIndex &&
256 I[i + 1]->Last->TotalLength < Limit;
257 i++, closingLine--) {
259 IndentTracker.skipLine(*I[i + 1]);
261 Limit -= I[i + 1]->Last->TotalLength;
266 if (isEndOfNamespace(TheLine, AnnotatedLines)) {
268 unsigned openingLine = TheLine->MatchingOpeningBlockLineIndex - 1;
269 for (; I + 1 + i != E && isEndOfNamespace(I[i + 1], AnnotatedLines) &&
270 openingLine == I[i + 1]->MatchingOpeningBlockLineIndex;
271 i++, openingLine--) {
273 I[i + 1]->First->SpacesRequiredBefore = !I[i]->Last->is(tok::r_brace);
276 IndentTracker.nextLine(*I[i + 1]);
282 if (TheLine->Last->is(TT_FunctionLBrace) &&
283 TheLine->First != TheLine->Last) {
284 return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0;
286 if (TheLine->Last->is(tok::l_brace)) {
287 return !
Style.BraceWrapping.AfterFunction
288 ? tryMergeSimpleBlock(I, E, Limit)
291 if (I[1]->First->is(TT_FunctionLBrace) &&
292 Style.BraceWrapping.AfterFunction) {
293 if (I[1]->Last->is(TT_LineComment))
297 if (Limit <= 2 || (
Style.ColumnLimit == 0 && containsMustBreak(TheLine)))
301 unsigned MergedLines = 0;
302 if (MergeShortFunctions ||
304 I[1]->First == I[1]->Last && I + 2 != E &&
305 I[2]->First->is(tok::r_brace))) {
306 MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
314 if (TheLine->First->is(tok::kw_if)) {
315 return Style.AllowShortIfStatementsOnASingleLine
316 ? tryMergeSimpleControlStatement(I, E, Limit)
319 if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while)) {
320 return Style.AllowShortLoopsOnASingleLine
321 ? tryMergeSimpleControlStatement(I, E, Limit)
324 if (TheLine->First->isOneOf(tok::kw_case, tok::kw_default)) {
325 return Style.AllowShortCaseLabelsOnASingleLine
326 ? tryMergeShortCaseLabels(I, E, Limit)
329 if (TheLine->InPPDirective &&
330 (TheLine->First->HasUnescapedNewline || TheLine->First->IsFirst)) {
331 return tryMergeSimplePPDirective(I, E, Limit);
337 tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
338 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
342 if (I + 2 != E && I[2]->InPPDirective && !I[2]->First->HasUnescapedNewline)
344 if (1 + I[1]->Last->TotalLength > Limit)
349 unsigned tryMergeSimpleControlStatement(
350 SmallVectorImpl<AnnotatedLine *>::const_iterator I,
351 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
unsigned Limit) {
354 if (
Style.BraceWrapping.AfterControlStatement &&
355 (I[1]->First->is(tok::l_brace) && !
Style.AllowShortBlocksOnASingleLine))
357 if (I[1]->InPPDirective != (*I)->InPPDirective ||
358 (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline))
360 Limit = limitConsideringMacros(I + 1, E, Limit);
361 AnnotatedLine &Line = **
I;
362 if (Line.Last->isNot(tok::r_paren))
364 if (1 + I[1]->Last->TotalLength > Limit)
366 if (I[1]->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, tok::kw_while,
370 if (I + 2 != E && Line.startsWith(tok::kw_if) &&
371 I[2]->First->is(tok::kw_else))
377 tryMergeShortCaseLabels(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
378 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
380 if (Limit == 0 || I + 1 == E ||
381 I[1]->First->isOneOf(tok::kw_case, tok::kw_default))
383 unsigned NumStmts = 0;
385 bool InPPDirective = I[0]->InPPDirective;
386 for (; NumStmts < 3; ++NumStmts) {
387 if (I + 1 + NumStmts == E)
389 const AnnotatedLine *Line = I[1 + NumStmts];
390 if (Line->InPPDirective != InPPDirective)
392 if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
394 if (Line->First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_switch,
395 tok::kw_while, tok::comment) ||
396 Line->Last->is(tok::comment))
398 Length += I[1 + NumStmts]->Last->TotalLength + 1;
400 if (NumStmts == 0 || NumStmts == 3 || Length > Limit)
406 tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
407 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
409 AnnotatedLine &Line = **
I;
415 Line.First->isOneOf(tok::at, tok::minus, tok::plus))
420 if (Line.First->isOneOf(tok::kw_else, tok::kw_case) ||
421 (Line.First->Next && Line.First->Next->is(tok::kw_else)))
423 if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try,
424 tok::kw___try, tok::kw_catch, tok::kw___finally,
425 tok::kw_for, tok::r_brace,
Keywords.kw___except)) {
426 if (!
Style.AllowShortBlocksOnASingleLine)
428 if (!
Style.AllowShortIfStatementsOnASingleLine &&
429 Line.startsWith(tok::kw_if))
431 if (!
Style.AllowShortLoopsOnASingleLine &&
432 Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for))
439 if (Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,
440 Keywords.kw___except, tok::kw___finally))
444 FormatToken *Tok = I[1]->First;
445 if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore &&
446 (Tok->getNextNonComment() ==
nullptr ||
447 Tok->getNextNonComment()->is(tok::semi))) {
449 Tok->SpacesRequiredBefore = 0;
450 Tok->CanBreakBefore =
true;
452 }
else if (Limit != 0 && !Line.startsWith(tok::kw_namespace) &&
453 !startsExternCBlock(Line)) {
455 FormatToken *RecordTok =
456 Line.First->is(tok::kw_typedef) ? Line.First->Next : Line.First;
458 RecordTok->isOneOf(tok::kw_class, tok::kw_union, tok::kw_struct,
465 Limit = limitConsideringMacros(I + 2, E, Limit);
467 if (!nextTwoLinesFitInto(I, Limit))
472 if (I[1]->Last->is(TT_LineComment))
475 if (Tok->is(tok::l_brace) && Tok->BlockKind !=
BK_BracedInit)
482 if (Tok->isNot(tok::r_brace))
486 if (Tok->Next && Tok->Next->is(tok::kw_else))
497 limitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
498 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
500 if (I[0]->InPPDirective && I + 1 != E &&
501 !I[1]->First->HasUnescapedNewline && !I[1]->First->is(
tok::eof)) {
502 return Limit < 2 ? 0 : Limit - 2;
507 bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
509 if (I[1]->First->MustBreakBefore || I[2]->First->MustBreakBefore)
511 return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit;
514 bool containsMustBreak(
const AnnotatedLine *Line) {
515 for (
const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
516 if (Tok->MustBreakBefore)
522 void join(AnnotatedLine &A,
const AnnotatedLine &B) {
523 assert(!A.Last->Next);
524 assert(!B.First->Previous);
527 A.Last->Next = B.First;
528 B.First->Previous = A.Last;
529 B.First->CanBreakBefore =
true;
530 unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
531 for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
532 Tok->TotalLength += LengthA;
537 const FormatStyle &
Style;
539 const SmallVectorImpl<AnnotatedLine *>::const_iterator
End;
541 SmallVectorImpl<AnnotatedLine *>::const_iterator
Next;
545 static void markFinalized(FormatToken *Tok) {
546 for (; Tok; Tok = Tok->Next) {
547 Tok->Finalized =
true;
548 for (AnnotatedLine *Child : Tok->Children)
549 markFinalized(Child->First);
554 static void printLineState(
const LineState &
State) {
555 llvm::dbgs() <<
"State: ";
556 for (
const ParenState &
P : State.Stack) {
557 llvm::dbgs() << P.Indent <<
"|" << P.LastSpace <<
"|" << P.NestedBlockIndent
560 llvm::dbgs() << State.NextToken->TokenText <<
"\n";
565 class LineFormatter {
568 const FormatStyle &
Style,
570 : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),
571 BlockFormatter(BlockFormatter) {}
572 virtual ~LineFormatter() {}
577 virtual unsigned formatLine(
const AnnotatedLine &Line,
unsigned FirstIndent,
601 bool formatChildren(LineState &State,
bool NewLine,
bool DryRun,
603 const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
604 FormatToken &
Previous = *State.NextToken->Previous;
605 if (!LBrace || LBrace->isNot(tok::l_brace) ||
606 LBrace->BlockKind !=
BK_Block || Previous.Children.size() == 0)
613 Previous.Children[0]->Level *
Style.IndentWidth;
616 BlockFormatter->format(Previous.Children, DryRun, AdditionalIndent,
621 if (Previous.Children[0]->First->MustBreakBefore)
625 if (Previous.is(tok::comment))
629 if (Previous.Children.size() > 1)
632 const AnnotatedLine *Child = Previous.Children[0];
634 if (Child->Last->isTrailingComment())
639 if (
Style.ColumnLimit > 0 &&
640 Child->Last->TotalLength + State.Column + 2 >
Style.ColumnLimit)
646 State.Column, State.Line->InPPDirective);
648 Penalty += formatLine(*Child, State.Column + 1, DryRun);
650 State.Column += 1 + Child->Last->TotalLength;
658 const FormatStyle &
Style;
663 class NoColumnLimitLineFormatter :
public LineFormatter {
665 NoColumnLimitLineFormatter(ContinuationIndenter *
Indenter,
667 const FormatStyle &
Style,
669 : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}
673 unsigned formatLine(
const AnnotatedLine &Line,
unsigned FirstIndent,
674 bool DryRun)
override {
677 Indenter->getInitialState(FirstIndent, &Line,
false);
678 while (State.NextToken) {
680 Indenter->mustBreak(State) ||
681 (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
682 unsigned Penalty = 0;
683 formatChildren(State, Newline,
false, Penalty);
684 Indenter->addTokenToState(State, Newline,
false);
691 class NoLineBreakFormatter :
public LineFormatter {
693 NoLineBreakFormatter(ContinuationIndenter *Indenter,
694 WhitespaceManager *Whitespaces,
const FormatStyle &Style,
695 UnwrappedLineFormatter *BlockFormatter)
696 : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}
699 unsigned formatLine(
const AnnotatedLine &Line,
unsigned FirstIndent,
700 bool DryRun)
override {
701 unsigned Penalty = 0;
702 LineState State = Indenter->getInitialState(FirstIndent, &Line, DryRun);
703 while (State.NextToken) {
704 formatChildren(State,
false, DryRun, Penalty);
705 Indenter->addTokenToState(
706 State, State.NextToken->MustBreakBefore, DryRun);
713 class OptimizingLineFormatter :
public LineFormatter {
715 OptimizingLineFormatter(ContinuationIndenter *Indenter,
716 WhitespaceManager *Whitespaces,
717 const FormatStyle &Style,
718 UnwrappedLineFormatter *BlockFormatter)
719 : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}
723 unsigned formatLine(
const AnnotatedLine &Line,
unsigned FirstIndent,
724 bool DryRun)
override {
725 LineState State = Indenter->getInitialState(FirstIndent, &Line, DryRun);
730 State.Stack.back().BreakBeforeParameter =
true;
733 return analyzeSolutionSpace(State, DryRun);
737 struct CompareLineStatePointers {
738 bool operator()(LineState *obj1, LineState *obj2)
const {
739 return *obj1 < *obj2;
748 typedef std::pair<unsigned, unsigned> OrderedPenalty;
753 StateNode(
const LineState &State,
bool NewLine, StateNode *Previous)
754 : State(State), NewLine(NewLine), Previous(Previous) {}
762 typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
765 typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
766 std::greater<QueueItem>> QueueType;
776 unsigned analyzeSolutionSpace(LineState &InitialState,
bool DryRun) {
777 std::set<LineState *, CompareLineStatePointers> Seen;
786 new (
Allocator.Allocate()) StateNode(InitialState,
false,
nullptr);
787 Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
790 unsigned Penalty = 0;
793 while (!Queue.empty()) {
794 Penalty = Queue.top().first.first;
795 StateNode *Node = Queue.top().second;
796 if (!Node->State.NextToken) {
797 DEBUG(llvm::dbgs() <<
"\n---\nPenalty for line: " << Penalty <<
"\n");
805 Node->State.IgnoreStackForComparison =
true;
807 if (!Seen.insert(&Node->State).second)
813 addNextStateToQueue(Penalty, Node,
false, &Count, &Queue);
815 addNextStateToQueue(Penalty, Node,
true, &Count, &Queue);
821 DEBUG(llvm::dbgs() <<
"Could not find a solution.\n");
827 reconstructPath(InitialState, Queue.top().second);
829 DEBUG(llvm::dbgs() <<
"Total number of analyzed states: " << Count <<
"\n");
830 DEBUG(llvm::dbgs() <<
"---\n");
839 void addNextStateToQueue(
unsigned Penalty, StateNode *PreviousNode,
840 bool NewLine,
unsigned *Count, QueueType *Queue) {
841 if (NewLine && !Indenter->canBreak(PreviousNode->State))
843 if (!NewLine && Indenter->mustBreak(PreviousNode->State))
846 StateNode *Node =
new (
Allocator.Allocate())
847 StateNode(PreviousNode->State, NewLine, PreviousNode);
848 if (!formatChildren(Node->State, NewLine,
true, Penalty))
851 Penalty += Indenter->addTokenToState(Node->State, NewLine,
true);
853 Queue->push(QueueItem(OrderedPenalty(Penalty, *Count), Node));
859 void reconstructPath(LineState &State, StateNode *Best) {
860 std::deque<StateNode *> Path;
862 while (Best->Previous) {
863 Path.push_front(Best);
864 Best = Best->Previous;
866 for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end();
868 unsigned Penalty = 0;
869 formatChildren(State, (*I)->NewLine,
false, Penalty);
870 Penalty += Indenter->addTokenToState(State, (*I)->NewLine,
false);
873 printLineState((*I)->Previous->State);
875 llvm::dbgs() <<
"Penalty for placing "
876 << (*I)->Previous->State.NextToken->Tok.getName() <<
": "
890 bool DryRun,
int AdditionalIndent,
891 bool FixBadIndentation) {
892 LineJoiner Joiner(Style, Keywords, Lines);
895 std::pair<const SmallVectorImpl<AnnotatedLine *> *,
unsigned> CacheKey(
896 &Lines, AdditionalIndent);
897 auto CacheIt = PenaltyCache.find(CacheKey);
898 if (DryRun && CacheIt != PenaltyCache.end())
899 return CacheIt->second;
901 assert(!Lines.empty());
902 unsigned Penalty = 0;
903 LevelIndentTracker IndentTracker(Style, Keywords, Lines[0]->
Level,
912 Joiner.getNextMergedLine(DryRun, IndentTracker);
913 Line; Line = NextLine) {
915 unsigned Indent = IndentTracker.getIndent();
921 bool ContinueFormatting =
922 TheLine.
Level > RangeMinLevel ||
925 bool FixIndentation = (FixBadIndentation || ContinueFormatting) &&
927 bool ShouldFormat = TheLine.
Affected || FixIndentation;
938 formatFirstToken(TheLine, PreviousLine, Indent);
940 NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
941 unsigned ColumnLimit = getColumnLimit(TheLine.
InPPDirective, NextLine);
942 bool FitsIntoOneLine =
946 !Style.JavaScriptWrapImports));
948 if (Style.ColumnLimit == 0)
949 NoColumnLimitLineFormatter(Indenter, Whitespaces, Style,
this)
950 .formatLine(TheLine, Indent, DryRun);
951 else if (FitsIntoOneLine)
952 Penalty += NoLineBreakFormatter(Indenter, Whitespaces, Style,
this)
953 .formatLine(TheLine, Indent, DryRun);
955 Penalty += OptimizingLineFormatter(Indenter, Whitespaces, Style,
this)
956 .formatLine(TheLine, Indent, DryRun);
963 if (!Tok->Children.empty())
964 format(Tok->Children, DryRun);
971 IndentTracker.adjustToUnmodifiedLine(TheLine);
973 bool ReformatLeadingWhitespace =
974 StartsNewLine && ((PreviousLine && PreviousLine->
Affected) ||
977 if (ReformatLeadingWhitespace)
978 formatFirstToken(TheLine, PreviousLine,
981 Whitespaces->addUntouchableToken(*TheLine.
First,
986 Whitespaces->addUntouchableToken(*Tok, TheLine.
InPPDirective);
988 NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
992 markFinalized(TheLine.
First);
993 PreviousLine = &TheLine;
995 PenaltyCache[CacheKey] = Penalty;
999 void UnwrappedLineFormatter::formatFirstToken(
const AnnotatedLine &Line,
1005 Whitespaces->replaceWhitespace(RootToken, Newlines, 0,
1012 if (RootToken.
is(tok::r_brace) &&
1016 if (Newlines == 0 && !RootToken.
IsFirst)
1022 if (!Style.KeepEmptyLinesAtTheStartOfBlocks && PreviousLine &&
1023 PreviousLine->
Last->
is(tok::l_brace) &&
1024 PreviousLine->
First->
isNot(tok::kw_namespace) &&
1025 !startsExternCBlock(*PreviousLine))
1029 if (PreviousLine && PreviousLine->
Last->
isOneOf(tok::semi, tok::r_brace) &&
1038 Whitespaces->replaceWhitespace(RootToken, Newlines, Indent, Indent,
1044 UnwrappedLineFormatter::getColumnLimit(
bool InPPDirective,
1045 const AnnotatedLine *NextLine)
const {
1048 bool ContinuesPPDirective =
1053 (NextLine->InPPDirective &&
1056 !NextLine->First->HasUnescapedNewline));
1057 return Style.ColumnLimit - (ContinuesPPDirective ? 2 : 0);
unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid=nullptr) const
detail::InMemoryDirectory::const_iterator I
WhitespaceManager class manages whitespace around tokens and their replacements.
char __ovld __cnfn min(char x, char y)
Returns y if y < x, otherwise it returns x.
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
ast_type_traits::DynTypedNode Node
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T-> getSizeExpr()))
detail::InMemoryDirectory::const_iterator E