Skip to content

Commit

Permalink
Audit the installed headers for C++ compatibility
Browse files Browse the repository at this point in the history
C++ name mangling applies to symbols: variable and function names. The
rule of thumb is to enclose public symbols declarations in blocks:

    #ifdef __cplusplus
    extern "C" {
    #endif

    /* symbols go here */

    #ifdef __cplusplus
    }
    #endif

Symbols protected by CAML_INTERNALS blocks need not to be covered.

Headers that contain definitions incompatible with C++, such as
_Atomic, also need protection.
  • Loading branch information
MisterDA committed Nov 4, 2024
1 parent dba6165 commit a1a4767
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 63 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ Working version
(Florian Angeletti, report by Nailen Matschke, review by Gabriel Scherer, and
Leo White)

- #13541, #?????: Fix headers for C++ inclusion.
(Antonin Décimo, review by ?, report by Kate Deplaix)

OCaml 5.3.0
___________

Expand Down
8 changes: 8 additions & 0 deletions otherlibs/runtime_events/caml/runtime_events_consumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

#include "caml/runtime_events.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Create a cursor to read events from an runtime_events. Cursors can be created
for runtime_events in and out of process. An runtime_events may have
multiple cursors reading from it at any point in time and a program may have
Expand Down Expand Up @@ -95,4 +99,8 @@ CAMLextern runtime_events_error caml_runtime_events_read_poll(
void *callback_data,
uintnat max_events, uintnat *events_consumed);

#ifdef __cplusplus
}
#endif

#endif /* CAML_RUNTIME_EVENTS_CONSUMER_H */
8 changes: 8 additions & 0 deletions runtime/caml/address_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
#include "misc.h"
#include "mlvalues.h"

#ifdef __cplusplus
extern "C" {
#endif

CAMLextern uintnat caml_minor_heaps_start;
CAMLextern uintnat caml_minor_heaps_end;

Expand All @@ -59,4 +63,8 @@ CAMLextern uintnat caml_minor_heaps_end;
#define Is_in_heap_or_young(a) 1
#define Is_in_value_area(a) 1

#ifdef __cplusplus
}
#endif

#endif /* CAML_ADDRESS_CLASS_H */
7 changes: 6 additions & 1 deletion runtime/caml/addrmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

#include "mlvalues.h"

#ifdef __cplusplus
extern "C" {
#endif

/* An addrmap is a value -> value hashmap, where
the values are blocks */

Expand Down Expand Up @@ -95,7 +99,8 @@ Caml_inline addrmap_iterator caml_addrmap_iterator(struct addrmap* t)
return caml_addrmap_next(t, (uintnat)(-1));
}


#ifdef __cplusplus
}
#endif

#endif /* CAML_ADDRMAP_H */
8 changes: 8 additions & 0 deletions runtime/caml/backtrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

#include "mlvalues.h"

#ifdef __cplusplus
extern "C" {
#endif

/* [caml_record_backtraces] controls backtrace recording.
* This function can be called at runtime by user-code, or during
* initialization if backtraces were requested.
Expand All @@ -27,6 +31,10 @@
*/
CAMLextern void caml_record_backtraces(int);

#ifdef __cplusplus
}
#endif

#ifdef CAML_INTERNALS

#include "exec.h"
Expand Down
8 changes: 4 additions & 4 deletions runtime/caml/custom.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ CAMLextern mlsize_t caml_custom_get_max_major (void);
/* Global variable moved to Caml_state in 4.10 */
#define caml_compare_unordered (Caml_state_field(compare_unordered))

#ifdef __cplusplus
}
#endif

#ifdef CAML_INTERNALS
extern struct custom_operations *
caml_find_custom_operations(const char * ident);
Expand All @@ -89,8 +93,4 @@ extern const struct custom_operations caml_int64_ops;
extern const struct custom_operations caml_ba_ops;
#endif /* CAML_INTERNALS */

#ifdef __cplusplus
}
#endif

#endif /* CAML_CUSTOM_H */
8 changes: 0 additions & 8 deletions runtime/caml/domain.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
#ifndef CAML_DOMAIN_H
#define CAML_DOMAIN_H

#ifdef __cplusplus
extern "C" {
#endif

#ifdef CAML_INTERNALS

#include <stdbool.h>
Expand Down Expand Up @@ -250,8 +246,4 @@ bool caml_free_domains(void);

#endif /* CAML_INTERNALS */

#ifdef __cplusplus
}
#endif

