[clang] e98939a - [clang-format] Use 64-bit penalties in the optimizing line formatter (#236087)

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 2 13:02:45 PST 2026


Author: Yash Aggarwal
Date: 2026-11-02T21:02:39Z
New Revision: e98939a7611d225ad49ec7f8dc2494dc32cdeaa8

URL: https://github.com/llvm/llvm-project/commit/e98939a7611d225ad49ec7f8dc2494dc32cdeaa8
DIFF: https://github.com/llvm/llvm-project/commit/e98939a7611d225ad49ec7f8dc2494dc32cdeaa8.diff

LOG: [clang-format] Use 64-bit penalties in the optimizing line formatter (#236087)

The penalty in OptimizingLineFormatter::analyzeSolutionSpace() was
summed in an unsigned. On a long braced list where every row goes over
the column limit, the total goes past 2^32 and wraps, so a worse layout
can end up with the smaller penalty. With 81 rows of 100-digit literals
the brace moved to its own line and everything got indented 4 more.

This keeps the search penalty (and OrderedPenalty) in 64 bits, and
clamps the value returned from formatLine() to UINT_MAX so callers that
add it up don't wrap either.

Penalties of a single token can still overflow inside
ContinuationIndenter if one token is thousands of columns past the
limit. I left that alone to keep this small.

Fixes #235788

Added: 
    

Modified: 
    clang/lib/Format/Format.cpp
    clang/lib/Format/UnwrappedLineFormatter.cpp
    clang/lib/Format/UnwrappedLineFormatter.h
    clang/unittests/Format/FormatTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index d83ca4a558b7f8..9b34a9d74e7d5b 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3032,7 +3032,7 @@ class Formatter : public TokenAnalyzer {
     ContinuationIndenter Indenter(Style, Tokens.getKeywords(),
                                   Env.getSourceManager(), Whitespaces, Encoding,
                                   BinPackInconclusiveFunctions);
-    unsigned Penalty =
+    uint64_t Penalty =
         UnwrappedLineFormatter(&Indenter, &Whitespaces, Style,
                                Tokens.getKeywords(), Env.getSourceManager(),
                                Status)
@@ -3045,7 +3045,8 @@ class Formatter : public TokenAnalyzer {
     for (const auto &R : Whitespaces.generateReplacements())
       if (Result.add(R))
         return std::make_pair(Result, 0);
-    return std::make_pair(Result, Penalty);
+    return std::make_pair(
+        Result, static_cast<unsigned>(std::min<uint64_t>(Penalty, UINT_MAX)));
   }
 
 private:

diff  --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index a76738d67848b2..90a3994703b664 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -1253,7 +1253,7 @@ class LineFormatter {
   /// Formats an \c AnnotatedLine and returns the penalty.
   ///
   /// If \p DryRun is \c false, directly applies the changes.
-  virtual unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
+  virtual uint64_t formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
                               unsigned FirstStartColumn, bool DryRun) = 0;
 
 protected:
@@ -1278,7 +1278,7 @@ class LineFormatter {
   /// \c UnwrappedLineFormatter, where we only have two options for each token:
   /// break or don't break.
   bool formatChildren(LineState &State, bool NewLine, bool DryRun,
-                      unsigned &Penalty) {
+                      uint64_t &Penalty) {
     const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
     bool HasLBrace = LBrace && LBrace->is(tok::l_brace) && LBrace->is(BK_Block);
     FormatToken &Previous = *State.NextToken->Previous;
@@ -1358,7 +1358,7 @@ class NoColumnLimitLineFormatter : public LineFormatter {
 
   /// Formats the line, simply keeping all of the input's line breaking
   /// decisions.
-  unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
+  uint64_t formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
                       unsigned FirstStartColumn, bool DryRun) override {
     assert(!DryRun);
     LineState State = Indenter->getInitialState(FirstIndent, FirstStartColumn,
@@ -1367,7 +1367,7 @@ class NoColumnLimitLineFormatter : public LineFormatter {
       bool Newline =
           Indenter->mustBreak(State) ||
           (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
-      unsigned Penalty = 0;
+      uint64_t Penalty = 0;
       formatChildren(State, Newline, /*DryRun=*/false, Penalty);
       Indenter->addTokenToState(State, Newline, /*DryRun=*/false);
     }
@@ -1384,9 +1384,9 @@ class NoLineBreakFormatter : public LineFormatter {
       : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}
 
   /// Puts all tokens into a single line.
-  unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
+  uint64_t formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
                       unsigned FirstStartColumn, bool DryRun) override {
-    unsigned Penalty = 0;
+    uint64_t Penalty = 0;
     LineState State =
         Indenter->getInitialState(FirstIndent, FirstStartColumn, &Line, DryRun);
     while (State.NextToken) {
@@ -1409,7 +1409,7 @@ class OptimizingLineFormatter : public LineFormatter {
 
   /// Formats the line by finding the best line breaks with line lengths
   /// below the column limit.
-  unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
+  uint64_t formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
                       unsigned FirstStartColumn, bool DryRun) override {
     LineState State =
         Indenter->getInitialState(FirstIndent, FirstStartColumn, &Line, DryRun);
@@ -1435,7 +1435,7 @@ class OptimizingLineFormatter : public LineFormatter {
   /// In case of equal penalties, we want to prefer states that were inserted
   /// first. During state generation we make sure that we insert states first
   /// that break the line as late as possible.
-  typedef std::pair<unsigned, unsigned> OrderedPenalty;
+  typedef std::pair<uint64_t, unsigned> OrderedPenalty;
 
   /// An edge in the solution space from \c Previous->State to \c State,
   /// inserting a newline dependent on the \c NewLine.
@@ -1464,7 +1464,7 @@ class OptimizingLineFormatter : public LineFormatter {
   /// to a state where all tokens are placed. Returns the penalty.
   ///
   /// If \p DryRun is \c false, directly applies the changes.
-  unsigned analyzeSolutionSpace(LineState &InitialState, bool DryRun) {
+  uint64_t analyzeSolutionSpace(LineState &InitialState, bool DryRun) {
     std::set<LineState *, CompareLineStatePointers> Seen;
 
     // Increasing count of \c StateNode items we have created. This is used to
@@ -1478,7 +1478,7 @@ class OptimizingLineFormatter : public LineFormatter {
     Queue.push(QueueItem(OrderedPenalty(0, Count), RootNode));
     ++Count;
 
-    unsigned Penalty = 0;
+    uint64_t Penalty = 0;
 
     // While not empty, take first element and follow edges.
     while (!Queue.empty()) {
@@ -1534,7 +1534,7 @@ class OptimizingLineFormatter : public LineFormatter {
   ///
   /// Assume the current state is \p PreviousNode and has been reached with a
   /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
-  void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
+  void addNextStateToQueue(uint64_t Penalty, StateNode *PreviousNode,
                            bool NewLine, unsigned *Count, QueueType *Queue) {
     if (NewLine && !Indenter->canBreak(PreviousNode->State))
       return;
@@ -1562,7 +1562,7 @@ class OptimizingLineFormatter : public LineFormatter {
       Best = Best->Previous;
     }
     for (const auto &Node : llvm::reverse(Path)) {
-      unsigned Penalty = 0;
+      uint64_t Penalty = 0;
       formatChildren(State, Node->NewLine, /*DryRun=*/false, Penalty);
       Penalty += Indenter->addTokenToState(State, Node->NewLine, false);
 
@@ -1582,7 +1582,7 @@ class OptimizingLineFormatter : public LineFormatter {
 
 } // anonymous namespace
 
-unsigned UnwrappedLineFormatter::format(
+uint64_t UnwrappedLineFormatter::format(
     const SmallVectorImpl<AnnotatedLine *> &Lines, bool DryRun,
     int AdditionalIndent, bool FixBadIndentation, unsigned FirstStartColumn,
     unsigned NextStartColumn, unsigned LastStartColumn) {
@@ -1596,7 +1596,7 @@ unsigned UnwrappedLineFormatter::format(
     return CacheIt->second;
 
   assert(!Lines.empty());
-  unsigned Penalty = 0;
+  uint64_t Penalty = 0;
   LevelIndentTracker IndentTracker(Style, Keywords, Lines[0]->Level,
                                    AdditionalIndent);
   const AnnotatedLine *PrevPrevLine = nullptr;

diff  --git a/clang/lib/Format/UnwrappedLineFormatter.h b/clang/lib/Format/UnwrappedLineFormatter.h
index a33a4f98df2560..1761ef53c47513 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.h
+++ b/clang/lib/Format/UnwrappedLineFormatter.h
@@ -35,7 +35,7 @@ class UnwrappedLineFormatter {
         Keywords(Keywords), SourceMgr(SourceMgr), Status(Status) {}
 
   /// Format the current block and return the penalty.
-  unsigned format(const SmallVectorImpl<AnnotatedLine *> &Lines,
+  uint64_t format(const SmallVectorImpl<AnnotatedLine *> &Lines,
                   bool DryRun = false, int AdditionalIndent = 0,
                   bool FixBadIndentation = false, unsigned FirstStartColumn = 0,
                   unsigned NextStartColumn = 0, unsigned LastStartColumn = 0);
@@ -57,7 +57,7 @@ class UnwrappedLineFormatter {
   // starting from a specific additional offset. Improves performance if there
   // are many nested blocks.
   std::map<std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned>,
-           unsigned>
+           uint64_t>
       PenaltyCache;
 
   ContinuationIndenter *Indenter;

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index f38287757ffba1..2be65b0d7301a7 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -20440,6 +20440,17 @@ TEST_F(FormatTest, BreakPenaltyScopeResolution) {
                Style);
 }
 
+TEST_F(FormatTest, PenaltyDoesNotOverflow) {
+  // Indenting the rows more would make the total penalty go past 2^32.
+  auto Style = getLLVMStyleWithColumns(20);
+  Style.PenaltyExcessCharacter = 200'000'000;
+  verifyFormat("int t[] = {\n"
+               "    11111111111111111111,\n"
+               "    11111111111111111111,\n"
+               "};",
+               Style);
+}
+
 TEST_F(FormatTest, WorksFor8bitEncodings) {
   // FIXME: unstable test case
   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"


        


More information about the cfe-commits mailing list