Skip to contentSkip to conversation

clang-format with examples

PATCH In review Oleksandr Andrushchenko 3 participants 10 messages Mar 1, 2025 – Mar 4, 2025 Clang-format file

lore
Share & tools
lore
#7 lore raw

PATCH 3/4 Changes done by clang-format to the previous commit

In reply to Oleksandr Andrushchenko

This patch is the result of the:

$ git-clang-format --style=file --verbose HEAD~1
Running clang-format on the following files:
    tools/xl/xl_info.c
    xen/drivers/char/ns16550.c
old tree: dceb97b3ef3c80cbb9c29311a7f5783df2f69127
new tree: a8d36f8f45c3ee0d75e674ecda102ffc6a0a0ef3
changed files:
    xen/drivers/char/ns16550.c

It is seen that only xen/ part of the patch was touched. The result can still be manually adjusted before committing.

---
 xen/drivers/char/ns16550.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
xen/drivers/char/ns16550.c +6 -3
index 53586f110008..4d79ef2b08bc 100644
@@ -340,7 +340,9 @@ static void ns16550_setup_preirq(struct ns16550 *uart)
         if ( divisor )
             uart->baud = uart->clock_hz / (divisor << 4);
         else
-            printk(XENLOG_ERR "Automatic baud rate determination was requested,"  " but a baud rate was not set up\n");
+            printk(
+                XENLOG_ERR
+                "Automatic baud rate determination was requested," " but a baud rate was not set up\n");
     }
     ns_write_reg(uart, UART_LCR, lcr);
 
@@ -348,8 +350,9 @@ static void ns16550_setup_preirq(struct ns16550 *uart)
     ns_write_reg(uart, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
 
     /* Enable and clear the FIFOs. Set a large trigger threshold. */
-    ns_write_reg(uart, UART_FCR, UART_FCR_ENABLE | UART_FCR_CLRX |
-                 UART_FCR_CLTX | UART_FCR_TRG14);
+    ns_write_reg(uart, UART_FCR,
+                 UART_FCR_ENABLE | UART_FCR_CLRX | UART_FCR_CLTX |
+                     UART_FCR_TRG14);
 }
 
 static void __init cf_check ns16550_init_preirq(struct serial_port *port)
Signature · 1 line
2.25.1
#6 lore raw

PATCH 2/4 Provide an example of a malformed patch

In reply to Oleksandr Andrushchenko

This patch touches tools/xl and xen/. clang-format, if applied, will only change xen/ part.

---
 tools/xl/xl_info.c         |  6 ++----
 xen/drivers/char/ns16550.c | 12 +++++-------
 2 files changed, 7 insertions(+), 11 deletions(-)
tools/xl/xl_info.c +2 -4
index 72e87eac46d1..cbb0f695685f 100644
@@ -59,8 +59,7 @@ static int maybe_printf(const char *fmt, ...)
     return count;
 }
 