#endif /* CAML_DOMAIN_H */
8 changes: 8 additions & 0 deletions runtime/caml/domain_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

#include "misc.h"

#ifdef __cplusplus
extern "C" {
#endif

#define NUM_EXTRA_PARAMS 64
typedef value extra_params_area[NUM_EXTRA_PARAMS];

Expand Down Expand Up @@ -67,4 +71,8 @@ CAMLnoret CAMLextern void caml_bad_caml_state(void);

#define Caml_state_field(field) (Caml_state->field)

#ifdef __cplusplus
}
#endif

#endif /* CAML_STATE_H */
8 changes: 0 additions & 8 deletions runtime/caml/hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
#include "misc.h"
#include "memory.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifdef CAML_INTERNALS

#ifdef NATIVE_CODE
Expand All @@ -36,8 +32,4 @@ CAMLextern void (*caml_natdynlink_hook)(void* handle, const char* unit);

#endif /* CAML_INTERNALS */

#ifdef __cplusplus
}
#endif

#endif /* CAML_HOOKS_H */
8 changes: 3 additions & 5 deletions runtime/caml/intext.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ void caml_free_extern_state (void);
void caml_output_val (struct channel * chan, value v, value flags);
/* Output [v] with flags [flags] on the channel [chan]. */

value caml_input_val (struct channel * chan);
/* Read a structured value from the channel [chan]. */

void caml_free_intern_state (void);

/* Compression hooks */
Expand Down Expand Up @@ -160,11 +163,6 @@ CAMLextern intnat caml_output_value_to_block(value v, value flags,
in bytes. Return the number of bytes actually written in buffer.
Raise [Failure] if buffer is too short. */

#ifdef CAML_INTERNALS
value caml_input_val (struct channel * chan);
/* Read a structured value from the channel [chan]. */
#endif /* CAML_INTERNALS */

CAMLextern value caml_input_val_from_string (value str, intnat ofs);
/* Read a structured value from the OCaml string [str], starting
at offset [ofs]. */
Expand Down
8 changes: 8 additions & 0 deletions runtime/caml/minor_gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
asize_t reserve; \
}

#ifdef __cplusplus
extern "C" {
#endif

/* Count of the total number of minor collections performed by the program */
CAMLextern atomic_uintnat caml_minor_collections_count;

Expand Down Expand Up @@ -69,6 +73,10 @@ struct caml_minor_tables {

CAMLextern void caml_minor_collection (void);

#ifdef __cplusplus
}
#endif

#ifdef CAML_INTERNALS
extern void caml_set_minor_heap_size (asize_t); /* size in bytes */
extern void caml_empty_minor_heap_no_major_slice_from_stw
Expand Down
28 changes: 17 additions & 11 deletions runtime/caml/osdeps.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@
#ifndef CAML_OSDEPS_H
#define CAML_OSDEPS_H

#ifdef _WIN32
#include <time.h>

extern unsigned short caml_win32_major;
extern unsigned short caml_win32_minor;
extern unsigned short caml_win32_build;
extern unsigned short caml_win32_revision;
#endif

#ifdef CAML_INTERNALS

#include "misc.h"
Expand Down Expand Up @@ -139,12 +130,14 @@ CAMLextern int caml_win32_isatty(int fd);

CAMLextern void caml_expand_command_line (int *, wchar_t ***);

CAMLextern clock_t caml_win32_clock(void);

CAMLextern value caml_win32_xdg_defaults(void);

CAMLextern value caml_win32_get_temp_path(void);

#include <time.h>

CAMLextern clock_t caml_win32_clock(void);

#endif /* _WIN32 */

/* Returns the current value of a counter that increments once per nanosecond.
Expand All @@ -162,6 +155,15 @@ extern void caml_init_os_params(void);

#ifdef _WIN32

#ifdef __cplusplus
extern "C" {
#endif

extern unsigned short caml_win32_major;
extern unsigned short caml_win32_minor;
extern unsigned short caml_win32_build;
extern unsigned short caml_win32_revision;

/* [caml_stat_strdup_to_utf16(s)] returns a null-terminated copy of [s],
re-encoded in UTF-16. The encoding of [s] is assumed to be UTF-8 if
[caml_windows_unicode_runtime_enabled] is non-zero **and** [s] is valid
Expand Down Expand Up @@ -200,6 +202,10 @@ CAMLextern char* caml_stat_strdup_of_utf16(const wchar_t *s);
*/
CAMLextern value caml_copy_string_of_utf16(const wchar_t *s);

#ifdef __cplusplus
}
#endif

