Skip to content

Commit

Permalink
Add abstraction layer for external function of Streams.error
Browse files Browse the repository at this point in the history
This avoids that Modelica utility function ModelicaError has to be provided by library ModelicaExternalC.
  • Loading branch information
beutlich committed Oct 27, 2024
1 parent bf60ef8 commit bdf4e0b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 55 deletions.
62 changes: 8 additions & 54 deletions Modelica/Resources/C-Sources/ModelicaInternal.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
*/

/* Changelog:
Oct. 27, 2024: by Thomas Beutlich
Added ModelicaInternal_error (ticket #4496)
Jan. 15, 2024: by Thomas Beutlich
Utilized ModelicaDuplicateString and
ModelicaDuplicateStringWithErrorReturn (ticket #3686)
Expand Down Expand Up @@ -144,61 +147,8 @@
#define _GNU_SOURCE 1
#endif

#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
#include "ModelicaInternal.h"

MODELICA_NORETURN static void ModelicaNotExistError(const char* name) MODELICA_NORETURNATTR;
static void ModelicaNotExistError(const char* name) {
Expand All @@ -209,6 +159,10 @@ static void ModelicaNotExistError(const char* name) {
"as for dSPACE or xPC systems)", name);
}

void ModelicaInternal_error(_In_z_ const char* string) {
ModelicaError(string);
}

#undef MODELICA_NORETURN
#undef MODELICA_NORETURNATTR

Expand Down
54 changes: 54 additions & 0 deletions Modelica/Resources/C-Sources/ModelicaInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,59 @@
#endif
#endif

/*
Some of the functions never return 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

/*
* Non-null pointers and esp. null-terminated strings need to be passed to
* external functions.
Expand Down Expand Up @@ -87,6 +140,7 @@
#define _Ret_z_
#endif

MODELICA_EXPORT MODELICA_NORETURN void ModelicaInternal_error(_In_z_ const char* string) MODELICA_NORETURNATTR MODELICA_NONNULLATTR;
MODELICA_EXPORT void ModelicaInternal_mkdir(_In_z_ const char* directoryName) MODELICA_NONNULLATTR;
MODELICA_EXPORT void ModelicaInternal_rmdir(_In_z_ const char* directoryName) MODELICA_NONNULLATTR;
MODELICA_EXPORT int ModelicaInternal_stat(_In_z_ const char* name) MODELICA_NONNULLATTR;
Expand Down
2 changes: 1 addition & 1 deletion Modelica/Utilities/Streams.mo
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ separated by LF or CR-LF.
pure function error "Print error message and cancel all actions - in case of an unrecoverable error"
extends Modelica.Icons.Function;
input String string "String to be printed to error message window";
external "C" ModelicaError(string) annotation(Include="#include \"ModelicaUtilities.h\"", Library="ModelicaExternalC");
external "C" ModelicaInternal_error(string) annotation(IncludeDirectory="modelica://Modelica/Resources/C-Sources", Include="#include \"ModelicaInternal.h\"", Library="ModelicaExternalC");
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Expand Down

0 comments on commit bdf4e0b

Please sign in to comment.