-static yajl_gen_status printf_info_one_json(yajl_gen hand, int domid,
-                                            libxl_domain_config *d_config)
+static yajl_gen_status printf_info_one_json(yajl_gen hand, int domid, libxl_domain_config *d_config)
 {
     yajl_gen_status s;
 
@@ -68,8 +67,7 @@ static yajl_gen_status printf_info_one_json(yajl_gen hand, int domid,
     if (s != yajl_gen_status_ok)
         goto out;
 
-    s = yajl_gen_string(hand, (const unsigned char *)"domid",
-                        sizeof("domid")-1);
+    s = yajl_gen_string(hand, (const unsigned char *)"domid", sizeof("domid")-1);
     if (s != yajl_gen_status_ok)
         goto out;
     if (domid != -1)
xen/drivers/char/ns16550.c +5 -7
index eaeb0e09d01e..53586f110008 100644
@@ -340,9 +340,7 @@ static void ns16550_setup_preirq(struct ns16550 *uart)
         if ( divisor )
             uart->baud = uart->clock_hz / (divisor << 4);
         else
-            printk(XENLOG_ERR
-                   "Automatic baud rate determination was requested,"
-                   " but a baud rate was not set up\n");
+            printk(XENLOG_ERR "Automatic baud rate determination was requested,"  " but a baud rate was not set up\n");
     }
     ns_write_reg(uart, UART_LCR, lcr);
 
@@ -350,8 +348,8 @@ static void ns16550_setup_preirq(struct ns16550 *uart)
     ns_write_reg(uart, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
 
     /* Enable and clear the FIFOs. Set a large trigger threshold. */
-    ns_write_reg(uart, UART_FCR,
-                 UART_FCR_ENABLE | UART_FCR_CLRX | UART_FCR_CLTX | UART_FCR_TRG14);
+    ns_write_reg(uart, UART_FCR, UART_FCR_ENABLE | UART_FCR_CLRX |
+                 UART_FCR_CLTX | UART_FCR_TRG14);
 }
 
 static void __init cf_check ns16550_init_preirq(struct serial_port *port)
@@ -398,8 +396,8 @@ static void ns16550_setup_postirq(struct ns16550 *uart)
     if ( uart->irq > 0 )
     {
         /* Master interrupt enable; also keep DTR/RTS asserted. */
-        ns_write_reg(uart,
-                     UART_MCR, UART_MCR_OUT2 | UART_MCR_DTR | UART_MCR_RTS);
+        ns_write_reg(uart, UART_MCR,
+                     UART_MCR_OUT2 | UART_MCR_DTR | UART_MCR_RTS);
 
         /* Enable receive interrupts. */
         ns_write_reg(uart, UART_IER, UART_IER_ERDAI);
Signature · 1 line
2.25.1
#0 lore raw

cover clang-format with examples

Hello, everybody!

In order to continue activity started at [1] I am sending this series which allows to manually run coding style checks and does not provide any automation yet.

Only the first patch is the goal of this series and patches 2-4 are for example only.

This proposal disables coding style checks for the project, but xen/
folder: this is done by providing a global .clang-format at the project top level which disables clang-format and only providing .clang-format for xen/ folder.

clang-format version expected to be >15 and the latest tool can be installed with:
python3 -m pip install clang-format
This allows having the tool without the need to compile or install anything else.

As no automatic code style checks are performed, so all those can be run manually:

- to see changes proposed to the patch being worked on (not committed yet):
git-clang-format --diff --style=file --verbose

- to run code formatting on the HEAD patch:
git-clang-format --style=file --verbose HEAD~1

Provided xen/.clang-format still has a lot of comments gathered from the previous discussions. This is for purpose of better seeing why some of the options have their values. Once option values are accepted all those comments can be removed.

Stay safe,
Oleksandr Andrushchenko

[1] https://lists.xen.org/archives/html/xen-devel/2025-02/msg00800.html

Oleksandr Andrushchenko (4):
Add .clang-format files to enable manual coding style checks [EXAMPLE] Provide an example of a malformed patch [EXAMPLE] Changes done by clang-format to the previous commit [EXAMPLE] Whole file code style formatting

 .clang-format              |    5 +
 tools/xl/xl_info.c         |    6 +-
 xen/.clang-format          | 1380 ++++++++++++++++++++++++++++++++++++
 xen/drivers/char/ns16550.c |   13 +-
 xen/drivers/pci/pci.c      |    8 +-
 5 files changed, 1397 insertions(+), 15 deletions(-)
 create mode 100644 .clang-format
 create mode 100644 xen/.clang-format
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/4 Add .clang-format files to enable manual coding style checks

Disable coding style checks for the project, but xen/ folder:
this is done by providing a global .clang-format at the top level which disables clang-format and only providing .clang-format for xen/.

clang-format version expected to be >15 and the latest tool can be installed with:
python3 -m pip install clang-format

Please note, that no automatic code style checks are performed and all those can be run manually:

- to see changes proposed to the patch being worked on (not committed yet):
git-clang-format --diff --style=file --verbose

- to run code formatting on the HEAD patch:
git-clang-format --style=file --verbose HEAD~1

Provided xen/.clang-format still has a lot of comments gathered from the previous discussions. This is for purpose of better seeing why some of the options have their values. Once option values are accepted all those comments can be removed.

---
 .clang-format     |    5 +
 xen/.clang-format | 1380 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 1385 insertions(+)
 create mode 100644 .clang-format
 create mode 100644 xen/.clang-format
.clang-format new file +5 -0
new file mode 100644
index 000000000000..fc3f8e167f65
@@ -0,0 +1,5 @@
+---
+DisableFormat: true
+SortIncludes: Never
+...
+
xen/.clang-format new file +1380 -0

1,380 lines not on this page · raw message

Signature · 1 line
2.25.1
#8 lore raw

PATCH 4/4 Whole file code style formatting

In reply to Oleksandr Andrushchenko

This patch was produced by running clang-format on a file:
clang-format -i xen/drivers/pci/pci.c

---
 xen/drivers/pci/pci.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
xen/drivers/pci/pci.c +3 -5
index edf5b9f7ae9f..f6d8222c1dd4 100644
@@ -56,10 +56,8 @@ unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos,
         if ( id == 0xff )
             break;
         for ( i = 0; i < n; i++ )
-        {
             if ( id == caps[i] )
                 return pos;
-        }
 
         pos = (pos & ~3) + PCI_CAP_LIST_NEXT;
     }
@@ -116,7 +114,8 @@ unsigned int pci_find_next_ext_capability(pci_sbdf_t sbdf, unsigned int start,
         return 0;
     ASSERT(start != pos || PCI_EXT_CAP_ID(header) == cap);
 
-    while ( ttl-- > 0 ) {
+    while ( ttl-- > 0 )
+    {
         if ( PCI_EXT_CAP_ID(header) == cap && pos != start )
             return pos;
         pos = PCI_EXT_CAP_NEXT(header);
@@ -174,8 +173,7 @@ const char *__init parse_pci_seg(const char *s, unsigned int *seg_p,
     }
     else
         func = 0;
-    if ( seg != (seg_p ? (u16)seg : 0) ||
-         bus != PCI_BUS(PCI_BDF(bus, 0)) ||
+    if ( seg != (seg_p ? (u16)seg : 0) || bus != PCI_BUS(PCI_BDF(bus, 0)) ||
          dev != PCI_SLOT(PCI_DEVFN(dev, 0)) ||
          func != PCI_FUNC(PCI_DEVFN(0, func)) )
         return NULL;
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 1/4] Add .clang-format files to enable manual coding style checks

In reply to Oleksandr Andrushchenko
Quoted 14 lines · Oleksandr Andrushchenko

Disable coding style checks for the project, but xen/ folder:
this is done by providing a global .clang-format at the top level which disables clang-format and only providing .clang-format for xen/.

clang-format version expected to be >15 and the latest tool can be installed with:
python3 -m pip install clang-format

Please note, that no automatic code style checks are performed and all those can be run manually:

- to see changes proposed to the patch being worked on (not committed yet):
git-clang-format --diff --style=file --verbose

Quoted 7 lines

- to run code formatting on the HEAD patch:
git-clang-format --style=file --verbose HEAD~1

Provided xen/.clang-format still has a lot of comments gathered from the previous discussions. This is for purpose of better seeing why some of the options have their values. Once option values are accepted all those comments can be removed.

I think the comments need to be trimmed to a concise explanation of why a setting is the way it is. The (IMO overly long) excerpts from email conversations are difficult to follow in this context. Someone has to spend the time to digest them and provide a shorter comment.

One thing that I'm missing: don't we need a list of excluded files somewhere because they are imports from Linux and we would like to keep the Linux coding style for avoiding conflicts when backporting fixes?

Quoted 27 lines
---
 .clang-format     |    5 +
 xen/.clang-format | 1380 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 1385 insertions(+)
 create mode 100644 .clang-format
 create mode 100644 xen/.clang-format
.clang-format new file +5 -0
new file mode 100644
index 000000000000..fc3f8e167f65
@@ -0,0 +1,5 @@
+---
+DisableFormat: true
+SortIncludes: Never
+...
+
xen/.clang-format new file +0 -0
new file mode 100644
index 000000000000..480628544cdd

xen/.clang-format line 3

Quoted 3 lines
xen/.clang-format new file +3 -0
@@ -0,0 +1,1380 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# clang-format configuration file. Intended for clang-format >= 15.

What happens if used with clang-format < 15? I expect the tool gives some kind of error about unknown options, rather than doing a wrong formatting?

Quoted 121 lines
xen/.clang-format new file +121 -0
+#
+# For more information, see:
+#
+#   Documentation/process/clang-format.rst
+#   https://clang.llvm.org/docs/ClangFormat.html
+#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+#
+---
+
+################################################################################
+# How to review the changes
+################################################################################
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg02126.html
+# ------------------------------------------------------------------------------
+# George Dunlap:
+# It's hard to agree on this one without seeing some of the examples of
+# what it does, some examples of the "weird behavior" Stefano &
+# Allejandro found, and some examples of places where it's going to
+# remove the alignment.
+#
+# I had tried to apply your series before and didn't get very far with
+# it for some reason ISTR.  One way to see the effect of individual
+# features would be:
+#
+# 1. Make a branch with one big patch applying clang-format for a given style
+#
+# 2. Change just one style line, re-run clang format, and create a new
+# patch from that
+#
+# Then it would be easy to see the difference between the two.  It might
+# actually be easier for individual reviewers to do that on their own
+# trees, rather than to ask you to try to generate and post such patches
+# somewhere.
+
+################################################################################
+# do/while loops
+################################################################################
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-07/msg02276.html
+# ------------------------------------------------------------------------------
+# Jan:
+# > I've read the past threads about the brave people who dared to try to
+# > introduce clang-format for the xen codebase, some of them from 5 years ago,
+# > two points were clear: 1) goto label needs to be indented and 2) do-while
+# > loops have the braket in the same line.
+# > While point 1) was quite a blocker, it seemd to me that point 2) was less
+# > controversial to be changed in the Xen codestyle, so the current wrapper
+# > script handles only the point 1 (which is easy), the point 2 can be more
+# > tricky to handle.
+#
+# I'm afraid I view the do/while part pretty much as a blocker as well.
+# While placing the opening brace according to our style elsewhere would
+# be okay-ish (just a little wasteful to have two almost empty lines),
+# having the closing brace on a separate line is problematic: At least I
+# consider a block / scope to end at the line where the closing brace is.
+# So the farther do and while are apart, the more
+#
+#    do
+#    {
+#        ...;
+#    }
+#    while ( cond );
+#    ...;
+#
+# is going to be misleading. While normally we would write potentially
+# conflicting constructs this way
+#
+#     while ( cond )
+#         ;
+#
+# the alternative spelling still isn't outright wrong in our style (I
+# believe):
+#
+#     while ( cond );
+#
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-07/msg02282.html
+# ------------------------------------------------------------------------------
+# Luca:
+# Thank you for your feedback, I could maybe misunderstood your reply, so please
+# tell me if I am wrong, the Xen coding style mandates this style for do-while
+# loops:
+#
+# do {
+# /* Do stuff. */
+# } while ( condition );
+#
+# Currently clang-format is able to do only this:
+#
+# do
+# {
+# /* Do stuff. */
+# } while ( condition );
+#
+# So the issue is only in the opening brackets, not the closing one. Is it a
+# blocker too?
+#
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-07/msg02284.html
+# ------------------------------------------------------------------------------
+# Jan:
+# > do
+# > {
+# > /* Do stuff. */
+# > } while ( condition );
+#
+# Oh, I hadn't understood your description that way.
+#
+# > So the issue is only in the opening brackets, not the closing one. Is it a
+# > blocker too?
+#
+# No. I don't like the longer form, but I could live with it.
+
+################################################################################
+# clang version to use
+################################################################################
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-07/msg02301.html
+# ------------------------------------------------------------------------------
+# Alejandro:

xen/.clang-format line 134

Quoted 10 lines
xen/.clang-format new file +10 -0
+# > The minimum clang-format version for the file is 15, my ubuntu 22.04 comes
+# > with it, we can reason if it's too high, or if we could also use the latest
+# > version maybe shipped inside a docker image.
+# 15 sounds ok \methinks. In practice we'll just stick it in GitLab and let
+# it check commits, so we'll be safe even in the case where every developer
+# has a slightly different version of the tool. I wouldn't try to make this
+# (hard) task harder by trying to retrofit it in an older version.
+#
+# It might be worth stitching a flag in the python script to scream if the
+#clang-format version doesn't match. Or it may do a mess of the tree.

^ missing space

Is it possible to add such flag and give a clear error when the wrong clang-format version is used?

Quoted 59 lines
xen/.clang-format new file +59 -0
+################################################################################
+# Options and their discussions
+################################################################################
+
+# [not specified]
+# Align function parameter that goes into a new line, under the open bracket
+# (supported in clang-format 3.8)
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg01993.html
+# ------------------------------------------------------------------------------
+# Luca: This one is to align function parameters that overflows the line length,
+# I chose to align them to the open bracket to match the current codebase
+# (hopefully) e.g.:
+# someLongFunction(argument1,
+#                  argument2);
+#
+# This one can be a candidate for an entry in our coding style
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg02143.html
+# ------------------------------------------------------------------------------
+# Jan:
+# > e.g.:
+# > someLongFunction(argument1,
+# >                  argument2);
+# The above matches neither of the two generally permitted styles:
+#
+#    someLongFunction(argument1,
+#                     argument2);
+#
+#    someLongFunction(
+#        argument1,
+#        argument2);
+#
+# Then again from its name I would infer this isn't just about function
+# arguments?
+#
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg02172.html
+# ------------------------------------------------------------------------------
+# Luca:
+# I think it applies to parameters and arguments of functions and macro, given
+# the description in the docs.
+# I see your two snippets above but I’ve always found at least on arm a
+# predominance of the style above for functions, so arguments aligned after the
+# opening bracket, for macros there is a mix.
+# I might be wrong though and so another opinion from another maintainer would
+# help.
+# In any case we can choose among many value:
+# https://clang.llvm.org/docs/ClangFormatStyleOptions.html#alignafteropenbracket,
+# but when we do so, we need to stick to one permitted style only, the tool don’t
+# allow to specify more than one.
+#
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg02311.html
+# ------------------------------------------------------------------------------
+# Jan:
+# > I see your two snippets above but I’ve always found at least on arm a
+# > predominance of the style above for functions, so arguments aligned after
+# > the opening bracket, for macros there is a mix.

xen/.clang-format line 204

Quoted 10 lines
xen/.clang-format new file +10 -0
+#
+# The latter "above" refers to which form exactly? The one you originally
+# spelled out, or the former of what my reply had?
+#
+# > In any case we can choose among many value:
+# > https://clang.llvm.org/docs/ClangFormatStyleOptions.html#alignafteropenbracket,
+# > but when we do so, we need to stick to one permitted style only, the tool
+# > don’t allow to specify more than one.
+#
+# a2k: RED FLAG

What does 'a2k' mean?

I'm afraid this needs to be trimmed to a more manageable comment, otherwise I simply get lost in the context. Maybe it makes sense more people involved in the discussion.

Thanks, Roger.

#3 lore raw

Re: [PATCH 1/4] Add .clang-format files to enable manual coding style checks

Hello, Roger!

Quoted 18 lines · Roger Pau Monné
Quoted 17 lines · Oleksandr Andrushchenko

Disable coding style checks for the project, but xen/ folder:
this is done by providing a global .clang-format at the top level which disables clang-format and only providing .clang-format for xen/.

clang-format version expected to be >15 and the latest tool can be installed with:
python3 -m pip install clang-format

Please note, that no automatic code style checks are performed and all those can be run manually:

- to see changes proposed to the patch being worked on (not committed yet):
git-clang-format --diff --style=file --verbose

- to run code formatting on the HEAD patch:
git-clang-format --style=file --verbose HEAD~1

Quoted 8 lines
Quoted 4 lines

Provided xen/.clang-format still has a lot of comments gathered from the previous discussions. This is for purpose of better seeing why some of the options have their values. Once option values are accepted all those comments can be removed.

I think the comments need to be trimmed to a concise explanation of why a setting is the way it is. The (IMO overly long) excerpts from email conversations are difficult to follow in this context. Someone has to spend the time to digest them and provide a shorter comment.

This is the goal. I do not expect the patch to be committed right now, so I left those intentionally to provide some background when those options were discussed before and, possibly, to address some questions which were brought earlier.
The final patch will have all those in the way you mention here

Quoted 5 lines

One thing that I'm missing: don't we need a list of excluded files somewhere because they are imports from Linux and we would like to keep the Linux coding style for avoiding conflicts when backporting fixes?

All files are excluded from formatting, but xen/ directory: this is by providing a top level .clang-format file which disables all formatting and also providing xen/.clang-format which is exactly for xen/.
What is more, there is no automation yet, so this is up to a user to decide whether they want to run git clang-format or not.

Quoted 25 lines
Quoted 25 lines
---
  .clang-format     |    5 +
  xen/.clang-format | 1380 +++++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 1385 insertions(+)

create mode 100644 .clang-format
create mode 100644 xen/.clang-format

.clang-format new file +5 -0
new file mode 100644
index 000000000000..fc3f8e167f65
@@ -0,0 +1,5 @@
+---
+DisableFormat: true
+SortIncludes: Never
+...
+
xen/.clang-format new file +0 -1
new file mode 100644
index 000000000000..480628544cdd
--- /dev/null
Quoted 8 lines

xen/.clang-format line 3

Quoted 5 lines

+++ b/xen/.clang-format

xen/.clang-format new file +3 -0
@@ -0,0 +1,1380 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# clang-format configuration file. Intended for clang-format >= 15.

What happens if used with clang-format < 15? I expect the tool gives some kind of error about unknown options, rather than doing a wrong formatting?

Exactly, it will throw an error.

Quoted 134 lines
Quoted 122 lines
xen/.clang-format new file +121 -0
+#
+# For more information, see:
+#
+#   Documentation/process/clang-format.rst
+#   https://clang.llvm.org/docs/ClangFormat.html
+#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+#
+---
+
+################################################################################
+# How to review the changes
+################################################################################
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg02126.html
+# ------------------------------------------------------------------------------
+# George Dunlap:
+# It's hard to agree on this one without seeing some of the examples of
+# what it does, some examples of the "weird behavior" Stefano &
+# Allejandro found, and some examples of places where it's going to
+# remove the alignment.
+#
+# I had tried to apply your series before and didn't get very far with
+# it for some reason ISTR.  One way to see the effect of individual
+# features would be:
+#
+# 1. Make a branch with one big patch applying clang-format for a given style
+#
+# 2. Change just one style line, re-run clang format, and create a new
+# patch from that
+#
+# Then it would be easy to see the difference between the two.  It might
+# actually be easier for individual reviewers to do that on their own
+# trees, rather than to ask you to try to generate and post such patches
+# somewhere.
+
+################################################################################
+# do/while loops
+################################################################################
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-07/msg02276.html
+# ------------------------------------------------------------------------------
+# Jan:
+# > I've read the past threads about the brave people who dared to try to
+# > introduce clang-format for the xen codebase, some of them from 5 years ago,
+# > two points were clear: 1) goto label needs to be indented and 2) do-while
+# > loops have the braket in the same line.
+# > While point 1) was quite a blocker, it seemd to me that point 2) was less
+# > controversial to be changed in the Xen codestyle, so the current wrapper
+# > script handles only the point 1 (which is easy), the point 2 can be more
+# > tricky to handle.
+#
+# I'm afraid I view the do/while part pretty much as a blocker as well.
+# While placing the opening brace according to our style elsewhere would
+# be okay-ish (just a little wasteful to have two almost empty lines),
+# having the closing brace on a separate line is problematic: At least I
+# consider a block / scope to end at the line where the closing brace is.
+# So the farther do and while are apart, the more
+#
+#    do
+#    {
+#        ...;
+#    }
+#    while ( cond );
+#    ...;
+#
+# is going to be misleading. While normally we would write potentially
+# conflicting constructs this way
+#
+#     while ( cond )
+#         ;
+#
+# the alternative spelling still isn't outright wrong in our style (I
+# believe):
+#
+#     while ( cond );
+#
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-07/msg02282.html
+# ------------------------------------------------------------------------------
+# Luca:
+# Thank you for your feedback, I could maybe misunderstood your reply, so please
+# tell me if I am wrong, the Xen coding style mandates this style for do-while
+# loops:
+#
+# do {
+# /* Do stuff. */
+# } while ( condition );
+#
+# Currently clang-format is able to do only this:
+#
+# do
+# {
+# /* Do stuff. */
+# } while ( condition );
+#
+# So the issue is only in the opening brackets, not the closing one. Is it a
+# blocker too?
+#
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-07/msg02284.html
+# ------------------------------------------------------------------------------
+# Jan:
+# > do
+# > {
+# > /* Do stuff. */
+# > } while ( condition );
+#
+# Oh, I hadn't understood your description that way.
+#
+# > So the issue is only in the opening brackets, not the closing one. Is it a
+# > blocker too?
+#
+# No. I don't like the longer form, but I could live with it.
+
+################################################################################
+# clang version to use
+################################################################################
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-07/msg02301.html
+# ------------------------------------------------------------------------------
+# Alejandro:

xen/.clang-format line 134

Quoted 10 lines
xen/.clang-format new file +10 -0
+# > The minimum clang-format version for the file is 15, my ubuntu 22.04 comes
+# > with it, we can reason if it's too high, or if we could also use the latest
+# > version maybe shipped inside a docker image.
+# 15 sounds ok \methinks. In practice we'll just stick it in GitLab and let
+# it check commits, so we'll be safe even in the case where every developer
+# has a slightly different version of the tool. I wouldn't try to make this
+# (hard) task harder by trying to retrofit it in an older version.
+#
+# It might be worth stitching a flag in the python script to scream if the
+#clang-format version doesn't match. Or it may do a mess of the tree.

^ missing space

Quoted 2 lines

Is it possible to add such flag and give a clear error when the wrong clang-format version is used?

This is actually a question if clang-format of a given version supports options in .clang-format or not. If it doesn't it will throw an error anyways.

Quoted 72 lines
Quoted 60 lines
xen/.clang-format new file +59 -0
+################################################################################
+# Options and their discussions
+################################################################################
+
+# [not specified]
+# Align function parameter that goes into a new line, under the open bracket
+# (supported in clang-format 3.8)
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg01993.html
+# ------------------------------------------------------------------------------
+# Luca: This one is to align function parameters that overflows the line length,
+# I chose to align them to the open bracket to match the current codebase
+# (hopefully) e.g.:
+# someLongFunction(argument1,
+#                  argument2);
+#
+# This one can be a candidate for an entry in our coding style
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg02143.html
+# ------------------------------------------------------------------------------
+# Jan:
+# > e.g.:
+# > someLongFunction(argument1,
+# >                  argument2);
+# The above matches neither of the two generally permitted styles:
+#
+#    someLongFunction(argument1,
+#                     argument2);
+#
+#    someLongFunction(
+#        argument1,
+#        argument2);
+#
+# Then again from its name I would infer this isn't just about function
+# arguments?
+#
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg02172.html
+# ------------------------------------------------------------------------------
+# Luca:
+# I think it applies to parameters and arguments of functions and macro, given
+# the description in the docs.
+# I see your two snippets above but I’ve always found at least on arm a
+# predominance of the style above for functions, so arguments aligned after the
+# opening bracket, for macros there is a mix.
+# I might be wrong though and so another opinion from another maintainer would
+# help.
+# In any case we can choose among many value:
+# https://clang.llvm.org/docs/ClangFormatStyleOptions.html#alignafteropenbracket,
+# but when we do so, we need to stick to one permitted style only, the tool don’t
+# allow to specify more than one.
+#
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg02311.html
+# ------------------------------------------------------------------------------
+# Jan:
+# > I see your two snippets above but I’ve always found at least on arm a
+# > predominance of the style above for functions, so arguments aligned after
+# > the opening bracket, for macros there is a mix.

xen/.clang-format line 204

Quoted 10 lines
xen/.clang-format new file +10 -0
+#
+# The latter "above" refers to which form exactly? The one you originally
+# spelled out, or the former of what my reply had?
+#
+# > In any case we can choose among many value:
+# > https://clang.llvm.org/docs/ClangFormatStyleOptions.html#alignafteropenbracket,
+# > but when we do so, we need to stick to one permitted style only, the tool
+# > don’t allow to specify more than one.
+#
+# a2k: RED FLAG

What does 'a2k' mean?

Quoted 3 lines

I'm afraid this needs to be trimmed to a more manageable comment, otherwise I simply get lost in the context. Maybe it makes sense more people involved in the discussion.

Sure, see my explanation above: this is just to give some more context and have some ready answers for the questions being asked before. I will remove all these right after we have some consensus.

Quoted 2 lines

Thanks, Roger.

Thank you,
Oleksandr

#4 lore raw

Re: [PATCH 1/4] Add .clang-format files to enable manual coding style checks

Quoted 35 lines · Oleksandr Andrushchenko

Hello, Roger!

Quoted 18 lines · Roger Pau Monné
Quoted 17 lines · Oleksandr Andrushchenko

Disable coding style checks for the project, but xen/ folder:
this is done by providing a global .clang-format at the top level which disables clang-format and only providing .clang-format for xen/.

clang-format version expected to be >15 and the latest tool can be installed with:
python3 -m pip install clang-format

Please note, that no automatic code style checks are performed and all those can be run manually:

- to see changes proposed to the patch being worked on (not committed yet):
git-clang-format --diff --style=file --verbose

- to run code formatting on the HEAD patch:
git-clang-format --style=file --verbose HEAD~1

Quoted 8 lines
Quoted 4 lines

Provided xen/.clang-format still has a lot of comments gathered from the previous discussions. This is for purpose of better seeing why some of the options have their values. Once option values are accepted all those comments can be removed.

I think the comments need to be trimmed to a concise explanation of why a setting is the way it is. The (IMO overly long) excerpts from email conversations are difficult to follow in this context. Someone has to spend the time to digest them and provide a shorter comment.

This is the goal. I do not expect the patch to be committed right now, so I left those intentionally to provide some background when those options were discussed before and, possibly, to address some questions which were brought earlier.
The final patch will have all those in the way you mention here

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

One thing that I'm missing: don't we need a list of excluded files somewhere because they are imports from Linux and we would like to keep the Linux coding style for avoiding conflicts when backporting fixes?

All files are excluded from formatting, but xen/ directory: this is by providing a top level .clang-format file which disables all formatting and also providing xen/.clang-format which is exactly for xen/.

There are files with intentional Linux coding style inside of xen/, for example xen/arch/x86/cpu/mwait-idle.c. We would need a way to filter those out so that clang-format doesn't attempt to apply the Xen coding style to them.

Otherwise it needs to be noted possibly in .clang-format that while the clang-format style will apply by default to all files inside of xen/, some of those use a different coding style on purpose, and those should not be reformatted.

We also have some scripts in python and shell inside of both tools/ and scripts/, are those already excluded by clang-format by not having a .c or .h extension?

Quoted 71 lines
Quoted 71 lines
Quoted 59 lines
xen/.clang-format new file +59 -0
+################################################################################
+# Options and their discussions
+################################################################################
+
+# [not specified]
+# Align function parameter that goes into a new line, under the open bracket
+# (supported in clang-format 3.8)
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg01993.html
+# ------------------------------------------------------------------------------
+# Luca: This one is to align function parameters that overflows the line length,
+# I chose to align them to the open bracket to match the current codebase
+# (hopefully) e.g.:
+# someLongFunction(argument1,
+#                  argument2);
+#
+# This one can be a candidate for an entry in our coding style
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg02143.html
+# ------------------------------------------------------------------------------
+# Jan:
+# > e.g.:
+# > someLongFunction(argument1,
+# >                  argument2);
+# The above matches neither of the two generally permitted styles:
+#
+#    someLongFunction(argument1,
+#                     argument2);
+#
+#    someLongFunction(
+#        argument1,
+#        argument2);
+#
+# Then again from its name I would infer this isn't just about function
+# arguments?
+#
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg02172.html
+# ------------------------------------------------------------------------------
+# Luca:
+# I think it applies to parameters and arguments of functions and macro, given
+# the description in the docs.
+# I see your two snippets above but I’ve always found at least on arm a
+# predominance of the style above for functions, so arguments aligned after the
+# opening bracket, for macros there is a mix.
+# I might be wrong though and so another opinion from another maintainer would
+# help.
+# In any case we can choose among many value:
+# https://clang.llvm.org/docs/ClangFormatStyleOptions.html#alignafteropenbracket,
+# but when we do so, we need to stick to one permitted style only, the tool don’t
+# allow to specify more than one.
+#
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg02311.html
+# ------------------------------------------------------------------------------
+# Jan:
+# > I see your two snippets above but I’ve always found at least on arm a
+# > predominance of the style above for functions, so arguments aligned after
+# > the opening bracket, for macros there is a mix.

xen/.clang-format line 204

Quoted 10 lines
xen/.clang-format new file +10 -0
+#
+# The latter "above" refers to which form exactly? The one you originally
+# spelled out, or the former of what my reply had?
+#
+# > In any case we can choose among many value:
+# > https://clang.llvm.org/docs/ClangFormatStyleOptions.html#alignafteropenbracket,
+# > but when we do so, we need to stick to one permitted style only, the tool
+# > don’t allow to specify more than one.
+#
+# a2k: RED FLAG

What does 'a2k' mean?

Quoted 6 lines
Quoted 3 lines

I'm afraid this needs to be trimmed to a more manageable comment, otherwise I simply get lost in the context. Maybe it makes sense more people involved in the discussion.

Sure, see my explanation above: this is just to give some more context and have some ready answers for the questions being asked before. I will remove all these right after we have some consensus.

Sorry for possibly not fully understanding the purpose and being stubborn, but IMO consensus should be reached on the email discussion, so that here you can make a proposal, possibly with a comment justifying the chosen value. Just copying excerpts from the email thread is not likely to help, as I at least find myself completely lost.

Otherwise if there's no consensus just add a TODO next to the option explaining why the current setting might not be optimal or the (known) shortcomings of it, so that we know there's some pending work in this area. But it needs to be a concrete list, not pieces from an email discussion.

Thanks, Roger.

#9 lore raw
In reply to Oleksandr Andrushchenko

Hi Oleksandr,

I really appreciate the examples you provided; they were very enlightening regarding the way forward.

Today we discussed the topic with the maintainers again, and I believe we have identified the best approach to take.

Since it is not feasible to use clang-format to make changes to an entire file at once, and even applying clang-format patch by patch, as demonstrated in this series, poses challenges, we think that clang-format is not a suitable solution. Many of the suggested changes are unwelcome, making its use impractical.

We believe the best approach is to start small and use a tool like checkpatch.pl to enforce a limited set of coding style rules that we all agree on. A simple starting point could be enforcing basic rules such as indentation must be four spaces.

The key idea is that it is better to have a tool that enforces a few rules correctly rather than one that enforces many rules but some of them wrong.

With this in mind, a tool similar to checkpatch.pl that verifies only the coding style rules explicitly defined in CODING_STYLE would be an excellent starting point. If we identify inconsistencies within Xen source files, the maintainers have agreed to review each issue individually and establish a consensus-based resolution, similar to how we handled MISRA rules.

It is important to note that we will need to make exceptions for files imported from Linux, many of which are already listed under docs/misra/exclude-list.json. We can reuse this exclusion list for coding style checks, ensuring that it does not interfere with how exclude-list.json is currently used by ECLAIR for MISRA scans.

Does this approach make sense? In summary, if we had a version of checkpatch.pl that enforced the coding style defined in CODING_STYLE instead of Linux-specific style rules, we would be happy to adopt it and integrate it into the CI loop. The tool does not necessarily have to be written in Perl; it could be implemented in another language, such as Python, if that is more convenient.

One final note: Please disregard the section "Header inclusion guards" in CODING_STYLE, as we are aware that it needs updating.

Cheers,

Stefano

Quoted 57 lines · Oleksandr Andrushchenko

Hello, everybody!

In order to continue activity started at [1] I am sending this series which allows to manually run coding style checks and does not provide any automation yet.

Only the first patch is the goal of this series and patches 2-4 are for example only.

This proposal disables coding style checks for the project, but xen/
folder: this is done by providing a global .clang-format at the project top level which disables clang-format and only providing .clang-format for xen/ folder.

clang-format version expected to be >15 and the latest tool can be installed with:
python3 -m pip install clang-format
This allows having the tool without the need to compile or install anything else.

As no automatic code style checks are performed, so all those can be run manually:

- to see changes proposed to the patch being worked on (not committed yet):
git-clang-format --diff --style=file --verbose

- to run code formatting on the HEAD patch:
git-clang-format --style=file --verbose HEAD~1

Provided xen/.clang-format still has a lot of comments gathered from the previous discussions. This is for purpose of better seeing why some of the options have their values. Once option values are accepted all those comments can be removed.

Stay safe,
Oleksandr Andrushchenko

[1] https://lists.xen.org/archives/html/xen-devel/2025-02/msg00800.html

Oleksandr Andrushchenko (4):
Add .clang-format files to enable manual coding style checks [EXAMPLE] Provide an example of a malformed patch [EXAMPLE] Changes done by clang-format to the previous commit [EXAMPLE] Whole file code style formatting

 .clang-format              |    5 +
 tools/xl/xl_info.c         |    6 +-
 xen/.clang-format          | 1380 ++++++++++++++++++++++++++++++++++++
 xen/drivers/char/ns16550.c |   13 +-
 xen/drivers/pci/pci.c      |    8 +-
 5 files changed, 1397 insertions(+), 15 deletions(-)
 create mode 100644 .clang-format
 create mode 100644 xen/.clang-format
Signature · 1 line
2.25.1
#5 lore raw

Re: [PATCH 1/4] Add .clang-format files to enable manual coding style checks

In reply to Roger Pau Monné

Hello, Roger!

Quoted 48 lines · Roger Pau Monné
Quoted 34 lines · Oleksandr Andrushchenko

Hello, Roger!

Quoted 18 lines · Roger Pau Monné
Quoted 17 lines · Oleksandr Andrushchenko

Disable coding style checks for the project, but xen/ folder:
this is done by providing a global .clang-format at the top level which disables clang-format and only providing .clang-format for xen/.

clang-format version expected to be >15 and the latest tool can be installed with:
python3 -m pip install clang-format

Please note, that no automatic code style checks are performed and all those can be run manually:

- to see changes proposed to the patch being worked on (not committed yet):
git-clang-format --diff --style=file --verbose

- to run code formatting on the HEAD patch:
git-clang-format --style=file --verbose HEAD~1

Quoted 8 lines
Quoted 4 lines

Provided xen/.clang-format still has a lot of comments gathered from the previous discussions. This is for purpose of better seeing why some of the options have their values. Once option values are accepted all those comments can be removed.

I think the comments need to be trimmed to a concise explanation of why a setting is the way it is. The (IMO overly long) excerpts from email conversations are difficult to follow in this context. Someone has to spend the time to digest them and provide a shorter comment.

This is the goal. I do not expect the patch to be committed right now, so I left those intentionally to provide some background when those options were discussed before and, possibly, to address some questions which were brought earlier.
The final patch will have all those in the way you mention here

Quoted 8 lines
Quoted 4 lines

One thing that I'm missing: don't we need a list of excluded files somewhere because they are imports from Linux and we would like to keep the Linux coding style for avoiding conflicts when backporting fixes?

All files are excluded from formatting, but xen/ directory: this is by providing a top level .clang-format file which disables all formatting and also providing xen/.clang-format which is exactly for xen/.

There are files with intentional Linux coding style inside of xen/, for example xen/arch/x86/cpu/mwait-idle.c. We would need a way to filter those out so that clang-format doesn't attempt to apply the Xen coding style to them.

Quoted 4 lines

Otherwise it needs to be noted possibly in .clang-format that while the clang-format style will apply by default to all files inside of xen/, some of those use a different coding style on purpose, and those should not be reformatted.

There is an analog of .gitignore which is .clang-format-ignore [1]

Quoted 4 lines

We also have some scripts in python and shell inside of both tools/ and scripts/, are those already excluded by clang-format by not having a .c or .h extension?

Currently it is only C/C++ files with the settings in the .clang-format provided

Quoted 78 lines
Quoted 72 lines
Quoted 72 lines
Quoted 60 lines
xen/.clang-format new file +59 -0
+################################################################################
+# Options and their discussions
+################################################################################
+
+# [not specified]
+# Align function parameter that goes into a new line, under the open bracket
+# (supported in clang-format 3.8)
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg01993.html
+# ------------------------------------------------------------------------------
+# Luca: This one is to align function parameters that overflows the line length,
+# I chose to align them to the open bracket to match the current codebase
+# (hopefully) e.g.:
+# someLongFunction(argument1,
+#                  argument2);
+#
+# This one can be a candidate for an entry in our coding style
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg02143.html
+# ------------------------------------------------------------------------------
+# Jan:
+# > e.g.:
+# > someLongFunction(argument1,
+# >                  argument2);
+# The above matches neither of the two generally permitted styles:
+#
+#    someLongFunction(argument1,
+#                     argument2);
+#
+#    someLongFunction(
+#        argument1,
+#        argument2);
+#
+# Then again from its name I would infer this isn't just about function
+# arguments?
+#
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg02172.html
+# ------------------------------------------------------------------------------
+# Luca:
+# I think it applies to parameters and arguments of functions and macro, given
+# the description in the docs.
+# I see your two snippets above but I’ve always found at least on arm a
+# predominance of the style above for functions, so arguments aligned after the
+# opening bracket, for macros there is a mix.
+# I might be wrong though and so another opinion from another maintainer would
+# help.
+# In any case we can choose among many value:
+# https://clang.llvm.org/docs/ClangFormatStyleOptions.html#alignafteropenbracket,
+# but when we do so, we need to stick to one permitted style only, the tool don’t
+# allow to specify more than one.
+#
+# ------------------------------------------------------------------------------
+# https://lists.xenproject.org/archives/html/xen-devel/2023-11/msg02311.html
+# ------------------------------------------------------------------------------
+# Jan:
+# > I see your two snippets above but I’ve always found at least on arm a
+# > predominance of the style above for functions, so arguments aligned after
+# > the opening bracket, for macros there is a mix.

xen/.clang-format line 204

Quoted 10 lines
xen/.clang-format new file +10 -0
+#
+# The latter "above" refers to which form exactly? The one you originally
+# spelled out, or the former of what my reply had?
+#
+# > In any case we can choose among many value:
+# > https://clang.llvm.org/docs/ClangFormatStyleOptions.html#alignafteropenbracket,
+# > but when we do so, we need to stick to one permitted style only, the tool
+# > don’t allow to specify more than one.
+#
+# a2k: RED FLAG

What does 'a2k' mean?

Quoted 6 lines
Quoted 3 lines

I'm afraid this needs to be trimmed to a more manageable comment, otherwise I simply get lost in the context. Maybe it makes sense more people involved in the discussion.

Sure, see my explanation above: this is just to give some more context and have some ready answers for the questions being asked before. I will remove all these right after we have some consensus.

Quoted 6 lines

Sorry for possibly not fully understanding the purpose and being stubborn, but IMO consensus should be reached on the email discussion, so that here you can make a proposal, possibly with a comment justifying the chosen value. Just copying excerpts from the email thread is not likely to help, as I at least find myself completely lost.

Agree, but the opposite happened before: same questions asked again as we are out of context each time...

Quoted 6 lines

Otherwise if there's no consensus just add a TODO next to the option explaining why the current setting might not be optimal or the (known) shortcomings of it, so that we know there's some pending work in this area. But it needs to be a concrete list, not pieces from an email discussion.

It is not that easy as options act as a set, we cannot have one option per rule unfortunately.

Quoted 2 lines

Thanks, Roger.

Thank  you,
Oleksandr

[1] https://clang.llvm.org/docs/ClangFormat.html#clang-format-ignore