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

    <tr>
        <th>Summary</th>
        <td>
            [clang-format] IncludeCategories: Add a regex group named main into the Regex Solver
        </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>
    There are many issues, regarding main file sorting.
Also, we have the issue, that a main source file can have 2 main headers, e.g. `a.cpp` `a.h`  and `a_priv.h`

With IncludeIsMainRegex and IncludeIsMainSourceRegex only a mapping between a header to a source can be defined, but not the other way round.

A solution to all issues would be, to add a parameter in the form of a named capture group to the regex.
For example, a hypothetical group definition (?<main>regex) would map to `\k<main>`, which then maps to the main file's stem.

```
IncludeCategories:
  - Regex: '\k<main>_priv\.h"$'
    Priority:        0
```

The above would then match `a_priv.h` as expected.
With that, programmers could also override the default behavior, to accomplish:

#58284
#37620 #39735 #53013

Possible implementation could be:
 - generate all possible main-header stems from IncludeIsMainRegex and IncludeIsMainSourceRegex 
 - supply all (including the original stem) to the Regex solver as group (do this in a loop, if it's not possible to accomplish this via one group)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVUFv4zYT_TX0ZWBDpizJPuiQL_kM7KHAortAj8VIHIlsKVIgKSf-98VQsndT7KVBoFDUzPDN43sTjNGMjqgV1f9E9bbDJWkf2it1He06r-7td02BAAPBhO4OJsaFopCvEGjEoIwbYULjYDCWIPqQjBsPongTxcuLjZ4j3wk03giSpjWfN5PGBLjmRr-EntYSPbo1Wq7fNKGikE-kw3gAURd46OdZ1MW61rwCdCq__jkHc8t7K4b1-YdJGr643i6KvsTf0LjfaaSPnPVp-1tGsn70zt4zwnnmLjtK70QOcIMEyQM-oDPqjkDRYBwpBtstCZxPuWmfNAV4xzsEvzh1-Bn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