[libcxx-commits] [PATCH] D109835: [NFC][libc++] Update clang-format style.

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Thu Sep 23 10:58:25 PDT 2021


ldionne added inline comments.


================
Comment at: libcxx/.clang-format:16
+IndentPPDirectives: AfterHash
+PPIndentWidth: 1
+
----------------
Quuxplusone wrote:
> ldionne wrote:
> > ldionne wrote:
> > > Mordante wrote:
> > > > ldionne wrote:
> > > > > Does this mean it'll always be only indented by one space? What we need is to indent it to tabs, whatever the size of tabs are in a given file. Is there a way to achieve that?
> > > > I assume you mean our standard indention of 2 spaces with "tabs". That would be achieved by removing this line, then it uses the default indention. I added this since you requested a one space indention D103368 and I've seen that indention used before in libc++.
> > > Sorry if we misunderstood each other -- what I want is for `#if`s to be indented consistently with the surrounding indentation, period. So if the surrounding indentation uses 4 tabs, nested preprocessor directives should look like:
> > > 
> > > ```
> > > #if CONDITION
> > > #   define FOO
> > >     ^ 5th column, i.e. indentation consists of 1x'#' and 3x' '
> > > #endif
> > > ```
> > > 
> > > Similarly, if surrounding indentation uses 2 spaces, then it should look like:
> > > 
> > > ```
> > > #if CONDITION
> > > # define FOO
> > >   ^ 3rd column, i.e. indentation consists of 1x'#' and 1x' '
> > > #endif
> > > ```
> > > 
> > > Does that make sense?
> > > So if the surrounding indentation uses ~~4 tabs~~ 4 spaces
> > 
> > (just to clarify what I meant)
> FWIW, bleagh. I've never seen libc++ do
> ```
> void f() {
> #if SOMETHING
>     int x;
> #    if SOMETHINGELSE
>     int y;
> #    endif // SOMETHINGELSE
> #endif // SOMETHING
> }
> ```
> nor do I think that's a good style. I think that in running code libc++'s style is generally "no indents at all":
> ```
> void f() {
> #if SOMETHING
>     int x;
> #if SOMETHINGELSE
>     int y;
> #endif // SOMETHINGELSE
> #endif // SOMETHING
> }
> ```
> and in preprocessor code it's generally "one space":
> ```
> #if SOMETHING
> # define X y
> # if SOMETHINGELSE
> #  define Y z
> # endif // SOMETHINGELSE
> #endif // SOMETHING
> ```
> Of course as usual clang-format can't handle that complicated of a rule (it has no notion of "preprocessor code" versus "normal code"). Ultimately we just have to just pick //something// for the .clang-format file and advise people to ignore it if it seems to be doing the wrong thing.
My comment only referred to "preprocessor code", not "normal code". Indeed, we don't do

```
void f() {
#if SOMETHING
    int x;
#    if SOMETHINGELSE
    int y;
#    endif // SOMETHINGELSE
#endif // SOMETHING
}
```

However, what I want to avoid at all costs is unreadable crazy stuff like this:

```
#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
#if defined(__ANDROID__) && __ANDROID_API__ >= 30
#define _LIBCPP_HAS_COND_CLOCKWAIT
#elif defined(_LIBCPP_GLIBC_PREREQ)
#if _LIBCPP_GLIBC_PREREQ(2, 30)
#define _LIBCPP_HAS_COND_CLOCKWAIT
#endif
#endif
#endif
```

If anyone wrote normal code with `if`s without indentation like that, we'd tell them it's unreadable. The same applies to the preprocessor.

To go back to this review, I think what we want is

```
# libc++'s preferred indentions of preprocessor statements.
IndentPPDirectives: AfterHash
```

That's it. We don't specify the tab size cause that depends on what file you're in.



Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D109835/new/

https://reviews.llvm.org/D109835



More information about the libcxx-commits mailing list