Skip to contentSkip to conversation

tools clang-format: Add a tools clang-format that overrides some kernel behaviors

PATCH Superseded Ian Rogers 5 participants 12 messages Apr 7 – May 20 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 tools clang-format: Add a tools clang-format that overrides some kernel behaviors

In particular, header file ordering is an issue in the tools/ directory given the larger number of depended upon libraries.

The order of header file includes was proposed in:
https://lore.kernel.org/linux-perf-users/CAP-5=fUitzKwJONTngiW17XkS7kVr2cDS4cDL_HccJKcnR2EgQ@mail.gmail.com/

Sorting headers is desirable to avoid issues like duplicate includes.

---
 tools/.clang-format | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100644 tools/.clang-format
tools/.clang-format new file +20 -0
new file mode 100644
index 000000000000..902b2f7456f6
@@ -0,0 +1,20 @@
+BasedOnStyle: InheritParentConfig
+SortIncludes: true
+IncludeBlocks: Regroup
+IncludeCategories:
+  # Implicitly the corresponding header for the C file has Priority 0
+  # C Standard Library Headers
+  - Regex:           '^<(assert|complex|ctype|errno|fenv|float|inttypes|iso646|limits|locale|math|setjmp|signal|stdalign|stdarg|stdatomic|stdbool|stddef|stdint|stdio|stdlib|stdnoreturn|string|tgmath|threads|time|uchar|wchar|wctype)\.h>'
+    Priority:        1
+  # OS/System-Specific Headers (directories)
+  - Regex:           '^<(sys|linux|asm|arpa|net|netinet|x86_64|machine)/.*>'
+    Priority:        2
+  # OS/System-Specific Headers (POSIX/System flat headers)
+  - Regex:           '^<(unistd|pthread|fcntl|dirent|dlfcn|poll|sched|semaphore|spawn|syslog|termios|pwd|grp|netdb|sysexits|err|paths|pty|utmp|resolv|ifaddrs|elf|libelf|gelf)\.h>'
+    Priority:        2
+  # Third-Party Library Headers
+  - Regex:           '^<.*>'
+    Priority:        3
+  # Your Project's Other Headers
+  - Regex:           '^".*"'
+    Priority:        4
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
#1 lore raw
Quoted 1 line · Ian Rogers

Sorting headers is desirable to avoid issues like duplicate includes.

Perhaps.

Some others prefer reverse-christmas-tree by length ordering for #includes though I think it's poor style.

author reviewer
#2 lore raw
Quoted 9 lines · Joe Perches
Quoted 1 line · Ian Rogers

Sorting headers is desirable to avoid issues like duplicate includes.

Perhaps.

Some others prefer reverse-christmas-tree by length ordering for #includes though I think it's poor style.

Thanks Joe. I second not switching to a reverse-christmas-tree style :-)

If this patch is okay, could we bring it in through the perf tree? Namhyung/Arnaldo, are you okay with doing this? Being able to easily git clang-format is useful for my work in perf, and the motivating conversation occurred on linux-perf-users.

Thanks,
Ian

#3 lore raw
Quoted 11 lines · Ian Rogers
Quoted 1 line · Joe Perches

On Tue, 2026-04-07 at 09:55 -0700, Ian Rogers wrote:

Sorting headers is desirable to avoid issues like duplicate includes.

Quoted 4 lines

Perhaps.

Some others prefer reverse-christmas-tree by length ordering for #includes though I think it's poor style.

Thanks Joe. I second not switching to a reverse-christmas-tree style :-)

If this patch is okay, could we bring it in through the perf tree?

It's likely better to avoid this suggestion.

The issue is that others strongly prefer that reverse-christmas-tree style and would object so nak.

author reviewer
#4 lore raw
Quoted 10 lines · Joe Perches
Quoted 8 lines · Ian Rogers
Quoted 1 line · Joe Perches

On Tue, 2026-04-07 at 09:55 -0700, Ian Rogers wrote:

Sorting headers is desirable to avoid issues like duplicate includes.

Quoted 4 lines

Perhaps.

Some others prefer reverse-christmas-tree by length ordering for #includes though I think it's poor style.

Quoted 8 lines
Quoted 3 lines

Thanks Joe. I second not switching to a reverse-christmas-tree style :-)

If this patch is okay, could we bring it in through the perf tree?

It's likely better to avoid this suggestion.

The issue is that others strongly prefer that reverse-christmas-tree style and would object so nak.

Presumably that is a kernel suggestion rather than a tools directory suggestion. We can move the file into tools/perf, tools/lib/perf, etc. if that removes more global concerns. perf hasn't adopted a reverse-christmas-tree style.

Thanks,
Ian

#5 lore raw
Quoted 10 lines · Ian Rogers
Quoted 1 line · Joe Perches

On Mon, 2026-04-13 at 08:39 -0700, Ian Rogers wrote:

