From ca6e3dcdc4ff63b335d27a2d49c5a9ca8960c337 Mon Sep 17 00:00:00 2001 From: Peter Bigot Date: Mon, 12 Aug 2019 12:55:06 -0500 Subject: [PATCH] include/posix: rearrange for standard use of extern "C" Consistently place C++ use of extern "C" after all include directives, within the negative branch of _ASMLANGUAGE if used. Background from issue #17997: Declarations that use C linkage should be placed within extern "C" so the language linkage is correct when the header is included by a C++ compiler. Similarly #include directives should be outside the extern "C" to ensure the language-specific default linkage is applied to any declarations provided by the included header. See: https://en.cppreference.com/w/cpp/language/language_linkage Signed-off-by: Peter Bigot --- include/posix/arpa/inet.h | 4 ++-- include/posix/dirent.h | 12 ++++++------ include/posix/netdb.h | 4 ++-- include/posix/posix_types.h | 8 ++++---- include/posix/pthread_key.h | 8 ++++---- include/posix/semaphore.h | 6 +++--- include/posix/signal.h | 4 ++-- include/posix/sys/socket.h | 6 +++--- include/posix/time.h | 33 ++++++++++++++++++++++++++------- include/posix/unistd.h | 8 ++++---- 10 files changed, 56 insertions(+), 37 deletions(-) diff --git a/include/posix/arpa/inet.h b/include/posix/arpa/inet.h index c7655584d05..811814f26f2 100644 --- a/include/posix/arpa/inet.h +++ b/include/posix/arpa/inet.h @@ -6,12 +6,12 @@ #ifndef ZEPHYR_INCLUDE_POSIX_ARPA_INET_H_ #define ZEPHYR_INCLUDE_POSIX_ARPA_INET_H_ +#include + #ifdef __cplusplus extern "C" { #endif -#include - static inline char *inet_ntop(sa_family_t family, const void *src, char *dst, size_t size) { diff --git a/include/posix/dirent.h b/include/posix/dirent.h index 0496da13e0e..08449942435 100644 --- a/include/posix/dirent.h +++ b/include/posix/dirent.h @@ -6,16 +6,16 @@ #ifndef ZEPHYR_INCLUDE_POSIX_DIRENT_H_ #define ZEPHYR_INCLUDE_POSIX_DIRENT_H_ -#ifdef __cplusplus -extern "C" { -#endif - #include #include "posix_types.h" #ifdef CONFIG_POSIX_FS #include +#ifdef __cplusplus +extern "C" { +#endif + typedef void DIR; struct dirent { @@ -28,10 +28,10 @@ extern DIR *opendir(const char *dirname); extern int closedir(DIR *dirp); extern struct dirent *readdir(DIR *dirp); -#endif - #ifdef __cplusplus } #endif +#endif /* CONFIG_POSIX_FS */ + #endif /* ZEPHYR_INCLUDE_POSIX_DIRENT_H_ */ diff --git a/include/posix/netdb.h b/include/posix/netdb.h index 64286eacea0..94a09d4dbf4 100644 --- a/include/posix/netdb.h +++ b/include/posix/netdb.h @@ -6,12 +6,12 @@ #ifndef ZEPHYR_INCLUDE_POSIX_NETDB_H_ #define ZEPHYR_INCLUDE_POSIX_NETDB_H_ +#include + #ifdef __cplusplus extern "C" { #endif -#include - #define addrinfo zsock_addrinfo static inline int getaddrinfo(const char *host, const char *service, diff --git a/include/posix/posix_types.h b/include/posix/posix_types.h index 86b3f04067c..15b0ba8d2d4 100644 --- a/include/posix/posix_types.h +++ b/include/posix/posix_types.h @@ -7,16 +7,16 @@ #ifndef ZEPHYR_INCLUDE_POSIX_SYS_TYPES_H_ #define ZEPHYR_INCLUDE_POSIX_SYS_TYPES_H_ -#ifdef __cplusplus -extern "C" { -#endif - #ifndef CONFIG_ARCH_POSIX #include #endif #include +#ifdef __cplusplus +extern "C" { +#endif + typedef unsigned long useconds_t; /* time related attributes */ diff --git a/include/posix/pthread_key.h b/include/posix/pthread_key.h index e2493e20002..808cd7cd270 100644 --- a/include/posix/pthread_key.h +++ b/include/posix/pthread_key.h @@ -7,14 +7,14 @@ #ifndef ZEPHYR_INCLUDE_POSIX_PTHREAD_KEY_H_ #define ZEPHYR_INCLUDE_POSIX_PTHREAD_KEY_H_ -#ifdef __cplusplus -extern "C" { -#endif - #ifdef CONFIG_PTHREAD_IPC #include #include +#ifdef __cplusplus +extern "C" { +#endif + typedef u32_t pthread_once_t; /* pthread_key */ diff --git a/include/posix/semaphore.h b/include/posix/semaphore.h index bea48ffb327..f19c741a1b7 100644 --- a/include/posix/semaphore.h +++ b/include/posix/semaphore.h @@ -6,13 +6,13 @@ #ifndef ZEPHYR_INCLUDE_POSIX_SEMAPHORE_H_ #define ZEPHYR_INCLUDE_POSIX_SEMAPHORE_H_ +#include +#include "posix_types.h" + #ifdef __cplusplus extern "C" { #endif -#include -#include "posix_types.h" - int sem_destroy(sem_t *semaphore); int sem_getvalue(sem_t *restrict semaphore, int *restrict value); int sem_init(sem_t *semaphore, int pshared, unsigned int value); diff --git a/include/posix/signal.h b/include/posix/signal.h index d4faff19d31..85b5c81d504 100644 --- a/include/posix/signal.h +++ b/include/posix/signal.h @@ -6,12 +6,12 @@ #ifndef ZEPHYR_INCLUDE_POSIX_SIGNAL_H_ #define ZEPHYR_INCLUDE_POSIX_SIGNAL_H_ +#include "posix_types.h" + #ifdef __cplusplus extern "C" { #endif -#include "posix_types.h" - #ifndef SIGEV_NONE #define SIGEV_NONE 1 #endif diff --git a/include/posix/sys/socket.h b/include/posix/sys/socket.h index e644fe45adf..805ac08a1ca 100644 --- a/include/posix/sys/socket.h +++ b/include/posix/sys/socket.h @@ -6,13 +6,13 @@ #ifndef ZEPHYR_INCLUDE_POSIX_SYS_SOCKET_H_ #define ZEPHYR_INCLUDE_POSIX_SYS_SOCKET_H_ +#include +#include + #ifdef __cplusplus extern "C" { #endif -#include -#include - static inline int socket(int family, int type, int proto) { return zsock_socket(family, type, proto); diff --git a/include/posix/time.h b/include/posix/time.h index 72b3b8b386b..ca1e262f1f4 100644 --- a/include/posix/time.h +++ b/include/posix/time.h @@ -6,10 +6,6 @@ #ifndef ZEPHYR_INCLUDE_POSIX_TIME_H_ #define ZEPHYR_INCLUDE_POSIX_TIME_H_ -#ifdef __cplusplus -extern "C" { -#endif - #ifdef CONFIG_NEWLIB_LIBC /* Kludge to support outdated newlib version as used in SDK 0.10 for Xtensa */ #include @@ -17,20 +13,30 @@ extern "C" { #ifdef __NEWLIB__ /* Newever Newlib 3.x+ */ #include -#else +#else /* __NEWLIB__ */ /* Workaround for older Newlib 2.x, as used by Xtensa. It lacks sys/_timeval.h, * so mimic it here. */ #include #ifndef __timespec_defined + +#ifdef __cplusplus +extern "C" { +#endif + struct timespec { time_t tv_sec; long tv_nsec; }; -#endif + +#ifdef __cplusplus +} #endif -#else +#endif /* __timespec_defined */ +#endif /* __NEWLIB__ */ + +#else /* CONFIG_NEWLIB_LIBC */ /* Not Newlib */ #include #endif /* CONFIG_NEWLIB_LIBC */ @@ -44,10 +50,19 @@ struct timespec { */ #if !defined(__NEWLIB_H__) || (__NEWLIB__ >= 3) || \ (__NEWLIB__ == 2 && __NEWLIB_MINOR__ >= 2) + +#ifdef __cplusplus +extern "C" { +#endif + struct itimerspec { struct timespec it_interval; /* Timer interval */ struct timespec it_value; /* Timer expiration */ }; +#ifdef __cplusplus +} +#endif + #endif #include @@ -55,6 +70,10 @@ struct itimerspec { #include "posix_types.h" #include +#ifdef __cplusplus +extern "C" { +#endif + #ifndef CLOCK_REALTIME #define CLOCK_REALTIME 0 #endif diff --git a/include/posix/unistd.h b/include/posix/unistd.h index fb1fe806b5c..b5957c8b0aa 100644 --- a/include/posix/unistd.h +++ b/include/posix/unistd.h @@ -6,10 +6,6 @@ #ifndef ZEPHYR_INCLUDE_POSIX_UNISTD_H_ #define ZEPHYR_INCLUDE_POSIX_UNISTD_H_ -#ifdef __cplusplus -extern "C" { -#endif - #include "posix_types.h" #include "sys/stat.h" #ifdef CONFIG_NETWORKING @@ -20,6 +16,10 @@ extern "C" { #ifdef CONFIG_POSIX_API #include +#ifdef __cplusplus +extern "C" { +#endif + /* File related operations */ extern int close(int file); extern ssize_t write(int file, const void *buffer, size_t count);