Skip to content

Commit 798dc3a

Browse files
authored
Merge pull request Kvixen#34 from tkorvola/assert
utAssert support
2 parents 031a3e7 + 3c56644 commit 798dc3a

File tree

6 files changed

+77
-2
lines changed

6 files changed

+77
-2
lines changed

includes/rt_assert.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef RT_ASSERT_H_
2+
#define RT_ASSERT_H_
3+
4+
/* Assertion support for Simulix.
5+
*
6+
* A file with the same name may be generated by Matlab Coder. This file
7+
* replaces it.
8+
*/
9+
10+
#include "sx_assert.h"
11+
12+
#define utAssert(expr) sx_assert((expr), "Assertion " #expr " failed")
13+
14+
#endif

includes/sx_assert.c

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <setjmp.h>
2+
#include <stdbool.h>
3+
#include <stdio.h>
4+
5+
#include "sx_assert.h"
6+
#include "sx_assert_int.h"
7+
8+
jmp_buf fmu_exit;
9+
bool has_jmp = false;
10+
11+
/*TODO proper logging */
12+
void sx_assert_f(int cond, const char *file, int line, const char *msg) {
13+
if (cond)
14+
return;
15+
fprintf(stderr, "%s:%d: %s\n", file, line, msg);
16+
if (has_jmp)
17+
longjmp(fmu_exit, 1);
18+
}

includes/sx_assert.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef SX_ASSERT_H_
2+
#define SX_ASSERT_H_
3+
4+
/* Simulix assertion support.
5+
*/
6+
7+
extern void sx_assert_f(int cond, const char *file, int line, const char *msg);
8+
9+
#define sx_assert(expr, msg) sx_assert_f((expr), __FILE__, __LINE__, (msg))
10+
11+
#endif

includes/sx_assert_int.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef SX_ASSERT_INT_H_
2+
#define SX_ASSERT_INT_H_
3+
4+
/* Internal details of Simulix assertion support.
5+
*/
6+
7+
#include <setjmp.h>
8+
#include <stdbool.h>
9+
10+
extern jmp_buf fmu_exit;
11+
extern bool has_jmp;
12+
13+
#endif

templates/Simulix_dllmain_template.c

+12
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
#include "rtwtypes.h"
99
#include "rtw_modelmap.h"
1010
#include <stdint.h>
11+
#include <stdbool.h>
12+
#include <setjmp.h>
1113

1214
// include fmu header files, typedefs and macros
1315
#include "fmuTemplate.h"
1416

17+
#include "sx_assert_int.h"
18+
1519
//! System DLL base address
1620
size_t dllBaseAddress_U32 = 0;
1721
//! System DLL image size in bytes
@@ -217,13 +221,21 @@ static void eventUpdate(ModelInstance *comp, fmi2EventInfo *eventInfo, int isTim
217221
fmi2Boolean error = fmi2False;
218222

219223
if (isTimeEvent) {{
224+
if (setjmp(fmu_exit)) {{
225+
/* Assertion failed in model. */
226+
has_jmp = false;
227+
eventInfo->terminateSimulation = fmi2True;
228+
return;
229+
}}
230+
has_jmp = true;
220231
while (current_time < (comp->time - 0.1 * step_size) )
221232
{{
222233
{modelName}_step();
223234
current_time += step_size;
224235
{modelName}_time += 1; // Only for Vision/Testlogger that cannot handle double-presicion floats
225236
simulation_ticks += 1;
226237
}}
238+
has_jmp = false;
227239

228240
/*
229241
{{

unpack.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@
2929
TEMPLATE_REPLACE = {
3030
}
3131

32+
# Matching paths are always included (regardless of BAD_SOURCES etc.).
33+
GOOD_SOURCE_PATTERN = re.compile(r"includes/rt_assert\.h$")
34+
3235
BAD_SOURCES = {
36+
"rt_assert.h",
3337
"rt_main.c",
3438
"Simulix_exemain.c",
3539
"cJSON.c",
@@ -196,9 +200,12 @@ def generate_files(src, dst, zip_path, zip_name, extension_path, temp_path):
196200
def extract_file_names(dst):
197201
for root, dirs, files in walk(dst):
198202
for file in files:
199-
if not file in BAD_SOURCES and SOURCE_OR_HEADER_PATTERN.search(file):
203+
p = path.join(root, file)
204+
if (GOOD_SOURCE_PATTERN.search(p)
205+
or not file in BAD_SOURCES
206+
and SOURCE_OR_HEADER_PATTERN.search(file)):
200207
LIST_OF_SOURCES.append(path.join(root, file))
201-
208+
202209
# Fmu Functions
203210
def handle_fmu(dst, fmu_path):
204211
file_name, file_ext = path.splitext(fmu_path)

0 commit comments

Comments
 (0)