Skip to contentSkip to conversation

qemu-common.h include cleanup

PATCH In review Peter Maydell 6 participants 11 messages Nov 29, 2021 – Dec 7, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover qemu-common.h include cleanup

qemu-common.h has a comment at the top:

* This file is supposed to be included only by .c files. No header file should
* depend on qemu-common.h, as this would easily lead to circular header
* dependencies.

We still have a few .h files which include it, though. The first 3 patches in this series fix that: in 3 out of 4 cases we didn't need the #include at all, and in the 4th case we can instead #include qemu-common.h from just one .c file.

Patch 4 is just removing the #include from 8 files in hw/arm which don't need it at all. (Probably there are other files like this, but I just did the Arm related ones.)

Tested by pushing to gitlab for the CI build.

-- PMM

Peter Maydell (4):
include/hw/i386: Don't include qemu-common.h in .h files target/hexagon/cpu.h: don't include qemu-common.h target/rx/cpu.h: Don't include qemu-common.h hw/arm: Don't include qemu-common.h unnecessarily

 include/hw/i386/microvm.h     | 1 -
 include/hw/i386/x86.h         | 1 -
 target/hexagon/cpu.h          | 1 -
 target/rx/cpu.h               | 1 -
 hw/arm/boot.c                 | 1 -
 hw/arm/digic_boards.c         | 1 -
 hw/arm/highbank.c             | 1 -
 hw/arm/npcm7xx_boards.c       | 1 -
 hw/arm/sbsa-ref.c             | 1 -
 hw/arm/stm32f405_soc.c        | 1 -
 hw/arm/vexpress.c             | 1 -
 hw/arm/virt.c                 | 1 -
 linux-user/hexagon/cpu_loop.c | 1 +
 13 files changed, 1 insertion(+), 12 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/4 include/hw/i386: Don't include qemu-common.h in .h files

The qemu-common.h header is not supposed to be included from any other header files, only from .c files (as documented in a comment at the start of it).

include/hw/i386/x86.h and include/hw/i386/microvm.h break this rule. In fact, the include is not required at all, so we can just drop it from both files.

---
 include/hw/i386/microvm.h | 1 -
 include/hw/i386/x86.h     | 1 -
 2 files changed, 2 deletions(-)
include/hw/i386/microvm.h +0 -1
index 4d9c732d4b2..efcbd926fd4 100644
@@ -18,7 +18,6 @@
 #ifndef HW_I386_MICROVM_H
 #define HW_I386_MICROVM_H
 
-#include "qemu-common.h"
 #include "exec/hwaddr.h"
 #include "qemu/notify.h"
 
include/hw/i386/x86.h +0 -1
index bb1cfb88966..a145a303703 100644
@@ -17,7 +17,6 @@
 #ifndef HW_I386_X86_H
 #define HW_I386_X86_H
 
-#include "qemu-common.h"
 #include "exec/hwaddr.h"
 #include "qemu/notify.h"
 
Signature · 1 line
2.25.1
#2 lore raw

PATCH 2/4 target/hexagon/cpu.h: don't include qemu-common.h

In reply to Peter Maydell

The qemu-common.h header is not supposed to be included from any other header files, only from .c files (as documented in a comment at the start of it).

Move the include to linux-user/hexagon/cpu_loop.c, which needs it for the declaration of cpu_exec_step_atomic().

---
 target/hexagon/cpu.h          | 1 -
 linux-user/hexagon/cpu_loop.c | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
target/hexagon/cpu.h +0 -1
index de121d950f2..58a0d3870bb 100644
@@ -23,7 +23,6 @@ typedef struct CPUHexagonState CPUHexagonState;
 
 #include "fpu/softfloat-types.h"
 
-#include "qemu-common.h"
 #include "exec/cpu-defs.h"
 #include "hex_regs.h"
 #include "mmvec/mmvec.h"
linux-user/hexagon/cpu_loop.c +1 -0
index 6b24cbaba93..e47f8348d56 100644
@@ -19,6 +19,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu-common.h"
 #include "qemu.h"
 #include "user-internals.h"
 #include "cpu_loop-common.h"
Signature · 1 line
2.25.1
Reviewed-by on v1 2/4
#3 lore raw

RE: [PATCH for-7.0 2/4] target/hexagon/cpu.h: don't include qemu-common.h

Quoted 17 lines

-----Original Message-----
From: Peter Maydell <[email protected]>
Sent: Monday, November 29, 2021 2:05 PM
To: [email protected]; [email protected]
Cc: Paolo Bonzini <[email protected]>; Sergio Lopez <[email protected]>; Taylor Simpson <[email protected]>; Yoshinori Sato <[email protected]>; Marcel Apfelbaum <[email protected]>
Subject: [PATCH for-7.0 2/4] target/hexagon/cpu.h: don't include qemu- common.h

