# [PATCH v2 1/3] clang-format: change column limit to 96 characters

7 messages from 2024-10-10 to 2024-10-10. Participants: Karthik Nayak, Kyle Lippincott, karthik nayak.
Thread: https://gitlist.dev/t/62304

## Karthik Nayak, 2024-10-10 17:59

Subject: [PATCH v2 0/3] clang-format: fix rules to make the CI job cleaner
Message-ID: <cover.1728582927.git.karthik.188@gmail.com>
URL: https://gitlist.dev/e/cover.1728582927.git.karthik.188%40gmail.com
In-Reply-To: <CAOLa=ZRvFBhageS65uE5enzLBz7H_CAvvnEcPsi_QAi0exRx2w@mail.gmail.com>

```
The clang-format CI job is currently cluttered due to too many errors being
reported. See some of the examples here:

* https://gitlab.com/gitlab-org/git/-/jobs/7854601948
* https://gitlab.com/gitlab-org/git/-/jobs/7843131109

So modify the clang-format with the following changes:
1. Modify the column limit to 96 to provide some slack so we can make code more
readable and don't have to be constrained to 80 characters always. This is the
cause of most of the errors reported by the tool and should cleanup the reports
so we can actually focus on the real remaining issues.
2. Don't align expressions after linebreaks to ensure that we instead rely on
'ContinuationIndentWidth'. This fix is rather small and ensures that instead of
trying to align wrapped expressions, we follow the indentation width.
3. Align the macro definitions. This is something we follow to keep the macros
readable. 

I will still keep monitoring the jobs from time to time to ensure we can fine
tune more as needed, if someone see's something odd, do keep me in the loop.

Thanks

Changes over the previous version:
1. Change the column limit from 0 to 96, this way we still have a column limit
but provide some slack.
2. Add an example for the third commit.

Karthik Nayak (3):
  clang-format: change column limit to 96 characters
  clang-format: don't align expressions after linebreaks
  clang-format: align consecutive macro definitions

 .clang-format | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

Range-diff against v1:
1:  34cd0c29f6 ! 1:  e22ffbe0f6 clang-format: don't enforce the column limit
    @@ Metadata
     Author: Karthik Nayak <karthik.188@gmail.com>
     
      ## Commit message ##
    -    clang-format: don't enforce the column limit
    +    clang-format: change column limit to 96 characters
     
         The current value for the column limit is set to 80. While this is as
         expected, we often prefer readability over this strict limit. This means
         it is common to find code which extends over 80 characters. So let's
    -    change the column limit to be 0 instead. This ensures that the formatter
    -    doesn't complain about code strictly not following the column limit.
    +    change the column limit to be 96 instead. This provides some slack so we
    +    can ensure readability takes preference over the 80 character hard
    +    limit.
     
         Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
     
    @@ .clang-format: UseTab: Always
      ContinuationIndentWidth: 8
     -ColumnLimit: 80
     +
    -+# While we recommend keeping column limit to 80, we don't want to
    -+# enforce it as we generally are more lenient with this rule and
    -+# prefer to prioritize readability.
    -+ColumnLimit: 0
    ++# While we recommend keeping column limit to 80, we want to also provide
    ++# some slack to maintain readability.
    ++ColumnLimit: 96
      
      # C Language specifics
      Language: Cpp
2:  3557012fea = 2:  b55d5d2c14 clang-format: don't align expressions after linebreaks
3:  ad023a6cf6 ! 3:  6ebcd2690e clang-format: align consecutive macro definitions
    @@ Metadata
      ## Commit message ##
         clang-format: align consecutive macro definitions
     
    -    We generally align consecutive macro definitions for better readability.
    +    We generally align consecutive macro definitions for better readability:
    +
    +      #define OUTPUT_ANNOTATE_COMPAT      (1U<<0)
    +      #define OUTPUT_LONG_OBJECT_NAME     (1U<<1)
    +      #define OUTPUT_RAW_TIMESTAMP        (1U<<2)
    +      #define OUTPUT_PORCELAIN            (1U<<3)
    +
    +    over
    +
    +      #define OUTPUT_ANNOTATE_COMPAT (1U<<0)
    +      #define OUTPUT_LONG_OBJECT_NAME (1U<<1)
    +      #define OUTPUT_RAW_TIMESTAMP (1U<<2)
    +      #define OUTPUT_PORCELAIN (1U<<3)
    +
         So let's add the rule in clang-format to follow this.
     
         Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
-- 
2.47.0


```

