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

Björn Schäpers via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 23 15:10:05 PST 2024


HazardyKnusperkeks wrote:

> > > I struggle with changes that encourage people to not be fully clang-formatted, I would prefer to ask why we need this feature, can we have some examples of where this would be used?
> > 
> > 
> > This makes it so only one line isn't formatted instead of the current solution which is 2 lines unformatted. Additionally, if you start writing code in between the unformatted region you inherit the unformatted scope. Being able to disable the formatter for only a single line means the formatter will be disabled for the shortest amount of code possible.
> 
> I understand what is can be used for, I'm asking why its needed? I don't understand why people are needing to unformat just one line, what is broken?
> 
> The implementation IMHO just complicates the code (I much prefer the isClangFormatOn() function than the parseXXXX())
> 
> I don't even deny it might be a nice to have, my concern is why do we continue to appease the people who don't want to use clang-format warts and all. I would prefer we put the effort into fixing the formatting issues which mean people are having to use this rather than marking hundreds of lines as // clang-format off.
> 
> Rather than complicating the code with a myriad of rules that effectively give half a dozen ways of doing the same thing. To me the current // clang-format off/on is enough, for everything else log an issue, or better still submit a pull request.

I think there are multiple reasons to disable formatting for a range, and be the range only one line. 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'}
    };
```
(Kindly borrowed from https://en.cppreference.com/w/cpp/coroutine/generator)
We will never be able to format something like this with clang-format.

I myself have a few places where automatic formatting would be possible, but kind of hard, they all have to do with some aligning.

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

Also this would bring us in par with clang-tidy which has `NOLINT` and `NOLINTNEXTLINE`.

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


More information about the cfe-commits mailing list