On Tue, Apr 7, 2026 at 10:11 AM Joe Perches <[[[email protected]](mailto:[email protected])](mailto:[[email protected]](mailto:[email protected]))> wrote:

Quoted 6 lines · Ian Rogers
Quoted 6 lines

Sorting headers is desirable to avoid issues like duplicate includes.

Quoted 3 lines

Perhaps.
Some others prefer reverse-christmas-tree by length ordering for #includes though I think it's poor style.

Thanks Joe. I second not switching to a reverse-christmas-tree style :-) If this patch is okay, could we bring it in through the perf tree?

Quoted 7 lines
Quoted 3 lines

It's likely better to avoid this suggestion.
The issue is that others strongly prefer that reverse-christmas-tree style and would object so nak.

Presumably that is a kernel suggestion rather than a tools directory suggestion. We can move the file into tools/perf, tools/lib/perf, etc. if that removes more global concerns. perf hasn't adopted a reverse-christmas-tree style.

Fine by me.
Just curious: is this a style control file used by directory depth/path?

author reviewer
#6 lore raw
Quoted 15 lines · Joe Perches
Quoted 13 lines · Ian Rogers
Quoted 1 line · Joe Perches

On Mon, 2026-04-13 at 08:39 -0700, Ian Rogers wrote:

On Tue, Apr 7, 2026 at 10:11 AM Joe Perches <[[[email protected]](mailto:[email protected])](mailto:[[email protected]](mailto:[email protected]))> wrote:

Quoted 9 lines · Ian Rogers
Quoted 6 lines

Sorting headers is desirable to avoid issues like duplicate includes.

Quoted 3 lines

Perhaps.
Some others prefer reverse-christmas-tree by length ordering for #includes though I think it's poor style.

Thanks Joe. I second not switching to a reverse-christmas-tree style :-) If this patch is okay, could we bring it in through the perf tree?

It's likely better to avoid this suggestion.
The issue is that others strongly prefer that reverse-christmas-tree style and would object so nak.

Quoted 7 lines
Quoted 4 lines

Presumably that is a kernel suggestion rather than a tools directory suggestion. We can move the file into tools/perf, tools/lib/perf, etc. if that removes more global concerns. perf hasn't adopted a reverse-christmas-tree style.

Fine by me.
Just curious: is this a style control file used by directory depth/path?

clang-format searches from the directory containing the file to be formatted upwards until it finds a .clang-format file. This change places the style in tools/, which is my preferred location for consistency across different tools and libraries. Header file ordering is a different issue within the kernel tree because it generally only references itself.

Thanks,
Ian

maintainer
#7 lore raw
Quoted 24 lines · Ian Rogers
Quoted 15 lines · Joe Perches
Quoted 13 lines · Ian Rogers
Quoted 1 line · Joe Perches

On Mon, 2026-04-13 at 08:39 -0700, Ian Rogers wrote:

On Tue, Apr 7, 2026 at 10:11 AM Joe Perches <[[[email protected]](mailto:[email protected])](mailto:[[email protected]](mailto:[email protected]))> wrote:

Quoted 9 lines · Ian Rogers
Quoted 6 lines

Sorting headers is desirable to avoid issues like duplicate includes.

Quoted 3 lines

Perhaps.
Some others prefer reverse-christmas-tree by length ordering for #includes though I think it's poor style.

Thanks Joe. I second not switching to a reverse-christmas-tree style :-) If this patch is okay, could we bring it in through the perf tree?

It's likely better to avoid this suggestion.
The issue is that others strongly prefer that reverse-christmas-tree style and would object so nak.

Quoted 7 lines
Quoted 4 lines

Presumably that is a kernel suggestion rather than a tools directory suggestion. We can move the file into tools/perf, tools/lib/perf, etc. if that removes more global concerns. perf hasn't adopted a reverse-christmas-tree style.

Fine by me.
Just curious: is this a style control file used by directory depth/path?

Quoted 6 lines

clang-format searches from the directory containing the file to be formatted upwards until it finds a .clang-format file. This change places the style in tools/, which is my preferred location for consistency across different tools and libraries. Header file ordering is a different issue within the kernel tree because it generally only references itself.

I think we need confirmation from other folks in tools/ if we want to put it there.

Thanks,
Namhyung

author reviewer
#9 lore raw

PATCH v2 1/1 perf clang-format: Add a perf clang-format that overrides some kernel behaviors

In reply to Ian Rogers

In particular, header file ordering is an issue in the tools/perf directory given the larger number of depended upon libraries.

The order of header file includes was proposed in:
https://lore.kernel.org/linux-perf-users/CAP-5=fUitzKwJONTngiW17XkS7kVr2cDS4cDL_HccJKcnR2EgQ@mail.gmail.com/

Sorting headers is desirable to avoid issues like duplicate includes.

---
v2: Move the .clang-format file under tools/perf rather than tools/.

---
 tools/perf/.clang-format | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100644 tools/perf/.clang-format
