Skip to contentSkip to conversation

docs/devel: Document conventional file prefixes and suffixes

PATCH In review Philippe Mathieu-Daudé 4 participants 5 messages Dec 26, 2023 – Jan 2, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 docs/devel: Document conventional file prefixes and suffixes

Some header and source file names use common prefix / suffix but we never really ruled a convention. Start doing so with the current patterns from the tree.

---
 docs/devel/style.rst | 49 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
docs/devel/style.rst +49 -0
index 2f68b50079..4da50eb2ea 100644
@@ -162,6 +162,55 @@ pre-processor. Another common suffix is ``_impl``; it is used for the
 concrete implementation of a function that will not be called
 directly, but rather through a macro or an inline function.
 
+File Naming Conventions
+-----------------------
+
+Public headers
+~~~~~~~~~~~~~~
+
+Headers expected to be access by multiple subsystems must reside in
+the ``include/`` folder. Headers local to a subsystem should reside in
+the sysbsystem folder, if any (for example ``qobject/qobject-internal.h``
+can only be included by files within the ``qobject/`` folder).
+
+Header file prefix and suffix hints
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+When headers relate to common concept, it is useful to use a common
+prefix or suffix.
+
+When headers relate to the same (guest) subsystem, the subsystem name is
+often used as prefix. If headers are already in a folder named as the
+subsystem, prefixing them is optional.
+
+For example, hardware models related to the Aspeed systems are named
+using the ``aspeed_`` prefix.
+
+Headers related to the same (host) concept can also use a common prefix.
+For example OS specific headers use the ``-posix`` and ``-win32`` suffixes.
+
+Registered file suffixes
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+* ``.inc``
+
+  Source files meant to be included by other source files as templates
+  must use the ``.c.inc`` suffix. Similarly, headers meant to be included
+  multiple times as template must use the ``.h.inc`` suffix.
+
+Recommended file prefixes / suffixes
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+* ``target`` and ``common`` suffixes
+
+  Files which are specific to a target should use the ``target`` suffix.
+  Such ``target`` suffixed headers usually *taint* the files including them
+  by making them target specific.
+
+  Files common to all targets should use the ``common`` suffix, to provide
+  a hint that these files can be safely included from common code.
+
+
 Block structure
 ===============
 
Signature · 1 line
2.41.0
#1 lore raw

Hi Philippe,

Quoted 39 lines · Philippe Mathieu-Daudé

Date: Tue, 26 Dec 2023 16:04:41 +0100
From: Philippe Mathieu-Daudé <[email protected]>
Subject: [PATCH] docs/devel: Document conventional file prefixes and
suffixes
X-Mailer: git-send-email 2.41.0

Some header and source file names use common prefix / suffix but we never really ruled a convention. Start doing so with the current patterns from the tree.

---
 docs/devel/style.rst | 49 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
docs/devel/style.rst +15 -0
index 2f68b50079..4da50eb2ea 100644
@@ -162,6 +162,55 @@ pre-processor. Another common suffix is ``_impl``; it is used for the
 concrete implementation of a function that will not be called
 directly, but rather through a macro or an inline function.
 
+File Naming Conventions
+-----------------------
+
+Public headers
+~~~~~~~~~~~~~~
+
+Headers expected to be access by multiple subsystems must reside in
+the ``include/`` folder. Headers local to a subsystem should reside in
+the sysbsystem folder, if any (for example ``qobject/qobject-internal.h``
+can only be included by files within the ``qobject/`` folder).
+
+Header file prefix and suffix hints
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+When headers relate to common concept, it is useful to use a common

docs/devel/style.rst pre-processor. Another common suffix is ``_impl``; it is used for the · line 189

Quoted 10 lines
docs/devel/style.rst +10 -0
+prefix or suffix.
+
+When headers relate to the same (guest) subsystem, the subsystem name is
+often used as prefix. If headers are already in a folder named as the
+subsystem, prefixing them is optional.
+
+For example, hardware models related to the Aspeed systems are named
+using the ``aspeed_`` prefix.
+
+Headers related to the same (host) concept can also use a common prefix.
                                                                    ^^^^^^
                                                             Maybe "suffix"?

