llvm / llvm/llvm-project

[clang-format] formatting causes 'misleading indentation' warning

Open
#195,633 4 comments 1 reaction 0 assignees View on GitHub
clang-format confirmed
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

The following code causes clang to print a 'Misleading indentation' warning when formatted ~with Microsoft style~ (not dependent on style).

This occurred in a big company codebase and the following is the reduced example with names replaced by placeholders and minimized as far as possible while still triggering the bug.

```c++
#define CONFIG_ENABLED 1
#define CONFIG_DISABLED1 0
#define CONFIG_DISABLED2 0
#define CONFIG_DISABLED3 0

void func() {}

struct container {
#if CONFIG_ENABLED
struct
#if CONFIG_DISABLED1
{
#else
{
#endif
} test1;
#endif

#if CONFIG_ENABLED
struct {
int test() {
#if CONFIG_DISABLED2
#elif CONFIG_DISABLED3
#else
if (true)
func();
#endif
return 0;
}
} test2;
#endif
};
```

Test commands:

```sh
$ clang++ -Wmisleading-indentation -c test.cpp
$ clang-format -i test.cpp
$ clang++ -Wmisleading-indentation -c test.cpp
test.cpp:28:7: warning: misleading indentation; statement is not part of the previous 'if' [-Wmisleading-indentation]
28 | return 0;
| ^
test.cpp:25:5: note: previous statement is here
25 | if (true)
| ^
1 warning generated.
```

Versions:

```sh
$ clang++ --version
clang version 22.1.3
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$ clang-format --version
clang-format version 22.1.3
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.