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;
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())
// 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) {
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 ||
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;
}
};
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),
/// 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:
template <>
struct ScalarEnumerationTraits<clang::format::FormatStyle::UseTabStyle> {
- static void
- enumeration(IO &IO, clang::format::FormatStyle::UseTabStyle &Value) {
+ static void enumeration(IO &IO,
+ clang::format::FormatStyle::UseTabStyle &Value) {
IO.enumCase(Value, "Never", clang::format::FormatStyle::UT_Never);
IO.enumCase(Value, "false", clang::format::FormatStyle::UT_Never);
IO.enumCase(Value, "Always", clang::format::FormatStyle::UT_Always);
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]);
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);
}
// 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, PreviousLine, Indent,
+ formatFirstToken(*TheLine.First, PreviousLine, TheLine.Level, Indent,
TheLine.InPPDirective);
} else {
Indent = LevelIndent = FirstTok->OriginalColumn;
// Remove trailing whitespace of the previous line if it was
// touched.
if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine)) {
- formatFirstToken(*Tok, PreviousLine, LevelIndent,
+ formatFirstToken(*Tok, PreviousLine, TheLine.Level, LevelIndent,
TheLine.InPPDirective);
} else {
Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
/// \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(FormatToken &RootToken,
- const AnnotatedLine *PreviousLine, unsigned Indent,
- bool InPPDirective) {
+ const AnnotatedLine *PreviousLine, unsigned IndentLevel,
+ unsigned Indent, bool InPPDirective) {
unsigned Newlines =
std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
// Remove empty lines before "}" where applicable.
Newlines = std::min(1u, Newlines);
Whitespaces.replaceWhitespace(
- RootToken, Newlines, Indent / Style.IndentWidth, Indent, Indent,
+ RootToken, Newlines, IndentLevel, Indent, Indent,
InPPDirective && !RootToken.HasUnescapedNewline);
}
if (StyleName.startswith("{")) {
// Parse YAML/JSON style from the command line.
if (llvm::error_code ec = parseConfiguration(StyleName, &Style)) {
- llvm::errs() << "Error parsing -style: " << ec.message()
- << ", using " << FallbackStyle << " style\n";
+ llvm::errs() << "Error parsing -style: " << ec.message() << ", using "
+ << FallbackStyle << " style\n";
}
return Style;
}
SmallString<128> Path(FileName);
llvm::sys::fs::make_absolute(Path);
- for (StringRef Directory = Path;
- !Directory.empty();
+ for (StringRef Directory = Path; !Directory.empty();
Directory = llvm::sys::path::parent_path(Directory)) {
if (!llvm::sys::fs::is_directory(Directory))
continue;