Skip to contentSkip to conversation

osdep: allow including qemu/osdep.h outside extern "C"

PATCH In review Paolo Bonzini 5 participants 7 messages Apr 13, 2021 – Apr 15, 2021

lore
Share & tools
lore
#0 lore raw

cover osdep: allow including qemu/osdep.h outside extern "C"

qemu/osdep.h is quite special in that, despite being part of QEMU sources, it is included by C++ source files as well.

disas/nanomips.cpp is doing so within an 'extern "C"' block, which breaks with latest glib due to the inclusion of templates in glib.h.

These patches implement Daniel Berrangé's idea of pushing the 'extern "C"' block within glib.h and including system headers (including glib.h, and in fact QEMU's own glib-compat.h too) *outside* the block.

(CI has not finished running yet, but it seems encouraging).

Paolo

Paolo Bonzini (2):
osdep: include glib-compat.h before other QEMU headers
osdep: protect qemu/osdep.h with extern "C"

 disas/nanomips.cpp      |  2 +-
 include/qemu/compiler.h |  6 ++++++
 include/qemu/osdep.h    | 13 +++++++++++--
 3 files changed, 18 insertions(+), 3 deletions(-)
Signature · 1 line
2.30.1
#1 lore raw

PATCH 1/2 osdep: include glib-compat.h before other QEMU headers

glib-compat.h is sort of like a system header, and it needs to include system headers (glib.h) that may dislike being included under 'extern "C"'. Move it right after all system headers and before all other QEMU headers.

---
 include/qemu/osdep.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/qemu/osdep.h +2 -1
index ba15be9c56..b67b0a1e8c 100644
@@ -111,6 +111,8 @@ extern int daemon(int, int);
 #define WEXITSTATUS(x) (x)
 #endif
 
+#include "glib-compat.h"
+
 #ifdef _WIN32
 #include "sysemu/os-win32.h"
 #endif
@@ -123,7 +125,6 @@ extern int daemon(int, int);
 #include <AvailabilityMacros.h>
 #endif
 
-#include "glib-compat.h"
 #include "qemu/typedefs.h"
 
 /*
Signature · 1 line
2.30.1
#2 lore raw

PATCH 2/2 osdep: protect qemu/osdep.h with extern "C"

In reply to Paolo Bonzini

System headers may include templates if compiled with a C++ compiler, which cause the compiler to complain if qemu/osdep.h is included within a C++ source file's 'extern "C"' block. Add
an 'extern "C"' block directly to qemu/osdep.h, so that system headers can be kept out of it.

There is a stray declaration early in qemu/osdep.h, which needs to be special cased. Add a definition in qemu/compiler.h to make it look nice.

config-host.h, CONFIG_TARGET, exec/poison.h and qemu/compiler.h are included outside the 'extern "C"' block; that is not an issue because they consist entirely of preprocessor directives.

---
 disas/nanomips.cpp      |  2 +-
 include/qemu/compiler.h |  6 ++++++
 include/qemu/osdep.h    | 10 +++++++++-
 3 files changed, 16 insertions(+), 2 deletions(-)
disas/nanomips.cpp +1 -1
index 2b09655271..8ddef897f0 100644
@@ -27,8 +27,8 @@
  *      Reference Manual", Revision 01.01, April 27, 2018
  */
 
-extern "C" {
 #include "qemu/osdep.h"
+extern "C" {
 #include "disas/dis-asm.h"
 }
 
include/qemu/compiler.h +6 -0
index cf28bb2bcd..091c45248b 100644
@@ -11,6 +11,12 @@
 #define QEMU_STATIC_ANALYSIS 1
 #endif
 
+#ifdef __cplusplus
+#define QEMU_EXTERN_C extern "C"
+#else
+#define QEMU_EXTERN_C extern
+#endif
+
 #define QEMU_NORETURN __attribute__ ((__noreturn__))
 
 #define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
include/qemu/osdep.h +9 -1
index b67b0a1e8c..3f8785a471 100644
@@ -57,7 +57,7 @@
 #define daemon qemu_fake_daemon_function
 #include <stdlib.h>
 #undef daemon
-extern int daemon(int, int);
+QEMU_EXTERN_C int daemon(int, int);
 #endif
 
 #ifdef _WIN32
@@ -113,6 +113,10 @@ extern int daemon(int, int);
 
 #include "glib-compat.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #ifdef _WIN32
 #include "sysemu/os-win32.h"
 #endif
@@ -723,4 +727,8 @@ static inline int platform_does_not_support_system(const char *command)
 }
 #endif /* !HAVE_SYSTEM_FUNCTION */
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif
Signature · 1 line
2.30.1
#3 lore raw

Re: [PATCH 2/2] osdep: protect qemu/osdep.h with extern "C"

Quoted 67 lines · Paolo Bonzini

System headers may include templates if compiled with a C++ compiler, which cause the compiler to complain if qemu/osdep.h is included within a C++ source file's 'extern "C"' block. Add
an 'extern "C"' block directly to qemu/osdep.h, so that system headers can be kept out of it.

There is a stray declaration early in qemu/osdep.h, which needs to be special cased. Add a definition in qemu/compiler.h to make it look nice.

config-host.h, CONFIG_TARGET, exec/poison.h and qemu/compiler.h are included outside the 'extern "C"' block; that is not an issue because they consist entirely of preprocessor directives.

---
 disas/nanomips.cpp      |  2 +-
 include/qemu/compiler.h |  6 ++++++
 include/qemu/osdep.h    | 10 +++++++++-
 3 files changed, 16 insertions(+), 2 deletions(-)
disas/nanomips.cpp +1 -1
index 2b09655271..8ddef897f0 100644
@@ -27,8 +27,8 @@
  *      Reference Manual", Revision 01.01, April 27, 2018
  */
 
-extern "C" {
 #include "qemu/osdep.h"
+extern "C" {
 #include "disas/dis-asm.h"
 }
 
include/qemu/compiler.h +6 -0
index cf28bb2bcd..091c45248b 100644
@@ -11,6 +11,12 @@
 #define QEMU_STATIC_ANALYSIS 1
 #endif
 
+#ifdef __cplusplus
+#define QEMU_EXTERN_C extern "C"
+#else
+#define QEMU_EXTERN_C extern
+#endif
+
 #define QEMU_NORETURN __attribute__ ((__noreturn__))
 
 #define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
include/qemu/osdep.h +1 -1
index b67b0a1e8c..3f8785a471 100644
@@ -57,7 +57,7 @@
 #define daemon qemu_fake_daemon_function
 #include <stdlib.h>
 #undef daemon
-extern int daemon(int, int);
+QEMU_EXTERN_C int daemon(int, int);
 #endif
 
 #ifdef _WIN32

include/qemu/osdep.h extern int daemon(int, int); · line 122

Quoted 10 lines
include/qemu/osdep.h +4 -0
@@ -113,6 +113,10 @@ extern int daemon(int, int);
 
 #include "glib-compat.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #ifdef _WIN32
 #include "sysemu/os-win32.h"
 #endif

There are some system header includes in osdep.h below this point (sys/shm.h and sys/uio.h) -- don't they need to be moved up to go with the other system includes first ?

thanks
-- PMM

#4 lore raw
In reply to Paolo Bonzini
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [PATCH 0/2] osdep: allow including qemu/osdep.h outside extern "C"

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 From https://github.com/patchew-project/qemu
- [tag update] patchew/[email protected] -> patchew/[email protected]
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
992fa52 osdep: protect qemu/osdep.h with extern "C" bc2310f osdep: include glib-compat.h before other QEMU headers

=== OUTPUT BEGIN ===
1/2 Checking commit bc2310f731a5 (osdep: include glib-compat.h before other QEMU headers) 2/2 Checking commit 992fa52da413 (osdep: protect qemu/osdep.h with extern "C")
WARNING: architecture specific defines should be avoided
#51: FILE: include/qemu/compiler.h:14:
+#ifdef __cplusplus

ERROR: storage class should be at the beginning of the declaration #52: FILE: include/qemu/compiler.h:15:
+#define QEMU_EXTERN_C extern "C"

ERROR: storage class should be at the beginning of the declaration #54: FILE: include/qemu/compiler.h:17:
+#define QEMU_EXTERN_C extern

WARNING: architecture specific defines should be avoided #77: FILE: include/qemu/osdep.h:116:
+#ifdef __cplusplus

WARNING: architecture specific defines should be avoided #88: FILE: include/qemu/osdep.h:730:
+#ifdef __cplusplus

total: 2 errors, 3 warnings, 47 lines checked

Patch 2/2 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

=== OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

#5 lore raw
In reply to Paolo Bonzini

Cc'ing MediaTek reviewers.

Quoted 23 lines · Paolo Bonzini

qemu/osdep.h is quite special in that, despite being part of QEMU sources, it is included by C++ source files as well.

disas/nanomips.cpp is doing so within an 'extern "C"' block, which breaks with latest glib due to the inclusion of templates in glib.h.

These patches implement Daniel Berrangé's idea of pushing the 'extern "C"' block within glib.h and including system headers (including glib.h, and in fact QEMU's own glib-compat.h too) *outside* the block.

