<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/56114>56114</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            CompactNamespaces misbehaves with a peculiar size namespace chain
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          veimmone
      </td>
    </tr>
</table>

<pre>
    Hello,

Just started using clang-format and ran into a weird issue - for a specific namespace chain that I use, the namespaces are formatted as
```
namespace Foo {
  namespace Bar {
    namespace Baz {

int foo{42};

}}}
```
instead of the expected
```
namespace Foo { namespace Bar {
namespace Baz {

int foo{42};

}}}
```

For easy reproduction, I boiled the test case down to this:

`.clang-format`:
```
BasedOnStyle: LLVM

CompactNamespaces: true
ColumnLimit: 100
```

The test input I'm using is:
```
// When the namespace chain is 5-deep and of a peculiar length in total, nested indentation is used:
namespace Aaaaaaaa { namespace Bbbbbbbb { namespace Cccccccc { namespace Dddddddd { namespace Eeeeeee {

int foo{42};

}}}}}

// A just slighly longer 5-deep namespace chain works correctly:
namespace Aaaaaaaa { namespace Bbbbbbbb { namespace Cccccccc { namespace Dddddddd { namespace Eeeeeeee {

int foo{42};

}}}}}

// 6-deep decides to combine on the lower line - not correct either
namespace Aa { namespace Bb { namespace Cc { namespace Dd { namespace Ee { namespace Ff { namespace G {

int foo{42};

}}}}}}

// 4-deep works correctly
namespace Aaaaaaaaaaaaa { namespace Bbbbbbbbbbbbb { namespace Ccccccccccccc { namespace Ddddddddddddd {

int foo{42};

}}}}

// Also a simple comment at the end fixes it:
namespace Aaaaaaaa { namespace Bbbbbbbb { namespace Cccccccc { namespace Dddddddd { namespace Eeeeeee { // Format-fix

int foo{42};

}}}}}
```

And the result I'm getting is:
```
// When the namespace chain is 5-deep and of a peculiar length in total, nested indentation is used:
namespace Aaaaaaaa {
  namespace Bbbbbbbb {
    namespace Cccccccc {
      namespace Dddddddd {
        namespace Eeeeeee {

int foo{42};

}}}}}

// A just slighly longer 5-deep namespace chain works correctly:
namespace Aaaaaaaa { namespace Bbbbbbbb { namespace Cccccccc { namespace Dddddddd {
namespace Eeeeeeee {

int foo{42};

}}}}}

// 6-deep decides to combine on the lower line - not correct either
namespace Aa {
namespace Bb { namespace Cc { namespace Dd { namespace Ee { namespace Ff { namespace G {

int foo{42};

}}}}}}

// 4-deep works correctly
namespace Aaaaaaaaaaaaa { namespace Bbbbbbbbbbbbb { namespace Ccccccccccccc {
namespace Ddddddddddddd {

int foo{42};

}}}}

// Also a simple comment at the end fixes it:
namespace Aaaaaaaa { namespace Bbbbbbbb { namespace Cccccccc { namespace Dddddddd {
namespace Eeeeeee { // Format-fix

int foo{42};

}}}}}
```

Note that the different namespace chains have the same length in characters. I was also able reproduce this with `ColumnLimit: 80` and correspondingly a 5-deep namespace chain 20 characters shorter, so it appears something peculiar happens with a 5-deep namespace chain just slightly longer than `ColumnLimit`.

I tested this with `Ubuntu clang-format version 14.0.0-1ubuntu1` and `clang-format version 15.0.0 (https://github.com/llvm/llvm-project.git 21eb1af469c3257606aec2270d544e0e8ecf77b2)` the latter of which I compiled from source - with identical results from both.

Not a huge issue since it's easily fixed by that comment at the end, but maybe this is an edge case in the code that affects other things as well.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztV02T2zYM_TXyBWONJMtfBx-8u912O2l6aNqeKRGymFKkh6TWcX59QcofWtneaZO2mUwja2SLpAng4fGRKDTfr35AKXWU3UfJQ5Ssu-ePrXVgHTMOObRWqA2UkqnNuNKmYQ6Y4mCYAqGcBgY7FIaDsLZFGAONoTa7xVJUogTFGrRbViKUNRMKXE0TPNGsSEbpDc8jLDCD0Nnwlpk9ODVLDnd4Pc_4qDVE87uuGXqm7pjpd7zs-nju6p4UBlnV1Jpn0fwhmrzo9S3dfc0ZoaxDxkFXIRb8QHGT73_N8Vse_2veds9HyhAyuweD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