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

    <tr>
        <th>Summary</th>
        <td>
            clang-format does not allow main header file to be at bottom
        </td>
    </tr>

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

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

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

<pre>
    According to the IncludeCategories section in [the clang format style options docs](https://clang.llvm.org/docs/ClangFormatStyleOptions.html) "The main header for a source file automatically gets category 0 " and "However, you can also assign negative priorities if you have certain headers that always need to be first".

This suggest it should be possible to place other includes below the "main" header by using negative numbers for the priority. However, this does not seem to work. Instead the 

Tested with clang-format version 15.0.2 and 12.0.0.

E.g:

``` yaml
IncludeBlocks: Preserve
SortIncludes: CaseInsensitive
IncludeCategories:
  - Regex: '^(<.*>)'
    Priority: -2
  - Regex: '^(".*")'
    Priority: -1
```

``` c
#include "a.h"
#include "Aa.h"
#include <os.h>
#include "test.h"
#include "u.h"
```

``` bash
$ clang-format test.c
#include <os.h>
#include "a.h"
#include "Aa.h"
#include "test.h"
#include "u.h"
```

Note test.h, the "main" header, is sorted along with the other local includes.

Expected output:

```c
#include <os.h>
#include "a.h"
#include "Aa.h"
#include "u.h"
#include "test.h"
```

Likely the issue here is that the main header will be matched first by the `'^(".*")'` regex. If this has a higher priority, ctags-format will not apply the lower priority. I tried setting `SortPriority` to 1 for the local headers, but this did not have the required result.

### Use-case

My usual preferred way of sorting includes is basically the opposite of the LLVM / generally preferred style: have "system" includes on the top, followed by local includes and the main includes last.

The reasoning here is that this prevents behavioral changes due to macro flags. Preprocessor configurations as well as header files with many, often badly namespaced, macros are quite common in embedded C projects. This way e.g. headers setting macros impacting system headers need to be included explicitly in their own block, before system includes.

I get the idea behind the opposite order in order to ensure headers can stand alone, but for me this is often ensured in unit tests. Are there any other reasons for sticking to the "local includes first" approach?

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy9Vttu4zYQ_Rr5hYggy5fED37IOht0gWxbdLd9p8WRxEYSVZKK13_fM6Ss2IETFC3QQIhtkZzLmTOHszfquL0vCmOV7irhjfA1iS9d0QyKdtJTZawmJxwVXptO6E4kq0-8p2gkDpTGttIL548NCdPzHieUKVyyekjyu9r73iWL-yR_xBOOpE3z0qbGVngRNuaPO37_GCx9Y0O_RDtp7dsmyTciyfPv8NhKeK9JKrLsV0jhzGALEqWGczl4AwO6kE1zFBV5J4oY_1FkbELITvHnT-ZAL2STfCeOZsCmTsjGGSGd01UnOqpg5YVEbzVy95y9LsPWWuJ1Qda_BuKAF_KXzUEeHc6SYgz3HJN1Ht7SJHtIsvv4_3utAeVQVeS80ICtNkOjeHtv4H2PNHC6bySSMgDZAu9QCYc9jTmE4sAoI8EZjWDsj2JwXL4p9m5o9xwcw8RnxlyOqTjL3nM0ysB4ZxALUcveD8Y-p2CA8zAeHZ5ngMiR40H7OjLgZmQAbDomyHyVZmnEep7ja3YBwOe0YjacvUnWWXzEUaLa4d1Iv0-NKZ6ZPeJXS47sC8Xlb8b6cUtY3UlHiJc6pzn5CxuvFJ78CnEjfqOKfvDZJL9NVp_B1GSxS5P8Plngx4bfjnsFnEfsePtN_qENLjds5PmHNuZvMr8ORzG-yBcjB7jwMq3Z-pWV-_eWFjuDTuK8rpzyKOe7JofzlY-D3UtXn0wsL4kRXFxJ5sO4_kWe_zWZn40ncTKxu95pvMAdDAKiCWRj0HOhFXh3bFhwVjZT216S_0cPFcVBM_h-8O81wv-C1fCPUbyG1ZN-JogsZ62dGwjwWP4axdC_0eqDbhrWOLChqJF-kEYWrYAxLL_bP2CW5S6DHpVRrmrpoPu1rhjrk6pxWQovK3fiXPDIoib7fowT6nl2AgaFhyooyJ73LJ3wxboyNSpcQwznk4LGuo6izw73gx8VVKvgK9wOvNXSX4O2sA3VGhp_wQFgHR_xu6ObAsp1vvqVhXyAn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