[PATCH] Keep track of indentation levels in static initializers for correct indentation with tabs.
Alexander Kornienko
alexfh at google.com
Tue Oct 1 08:56:59 PDT 2013
Hi djasper,
Store IndentationLevel in ParentState and use it instead of the
Line::Level when indening.
Also fixed incorrect indentation level calculation in formatFirstToken.
http://llvm-reviews.chandlerc.com/D1797
Files:
lib/Format/ContinuationIndenter.cpp
lib/Format/ContinuationIndenter.h
lib/Format/Format.cpp
unittests/Format/FormatTest.cpp
Index: lib/Format/ContinuationIndenter.cpp
===================================================================
--- lib/Format/ContinuationIndenter.cpp
+++ lib/Format/ContinuationIndenter.cpp
@@ -61,7 +61,7 @@
State.Column = FirstIndent;
State.Line = Line;
State.NextToken = Line->First;
- State.Stack.push_back(ParenState(FirstIndent, FirstIndent,
+ State.Stack.push_back(ParenState(FirstIndent, Line->Level, FirstIndent,
/*AvoidBinPacking=*/false,
/*NoLineBreak=*/false));
State.LineContainsContinuedForLoopSection = false;
@@ -397,9 +397,9 @@
if (Current.is(tok::comment))
Newlines = std::max(Newlines, std::min(Current.NewlinesBefore,
Style.MaxEmptyLinesToKeep + 1));
- Whitespaces.replaceWhitespace(Current, Newlines, State.Line->Level,
- State.Column, State.Column,
- State.Line->InPPDirective);
+ Whitespaces.replaceWhitespace(Current, Newlines,
+ State.Stack.back().IndentLevel, State.Column,
+ State.Column, State.Line->InPPDirective);
}
if (!Current.isTrailingComment())
@@ -525,6 +525,7 @@
// prepare for the following tokens.
if (Current.opensScope()) {
unsigned NewIndent;
+ unsigned NewIndentLevel = State.Stack.back().IndentLevel;
bool AvoidBinPacking;
if (Current.is(tok::l_brace)) {
if (Current.MatchingParen && Current.BlockKind == BK_Block) {
@@ -546,9 +547,15 @@
for (unsigned i = 0; i != Current.MatchingParen->FakeRParens; ++i)
State.Stack.pop_back();
NewIndent = State.Stack.back().LastSpace + Style.IndentWidth;
+ ++NewIndentLevel;
} else {
- NewIndent = State.Stack.back().LastSpace +
- (Style.Cpp11BracedListStyle ? 4 : Style.IndentWidth);
+ NewIndent = State.Stack.back().LastSpace;
+ if (Style.Cpp11BracedListStyle)
+ NewIndent += 4;
+ else {
+ NewIndent += Style.IndentWidth;
+ ++NewIndentLevel;
+ }
}
const FormatToken *NextNoComment = Current.getNextNonComment();
AvoidBinPacking = Current.BlockKind == BK_Block ||
@@ -564,9 +571,9 @@
Current.PackingKind == PPK_Inconclusive)));
}
- State.Stack.push_back(ParenState(NewIndent, State.Stack.back().LastSpace,
- AvoidBinPacking,
- State.Stack.back().NoLineBreak));
+ State.Stack.push_back(
+ ParenState(NewIndent, NewIndentLevel, State.Stack.back().LastSpace,
+ AvoidBinPacking, State.Stack.back().NoLineBreak));
State.Stack.back().BreakBeforeParameter = Current.BlockKind == BK_Block;
++State.ParenLevel;
}
Index: lib/Format/ContinuationIndenter.h
===================================================================
--- lib/Format/ContinuationIndenter.h
+++ lib/Format/ContinuationIndenter.h
@@ -125,10 +125,10 @@
};
struct ParenState {
- ParenState(unsigned Indent, unsigned LastSpace, bool AvoidBinPacking,
- bool NoLineBreak)
- : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0),
- BreakBeforeClosingBrace(false), QuestionColumn(0),
+ ParenState(unsigned Indent, unsigned IndentLevel, unsigned LastSpace,
+ bool AvoidBinPacking, bool NoLineBreak)
+ : Indent(Indent), IndentLevel(IndentLevel), LastSpace(LastSpace),
+ FirstLessLess(0), BreakBeforeClosingBrace(false), QuestionColumn(0),
AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false),
NoLineBreak(NoLineBreak), ColonPos(0), StartOfFunctionCall(0),
StartOfArraySubscripts(0), NestedNameSpecifierContinuation(0),
@@ -139,6 +139,9 @@
/// indented.
unsigned Indent;
+ /// \brief The number of indentation levels of the block.
+ unsigned IndentLevel;
+
/// \brief The position of the last space on each level.
///
/// Used e.g. to break like:
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -572,7 +572,7 @@
if (!DryRun) {
Whitespaces->replaceWhitespace(
*LBrace.Children[0]->First,
- /*Newlines=*/0, /*IndentLevel=*/1, /*Spaces=*/1,
+ /*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1,
/*StartOfTokenColumn=*/State.Column, State.Line->InPPDirective);
UnwrappedLineFormatter Formatter(Indenter, Whitespaces, Style,
*LBrace.Children[0]);
@@ -860,8 +860,8 @@
bool WasMoved = PreviousLineWasTouched && FirstTok->NewlinesBefore == 0;
if (TheLine.First->is(tok::eof)) {
if (PreviousLineWasTouched) {
- unsigned NewLines = std::min(FirstTok->NewlinesBefore, 1u);
- Whitespaces.replaceWhitespace(*TheLine.First, NewLines,
+ unsigned Newlines = std::min(FirstTok->NewlinesBefore, 1u);
+ Whitespaces.replaceWhitespace(*TheLine.First, Newlines,
/*IndentLevel=*/0, /*Spaces=*/0,
/*TargetColumn=*/0);
}
@@ -872,8 +872,8 @@
// Insert a break even if there is a structural error in case where
// we break apart a line consisting of multiple unwrapped lines.
(FirstTok->NewlinesBefore == 0 || !StructuralError)) {
- formatFirstToken(*TheLine.First, PreviousLineLastToken, Indent,
- TheLine.InPPDirective);
+ formatFirstToken(*TheLine.First, PreviousLineLastToken, TheLine.Level,
+ Indent, TheLine.InPPDirective);
} else {
Indent = LevelIndent = FirstTok->OriginalColumn;
}
@@ -914,8 +914,8 @@
// Remove trailing whitespace of the previous line if it was
// touched.
if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine)) {
- formatFirstToken(*Tok, PreviousLineLastToken, LevelIndent,
- TheLine.InPPDirective);
+ formatFirstToken(*Tok, PreviousLineLastToken, TheLine.Level,
+ LevelIndent, TheLine.InPPDirective);
} else {
Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
}
@@ -1220,10 +1220,9 @@
/// \brief Add a new line and the required indent before the first Token
/// of the \c UnwrappedLine if there was no structural parsing error.
- /// Returns the indent level of the \c UnwrappedLine.
void formatFirstToken(const FormatToken &RootToken,
- const FormatToken *PreviousToken, unsigned Indent,
- bool InPPDirective) {
+ const FormatToken *PreviousToken, unsigned IndentLevel,
+ unsigned Indent, bool InPPDirective) {
unsigned Newlines =
std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
// Remove empty lines before "}" where applicable.
@@ -1240,7 +1239,7 @@
++Newlines;
Whitespaces.replaceWhitespace(
- RootToken, Newlines, Indent / Style.IndentWidth, Indent, Indent,
+ RootToken, Newlines, IndentLevel, Indent, Indent,
InPPDirective && !RootToken.HasUnescapedNewline);
}
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -5867,6 +5867,15 @@
Tab));
Tab.UseTab = FormatStyle::UT_ForIndentation;
+ verifyFormat("T t[] = {\n"
+ "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ "};",
+ Tab);
EXPECT_EQ("if (aaaaaaaa && // q\n"
" bb) // w\n"
"\t;",
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1797.1.patch
Type: text/x-patch
Size: 8257 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131001/56f29890/attachment.bin>
More information about the cfe-commits
mailing list