The qemu-common.h header is not supposed to be included from any other header files, only from .c files (as documented in a comment at the start of it).

Quoted 8 lines

Move the include to linux-user/hexagon/cpu_loop.c, which needs it for the declaration of cpu_exec_step_atomic().

---
 target/hexagon/cpu.h          | 1 -
 linux-user/hexagon/cpu_loop.c | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
#4 lore raw

PATCH 3/4 target/rx/cpu.h: Don't include qemu-common.h

In reply to Peter Maydell

The qemu-common.h header is not supposed to be included from any other header files, only from .c files (as documented in a comment at the start of it).

Nothing actually relies on target/rx/cpu.h including it, so we can just drop the include.

---
 target/rx/cpu.h | 1 -
 1 file changed, 1 deletion(-)
target/rx/cpu.h +0 -1
index 4ac71aec370..657db84ef0a 100644
@@ -20,7 +20,6 @@
 #define RX_CPU_H
 
 #include "qemu/bitops.h"
-#include "qemu-common.h"
 #include "hw/registerfields.h"
 #include "cpu-qom.h"
 
Signature · 1 line
2.25.1
Reviewed-by on v1 3/4
#5 lore raw

Re: [PATCH for-7.0 3/4] target/rx/cpu.h: Don't include qemu-common.h

Quoted 9 lines · Peter Maydell

The qemu-common.h header is not supposed to be included from any other header files, only from .c files (as documented in a comment at the start of it).

Nothing actually relies on target/rx/cpu.h including it, so we can just drop the include.

Quoted 20 lines
---
 target/rx/cpu.h | 1 -
 1 file changed, 1 deletion(-)
target/rx/cpu.h +0 -1
index 4ac71aec370..657db84ef0a 100644
@@ -20,7 +20,6 @@
 #define RX_CPU_H
 
 #include "qemu/bitops.h"
-#include "qemu-common.h"
 #include "hw/registerfields.h"
 #include "cpu-qom.h"
 
Signature · 1 line
2.25.1
#6 lore raw

PATCH 4/4 hw/arm: Don't include qemu-common.h unnecessarily

In reply to Peter Maydell

A lot of C files in hw/arm include qemu-common.h when they don't need anything from it. Drop the include lines.

omap1.c, pxa2xx.c and strongarm.c retain the include because they use it for the prototype of qemu_get_timedate().

---
 hw/arm/boot.c           | 1 -
 hw/arm/digic_boards.c   | 1 -
 hw/arm/highbank.c       | 1 -
 hw/arm/npcm7xx_boards.c | 1 -
 hw/arm/sbsa-ref.c       | 1 -
 hw/arm/stm32f405_soc.c  | 1 -
 hw/arm/vexpress.c       | 1 -
 hw/arm/virt.c           | 1 -
 8 files changed, 8 deletions(-)
hw/arm/boot.c +0 -1
index 74ad397b1ff..399f8e837ce 100644
@@ -8,7 +8,6 @@
  */
 
 #include "qemu/osdep.h"
-#include "qemu-common.h"
 #include "qemu/datadir.h"
 #include "qemu/error-report.h"
 #include "qapi/error.h"
hw/arm/digic_boards.c +0 -1
index b771a3d8b74..4093af09cb2 100644
@@ -25,7 +25,6 @@
 
 #include "qemu/osdep.h"
 #include "qapi/error.h"
-#include "qemu-common.h"
 #include "qemu/datadir.h"
 #include "hw/boards.h"
 #include "qemu/error-report.h"
hw/arm/highbank.c +0 -1
index c3cb315dbc6..4210894d814 100644
@@ -18,7 +18,6 @@
  */
 
 #include "qemu/osdep.h"
-#include "qemu-common.h"
 #include "qemu/datadir.h"
 #include "qapi/error.h"
 #include "hw/sysbus.h"
hw/arm/npcm7xx_boards.c +0 -1
index dec7d16ae51..aff8c870420 100644
@@ -24,7 +24,6 @@
 #include "hw/qdev-core.h"
 #include "hw/qdev-properties.h"
 #include "qapi/error.h"
-#include "qemu-common.h"
 #include "qemu/datadir.h"
 #include "qemu/units.h"
 #include "sysemu/blockdev.h"
hw/arm/sbsa-ref.c +0 -1
index 358714bd3e8..dd944553f78 100644
@@ -18,7 +18,6 @@
  */
 
 #include "qemu/osdep.h"
-#include "qemu-common.h"
 #include "qemu/datadir.h"
 #include "qapi/error.h"
 #include "qemu/error-report.h"
hw/arm/stm32f405_soc.c +0 -1
index 0019b7f4785..c07947d9f8b 100644
@@ -24,7 +24,6 @@
 
 #include "qemu/osdep.h"
 #include "qapi/error.h"
-#include "qemu-common.h"
 #include "exec/address-spaces.h"
 #include "sysemu/sysemu.h"
 #include "hw/arm/stm32f405_soc.h"
