fix(posix): guard Windows shims with _WIN32 instead of #error

clang-tidy's Linux pre-merge check parses these headers via the global
include path, sees the #error guard, and reports clang-diagnostic-error
on every PR. Wrap the Windows-only bodies in #ifdef _WIN32 / #endif so
the shims are silent no-ops on non-Windows toolchains while still being
correctness-sized on the targets that actually compile against MinGW or
MSVC. Also covers libgen.h / time.h / sys/types.h whose static inlines
referenced MS-specific identifiers (gmtime_s family, basename
redeclaration, quad_t typedef redefinition) and px4_windows_internal.h
which is consumed only by the Windows backend.

Signed-off-by: Nuno Marques <n.marques21@hotmail.com>
This commit is contained in:
Nuno Marques
2026-05-07 16:36:52 -07:00
parent d1734ef561
commit a4e356a265
12 changed files with 46 additions and 36 deletions
@@ -43,9 +43,7 @@
*/
#pragma once
#ifndef _WIN32
# error "afunix.h shim only valid on Windows"
#endif
#ifdef _WIN32
#include <stddef.h>
#include <string.h>
@@ -114,3 +112,5 @@ typedef struct sockaddr_un sockaddr_un;
#ifdef __cplusplus
}
#endif
#endif /* _WIN32 */
@@ -40,9 +40,7 @@
*/
#pragma once
#ifndef _WIN32
# error "arpa/inet.h shim only valid on Windows"
#endif
#ifdef _WIN32
#include <stddef.h>
@@ -73,3 +71,5 @@ struct in_addr inet_makeaddr(in_addr_t net, in_addr_t host);
#ifdef __cplusplus
}
#endif
#endif /* _WIN32 */
@@ -38,6 +38,8 @@
#pragma once
#ifdef _WIN32
#include <string.h>
#ifdef __cplusplus
@@ -79,7 +81,7 @@ static inline char *dirname(char *path)
return (char *)".";
}
char *last = NULL;
char *last = nullptr;
for (char *p = path; *p; ++p) {
if (*p == '/' || *p == '\\') {
@@ -103,3 +105,5 @@ static inline char *dirname(char *path)
#ifdef __cplusplus
}
#endif
#endif /* _WIN32 */
@@ -41,9 +41,7 @@
*/
#pragma once
#ifndef _WIN32
# error "net/if.h shim only valid on Windows"
#endif
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
@@ -241,3 +239,5 @@ void if_freenameindex(struct if_nameindex *ptr);
#ifdef __cplusplus
}
#endif
#endif /* _WIN32 */
+3 -3
View File
@@ -42,9 +42,7 @@
*/
#pragma once
#ifndef _WIN32
# error "netdb.h shim only valid on Windows"
#endif
#ifdef _WIN32
#include <stdint.h>
@@ -164,3 +162,5 @@ struct servent *getservent(void);
#ifdef __cplusplus
}
#endif
#endif /* _WIN32 */
@@ -40,9 +40,7 @@
*/
#pragma once
#ifndef _WIN32
# error "netinet/in.h shim only valid on Windows"
#endif
#ifdef _WIN32
#include <stdint.h>
@@ -91,3 +89,5 @@ typedef uint16_t in_port_t;
#ifndef IN6_ARE_ADDR_EQUAL
#define IN6_ARE_ADDR_EQUAL(a, b) IN6_ADDR_EQUAL((a), (b))
#endif
#endif /* _WIN32 */
@@ -39,9 +39,7 @@
*/
#pragma once
#ifndef _WIN32
# error "netinet/tcp.h shim only valid on Windows"
#endif
#ifdef _WIN32
#include <netinet/in.h>
#include <winsock2.h>
@@ -70,3 +68,5 @@
#ifndef TCP_USER_TIMEOUT
#define TCP_USER_TIMEOUT 18
#endif
#endif /* _WIN32 */
@@ -38,12 +38,12 @@
*/
#pragma once
#ifndef _WIN32
# error "netinet/udp.h shim only valid on Windows"
#endif
#ifdef _WIN32
#include <netinet/in.h>
#ifndef SOL_UDP
#define SOL_UDP IPPROTO_UDP
#endif
#endif /* _WIN32 */
@@ -78,6 +78,7 @@ typedef unsigned int useconds_t;
#include_next <sys/types.h>
#endif
#ifdef _WIN32
#ifndef _PX4_SYS_TYPES_ALIASES_DEFINED
#define _PX4_SYS_TYPES_ALIASES_DEFINED
/** @name BSD/GNU scalar aliases
@@ -99,3 +100,4 @@ typedef long long quad_t;
typedef unsigned long long u_quad_t;
/** @} */
#endif
#endif /* _WIN32 */
@@ -42,9 +42,7 @@
*/
#pragma once
#ifndef _WIN32
# error "sys/un.h shim only valid on Windows"
#endif
#ifdef _WIN32
#include <stddef.h>
#include <string.h>
@@ -64,3 +62,5 @@ typedef struct sockaddr_un sockaddr_un;
/* afunix.h already provides struct sockaddr_un. Re-export sun_path/sun_family
* aliases if any translation unit expects them. afunix.h on Windows already
* names them that way, so no further work is needed. */
#endif /* _WIN32 */
+12 -8
View File
@@ -40,6 +40,8 @@
*/
#pragma once
#ifdef _WIN32
#if defined(_MSC_VER) && !defined(__clang__)
#if defined(__has_include)
# if __has_include(<../ucrt/time.h>)
@@ -104,30 +106,30 @@ int nanosleep(const struct timespec *req, struct timespec *rem);
/** @brief Thread-safe UTC conversion using the Windows gmtime_s() order. */
static inline struct tm *gmtime_r(const time_t *timep, struct tm *result)
{
if (!timep || !result) { return NULL; }
return (gmtime_s(result, timep) == 0) ? result : NULL;
if (!timep || !result) { return nullptr; }
return (gmtime_s(result, timep) == 0) ? result : nullptr;
}
/** @brief Thread-safe local-time conversion using the Windows localtime_s(). */
static inline struct tm *localtime_r(const time_t *timep, struct tm *result)
{
if (!timep || !result) { return NULL; }
return (localtime_s(result, timep) == 0) ? result : NULL;
if (!timep || !result) { return nullptr; }
return (localtime_s(result, timep) == 0) ? result : nullptr;
}
/** @brief Thread-safe asctime() wrapper; @p buf must hold at least 26 bytes. */
static inline char *asctime_r(const struct tm *tm, char *buf)
{
if (!tm || !buf) { return NULL; }
if (!tm || !buf) { return nullptr; }
/* POSIX requires a 26-byte buffer; match that to asctime_s. */
return (asctime_s(buf, 26, tm) == 0) ? buf : NULL;
return (asctime_s(buf, 26, tm) == 0) ? buf : nullptr;
}
/** @brief Thread-safe ctime() wrapper; @p buf must hold at least 26 bytes. */
static inline char *ctime_r(const time_t *timep, char *buf)
{
if (!timep || !buf) { return NULL; }
return (ctime_s(buf, 26, timep) == 0) ? buf : NULL;
if (!timep || !buf) { return nullptr; }
return (ctime_s(buf, 26, timep) == 0) ? buf : nullptr;
}
#ifndef timerisset
@@ -167,3 +169,5 @@ static inline char *ctime_r(const time_t *timep, char *buf)
#endif
#endif /* _PX4_TIME_R_SHIM_DEFINED */
#endif /* _WIN32 */
@@ -49,9 +49,7 @@
*/
#pragma once
#ifndef _WIN32
# error "px4_windows_internal.h is Windows-only"
#endif
#ifdef _WIN32
#define _WIN32_WINNT 0x0A00
#include <string.h>
@@ -110,3 +108,5 @@ int px4_wsa_error_to_errno(int err);
* @return Static, human-readable error string.
*/
const char *px4_hstrerror_text(int err);
#endif /* _WIN32 */