## Karthik Nayak, 2024-10-10 17:59

Subject: [PATCH v2 1/3] clang-format: change column limit to 96 characters
Message-ID: <e22ffbe0f65971579809d817984766af12898127.1728582927.git.karthik.188@gmail.com>
URL: https://gitlist.dev/e/e22ffbe0f65971579809d817984766af12898127.1728582927.git.karthik.188%40gmail.com
In-Reply-To: <cover.1728582927.git.karthik.188@gmail.com>

```
The current value for the column limit is set to 80. While this is as
expected, we often prefer readability over this strict limit. This means
it is common to find code which extends over 80 characters. So let's
change the column limit to be 96 instead. This provides some slack so we
can ensure readability takes preference over the 80 character hard
limit.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 .clang-format | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/.clang-format b/.clang-format
index 41969eca4b..684ab32d28 100644
--- a/.clang-format
+++ b/.clang-format
@@ -12,7 +12,10 @@ UseTab: Always
 TabWidth: 8
 IndentWidth: 8
 ContinuationIndentWidth: 8
-ColumnLimit: 80
+
+# While we recommend keeping column limit to 80, we want to also provide
+# some slack to maintain readability.
+ColumnLimit: 96
 
 # C Language specifics
 Language: Cpp
-- 
2.47.0


```

## Karthik Nayak, 2024-10-10 17:59

Subject: [PATCH v2 2/3] clang-format: don't align expressions after linebreaks
Message-ID: <b55d5d2c14142fddad0b5bb84f41dd1c4eb6397a.1728582927.git.karthik.188@gmail.com>
URL: https://gitlist.dev/e/b55d5d2c14142fddad0b5bb84f41dd1c4eb6397a.1728582927.git.karthik.188%40gmail.com
In-Reply-To: <cover.1728582927.git.karthik.188@gmail.com>

```
We enforce alignment of expressions after linebreaks. Which means for
code such as

    return a || b;

it will expect:

   return a ||
          b;

we instead want 'b' to be indent with tabs, which is already done by the
'ContinuationIndentWidth' variable. So let's explicitly set
'AlignOperands' to false.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 .clang-format | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/.clang-format b/.clang-format
index 684ab32d28..d655b08ff6 100644
--- a/.clang-format
+++ b/.clang-format
@@ -42,10 +42,9 @@ AlignConsecutiveDeclarations: false
 #   int cccccccc;
 AlignEscapedNewlines: Left
 
-# Align operands of binary and ternary expressions
-# int aaa = bbbbbbbbbbb +
-#           cccccc;
-AlignOperands: true
+# Don't enforce alignment after linebreaks and instead
+# rely on the ContinuationIndentWidth value.
+AlignOperands: false
 
 # Don't align trailing comments
 # int a; // Comment a
-- 
2.47.0


```

## Karthik Nayak, 2024-10-10 17:59

Subject: [PATCH v2 3/3] clang-format: align consecutive macro definitions
Message-ID: <6ebcd2690e05820e15584fe5459dc46011fde101.1728582927.git.karthik.188@gmail.com>
URL: https://gitlist.dev/e/6ebcd2690e05820e15584fe5459dc46011fde101.1728582927.git.karthik.188%40gmail.com
In-Reply-To: <cover.1728582927.git.karthik.188@gmail.com>

```
We generally align consecutive macro definitions for better readability:

  #define OUTPUT_ANNOTATE_COMPAT      (1U<<0)
  #define OUTPUT_LONG_OBJECT_NAME     (1U<<1)
  #define OUTPUT_RAW_TIMESTAMP        (1U<<2)
  #define OUTPUT_PORCELAIN            (1U<<3)

over

  #define OUTPUT_ANNOTATE_COMPAT (1U<<0)
  #define OUTPUT_LONG_OBJECT_NAME (1U<<1)
  #define OUTPUT_RAW_TIMESTAMP (1U<<2)
  #define OUTPUT_PORCELAIN (1U<<3)

So let's add the rule in clang-format to follow this.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 .clang-format | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/.clang-format b/.clang-format
index d655b08ff6..22ae882923 100644
--- a/.clang-format
+++ b/.clang-format
@@ -35,6 +35,9 @@ AlignConsecutiveAssignments: false
 # double b = 3.14;
 AlignConsecutiveDeclarations: false
 
+# Align consecutive macro definitions.
+AlignConsecutiveMacros: true
+
 # Align escaped newlines as far left as possible
 # #define A   \
 #   int aaaa; \
-- 
2.47.0


```