hw/arm/vexpress.c +0 -1
index 58481c07629..3e6d63c7f96 100644
@@ -23,7 +23,6 @@
 
 #include "qemu/osdep.h"
 #include "qapi/error.h"
-#include "qemu-common.h"
 #include "qemu/datadir.h"
 #include "cpu.h"
 #include "hw/sysbus.h"
hw/arm/virt.c +0 -1
index 30da05dfe04..3e2144e31af 100644
@@ -29,7 +29,6 @@
  */
 
 #include "qemu/osdep.h"
-#include "qemu-common.h"
 #include "qemu/datadir.h"
 #include "qemu/units.h"
 #include "qemu/option.h"
Signature · 1 line
2.25.1
#7 lore raw
In reply to Peter Maydell
Quoted 6 lines · Peter Maydell

qemu-common.h has a comment at the top:

* This file is supposed to be included only by .c files. No header file should
* depend on qemu-common.h, as this would easily lead to circular header
* dependencies.

As a side note, that comment was added back in 2012 when qemu-common.h was bigger, included other headers, and did some of the work we currently use osdep.h for. As it stands today qemu-common.h includes no other files so it isn't a source of possible circular dependencies -- it's just a grab-bag of miscellaneous prototypes that in an ideal world would be in more focused individual headers[*]. So there's an argument for deleting this comment...

[*] A cleanup that would be nice, and I'm about to send out a patchset that splits out the rtc related functions; but the grab-bag at the bottom of osdep.h is probably higher priority because that header gets pulled in by an order of magnitude more C files.

-- PMM

#8 lore raw
Quoted 8 lines · Peter Maydell
Quoted 6 lines · Peter Maydell

qemu-common.h has a comment at the top:

* This file is supposed to be included only by .c files. No header file should
* depend on qemu-common.h, as this would easily lead to circular header
* dependencies.

Quoted 7 lines

As a side note, that comment was added back in 2012 when qemu-common.h was bigger, included other headers, and did some of the work we currently use osdep.h for. As it stands today qemu-common.h includes no other files so it isn't a source of possible circular dependencies -- it's just a grab-bag of miscellaneous prototypes that in an ideal world would be in more focused individual headers[*]. So there's an argument for deleting this comment...

First, thank you for this cleanup series.

The comment is from commit 04509ad939a:

qemu-common.h: Comment about usage rules

Every time we make a tiny change on a header file, we often find circular header dependency problems. To avoid this nightmare, we need to stop including qemu-common.h from other headers, and we should gradually move the declarations from the catch-all qemu-common.h header to their specific headers.

This simply adds a comment documenting the rules about qemu-common.h, hoping that people will see it before including qemu-common.h from other header files, and before adding more declarations to qemu-common.h.

You're right: we've since moved all #include out of qemu-common.h, so the risk of circular header dependency is gone, and the comment's claim "this would easily lead to circular header dependencies" is no longer correct.

In my opinion, including qemu-common.h in a header is a bad idea regardless. Headers should include the headers they need to make them self-contained, and no more, because more risks slower compiles, in particular frequent recompiles of pretty much everything. Previously discussed at

    https://lists.gnu.org/archive/html/qemu-devel/2019-05/msg06291.html
    Message-ID: <[email protected]>

Nothing in qemu-common.h should be required to make another header self-contained.

This is likely the case for other headers as well, which don't carry a comment forbidding inclusion into headers. You could argue that qemu-common.h should not carry one, either. I can accept that. I'm not attached to the comment. I am interested in keeping unwanted #include in headers under control.

Quoted 4 lines

[*] A cleanup that would be nice, and I'm about to send out a patchset that splits out the rtc related functions; but the grab-bag at the bottom of osdep.h is probably higher priority because that header gets pulled in by an order of magnitude more C files.

By all "normal" ones, in fact.

Reviewed-by on the v1 series
#9 lore raw
In reply to Peter Maydell
Quoted 17 lines · Peter Maydell

qemu-common.h has a comment at the top:

* This file is supposed to be included only by .c files. No header file should
* depend on qemu-common.h, as this would easily lead to circular header
* dependencies.

We still have a few .h files which include it, though. The first 3 patches in this series fix that: in 3 out of 4 cases we didn't need the #include at all, and in the 4th case we can instead #include qemu-common.h from just one .c file.

Patch 4 is just removing the #include from 8 files in hw/arm which don't need it at all. (Probably there are other files like this, but I just did the Arm related ones.)

Tested by pushing to gitlab for the CI build.

Quoted 7 lines

-- PMM

Peter Maydell (4):
include/hw/i386: Don't include qemu-common.h in .h files target/hexagon/cpu.h: don't include qemu-common.h target/rx/cpu.h: Don't include qemu-common.h hw/arm: Don't include qemu-common.h unnecessarily

r~