Skip to content

Commit

Permalink
Add missing macro definitions (modelica#4282)
Browse files Browse the repository at this point in the history
  • Loading branch information
beutlich committed Jan 26, 2024
1 parent 1ed0e58 commit b13e4d6
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 8 deletions.
14 changes: 8 additions & 6 deletions .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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions Modelica/Resources/C-Sources/ModelicaIO.c
Expand Up @@ -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 */
Expand All @@ -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"); }
Expand Down
56 changes: 56 additions & 0 deletions Modelica/Resources/C-Sources/ModelicaInternal.c
Expand Up @@ -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 */
Expand All @@ -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"); }
Expand Down
2 changes: 1 addition & 1 deletion 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.
Expand Down
2 changes: 1 addition & 1 deletion 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.
Expand Down

0 comments on commit b13e4d6

Please sign in to comment.