## Kyle Lippincott, 2024-10-10 18:11

Subject: Re: [PATCH v2 1/3] clang-format: change column limit to 96 characters
Message-ID: <CAO_smVjuseH6WJgU5CRg5kyZ7J1mgqLjrvxzapR0xB6XvQ7VdA@mail.gmail.com>
URL: https://gitlist.dev/e/CAO_smVjuseH6WJgU5CRg5kyZ7J1mgqLjrvxzapR0xB6XvQ7VdA%40mail.gmail.com
In-Reply-To: <e22ffbe0f65971579809d817984766af12898127.1728582927.git.karthik.188@gmail.com>

```
On Thu, Oct 10, 2024 at 11:00 AM Karthik Nayak <karthik.188@gmail.com> wrote:
>
> The current value for the column limit is set to 80. While this is as
> expected, we often prefer readability over this strict limit. This means
> it is common to find code which extends over 80 characters. So let's
> change the column limit to be 96 instead. This provides some slack so we
> can ensure readability takes preference over the 80 character hard
> limit.
>
> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
> ---
>  .clang-format | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/.clang-format b/.clang-format
> index 41969eca4b..684ab32d28 100644
> --- a/.clang-format
> +++ b/.clang-format
> @@ -12,7 +12,10 @@ UseTab: Always
>  TabWidth: 8
>  IndentWidth: 8
>  ContinuationIndentWidth: 8
> -ColumnLimit: 80
> +
> +# While we recommend keeping column limit to 80, we want to also provide
> +# some slack to maintain readability.
> +ColumnLimit: 96
>
>  # C Language specifics
>  Language: Cpp
> --
> 2.47.0
>
>

I think this means that the next automated `clang-format` invocation
will un-wrap lines that were wrapped at 80 columns (not characters)
but fit in 96 columns. Modifying this setting and running
`clang-format -i *.{c,h}` produces a lot of diffs of that kind. I
don't think there's a way of setting a soft column limit in
clang-format.

Personally, I'd be fine with a higher column limit, but we'd need to
make a conscious change to the style guidelines for that.

```

## karthik nayak, 2024-10-10 19:49

Subject: Re: [PATCH v2 1/3] clang-format: change column limit to 96 characters
Message-ID: <CAOLa=ZQ=-GwnCX7CP6sqmoArphQuUNbfcCje2_64k9g8yJ1PBA@mail.gmail.com>
URL: https://gitlist.dev/e/CAOLa%3DZQ%3D-GwnCX7CP6sqmoArphQuUNbfcCje2_64k9g8yJ1PBA%40mail.gmail.com
In-Reply-To: <CAO_smVjuseH6WJgU5CRg5kyZ7J1mgqLjrvxzapR0xB6XvQ7VdA@mail.gmail.com>

```
Kyle Lippincott <spectral@google.com> writes:

> On Thu, Oct 10, 2024 at 11:00 AM Karthik Nayak <karthik.188@gmail.com> wrote:
>>
>> The current value for the column limit is set to 80. While this is as
>> expected, we often prefer readability over this strict limit. This means
>> it is common to find code which extends over 80 characters. So let's
>> change the column limit to be 96 instead. This provides some slack so we
>> can ensure readability takes preference over the 80 character hard
>> limit.
>>
>> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
>> ---
>>  .clang-format | 5 ++++-
>>  1 file changed, 4 insertions(+), 1 deletion(-)
>>
>> diff --git a/.clang-format b/.clang-format
>> index 41969eca4b..684ab32d28 100644
>> --- a/.clang-format
>> +++ b/.clang-format
>> @@ -12,7 +12,10 @@ UseTab: Always
>>  TabWidth: 8
>>  IndentWidth: 8
>>  ContinuationIndentWidth: 8
>> -ColumnLimit: 80
>> +
>> +# While we recommend keeping column limit to 80, we want to also provide
>> +# some slack to maintain readability.
>> +ColumnLimit: 96
>>
>>  # C Language specifics
>>  Language: Cpp
>> --
>> 2.47.0
>>
>>
>
> I think this means that the next automated `clang-format` invocation
> will un-wrap lines that were wrapped at 80 columns (not characters)
> but fit in 96 columns. Modifying this setting and running
> `clang-format -i *.{c,h}` produces a lot of diffs of that kind. I
> don't think there's a way of setting a soft column limit in
> clang-format.

Ah! Good point.

> Personally, I'd be fine with a higher column limit, but we'd need to
> make a conscious change to the style guidelines for that.

With this, I would say that the best choice here would be to actually
set it to 0 like the previous version. So that we don't actually enforce
the column limit.

We could perhaps set the value here in the '.clang-format' to 0. While
also setting 'max_line_length = 95' in the '.editorconfig'. That would
mean that we don't enforce a width, but we nudge editors to wrap at 95
characters. Here contributors would still have the power to decide the
adequate width as needed.

```