since below you provide examples of "suffix".

Quoted 7 lines
docs/devel/style.rst +7 -0
+For example OS specific headers use the ``-posix`` and ``-win32`` suffixes.
+
+Registered file suffixes
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+* ``.inc``
+

docs/devel/style.rst pre-processor. Another common suffix is ``_impl``; it is used for the · line 206

Quoted 10 lines
docs/devel/style.rst +10 -0
+  Source files meant to be included by other source files as templates
+  must use the ``.c.inc`` suffix. Similarly, headers meant to be included
+  multiple times as template must use the ``.h.inc`` suffix.
+
+Recommended file prefixes / suffixes
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+* ``target`` and ``common`` suffixes
+
+  Files which are specific to a target should use the ``target`` suffix.

emm, it seems linux-use/* and bsd-user/* have many ``target`` prefix headers. Should they get cleaned up?

docs/devel/style.rst pre-processor. Another common suffix is ``_impl``; it is used for the · line 213

Quoted 7 lines
docs/devel/style.rst +7 -0
+  Such ``target`` suffixed headers usually *taint* the files including them
+  by making them target specific.
+
+  Files common to all targets should use the ``common`` suffix, to provide
+  a hint that these files can be safely included from common code.
+
+

An additional question that kind of confuses me is whether header file naming should use "-" or "_" to connect prefixes/suffixes?

Quoted 6 lines
docs/devel/style.rst +0 -0
 Block structure
 ===============
 
Signature · 1 line
2.41.0

Thanks,
Zhao

#2 lore raw

Hi,

Quoted 55 lines · Zhao Liu

Hi Philippe,

Quoted 39 lines · Philippe Mathieu-Daudé

Date: Tue, 26 Dec 2023 16:04:41 +0100
From: Philippe Mathieu-Daudé <[email protected]>
Subject: [PATCH] docs/devel: Document conventional file prefixes and
suffixes
X-Mailer: git-send-email 2.41.0

Some header and source file names use common prefix / suffix but we never really ruled a convention. Start doing so with the current patterns from the tree.

---
  docs/devel/style.rst | 49 ++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 49 insertions(+)
docs/devel/style.rst +15 -0
index 2f68b50079..4da50eb2ea 100644
@@ -162,6 +162,55 @@ pre-processor. Another common suffix is ``_impl``; it is used for the
  concrete implementation of a function that will not be called
  directly, but rather through a macro or an inline function.
 
+File Naming Conventions
+-----------------------
+
+Public headers
+~~~~~~~~~~~~~~
+
+Headers expected to be access by multiple subsystems must reside in
+the ``include/`` folder. Headers local to a subsystem should reside in
+the sysbsystem folder, if any (for example ``qobject/qobject-internal.h``
+can only be included by files within the ``qobject/`` folder).
+
+Header file prefix and suffix hints
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+When headers relate to common concept, it is useful to use a common

docs/devel/style.rst pre-processor. Another common suffix is ``_impl``; it is used for the · line 189

Quoted 10 lines
docs/devel/style.rst +10 -0
+prefix or suffix.
+
+When headers relate to the same (guest) subsystem, the subsystem name is
+often used as prefix. If headers are already in a folder named as the
+subsystem, prefixing them is optional.
+
+For example, hardware models related to the Aspeed systems are named
+using the ``aspeed_`` prefix.
+
+Headers related to the same (host) concept can also use a common prefix.
                                                                     ^^^^^^
                                                              Maybe "suffix"?
Quoted 1 line

since below you provide examples of "suffix".

Oops, indeed :)

Quoted 12 lines
Quoted 12 lines
docs/devel/style.rst +12 -0
+For example OS specific headers use the ``-posix`` and ``-win32`` suffixes.
+
+Registered file suffixes
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+* ``.inc``
+
+  Source files meant to be included by other source files as templates
+  must use the ``.c.inc`` suffix. Similarly, headers meant to be included
+  multiple times as template must use the ``.h.inc`` suffix.
+
+Recommended file prefixes / suffixes
Quoted 8 lines

docs/devel/style.rst pre-processor. Another common suffix is ``_impl``; it is used for the · line 206

Quoted 5 lines
docs/devel/style.rst +5 -0
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+* ``target`` and ``common`` suffixes
+
+  Files which are specific to a target should use the ``target`` suffix.

emm, it seems linux-use/* and bsd-user/* have many ``target`` prefix headers. Should they get cleaned up?

No, these are special to user emulation, and are defined for each target, to be included once per target build, i.e. for Linux:

$ git grep '#include "target_' linux-user
linux-user/elfload.c:23:#include "target_signal.h"
linux-user/flatload.c:43:#include "target_flat.h"
linux-user/main.c:50:#include "target_elf.h"
linux-user/mmap.c:26:#include "target_mman.h"
linux-user/qemu.h:12:#include "target_syscall.h"
linux-user/strace.c:21:#include "target_mman.h"
linux-user/syscall.c:27:#include "target_mman.h"
linux-user/syscall.c:6313:#include "target_prctl.h"
linux-user/syscall.c:8252:#include "target_proc.h"
linux-user/syscall_defs.h:497:#include "target_signal.h"
linux-user/syscall_defs.h:701:#include "target_resource.h"
linux-user/syscall_defs.h:1230:#include "target_mman.h"
linux-user/syscall_defs.h:2256:#include "target_fcntl.h"
linux-user/syscall_defs.h:2577:#include "target_errno_defs.h"
linux-user/user-internals.h:184:#include "target_cpu.h"
linux-user/user-internals.h:185:#include "target_structs.h"

I'll add a paragraph to describe that.

Quoted 11 lines

docs/devel/style.rst pre-processor. Another common suffix is ``_impl``; it is used for the · line 213

Quoted 8 lines
docs/devel/style.rst +7 -0
+  Such ``target`` suffixed headers usually *taint* the files including them
+  by making them target specific.
+
+  Files common to all targets should use the ``common`` suffix, to provide
+  a hint that these files can be safely included from common code.
+
+

An additional question that kind of confuses me is whether header file naming should use "-" or "_" to connect prefixes/suffixes?

Yeah, we use a mix of both with no particular preference.

Not sure it is worth cleaning only for aesthetic style, let's see what other think.

Quoted 7 lines
Quoted 7 lines
docs/devel/style.rst +0 -0
  Block structure
  ===============
 
Signature · 1 line
2.41.0

Thanks!

Phil.

#3 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 17 lines · Philippe Mathieu-Daudé

Some header and source file names use common prefix / suffix but we never really ruled a convention. Start doing so with the current patterns from the tree.

---
  docs/devel/style.rst | 49 ++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 49 insertions(+)
docs/devel/style.rst +0 -0
index 2f68b50079..4da50eb2ea 100644
@@ -162,6 +162,55 @@ pre-processor. Another common suffix is ``_impl``; it is used for the
  concrete implementation of a function that will not be called
  directly, but rather through a macro or an inline function.

docs/devel/style.rst pre-processor. Another common suffix is ``_impl``; it is used for the · line 173

Quoted 10 lines
docs/devel/style.rst +9 -0
 
+File Naming Conventions
+-----------------------
+
+Public headers
+~~~~~~~~~~~~~~
+
+Headers expected to be access by multiple subsystems must reside in
+the ``include/`` folder. Headers local to a subsystem should reside in
+the sysbsystem folder, if any (for example ``qobject/qobject-internal.h``

subsystem.

r~

#4 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 15 lines · Philippe Mathieu-Daudé

Some header and source file names use common prefix / suffix but we never really ruled a convention. Start doing so with the current patterns from the tree.

---
 docs/devel/style.rst | 49 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
docs/devel/style.rst +0 -0
index 2f68b50079..4da50eb2ea 100644

docs/devel/style.rst pre-processor. Another common suffix is ``_impl``; it is used for the · line 171

Quoted 10 lines
docs/devel/style.rst +7 -0
@@ -162,6 +162,55 @@ pre-processor. Another common suffix is ``_impl``; it is used for the
 concrete implementation of a function that will not be called
 directly, but rather through a macro or an inline function.
 
+File Naming Conventions
+-----------------------
+
+Public headers
+~~~~~~~~~~~~~~
+
+Headers expected to be access by multiple subsystems must reside in

s/access/accessed/

docs/devel/style.rst pre-processor. Another common suffix is ``_impl``; it is used for the · line 173

Quoted 2 lines
docs/devel/style.rst +2 -0
+the ``include/`` folder. Headers local to a subsystem should reside in
+the sysbsystem folder, if any (for example ``qobject/qobject-internal.h``

s/sysbsystem/subsystem/

docs/devel/style.rst pre-processor. Another common suffix is ``_impl``; it is used for the · line 179

Quoted 6 lines
docs/devel/style.rst +6 -0
+can only be included by files within the ``qobject/`` folder).
+
+Header file prefix and suffix hints
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+When headers relate to common concept, it is useful to use a common

Either "common concepts" (plural) or "a common concept" (singular with an indefinite article).

docs/devel/style.rst pre-processor. Another common suffix is ``_impl``; it is used for the · line 184

Quoted 5 lines
docs/devel/style.rst +5 -0
+prefix or suffix.
+
+When headers relate to the same (guest) subsystem, the subsystem name is
+often used as prefix. If headers are already in a folder named as the
+subsystem, prefixing them is optional.

"named as the subsystem" sounds strange. I suggest something like:

"If headers are already in a folder with the subsystem in its name, prefixing them is optional."

or

"Prefixing header files is optional if the folder name already contains the subsystem name."

docs/devel/style.rst pre-processor. Another common suffix is ``_impl``; it is used for the · line 189

Quoted 5 lines
docs/devel/style.rst +5 -0
+
+For example, hardware models related to the Aspeed systems are named
+using the ``aspeed_`` prefix.
+
+Headers related to the same (host) concept can also use a common prefix.

Is there a need to distinguish between "(guest)" above and "(host)" here since we end up recommending the same thing for both?

docs/devel/style.rst pre-processor. Another common suffix is ``_impl``; it is used for the · line 190

Quoted 1 line
docs/devel/style.rst +1 -0
+For example OS specific headers use the ``-posix`` and ``-win32`` suffixes.

The previous sentence is about prefixes but this sentence focusses on suffixes. That's a little confusing. I guess you mean "foo-posix" and "foo-win32" have a common prefix. It may help to express it in terms of the prefix instead of mentioning the suffix.

Quoted 8 lines
docs/devel/style.rst +8 -0
+
+Registered file suffixes
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+* ``.inc``
+
+  Source files meant to be included by other source files as templates
+  must use the ``.c.inc`` suffix. Similarly, headers meant to be included

docs/devel/style.rst pre-processor. Another common suffix is ``_impl``; it is used for the · line 208

Quoted 10 lines
docs/devel/style.rst +10 -0
+  multiple times as template must use the ``.h.inc`` suffix.
+
+Recommended file prefixes / suffixes
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+* ``target`` and ``common`` suffixes
+
+  Files which are specific to a target should use the ``target`` suffix.
+  Such ``target`` suffixed headers usually *taint* the files including them
+  by making them target specific.

Is there any particular macro or pattern for enforcing this? I remember there are #error preprocessor directives in some header files to prevent including them from the wrong source file, but I'm not sure if you're referring to anything specific here.

docs/devel/style.rst pre-processor. Another common suffix is ``_impl``; it is used for the · line 211

Quoted 3 lines
docs/devel/style.rst +3 -0
+
+  Files common to all targets should use the ``common`` suffix, to provide
+  a hint that these files can be safely included from common code.

This statement is too general. For example, files in util/ can be used from common code but don't have a suffix. I think target and common suffixes are useful when something is split into target-specific and common parts. Otherwise it's not necessary.