Skip to content

Commit e4aa142

Browse files
Sedenionoowenca
authored andcommitted
[clang-format] Refactoring and asserts in LevelIndentTracker. (NFC)
adjustToUnmodifiedLine: The code does something only for non-PP-directives. This is now reflected by putting the if-check to the top. This also ensures that the assert() there is executed only if IndentForLevel is actually accessed. getIndent(): assert valid index into IndentForLevel. Added explanation regarding the intention of IndentForLevel. Differential Revision: https://reviews.llvm.org/D155094
1 parent 4bbf371 commit e4aa142

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,15 @@ class LevelIndentTracker {
8888
/// level to the same indent.
8989
/// Note that \c nextLine must have been called before this method.
9090
void adjustToUnmodifiedLine(const AnnotatedLine &Line) {
91+
if (Line.InPPDirective)
92+
return;
93+
assert(Line.Level < IndentForLevel.size());
94+
if (Line.First->is(tok::comment) && IndentForLevel[Line.Level] != -1)
95+
return;
9196
unsigned LevelIndent = Line.First->OriginalColumn;
9297
if (static_cast<int>(LevelIndent) - Offset >= 0)
9398
LevelIndent -= Offset;
94-
assert(Line.Level < IndentForLevel.size());
95-
if ((!Line.First->is(tok::comment) || IndentForLevel[Line.Level] == -1) &&
96-
!Line.InPPDirective) {
97-
IndentForLevel[Line.Level] = LevelIndent;
98-
}
99+
IndentForLevel[Line.Level] = LevelIndent;
99100
}
100101

101102
private:
@@ -148,6 +149,7 @@ class LevelIndentTracker {
148149
/// at \p IndentForLevel[l], or a value < 0 if the indent for
149150
/// that level is unknown.
150151
unsigned getIndent(unsigned Level) const {
152+
assert(Level < IndentForLevel.size());
151153
if (IndentForLevel[Level] != -1)
152154
return IndentForLevel[Level];
153155
if (Level == 0)
@@ -159,7 +161,10 @@ class LevelIndentTracker {
159161
const AdditionalKeywords &Keywords;
160162
const unsigned AdditionalIndent;
161163

162-
/// The indent in characters for each level.
164+
/// The indent in characters for each level. It remembers the indent of
165+
/// previous lines (that are not PP directives) of equal or lower levels. This
166+
/// is used to align formatted lines to the indent of previous non-formatted
167+
/// lines. Think about the --lines parameter of clang-format.
163168
SmallVector<int> IndentForLevel;
164169

165170
/// Offset of the current line relative to the indent level.

0 commit comments

Comments
 (0)