[clang] [clang-format] extend clang-format directive with options to prevent formatting for one line (PR #118566)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 23 19:55:38 PST 2024


owenca wrote:

> A nice multiline example I stumbled upon is:
> 
> ```c++
>     Tree<char> tree[]
>     {
>                                     {'D', tree + 1, tree + 2},
>         //                            │
>         //            ┌───────────────┴────────────────┐
>         //            │                                │
>                     {'B', tree + 3, tree + 4},       {'F', tree + 5, tree + 6},
>         //            │                                │
>         //  ┌─────────┴─────────────┐      ┌───────────┴─────────────┐
>         //  │                       │      │                         │
>           {'A'},                  {'C'}, {'E'},                    {'G'}
>     };
> ```

Which line(s) do you want clang-format to skip here?

> And [#54334 (comment)](https://github.com/llvm/llvm-project/issues/54334#issuecomment-2531049984) makes a good example on why only one line disabling would be desired.

The current way to skip the line in that example is as follows:
```cpp
int foo(Resources *resources, int i, int j, int k) {
  if (i < 0 && j < 0) {
    // clang-format off
    myproject::detail::LogErrorPrintf( resources->logger, "both i and j can not be negative at the same time.\ni = %d, j = %d\n", i, j);
    // clang-format on
    return -1;
  }

  if (i < 0) {
    j *= 10;
  }
  if (j < 0) {
    k += 5;
  }
  return i + j * k;
}
```
What's wrong with that other than it may be less convenient? Actually, I prefer the current way as it makes the skipped line stand out. If we were to add `// clang-format off-next-line`, would "next line" mean the next physical or logical/unwrapped line?


https://github.com/llvm/llvm-project/pull/118566


More information about the cfe-commits mailing list