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

    <tr>
        <th>Summary</th>
        <td>
            [clang-format] IncludeCategories: Placing of main file is broken for specific orders
        </td>
    </tr>

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

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

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

<pre>
     - As soon, any regex is placed at **position/`Priority: 0` with `SortPriority:    0`**, the main include will be moved to the first group/priority matching it, if it is not the first include in this group.  If it is the first include, it stays in block / at priority 0, if not it is moved to group/priority matching it.
 - If the group shares the same Priority(0) but the SortPriority is greater to zero, the main file stays in its group, when it is at position 1 and is not resorted. 
 - If the group shares the same Priority(0) but the SortPriority is less to zero, there is no way to prevent resorting. A new group is created, all other headers will be placed in here, the new group is ordered by its `SortPriority`, not `Priority !`, so it may be placed at the top instead of 0.
 - the main file stays in its group, when it is at position 1 and is not resorted. 
 - As soon as any regex, which matches also the main-include is ordered before (Priority < 0), the main-include is not treated anymore as main include and is added to that group matching it. That will prevent it from staying at priority 0.

Expectation: the main include should __always__ stay in group() 0 / sorted with priority 0, since it is currently not controllable, where to match it. All other includes, ordered into group/ with priority 0 should be ordered with the main include, depending on the `SortPriority` parameter. It is the `.clang-format`'s file maintainers job to sort the main-include at the top if that's still desired.
This is also that what the documentation states.


Example code to mess around with:
```
IncludeCategories:
  - Regex: '^<[Q][^/.]*>$'
    Priority:        -1
    CaseSensitive:   true
 - Regex:           '^<[a-z_]*>$'
    Priority:        -1
 CaseSensitive:   true
  - Regex:           '^<[^/]*\.h>$'
 Priority:        -1
  - Regex:           '^<.*\.h>$'
    Priority: -1
  - Regex:           '^<.*>$'
    Priority:        -1
  - Regex:           '^"[^/]*\.h"$'
    Priority:        0
 SortPriority:    -1
  - Regex:           '^".*\.h"$'
    Priority: 1
    SortPriority:    1
  - Regex:           '^".*"$'
 Priority:        10

```

Related: #58284
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VlGP2joT_TXmZURknE2ABx4ou0h969f2feXEA3Hr2JHtLKW__pPtEAK7d2_36l5UUbEez5k5c84k3Dl51IgbUnwixeOM974xdrPHqsJZZcR5A3PYOnDGaMJ2wPUZLB7xF0gHneI1CuAeCNsStu2Mk16GwD0p6RcrjZX-TPItUFJSOEnfACnpN2P99BAgnqccAcQ3CC2XGqSuVS8QTlIpqBBa84ICvIkRB2mdh6M1fUfYvhsSQst93Uh9BOlDLnkA6UOx2vjJtUtmqcE30qU0GcDnS_ir0JjMg_P87MK1Spn6JxC2D_2P6HTADGgp0Vjze5VmhD4SuoV5KCBAx2BwDbeYanG8RRhZYytK2BqqPvU0ZRRiN8g92oD6G6254fQgFV67kN5dCtvBqUE9VB16GqYJC-BaXCi06Iz1KDL4d0tW6NxdvRYTKJz4ORx1Fl9QX0qQ-pjBFjSeBmTpoI59iyhUpcCEJNAgF2jdKKJBtVJDgLhwc5PHWIEWBVTnSNC9Zss45cDGROZA2GI4cSaw2PLzBI6ntr3pQGrnkQswB6DXwf-HAxr8C9xd_ZvSybpJMkQHXDkzVjEfDTJhAw_GIhC2uvac74Lk11OFTa9G06WhBOg23Ofu1txD7VyIi7f5YOsbh8D38Pc4xIsQpIeDNW0kK0TdGHFgNn0__eqw9jwup3z7esG4xvRKwPMzVyd-ds_PMWfgf-B-FbRLo90TvWmb3freSV3jMJ-6txa1V-fIQW20t0YpXikcBmmDGFKHsb3tKNihKBcCL9xLPdkg99iX8isc42PEfZshocAOtQhsGR0DXosbOm55ix5tBp_HZUhKmtWK6-P8YGzLfdT60iW5BhTPpQ4--2Gq0Fmg6bUmpjY4xFHHJM6HuQp00qIYJvc97GU56jIMvxmuC1P3Leo00DAqj-5m3pep87ZTCLURieywY7g1vU4EkfwSXtLhX_z5ORW74x6Pxkp0YyDAHL5G--RbIGxJiieS70jx6X-ke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