<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/63187>63187</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang-format] Wrong indentation of macro body with selective formatting
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Sedeniono
</td>
</tr>
</table>
<pre>
Consider the file `test.cpp`:
```C++
class Foo{
void test() {
#define A \
int aaaa; \
int b; \
int dddddddddd;
}
};
```
The `.clang-format` file contains only
```
IndentPPDirectives: None
```
Running `clang-format.exe ./test.cpp --lines=5:5`, where line 5 is the one with `int b` results in
```C++
class Foo{
void test() {
#define A \
int aaaa; \
int b; \
int dddddddddd;
}
};
```
I.e. the macro body gets aligned with `void test()`. The expectation is that the macro body has an indent of 2 spaces:
```C++
class Foo {
void test() {
#define A \
int aaaa; \
int b; \
int dddddddddd;
}
};
```
I noticed that while working on [D151047](https://reviews.llvm.org/D151047) and shortly debugged into: In `LevelIndentTracker::nextLine()`, the code calls `getIndent(Line.Level)` when formatting the macro because of `PPDIS_None` (see [here](https://github.com/llvm/llvm-project/blob/9b9848fb4d04fb8d341c5035595c1531b7bd36e0/clang/lib/Format/UnwrappedLineFormatter.cpp#L65-L78)). The `Level` of the macro body is 1. `LevelIndentTracker::IndentForLevel[Level=1]` contains the indent of the `void test()` line. Hence the wrong indent.
>From what I could gather, `LevelIndentTracker::IndentForLevel` should **never** be used when formatting PP directives. `LevelIndentTracker::IndentForLevel` appears to be for non-PP code only?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8Vl1v6jgT_jXmZkTkOOTrggtaFL2Vqldoz1nt5cqxJ4n3BBvZBtp_v5qkpbSHs2q10iIkYOwZe56PCTIE01vENcvvWL5dyGMcnF9_Q43WOOsWrdPP63tng9HoIQ4InRkRWMEjhpiow4EVnGUbxreMb-j79L5n4o7eU1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