tools/perf/.clang-format new file +20 -0
new file mode 100644
index 000000000000..902b2f7456f6
@@ -0,0 +1,20 @@
+BasedOnStyle: InheritParentConfig
+SortIncludes: true
+IncludeBlocks: Regroup
+IncludeCategories:
+  # Implicitly the corresponding header for the C file has Priority 0
+  # C Standard Library Headers
+  - Regex:           '^<(assert|complex|ctype|errno|fenv|float|inttypes|iso646|limits|locale|math|setjmp|signal|stdalign|stdarg|stdatomic|stdbool|stddef|stdint|stdio|stdlib|stdnoreturn|string|tgmath|threads|time|uchar|wchar|wctype)\.h>'
+    Priority:        1
+  # OS/System-Specific Headers (directories)
+  - Regex:           '^<(sys|linux|asm|arpa|net|netinet|x86_64|machine)/.*>'
+    Priority:        2
+  # OS/System-Specific Headers (POSIX/System flat headers)
+  - Regex:           '^<(unistd|pthread|fcntl|dirent|dlfcn|poll|sched|semaphore|spawn|syslog|termios|pwd|grp|netdb|sysexits|err|paths|pty|utmp|resolv|ifaddrs|elf|libelf|gelf)\.h>'
+    Priority:        2
+  # Third-Party Library Headers
+  - Regex:           '^<.*>'
+    Priority:        3
+  # Your Project's Other Headers
+  - Regex:           '^".*"'
+    Priority:        4
Signature · 1 line
2.54.0.545.g6539524ca2-goog
author reviewer
#10 lore raw

Re: [PATCH v2] perf clang-format: Add a perf clang-format that overrides some kernel behaviors

Quoted 4 lines · Ian Rogers

In particular, header file ordering is an issue in the tools/perf directory given the larger number of depended upon libraries.

Quoted 8 lines

The order of header file includes was proposed in:
https://lore.kernel.org/linux-perf-users/CAP-5=fUitzKwJONTngiW17XkS7kVr2cDS4cDL_HccJKcnR2EgQ@mail.gmail.com/

Sorting headers is desirable to avoid issues like duplicate includes.

---
v2: Move the .clang-format file under tools/perf rather than tools/.

This change helps format header files in tools/perf, otherwise following the parent linux clang-format options. Could we land this in perf-tools-next?

Thanks,
Ian

Quoted 34 lines
---
 tools/perf/.clang-format | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100644 tools/perf/.clang-format
tools/perf/.clang-format new file +20 -1
new file mode 100644
index 000000000000..902b2f7456f6
@@ -0,0 +1,20 @@
+BasedOnStyle: InheritParentConfig
+SortIncludes: true
+IncludeBlocks: Regroup
+IncludeCategories:
+  # Implicitly the corresponding header for the C file has Priority 0
+  # C Standard Library Headers
+  - Regex:           '^<(assert|complex|ctype|errno|fenv|float|inttypes|iso646|limits|locale|math|setjmp|signal|stdalign|stdarg|stdatomic|stdbool|stddef|stdint|stdio|stdlib|stdnoreturn|string|tgmath|threads|time|uchar|wchar|wctype)\.h>'
+    Priority:        1
+  # OS/System-Specific Headers (directories)
+  - Regex:           '^<(sys|linux|asm|arpa|net|netinet|x86_64|machine)/.*>'
+    Priority:        2
+  # OS/System-Specific Headers (POSIX/System flat headers)
+  - Regex:           '^<(unistd|pthread|fcntl|dirent|dlfcn|poll|sched|semaphore|spawn|syslog|termios|pwd|grp|netdb|sysexits|err|paths|pty|utmp|resolv|ifaddrs|elf|libelf|gelf)\.h>'
+    Priority:        2
+  # Third-Party Library Headers
+  - Regex:           '^<.*>'
+    Priority:        3
+  # Your Project's Other Headers
+  - Regex:           '^".*"'
+    Priority:        4
--

2.54.0.545.g6539524ca2-goog

maintainer Applied
#11 lore raw

Re: [PATCH v2] perf clang-format: Add a perf clang-format that overrides some kernel behaviors

Quoted 10 lines · Ian Rogers
Quoted 9 lines · Ian Rogers

In particular, header file ordering is an issue in the tools/perf directory given the larger number of depended upon libraries.

The order of header file includes was proposed in:
https://lore.kernel.org/linux-perf-users/CAP-5=fUitzKwJONTngiW17XkS7kVr2cDS4cDL_HccJKcnR2EgQ@mail.gmail.com/

Sorting headers is desirable to avoid issues like duplicate includes.

Quoted 7 lines
Quoted 3 lines

---
v2: Move the .clang-format file under tools/perf rather than tools/.

This change helps format header files in tools/perf, otherwise following the parent linux clang-format options. Could we land this in perf-tools-next?

Thanks, applied to perf-tools-next, for v7.2.

- Arnaldo