[PATCH] D155094: Refactoring and asserts in LevelIndentTracker. (NFC)

Sedenion via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 12 08:32:14 PDT 2023


Sedeniono created this revision.
Sedeniono added reviewers: owenpan, MyDeveloperDay, HazardyKnusperkeks.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added a reviewer: rymiel.
Sedeniono requested review of this revision.

- 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 valud index into IndentForLevel.

- Added explanation regarding the intention of IndentForLevel.

This refactoring was split from https://reviews.llvm.org/D151047.
But both patches are independent of each other.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155094

Files:
  clang/lib/Format/UnwrappedLineFormatter.cpp


Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ 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<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;
+    if (!Line.InPPDirective) {
+      unsigned LevelIndent = Line.First->OriginalColumn;
+      if (static_cast<int>(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<int> IndentForLevel;
 
   /// Offset of the current line relative to the indent level.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155094.539579.patch
Type: text/x-patch
Size: 1974 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230712/908f2030/attachment.bin>


More information about the cfe-commits mailing list