## Kyle Lippincott, 2024-10-10 20:09

Subject: Re: [PATCH v2 1/3] clang-format: change column limit to 96 characters
Message-ID: <CAO_smVgasPSWEH3UOnGo-wvKT-nE-pYmG-L8gHPCajBthGmndg@mail.gmail.com>
URL: https://gitlist.dev/e/CAO_smVgasPSWEH3UOnGo-wvKT-nE-pYmG-L8gHPCajBthGmndg%40mail.gmail.com
In-Reply-To: <CAOLa=ZQ=-GwnCX7CP6sqmoArphQuUNbfcCje2_64k9g8yJ1PBA@mail.gmail.com>

```
On Thu, Oct 10, 2024 at 12:49 PM karthik nayak <karthik.188@gmail.com> wrote:
>
> Kyle Lippincott <spectral@google.com> writes:
>
> > On Thu, Oct 10, 2024 at 11:00 AM Karthik Nayak <karthik.188@gmail.com> wrote:
> >>
> >> The current value for the column limit is set to 80. While this is as
> >> expected, we often prefer readability over this strict limit. This means
> >> it is common to find code which extends over 80 characters. So let's
> >> change the column limit to be 96 instead. This provides some slack so we
> >> can ensure readability takes preference over the 80 character hard
> >> limit.
> >>
> >> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
> >> ---
> >>  .clang-format | 5 ++++-
> >>  1 file changed, 4 insertions(+), 1 deletion(-)
> >>
> >> diff --git a/.clang-format b/.clang-format
> >> index 41969eca4b..684ab32d28 100644
> >> --- a/.clang-format
> >> +++ b/.clang-format
> >> @@ -12,7 +12,10 @@ UseTab: Always
> >>  TabWidth: 8
> >>  IndentWidth: 8
> >>  ContinuationIndentWidth: 8
> >> -ColumnLimit: 80
> >> +
> >> +# While we recommend keeping column limit to 80, we want to also provide
> >> +# some slack to maintain readability.
> >> +ColumnLimit: 96
> >>
> >>  # C Language specifics
> >>  Language: Cpp
> >> --
> >> 2.47.0
> >>
> >>
> >
> > I think this means that the next automated `clang-format` invocation
> > will un-wrap lines that were wrapped at 80 columns (not characters)
> > but fit in 96 columns. Modifying this setting and running
> > `clang-format -i *.{c,h}` produces a lot of diffs of that kind. I
> > don't think there's a way of setting a soft column limit in
> > clang-format.
>
> Ah! Good point.
>
> > Personally, I'd be fine with a higher column limit, but we'd need to
> > make a conscious change to the style guidelines for that.
>
> With this, I would say that the best choice here would be to actually
> set it to 0 like the previous version. So that we don't actually enforce
> the column limit.
>
> We could perhaps set the value here in the '.clang-format' to 0. While
> also setting 'max_line_length = 95' in the '.editorconfig'. That would
> mean that we don't enforce a width, but we nudge editors to wrap at 95
> characters. Here contributors would still have the power to decide the
> adequate width as needed.

One thing to be cautious of is column vs character distinctions.
Because we use tabs[^1], one character regularly has a display width
of 8 columns.

[^1]: Another personal opinion, and this is going to be very
contentious so please don't think that others agree with me on this,
because I suspect I'm in the minority: tabs are a massive mistake. The
*only* benefit we get from our current use of tabs is smaller source
files, and we get numerous headaches because of them. I could go on
about this, because I have Opinions here, but this probably isn't the
right place ;)

```
