From e4aa1428a28dce38cd4712b5720baa333cbdd25b Mon Sep 17 00:00:00 2001 From: Sedenion <39583823+Sedeniono@users.noreply.github.com> Date: Tue, 18 Jul 2023 13:23:45 -0700 Subject: [PATCH] [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 --- clang/lib/Format/UnwrappedLineFormatter.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index 9413dbe..b745838 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -88,14 +88,15 @@ public: /// 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(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: @@ -148,6 +149,7 @@ private: /// 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 +161,10 @@ private: 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. -- 2.7.4