Author: Sedenion Date: 2023-07-18T13:32:07-07:00 New Revision: e4aa1428a28dce38cd4712b5720baa333cbdd25b
URL: https://github.com/llvm/llvm-project/commit/e4aa1428a28dce38cd4712b5720baa333cbdd25b DIFF: https://github.com/llvm/llvm-project/commit/e4aa1428a28dce38cd4712b5720baa333cbdd25b.diff LOG: [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 Added: Modified: clang/lib/Format/UnwrappedLineFormatter.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index 9413dbe59553bd..b745838c1cc5e4 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -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: @@ -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) @@ -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. _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits