From 36f4988ecc2fe494aa0bc17548593aa4e3e483d1 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Sat, 24 Feb 2024 11:38:47 +0000 Subject: [PATCH 1/2] Conditionalize uses of C99 restrict keyword This is part of C99, but not part of Standard C++, so it can't be used unconditionally if the sysprof headers might get included into C++ code. Resolves: https://gitlab.gnome.org/GNOME/sysprof/-/issues/114 Signed-off-by: Simon McVittie --- src/libsysprof-capture/sysprof-macros.h | 2 ++ src/libsysprof/sysprof-document-ctrset.h | 2 +- src/libsysprof/sysprof-time-span.h | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libsysprof-capture/sysprof-macros.h b/src/libsysprof-capture/sysprof-macros.h index 9292f924..1bc92afc 100644 --- a/src/libsysprof-capture/sysprof-macros.h +++ b/src/libsysprof-capture/sysprof-macros.h @@ -91,9 +91,11 @@ #ifdef __cplusplus #define SYSPROF_BEGIN_DECLS extern "C" { #define SYSPROF_END_DECLS } +#define SYSPROF_RESTRICT /* nothing */ #else #define SYSPROF_BEGIN_DECLS #define SYSPROF_END_DECLS +#define SYSPROF_RESTRICT restrict #endif #if defined (__GNUC__) diff --git a/src/libsysprof/sysprof-document-ctrset.h b/src/libsysprof/sysprof-document-ctrset.h index 3bc6cc32..3806245e 100644 --- a/src/libsysprof/sysprof-document-ctrset.h +++ b/src/libsysprof/sysprof-document-ctrset.h @@ -42,7 +42,7 @@ SYSPROF_AVAILABLE_IN_ALL void sysprof_document_ctrset_get_raw_value (SysprofDocumentCtrset *self, guint nth, guint *id, - guint8 value[restrict 8]); + guint8 value[SYSPROF_RESTRICT 8]); G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofDocumentCtrset, g_object_unref) diff --git a/src/libsysprof/sysprof-time-span.h b/src/libsysprof/sysprof-time-span.h index a85595a8..b550ac04 100644 --- a/src/libsysprof/sysprof-time-span.h +++ b/src/libsysprof/sysprof-time-span.h @@ -60,7 +60,7 @@ sysprof_time_span_relative_to (SysprofTimeSpan time_span, static inline void sysprof_time_span_normalize (SysprofTimeSpan time_span, SysprofTimeSpan allowed, - float values[restrict 2]) + float values[SYSPROF_RESTRICT 2]) { double duration = allowed.end_nsec - allowed.begin_nsec; From b4a592a020bd3d440c4241ed7e3b5eaecb488edf Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Sat, 24 Feb 2024 11:36:59 +0000 Subject: [PATCH 2/2] Add test-cases that include sysprof headers into C++ code Reproduces: https://gitlab.gnome.org/GNOME/sysprof/-/issues/114 Signed-off-by: Simon McVittie --- src/libsysprof-capture/tests/meson.build | 17 +++++++++---- .../tests/test-cplusplus.cpp | 24 +++++++++++++++++++ src/libsysprof/tests/meson.build | 17 +++++++++---- src/libsysprof/tests/test-cplusplus.cpp | 17 +++++++++++++ 4 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 src/libsysprof-capture/tests/test-cplusplus.cpp create mode 100644 src/libsysprof/tests/test-cplusplus.cpp diff --git a/src/libsysprof-capture/tests/meson.build b/src/libsysprof-capture/tests/meson.build index db0a3681..aca73d26 100644 --- a/src/libsysprof-capture/tests/meson.build +++ b/src/libsysprof-capture/tests/meson.build @@ -21,6 +21,7 @@ libsysprof_capture_testsuite = { 'rewrite-pid' : {'skip': true}, 'test-capture' : {}, 'test-capture-cursor' : {}, + 'test-cplusplus' : {'cpp': true}, 'test-mapped-ring-buffer' : {}, } @@ -30,10 +31,18 @@ libsysprof_capture_testsuite_deps = [ ] foreach test, params: libsysprof_capture_testsuite - test_exe = executable(test, '@0@.c'.format(test), - c_args: libsysprof_capture_testsuite_c_args, - dependencies: libsysprof_capture_testsuite_deps, - ) + if params.get('cpp', false) + test_exe = executable(test, '@0@.cpp'.format(test), + cpp_args: libsysprof_capture_testsuite_c_args, + dependencies: libsysprof_capture_testsuite_deps, + ) + else + test_exe = executable(test, '@0@.c'.format(test), + c_args: libsysprof_capture_testsuite_c_args, + dependencies: libsysprof_capture_testsuite_deps, + ) + endif + if not params.get('skip', false) test(test, test_exe, env: libsysprof_capture_test_env) endif diff --git a/src/libsysprof-capture/tests/test-cplusplus.cpp b/src/libsysprof-capture/tests/test-cplusplus.cpp new file mode 100644 index 00000000..c604a336 --- /dev/null +++ b/src/libsysprof-capture/tests/test-cplusplus.cpp @@ -0,0 +1,24 @@ +/* + * Copyright 2024 Simon McVittie + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include + +#undef _NDEBUG +#include + +#ifdef __GNUC__ +#define UNUSED __attribute__((__unused__)) +#else +#define UNUSED +#endif + +int +main (UNUSED int argc, + UNUSED char *argv[]) +{ + assert (sysprof_getpagesize () > 0); + return 0; +} + diff --git a/src/libsysprof/tests/meson.build b/src/libsysprof/tests/meson.build index 12eabb1e..c4ffc92f 100644 --- a/src/libsysprof/tests/meson.build +++ b/src/libsysprof/tests/meson.build @@ -16,6 +16,7 @@ libsysprof_testsuite = { 'test-allocs-by-func' : {'skip': true}, 'test-callgraph' : {'skip': true}, 'test-capture-model' : {'skip': true}, + 'test-cplusplus' : {'cpp': true}, 'test-elf-loader' : {'skip': true}, 'test-leak-detector' : {'skip': true}, 'test-list-counters' : {'skip': true}, @@ -41,10 +42,18 @@ libsysprof_testsuite_deps = [ ] foreach test, params: libsysprof_testsuite - test_exe = executable(test, '@0@.c'.format(test), - c_args: libsysprof_testsuite_c_args, - dependencies: libsysprof_testsuite_deps, - ) + if params.get('cpp', false) + test_exe = executable(test, '@0@.cpp'.format(test), + cpp_args: libsysprof_testsuite_c_args, + dependencies: libsysprof_testsuite_deps, + ) + else + test_exe = executable(test, '@0@.c'.format(test), + c_args: libsysprof_testsuite_c_args, + dependencies: libsysprof_testsuite_deps, + ) + endif + if not params.get('skip', false) test(test, test_exe, env: libsysprof_test_env) endif diff --git a/src/libsysprof/tests/test-cplusplus.cpp b/src/libsysprof/tests/test-cplusplus.cpp new file mode 100644 index 00000000..d23e57ec --- /dev/null +++ b/src/libsysprof/tests/test-cplusplus.cpp @@ -0,0 +1,17 @@ +/* + * Copyright 2024 Simon McVittie + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include + +#undef _NDEBUG +#include + +int +main (G_GNUC_UNUSED int argc, + G_GNUC_UNUSED char *argv[]) +{ + assert (sysprof_callgraph_flags_get_type () != 0); + return 0; +}