Skip to content

Commit 3a8f493

Browse files
Fix module-import-in-extern-c compiler error
This fixes a compiler error when building C++ projects that use the 'modules' C++ language feature consume Mbed TLS. The specific error is module-import-in-extern-c, which occurs when `#include`s occur within `extern C` blocks. ``` $ clang-17 -std=c++20 -fmodules -Itf-psa-crypto/include -Itf-psa-crypto/drivers/builtin/include -o sample sample.cpp In file included from sample.cpp:8: In file included from tf-psa-crypto/drivers/builtin/include/mbedtls/private/chachapoly.h:44: tf-psa-crypto/drivers/builtin/include/mbedtls/private/chacha20.h:26:1: error: import of C++ module '_Builtin_stdint' appears within extern "C" language linkage specification [-Wmodule-import-in-extern-c] 26 | #include <stdint.h> | ^ tf-psa-crypto/drivers/builtin/include/mbedtls/private/chachapoly.h:35:1: note: extern "C" language linkage specification begins here 35 | extern "C" { | ^ 1 error generated. ``` This seems to primarily be an issue with clang. See https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fmodules
1 parent 1a4690b commit 3a8f493

7 files changed

Lines changed: 84 additions & 31 deletions

File tree

drivers/builtin/include/mbedtls/private/chachapoly.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131
/** Authenticated decryption failed: data was not authentic. */
3232
#define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED PSA_ERROR_INVALID_SIGNATURE
3333

