diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -88,13 +88,13 @@ /// level to the same indent. /// Note that \c nextLine must have been called before this method. void adjustToUnmodifiedLine(const AnnotatedLine &Line) { - unsigned LevelIndent = Line.First->OriginalColumn; - if (static_cast(LevelIndent) - Offset >= 0) - LevelIndent -= Offset; - assert(Line.Level < IndentForLevel.size()); - if ((!Line.First->is(tok::comment) || IndentForLevel[Line.Level] == -1) && - !Line.InPPDirective) { - IndentForLevel[Line.Level] = LevelIndent; + if (!Line.InPPDirective) { + unsigned LevelIndent = Line.First->OriginalColumn; + if (static_cast(LevelIndent) - Offset >= 0) + LevelIndent -= Offset; + assert(Line.Level < IndentForLevel.size()); + if (Line.First->isNot(tok::comment) || IndentForLevel[Line.Level] == -1) + IndentForLevel[Line.Level] = LevelIndent; } } @@ -148,6 +148,7 @@ /// at \p IndentForLevel[l], or a value < 0 if the indent for /// that level is unknown. unsigned getIndent(unsigned Level) const { + assert(Level < IndentForLevel.size()); if (IndentForLevel[Level] != -1) return IndentForLevel[Level]; if (Level == 0) @@ -159,7 +160,10 @@ const AdditionalKeywords &Keywords; const unsigned AdditionalIndent; - /// The indent in characters for each level. + /// The indent in characters for each level. It remembers the indent of + /// previous lines (that are not PP directives) of equal or lower levels. This + /// is used to align formatted lines to the indent of previous non-formatted + /// lines. Think about the --lines parameter of clang-format. SmallVector IndentForLevel; /// Offset of the current line relative to the indent level.