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

    <tr>
        <th>Summary</th>
        <td>
            [clang-format] Indent preprocessor directives according to current scope
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    clang-format cannot indent preprocessor directives to the current scope. There exists also no option for this. Whilst IndentPPDirectives allows for pp directives to be indented relative to each other, there is no option to allow this for the scope of the current code.
The desired behaviour is:
```c
void foo()
{
    if (bar) {
        A;
        #ifdef D
            #define E
            B;
        #endif
        C;
    }
}
```
clang-format's output is:
```c
void foo()
{
    if (bar){
        A;
#ifdef D
    #define E
        B;
#endif
        C;
    }
}
```
It seems this has been requested at least since 2013:
https://bugs.llvm.org/show_bug.cgi?id=17362
With follow-up issues having code suggestions, but never making it into clang-format it seems:
https://bugs.llvm.org/show_bug.cgi?id=36019
https://bugs.llvm.org/show_bug.cgi?id=36020
https://bugs.llvm.org/show_bug.cgi?id=39280

My personal use-case is with including of Unicode [codepoint lists](https://github.com/FrostKiwi/treasurechest#j%C5%8Dy%C5%8D-kanji-unicodes-lists) for custom font configs. I want:
```c
void init_gui(struct state *gctx)
{
    ...
    nk_rune font_ranges[] = {
        0x0020, 0x007E, /* Ascii */
        0x00A1, 0x00FF, /* Symbols + Umlaute */
        0x0410, 0x044F, /* Main Cyrillic */

        0x3001, 0x3003, /* 、。〃 */
        0x3005, 0x301F, /* 々〆〇 and brackets */
        0x3036, 0x3036, /* 〶 */
        0x3041, 0x309F, /* Hiragana */
        0x30A0, 0x30FF, /* Katakana */
        #include "joyo-unicode.txt"
        0};
    ...
}
```
But it always becomes:
```c
void init_gui(struct state *gctx)
{
    ...
    nk_rune font_ranges[] = {
        0x0020, 0x007E, /* Ascii */
        0x00A1, 0x00FF, /* Symbols + Umlaute */
        0x0410, 0x044F, /* Main Cyrillic */

        0x3001, 0x3003, /* 、。〃 */
        0x3005, 0x301F, /* 々〆〇 and brackets */
        0x3036, 0x3036, /* 〶 */
        0x3041, 0x309F, /* Hiragana */
        0x30A0, 0x30FF, /* Katakana */
#include "joyo-unicode.txt"
        0};
    ...
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztVt1v4jgQ_2vCi0WUOHwkD3mAUrTVaqWV7lb7WDnOkLgYO2c7pfz3N05ICxR60vVunxYhGH_MzM_j38y40OUh55KparzRZscc4Uwp7YhQJShHGgON0Rys1YaUwgB34hkscZq4GghvjfHbLNcNhOTPGgwQeBHWWcKk1URpohsntCJoHlWEDcnPWkjryEPn4fv31ZtVJqXe225r01y4K-CICUpiQDK_4OeB8ZpoBGMCeudBIQJhTxzjns5u5_0IA3rERG_OjsF1CWEQrYJogUchJViEUKLrmj0L3Ro0HCSLfkMwi_ov78fPWpRoXQc0DWh23DNf9gLBj9gQXCsY4szI2Yr_LILkYiagidiUsCGr8_njGq4IBeT-_eLymilQpdicz96d7QvmqwH06uKE_fCUJQGdW6Jb17TuP4vJByG5FoqbIVieqn3-2A_IboCd7elTM4t0AIUU_KsF68mIOSOBIaGtUBwIjeLkNSC1c00XHrrGb9FWNpTyeRdqU-HY1nr_iJMhr0SQrEUZJKt4nsxor_xTuBqj57k7bhuMs0WPxFNRVR1ViW2rCkEgy60nf4G3oeAZDNmxrd8kfB4j_88SXBwP9AmQySyKs08o0-hfK2c0PSr3v98OpAFjtWKStBbGnNku__c-eHghsi19JDDRfyjRBS2YLv1_ozE0RPpSFUxXyMJzMBXqt0XI9Q4Ha6Ot-yr2AmVn8LJbrEw1Rh4Z9hTQ6d0Uf9LV4U0cb5l6EuO292nHvR9MfF9_eGud3qHYlRy1EXhu8kD2TLmPU0ko4R6rFmGk1pmW40065vBIdFFx93I9x8IwfBuo7aNpMWe870eDrAA8_RIDQDC476tS9BL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