From e5d47ed4e01c9b66eb223113a9a77dc01db9b684 Mon Sep 17 00:00:00 2001 From: Thomas Beutlich Date: Mon, 29 Jan 2024 05:26:17 +0100 Subject: [PATCH] Add missing macro definitions (#4283) --- .CI/Test/ModelicaUtilities.h | 14 +++-- Modelica/Resources/C-Sources/ModelicaIO.c | 57 +++++++++++++++++++ .../Resources/C-Sources/ModelicaInternal.c | 56 ++++++++++++++++++ Modelica/Resources/C-Sources/ModelicaMatIO.c | 2 +- Modelica/Resources/C-Sources/ModelicaMatIO.h | 2 +- 5 files changed, 123 insertions(+), 8 deletions(-) diff --git a/.CI/Test/ModelicaUtilities.h b/.CI/Test/ModelicaUtilities.h index 9d0ae886b3..1dfa97cf56 100644 --- a/.CI/Test/ModelicaUtilities.h +++ b/.CI/Test/ModelicaUtilities.h @@ -1,6 +1,6 @@ /* ModelicaUtilities.h - External utility functions header - Copyright (C) 2010-2020, Modelica Association and contributors + Copyright (C) 2010-2024, Modelica Association and contributors All rights reserved. Redistribution and use in source and binary forms, with or without @@ -31,14 +31,11 @@ /* Utility functions which can be called by external Modelica functions. - These functions are defined in section 12.8.6 of the - Modelica Specification 3.0 and section 12.9.6 of the - Modelica Specification 3.1 and later. + These functions are defined in section 12.9.6 of the Modelica Specification 3.6. A generic C-implementation of these functions cannot be given, because it is tool dependent how strings are output in a - window of the respective simulation tool. Therefore, only - this header file is shipped with the Modelica Standard Library. + console or window of the respective simulation tool. */ #ifndef MODELICA_UTILITIES_H @@ -213,6 +210,11 @@ resources use, ModelicaError or ModelicaFormatError to signal the error. */ +#undef MODELICA_NORETURN +#undef MODELICA_NORETURNATTR +#undef MODELICA_FORMATATTR_PRINTF +#undef MODELICA_FORMATATTR_VPRINTF + #if defined(__cplusplus) } #endif diff --git a/Modelica/Resources/C-Sources/ModelicaIO.c b/Modelica/Resources/C-Sources/ModelicaIO.c index de5aeb58be..b71a733772 100644 --- a/Modelica/Resources/C-Sources/ModelicaIO.c +++ b/Modelica/Resources/C-Sources/ModelicaIO.c @@ -95,6 +95,60 @@ #include "ModelicaUtilities.h" #ifdef NO_FILE_SYSTEM + +/* + ModelicaNotExistError never returns to the caller. In order to compile + external Modelica C-code in most compilers, noreturn attributes need to + be present to avoid warnings or errors. + + The following macros handle noreturn attributes according to the + C11/C++11 standard with fallback to GNU, Clang or MSVC extensions if using + an older compiler. +*/ +#undef MODELICA_NORETURN +#undef MODELICA_NORETURNATTR +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L +#define MODELICA_NORETURN _Noreturn +#define MODELICA_NORETURNATTR +#elif defined(__cplusplus) && __cplusplus >= 201103L +#if (defined(__GNUC__) && __GNUC__ >= 5) || \ + (defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 4 && __GNUC_MINOR__ >= 8) +#define MODELICA_NORETURN [[noreturn]] +#define MODELICA_NORETURNATTR +#elif (defined(__GNUC__) && __GNUC__ >= 3) || \ + (defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 2 && __GNUC_MINOR__ >= 8) +#define MODELICA_NORETURN +#define MODELICA_NORETURNATTR __attribute__((noreturn)) +#elif defined(__GNUC__) +#define MODELICA_NORETURN +#define MODELICA_NORETURNATTR +#else +#define MODELICA_NORETURN [[noreturn]] +#define MODELICA_NORETURNATTR +#endif +#elif defined(__clang__) +/* Encapsulated for Clang since GCC fails to process __has_attribute */ +#if __has_attribute(noreturn) +#define MODELICA_NORETURN +#define MODELICA_NORETURNATTR __attribute__((noreturn)) +#else +#define MODELICA_NORETURN +#define MODELICA_NORETURNATTR +#endif +#elif (defined(__GNUC__) && __GNUC__ >= 3) || \ + (defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 2 && __GNUC_MINOR__ >= 8) || \ + (defined(__SUNPRO_C) && __SUNPRO_C >= 0x5110) +#define MODELICA_NORETURN +#define MODELICA_NORETURNATTR __attribute__((noreturn)) +#elif (defined(_MSC_VER) && _MSC_VER >= 1200) || \ + defined(__BORLANDC__) +#define MODELICA_NORETURN __declspec(noreturn) +#define MODELICA_NORETURNATTR +#else +#define MODELICA_NORETURN +#define MODELICA_NORETURNATTR +#endif + MODELICA_NORETURN static void ModelicaNotExistError(const char* name) MODELICA_NORETURNATTR; static void ModelicaNotExistError(const char* name) { /* Print error message if a function is not implemented */ @@ -104,6 +158,9 @@ static void ModelicaNotExistError(const char* name) { "as for dSPACE or xPC systems)\n", name); } +#undef MODELICA_NORETURN +#undef MODELICA_NORETURNATTR + void ModelicaIO_readMatrixSizes(_In_z_ const char* fileName, _In_z_ const char* matrixName, _Out_ int* dim) { ModelicaNotExistError("ModelicaIO_readMatrixSizes"); } diff --git a/Modelica/Resources/C-Sources/ModelicaInternal.c b/Modelica/Resources/C-Sources/ModelicaInternal.c index 7ab2dc67c9..701cc0d3ed 100644 --- a/Modelica/Resources/C-Sources/ModelicaInternal.c +++ b/Modelica/Resources/C-Sources/ModelicaInternal.c @@ -147,6 +147,59 @@ #include "ModelicaInternal.h" #include "ModelicaUtilities.h" +/* + ModelicaNotExistError never returns to the caller. In order to compile + external Modelica C-code in most compilers, noreturn attributes need to + be present to avoid warnings or errors. + + The following macros handle noreturn attributes according to the + C11/C++11 standard with fallback to GNU, Clang or MSVC extensions if using + an older compiler. +*/ +#undef MODELICA_NORETURN +#undef MODELICA_NORETURNATTR +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L +#define MODELICA_NORETURN _Noreturn +#define MODELICA_NORETURNATTR +#elif defined(__cplusplus) && __cplusplus >= 201103L +#if (defined(__GNUC__) && __GNUC__ >= 5) || \ + (defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 4 && __GNUC_MINOR__ >= 8) +#define MODELICA_NORETURN [[noreturn]] +#define MODELICA_NORETURNATTR +#elif (defined(__GNUC__) && __GNUC__ >= 3) || \ + (defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 2 && __GNUC_MINOR__ >= 8) +#define MODELICA_NORETURN +#define MODELICA_NORETURNATTR __attribute__((noreturn)) +#elif defined(__GNUC__) +#define MODELICA_NORETURN +#define MODELICA_NORETURNATTR +#else +#define MODELICA_NORETURN [[noreturn]] +#define MODELICA_NORETURNATTR +#endif +#elif defined(__clang__) +/* Encapsulated for Clang since GCC fails to process __has_attribute */ +#if __has_attribute(noreturn) +#define MODELICA_NORETURN +#define MODELICA_NORETURNATTR __attribute__((noreturn)) +#else +#define MODELICA_NORETURN +#define MODELICA_NORETURNATTR +#endif +#elif (defined(__GNUC__) && __GNUC__ >= 3) || \ + (defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 2 && __GNUC_MINOR__ >= 8) || \ + (defined(__SUNPRO_C) && __SUNPRO_C >= 0x5110) +#define MODELICA_NORETURN +#define MODELICA_NORETURNATTR __attribute__((noreturn)) +#elif (defined(_MSC_VER) && _MSC_VER >= 1200) || \ + defined(__BORLANDC__) +#define MODELICA_NORETURN __declspec(noreturn) +#define MODELICA_NORETURNATTR +#else +#define MODELICA_NORETURN +#define MODELICA_NORETURNATTR +#endif + MODELICA_NORETURN static void ModelicaNotExistError(const char* name) MODELICA_NORETURNATTR; static void ModelicaNotExistError(const char* name) { /* Print error message if a function is not implemented */ @@ -156,6 +209,9 @@ static void ModelicaNotExistError(const char* name) { "as for dSPACE or xPC systems)", name); } +#undef MODELICA_NORETURN +#undef MODELICA_NORETURNATTR + #ifdef NO_FILE_SYSTEM void ModelicaInternal_mkdir(_In_z_ const char* directoryName) { ModelicaNotExistError("ModelicaInternal_mkdir"); } diff --git a/Modelica/Resources/C-Sources/ModelicaMatIO.c b/Modelica/Resources/C-Sources/ModelicaMatIO.c index d613c424b5..957d6be0e6 100644 --- a/Modelica/Resources/C-Sources/ModelicaMatIO.c +++ b/Modelica/Resources/C-Sources/ModelicaMatIO.c @@ -1,6 +1,6 @@ /* ModelicaMatIO.c - MAT file I/O functions - Copyright (C) 2013-2023, Modelica Association and contributors + Copyright (C) 2013-2024, Modelica Association and contributors Copyright (C) 2015-2023, The matio contributors Copyright (C) 2005-2014, Christopher C. Hulbert All rights reserved. diff --git a/Modelica/Resources/C-Sources/ModelicaMatIO.h b/Modelica/Resources/C-Sources/ModelicaMatIO.h index 3a35f682e4..1e3f8288b6 100644 --- a/Modelica/Resources/C-Sources/ModelicaMatIO.h +++ b/Modelica/Resources/C-Sources/ModelicaMatIO.h @@ -1,6 +1,6 @@ /* ModelicaMatIO.h - MAT file I/O functions header - Copyright (C) 2013-2023, Modelica Association and contributors + Copyright (C) 2013-2024, Modelica Association and contributors Copyright (C) 2015-2023, The matio contributors Copyright (C) 2005-2014, Christopher C. Hulbert All rights reserved.