#endif /* _WIN32 */

#endif /* CAML_OSDEPS_H */
8 changes: 4 additions & 4 deletions runtime/caml/printexc.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
extern "C" {
#endif


CAMLextern char * caml_format_exception (value);
#ifdef CAML_INTERNALS
CAMLnoret void caml_fatal_uncaught_exception (value);
#endif /* CAML_INTERNALS */

#ifdef __cplusplus
}
#endif

#ifdef CAML_INTERNALS
CAMLnoret void caml_fatal_uncaught_exception (value);
#endif /* CAML_INTERNALS */

#endif /* CAML_PRINTEXC_H */
8 changes: 8 additions & 0 deletions runtime/caml/runtime_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ typedef enum {
E_CURSOR_POLL_BUSY = -8,
} runtime_events_error;

#ifdef __cplusplus
extern "C" {
#endif

/* Starts runtime_events. Needs to be called before
[caml_runtime_events_create_cursor]. Needs the runtime lock held to call and
will trigger a stop-the-world pause. */
Expand All @@ -197,6 +201,10 @@ CAMLextern void caml_runtime_events_resume(void);
[0] otherwise. */
CAMLextern int caml_runtime_events_are_active(void);

#ifdef __cplusplus
}
#endif

#ifdef CAML_INTERNALS

struct runtime_events_buffer_header {
Expand Down
18 changes: 8 additions & 10 deletions runtime/caml/signals.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ CAMLextern caml_result caml_process_pending_actions_res (void);
kept around for compatibility. */
CAMLextern value caml_process_pending_actions_exn (void);

CAMLextern int caml_check_pending_actions (void);
/* Returns 1 if there are pending actions, 0 otherwise. */

#ifdef __cplusplus
}
#endif

#ifdef CAML_INTERNALS

value caml_process_pending_actions_with_root (value extra_root); // raises
Expand All @@ -72,12 +79,6 @@ value caml_process_pending_actions_with_root (value extra_root); // raises
*/

caml_result caml_process_pending_actions_with_root_res (value extra_root);
#endif

CAMLextern int caml_check_pending_actions (void);
/* Returns 1 if there are pending actions, 0 otherwise. */

#ifdef CAML_INTERNALS

#ifndef NSIG
#define NSIG 65
Expand Down Expand Up @@ -112,10 +113,7 @@ CAMLextern void caml_free_signal_stack(void *);
/* These hooks are not modified after other threads are spawned. */
CAMLextern void (*caml_enter_blocking_section_hook)(void);
CAMLextern void (*caml_leave_blocking_section_hook)(void);
#endif /* CAML_INTERNALS */

#ifdef __cplusplus
}
#endif
#endif /* CAML_INTERNALS */

#endif /* CAML_SIGNALS_H */
8 changes: 0 additions & 8 deletions runtime/caml/sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@

#include "misc.h"

#ifdef __cplusplus
extern "C" {
#endif

CAMLextern char * caml_strerror(int errnum, char * buf, size_t buflen);

#define NO_ARG Val_int(0)
Expand All @@ -37,10 +33,6 @@ CAMLextern void caml_sys_init (const char_os * exe_name, char_os ** argv);

CAMLnoret CAMLextern void caml_do_exit (int);

#ifdef __cplusplus
}
#endif

#endif /* CAML_INTERNALS */

#endif /* CAML_SYS_H */
7 changes: 7 additions & 0 deletions runtime/caml/tsan.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#ifndef CAML_TSAN_H
#define CAML_TSAN_H

#ifdef __cplusplus
extern "C" {
#endif

/* Macro used to deactivate thread sanitizer on some functions. */
#define CAMLno_tsan
/* `__has_feature` is present in Clang and recent GCCs (14 and later). Older
Expand Down Expand Up @@ -72,6 +76,9 @@ extern void AnnotateHappensAfter(const char *f, int l, void *addr);
# define CAMLno_tsan_for_perf CAMLno_tsan
#endif

#ifdef __cplusplus
}
#endif

#ifdef CAML_INTERNALS

Expand Down
Loading

0 comments on commit a1a4767

Please sign in to comment.