(CI has not finished running yet, but it seems encouraging).

Paolo

Paolo Bonzini (2):
osdep: include glib-compat.h before other QEMU headers
osdep: protect qemu/osdep.h with extern "C"

 disas/nanomips.cpp      |  2 +-
 include/qemu/compiler.h |  6 ++++++
 include/qemu/osdep.h    | 13 +++++++++++--
 3 files changed, 18 insertions(+), 3 deletions(-)
#6 lore raw

Hi Paolo,

Can you specify how to reproduce the issue ? We need more details about environment.

In my case, everything seems to work fine for the newest version of glib (2.68).

Thank you,
Aleksandar

Quoted 18 lines

qemu/osdep.h is quite special in that, despite being part of QEMU sources, it is included by C++ source files as well.

disas/nanomips.cpp is doing so within an 'extern "C"' block, which breaks with latest glib due to the inclusion of templates in glib.h.

These patches implement Daniel Berrangé's idea of pushing the 'extern "C"' block within glib.h and including system headers (including glib.h, and in fact QEMU's own glib-compat.h too) *outside* the block.

(CI has not finished running yet, but it seems encouraging).

Paolo

Paolo Bonzini (2):
osdep: include glib-compat.h before other QEMU headers
osdep: protect qemu/osdep.h with extern "C"

Quoted 7 lines
 disas/nanomips.cpp      |  2 +-
 include/qemu/compiler.h |  6 ++++++
 include/qemu/osdep.h    | 13 +++++++++++--
 3 files changed, 18 insertions(+), 3 deletions(-)

--
2.30.1

________________________________
From: Philippe Mathieu-Daudé <[email protected]> on behalf of Philippe Mathieu-Daudé <[email protected]>
Sent: Tuesday, April 13, 2021 5:58 PM
To: Paolo Bonzini <[email protected]>; [email protected] <[email protected]>
Cc: [email protected] <[email protected]>; [email protected] <[email protected]>; Aleksandar Rikalo <[email protected]>; [email protected] <[email protected]>; Petar Jovanovic <[email protected]>; Filip Vidojevic <[email protected]>
Subject: Re: [PATCH 0/2] osdep: allow including qemu/osdep.h outside extern "C"

Cc'ing MediaTek reviewers.

Quoted 23 lines · Paolo Bonzini

qemu/osdep.h is quite special in that, despite being part of QEMU sources, it is included by C++ source files as well.

disas/nanomips.cpp is doing so within an 'extern "C"' block, which breaks with latest glib due to the inclusion of templates in glib.h.

These patches implement Daniel Berrangé's idea of pushing the 'extern "C"' block within glib.h and including system headers (including glib.h, and in fact QEMU's own glib-compat.h too) *outside* the block.

(CI has not finished running yet, but it seems encouraging).

Paolo

Paolo Bonzini (2):
osdep: include glib-compat.h before other QEMU headers
osdep: protect qemu/osdep.h with extern "C"

 disas/nanomips.cpp      |  2 +-
 include/qemu/compiler.h |  6 ++++++
 include/qemu/osdep.h    | 13 +++++++++++--
 3 files changed, 18 insertions(+), 3 deletions(-)