Skip to content

Commit

Permalink
[clang-format] Refactoring and asserts in LevelIndentTracker. (NFC)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Sedeniono authored and owenca committed Jul 18, 2023
1 parent 4bbf371 commit e4aa142
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions clang/lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,15 @@ class LevelIndentTracker {
/// level to the same indent.
/// Note that \c nextLine must have been called before this method.
void adjustToUnmodifiedLine(const AnnotatedLine &Line) {
if (Line.InPPDirective)
return;
assert(Line.Level < IndentForLevel.size());
if (Line.First->is(tok::comment) && IndentForLevel[Line.Level] != -1)
return;
unsigned LevelIndent = Line.First->OriginalColumn;
if (static_cast<int>(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;
}
IndentForLevel[Line.Level] = LevelIndent;
}

private:
Expand Down Expand Up @@ -148,6 +149,7 @@ class LevelIndentTracker {
/// 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)
Expand All @@ -159,7 +161,10 @@ class LevelIndentTracker {
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<int> IndentForLevel;

/// Offset of the current line relative to the indent level.
Expand Down

0 comments on commit e4aa142

Please sign in to comment.