34-
#ifdef __cplusplus
35-
extern "C" {
36-
#endif
37-
3834
typedef enum {
3935
MBEDTLS_CHACHAPOLY_ENCRYPT, /**< The mode value for performing encryption. */
4036
MBEDTLS_CHACHAPOLY_DECRYPT /**< The mode value for performing decryption. */
@@ -43,6 +39,10 @@ mbedtls_chachapoly_mode_t;
4339

4440
#include "mbedtls/private/chacha20.h"
4541

42+
#ifdef __cplusplus
43+
extern "C" {
44+
#endif
45+
4646
typedef struct mbedtls_chachapoly_context {
4747
mbedtls_chacha20_context MBEDTLS_PRIVATE(chacha20_ctx); /**< The ChaCha20 context. */
4848
mbedtls_poly1305_context MBEDTLS_PRIVATE(poly1305_ctx); /**< The Poly1305 context. */

include/mbedtls/platform.h

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@
3535

3636
#include <psa/crypto_driver_random.h>
3737

38-
#ifdef __cplusplus
39-
extern "C" {
40-
#endif
41-
4238
/**
4339
* \name SECTION: Module settings
4440
*
@@ -130,6 +126,10 @@ extern "C" {
130126
#define MBEDTLS_PLATFORM_STD_FREE
131127
#endif
132128

129+
#ifdef __cplusplus
130+
extern "C" {
131+
#endif
132+
133133
/** \} name SECTION: Module settings */
134134

135135
/*
@@ -145,8 +145,15 @@ extern "C" {
145145
#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
146146
#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
147147
#else
148+
#ifdef __cplusplus
149+
}
150+
#endif
148151
/* For size_t */
149152
#include <stddef.h>
153+
#ifdef __cplusplus
154+
extern "C" {
155+
#endif
156+
150157
extern void *mbedtls_calloc(size_t n, size_t size);
151158
extern void mbedtls_free(void *ptr);
152159

@@ -173,8 +180,14 @@ int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
173180
* The function pointers for fprintf
174181
*/
175182
#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
183+
#ifdef __cplusplus
184+
}
185+
#endif
176186
/* We need FILE * */
177187
#include <stdio.h>
188+
#ifdef __cplusplus
189+
extern "C" {
190+
#endif
178191
extern int (*mbedtls_fprintf)(FILE *stream, const char *format, ...);
179192

180193
/**
@@ -268,13 +281,25 @@ int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
268281
* the destination buffer is too short.
269282
*/
270283
#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
284+
#ifdef __cplusplus
285+
}
286+
#endif
271287
#include <stdarg.h>
288+
#ifdef __cplusplus
289+
extern "C" {
290+
#endif
272291
/* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
273292
int mbedtls_platform_win32_vsnprintf(char *s, size_t n, const char *fmt, va_list arg);
274293
#endif
275294

276295
#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
296+
#ifdef __cplusplus
297+
}
298+
#endif
277299
#include <stdarg.h>
300+
#ifdef __cplusplus
301+
extern "C" {
302+
#endif
278303
extern int (*mbedtls_vsnprintf)(char *s, size_t n, const char *format, va_list arg);
279304

280305
/**
@@ -299,7 +324,13 @@ int mbedtls_platform_set_vsnprintf(int (*vsnprintf_func)(char *s, size_t n,
299324
* The function pointers for setbuf
300325
*/
301326
#if defined(MBEDTLS_PLATFORM_SETBUF_ALT)
327+
#ifdef __cplusplus
328+
}
329+
#endif
302330
#include <stdio.h>
331+
#ifdef __cplusplus
332+
extern "C" {
333+
#endif
303334
/**
304335
* \brief Function pointer to call for `setbuf()` functionality
305336
* (changing the internal buffering on stdio calls).
@@ -445,7 +476,13 @@ typedef struct mbedtls_platform_context {
445476
mbedtls_platform_context;
446477

447478
#else
479+
#ifdef __cplusplus
480+
}
481+
#endif
448482
#include "platform_alt.h"
483+
#ifdef __cplusplus
484+
extern "C" {
485+
#endif
449486
#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
450487

451488
/**

include/mbedtls/platform_time.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212

1313
#include "tf-psa-crypto/build_info.h"
1414

15-
#ifdef __cplusplus
16-
extern "C" {
17-
#endif
18-
1915
/*
2016
* The time_t datatype
2117
*/
@@ -35,6 +31,10 @@ typedef MBEDTLS_PLATFORM_MS_TIME_TYPE_MACRO mbedtls_ms_time_t;
3531
typedef int64_t mbedtls_ms_time_t;
3632
#endif /* MBEDTLS_PLATFORM_MS_TIME_TYPE_MACRO */
3733

34+
#ifdef __cplusplus
35+
extern "C" {
36+
#endif
37+
3838
/**
3939
* \brief Get time in milliseconds.
4040
*

include/mbedtls/platform_util.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
#include <time.h>
2020
#endif /* MBEDTLS_HAVE_TIME_DATE */
2121

22-
#ifdef __cplusplus
23-
extern "C" {
24-
#endif
25-
2622
/* Internal helper macros for deprecating API constants. */
2723
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2824
#if defined(MBEDTLS_DEPRECATED_WARNING)
@@ -58,6 +54,10 @@ MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t;
5854
#endif
5955
#endif
6056

57+
#ifdef __cplusplus
58+
extern "C" {
59+
#endif
60+
6161
/** Critical-failure function
6262
*
6363
* This macro appearing at the beginning of the declaration of a function

include/mbedtls/threading.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@
1616

1717
#include <stdlib.h>
1818

19-
#ifdef __cplusplus
20-
extern "C" {
19+
#if defined(MBEDTLS_THREADING_PTHREAD)
20+
#include <pthread.h>
21+
#endif
22+
23+
#if defined(MBEDTLS_THREADING_ALT)
24+
/* You should define the mbedtls_threading_mutex_t type in your header */
25+
#include "threading_alt.h"
2126
#endif
2227

2328
/** Detected error in mutex or condition variable usage.
@@ -32,19 +37,18 @@ extern "C" {
3237
/** A historical alias for #MBEDTLS_ERR_THREADING_USAGE_ERROR. */
3338
#define MBEDTLS_ERR_THREADING_MUTEX_ERROR MBEDTLS_ERR_THREADING_USAGE_ERROR
3439

40+
#ifdef __cplusplus
41+
extern "C" {
42+
#endif
43+
3544
#if defined(MBEDTLS_THREADING_C)
3645

3746
#if defined(MBEDTLS_THREADING_PTHREAD)
38-
#include <pthread.h>
3947
typedef pthread_mutex_t mbedtls_platform_mutex_t;
4048
typedef pthread_cond_t mbedtls_platform_condition_variable_t;
4149
#endif
4250

4351
#if defined(MBEDTLS_THREADING_ALT)
44-
/* You should define the types mbedtls_platform_mutex_t and
45-
* mbedtls_platform_condition_variable_t in your header. */
46-
#include "threading_alt.h"
47-
4852
/**
4953
* \brief Set your alternate threading implementation function
5054
* pointers and initialize global mutexes. If used, this

include/psa/crypto.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131
/**@}*/
3232
#endif /* __DOXYGEN_ONLY__ */
3333

34-
#ifdef __cplusplus
35-
extern "C" {
36-
#endif
37-
3834
/* The file "crypto_types.h" declares types that encode errors,
3935
* algorithms, key types, policies, etc. */
4036
#include "crypto_types.h"
@@ -71,6 +67,10 @@ extern "C" {
7167
#include "crypto_struct.h"
7268
#endif
7369

70+
#ifdef __cplusplus
71+
extern "C" {
72+
#endif
73+
7474
/** \defgroup initialization Library initialization
7575
* @{
7676
*/

include/psa/crypto_struct.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@
5050
#define PSA_CRYPTO_STRUCT_H
5151
#include "mbedtls/private_access.h"
5252

53-
#ifdef __cplusplus
54-
extern "C" {
55-
#endif
56-
5753
#include "tf-psa-crypto/build_info.h"
5854

5955
/* Include the context definition for the compiled-in drivers for the primitive
6056
* algorithms. */
6157
#include "psa/crypto_driver_contexts_primitives.h"
6258

59+
#ifdef __cplusplus
60+
extern "C" {
61+
#endif
62+
6363
struct psa_hash_operation_s {
6464
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
6565
mbedtls_psa_client_handle_t handle;
@@ -117,9 +117,15 @@ static inline struct psa_cipher_operation_s psa_cipher_operation_init(void)
117117
return v;
118118
}
119119

120+
#ifdef __cplusplus
121+
}
122+
#endif
120123
/* Include the context definition for the compiled-in drivers for the composite
121124
* algorithms. */
122125
#include "psa/crypto_driver_contexts_composites.h"
126+
#ifdef __cplusplus
127+
extern "C" {
128+
#endif
123129

124130
struct psa_mac_operation_s {
125131
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
@@ -188,9 +194,15 @@ static inline struct psa_aead_operation_s psa_aead_operation_init(void)
188194
return v;
189195
}
190196

197+
#ifdef __cplusplus
198+
}
199+
#endif
191200
/* Include the context definition for the compiled-in drivers for the key
192201
* derivation algorithms. */
193202
#include "psa/crypto_driver_contexts_key_derivation.h"
203+
#ifdef __cplusplus
204+
extern "C" {
205+
#endif
194206

195207
struct psa_key_derivation_s {
196208
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)

0 commit comments

Comments
 (0)