Skip to content

Commit e769a61

Browse files
authored
[C] Pedantic, no C/C++ extensions for compilation (#15605)
* Add `-Wpedantic -pedantic-errors` scoped to SimulationRuntime/c * Exclude meta_modelica_builtin_boxvar.h on GCC * Add `-Werror=c20-extensions -Werror=c++20-extensions` for Clang to classifies these as error-level * Add `-Werror=c23-extensions -Werror=c++23-extensions` for Clang to classifies these as error-level * Excluding meta/meta_modelica_builtin_boxvar.h from pedantic, not compatible by design * Fixing designated initializer usage in simulation_result_mat4.cpp * Fixing pedantic errors in GBODE * Fixing cast signed / unsigned * Fixing zero-sized arrays * Fixing pointer arithmetic on void pointers * Updating __FUNCTION__ to C99 __func__
1 parent 58ace23 commit e769a61

20 files changed

Lines changed: 133 additions & 73 deletions

OMCompiler/Parser/Modelica.g

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ goto rule ## func ## Ex; }}
121121
#define MMC_STRUCTHDR(x,y) 0
122122
#endif
123123

124-
#define NYI(void) fprintf(stderr, "NYI \%s \%s:\%d\n", __FUNCTION__, __FILE__, __LINE__); exit(1);
124+
#define NYI(void) fprintf(stderr, "NYI \%s \%s:\%d\n", __func__, __FILE__, __LINE__); exit(1);
125125

126126
#define PARSER_INFO(start) ((void*) SourceInfo__SOURCEINFO(ModelicaParser_filename_OMC, mmc_mk_bcon(ModelicaParser_readonly), mmc_mk_icon(start->line), mmc_mk_icon(start->line == 1 ? start->charPosition+2 : start->charPosition+1), mmc_mk_icon(LT(1)->line), mmc_mk_icon(LT(1)->charPosition+1), ModelicaParser_timeStamp))
127127
#if !defined(OMC_GENERATE_RELOCATABLE_CODE) || defined(OMC_BOOTSTRAPPING)

OMCompiler/SimulationRuntime/c/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ set(C_STANDARD C17)
88
set(C_STANDARD_REQUIRED ON)
99
set(CMAKE_C_EXTENSIONS OFF)
1010

11+
## Enforce standards as strictly as possible
12+
if(NOT MSVC)
13+
add_compile_options(-Wpedantic -pedantic-errors)
14+
# Clang puts designated-initializer warnings under -Werror=c23-extensions and
15+
# -Wc++20-extensions / -Wc++23-extensions (warning-level, not
16+
# extension-level), so -pedantic-errors alone does not promote them to errors.
17+
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
18+
add_compile_options(-Werror=c++20-extensions)
19+
add_compile_options(-Werror=c23-extensions -Werror=c++23-extensions)
20+
endif()
21+
endif()
22+
1123
# ######################################################################################################################
1224
# INTERFACE target
1325
# Other targets can link to this interface to get the include path transitively.

OMCompiler/SimulationRuntime/c/meta/meta_modelica_builtin_boxvar.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,17 @@
2727

2828
/* Code generated by meta_modelica_gen_boxvar.py */
2929

30+
#include "meta_modelica_data.h"
31+
3032
#if !(defined(OMC_MINIMAL_RUNTIME) || defined(OMC_FMI_RUNTIME))
3133

34+
/* This generated code intentionally casts function pointers to object pointers
35+
* (void* / modelica_metatype) as part of the MetaModelica boxing mechanism. ISO
36+
* C forbids this, but it is a well-defined GNU extension on all supported
37+
* platforms. */
38+
#pragma GCC diagnostic push
39+
#pragma GCC diagnostic ignored "-Wpedantic"
40+
3241
#if !defined(OMC_GENERATE_RELOCATABLE_CODE)
3342
#define OMC_SYM_BOXPTR(X) &boxptr_##X
3443
#else
@@ -291,4 +300,6 @@ static const MMC_DEFSTRUCTLIT(boxvar_lit_valueHashMod,2,0) {(modelica_metatype)
291300
#define boxvar_valueHashMod MMC_REFSTRUCTLIT(boxvar_lit_valueHashMod)
292301

293302
#undef OMC_SYM_BOXPTR
303+
304+
#pragma GCC diagnostic pop
294305
#endif /* #if !(defined(OMC_MINIMAL_RUNTIME) || defined(OMC_FMI_RUNTIME)) */

OMCompiler/SimulationRuntime/c/meta/meta_modelica_gen_boxvar.py

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,42 @@
1-
#!/usr/bin/env python2
1+
#!/usr/bin/env python3
2+
3+
from datetime import datetime
4+
5+
CURRENT_YEAR: int = datetime.now().year
6+
7+
runtime_header = f"""/*
8+
* This file belongs to the OpenModelica Run-Time System
9+
*
10+
* Copyright (c) 1998-{CURRENT_YEAR}, Open Source Modelica Consortium (OSMC), c/o Linköpings
11+
* universitet, Department of Computer and Information Science, SE-58183 Linköping, Sweden. All rights
12+
* reserved.
13+
*
14+
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
15+
* AGPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. ANY
16+
* USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
17+
* ACCEPTANCE OF THE BSD NEW LICENSE OR THE OSMC PUBLIC LICENSE OR THE AGPL
18+
* VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
19+
*
20+
* The OpenModelica software and the OSMC (Open Source Modelica Consortium) Public License
21+
* (OSMC-PL) are obtained from OSMC, either from the above address, from the URLs:
22+
* http://www.openmodelica.org or https://github.com/OpenModelica/ or
23+
* http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica distribution. GNU
24+
* AGPL version 3 is obtained from: https://www.gnu.org/licenses/licenses.html#GPL. The BSD NEW
25+
* License is obtained from: http://www.opensource.org/licenses/BSD-3-Clause.
26+
*
27+
* This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of
28+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY
29+
* SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF
30+
* OSMC-PL.
31+
*
32+
*/
33+
"""
234

335
functions = [
436
'arrayAppend',
537
'arrayCopy',
638
'arrayGet',
739
'arrayList',
8-
'arrayNth',
940
'arrayUpdate',
1041
'boolAnd',
1142
'boolEq',
@@ -36,14 +67,14 @@
3667
'isNone',
3768
'isSome',
3869
'listAppend',
70+
'listAppendDestroy',
3971
'listArray',
4072
'listDelete',
4173
'listEmpty',
4274
'listGet',
4375
'listHead',
4476
'listLength',
4577
'listMember',
46-
'listNth',
4778
'listRest',
4879
'listReverse',
4980
'listReverseInPlace',
@@ -89,21 +120,31 @@
89120
'valueHashMod'
90121
]
91122

92-
print '/* Code generated by meta_modelica_gen_boxvar.py */'
93-
print ''
94-
print '#if !(defined(OMC_MINIMAL_RUNTIME) || defined(OMC_FMI_RUNTIME))'
95-
print ''
96-
print '#if !defined(OMC_GENERATE_RELOCATABLE_CODE)'
97-
print '#define OMC_SYM_BOXPTR(X) &boxptr_##X'
98-
print '#else'
99-
print '#define OMC_SYM_BOXPTR(X) &boxvar_fn_##X'
123+
print(runtime_header)
124+
print('/* Code generated by meta_modelica_gen_boxvar.py */')
125+
print('')
126+
print('#include "meta_modelica_data.h"')
127+
print('')
128+
print('#if !(defined(OMC_MINIMAL_RUNTIME) || defined(OMC_FMI_RUNTIME))')
129+
print('')
130+
print('/* This generated code intentionally casts function pointers to object pointers (void* /')
131+
print(' * modelica_metatype) as part of the MetaModelica boxing mechanism. ISO C forbids this,')
132+
print(' * but it is a well-defined GNU extension on all supported platforms. */')
133+
print('#pragma GCC diagnostic push')
134+
print('#pragma GCC diagnostic ignored "-Wpedantic"')
135+
print('')
136+
print('#if !defined(OMC_GENERATE_RELOCATABLE_CODE)')
137+
print('#define OMC_SYM_BOXPTR(X) &boxptr_##X')
138+
print('#else')
139+
print('#define OMC_SYM_BOXPTR(X) &boxvar_fn_##X')
100140
for f in functions:
101-
print 'static void* boxvar_fn_%s = (void*) boxptr_%s;' % (f,f)
102-
print '#endif'
141+
print('static void* boxvar_fn_%s = (void*) boxptr_%s;' % (f,f))
142+
print('#endif')
103143
for f in functions:
104-
print 'static const MMC_DEFSTRUCTLIT(boxvar_lit_%s,2,0) {(modelica_metatype) OMC_SYM_BOXPTR(%s),0}};' % (f,f)
105-
print '#define boxvar_%s MMC_REFSTRUCTLIT(boxvar_lit_%s)' % (f,f)
106-
print ''
107-
print '#undef OMC_SYM_BOXPTR'
108-
print '#endif /* #if !(defined(OMC_MINIMAL_RUNTIME) || defined(OMC_FMI_RUNTIME)) */'
109-
144+
print('static const MMC_DEFSTRUCTLIT(boxvar_lit_%s,2,0) {(modelica_metatype) OMC_SYM_BOXPTR(%s),0}};' % (f,f))
145+
print('#define boxvar_%s MMC_REFSTRUCTLIT(boxvar_lit_%s)' % (f,f))
146+
print('')
147+
print('#undef OMC_SYM_BOXPTR')
148+
print('')
149+
print('#pragma GCC diagnostic pop')
150+
print('#endif /* #if !(defined(OMC_MINIMAL_RUNTIME) || defined(OMC_FMI_RUNTIME)) */')

OMCompiler/SimulationRuntime/c/meta/meta_modelica_segv.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ static void handler(int signo, siginfo_t *si, void *ptr)
162162
{
163163
int unused __attribute__((unused)), isStackOverflow;
164164
threadData_t *threadData = (threadData_t*)pthread_getspecific(mmc_thread_data_key);
165-
isStackOverflow = si->si_addr < threadData->stackBottom && si->si_addr > threadData->stackBottom-LIMIT_FOR_STACK_OVERFLOW;
165+
isStackOverflow = si->si_addr < threadData->stackBottom
166+
&& (char*)si->si_addr > (char*)threadData->stackBottom - LIMIT_FOR_STACK_OVERFLOW;
166167
if (isStackOverflow) {
167168
mmc_setStacktraceMessages(1,0);
168169
sigprocmask(SIG_UNBLOCK, &segvset, NULL);
@@ -225,7 +226,7 @@ static void* getStackBase() {
225226
}
226227
#endif
227228
assert(size > 128*1024);
228-
return stackBottom + 64*1024;
229+
return (char*)stackBottom + 64*1024;
229230
}
230231

231232
void init_metamodelica_segv_handler()

OMCompiler/SimulationRuntime/c/optimization/DataManagement/MoveData.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ void setLocalVars(OptData * optData, DATA * data, const double * const vopt,
811811
data->simulationInfo->inputVars[k-nx] = (modelica_real) vopt[shift + k]*vnom[k];
812812
}
813813

814-
};
814+
}
815815

816816

817817
/*

OMCompiler/SimulationRuntime/c/simulation/results/simulation_result_mat4.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ struct variableCount count_name_description_signals(const MODEL_DATA *mData,
198198
modelica_boolean cpuTime)
199199
{
200200
struct variableCount count = {
201-
.maxLengthName = strlen(timeName) + 1,
202-
.maxLengthDesc = strlen(timeDesc) + 1,
203-
.nSignals = 1};
201+
strlen(timeName) + 1,
202+
strlen(timeDesc) + 1,
203+
1};
204204

205205
/* CPU-time */
206206
if (cpuTime)

OMCompiler/SimulationRuntime/c/simulation/solver/dassl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ typedef struct DASSL_DATA{
7575
int (*jacobianFunction)(double *t, double *y, double *yprime, double *deltaD,
7676
double *pd, double *cj, double *h, double *wt,
7777
double *rpar, int* ipar);
78-
void* zeroCrossingFunction;
78+
int (*zeroCrossingFunction)(int *neqm, double *t, double *y, double *yp,
79+
int *ng, double *gout, double *rpar, int* ipar);
7980

8081
#ifdef USE_PARJAC
8182
JACOBIAN* jacColumns; /* thread local analytic jacobians */

OMCompiler/SimulationRuntime/c/simulation/solver/embedded_server.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,14 @@ void* embedded_server_load_functions(const char *server_name)
108108
errorStreamPrint(OMC_LOG_DEBUG, 0, "Failed to load function omc_embedded_server_update: %s\n", dlerror());
109109
MMC_THROW();
110110
}
111-
embedded_server_init = funcInit;
112-
wait_for_step = funcWaitForStep;
113-
embedded_server_deinit = funcDeinit;
114-
embedded_server_update = funcUpdate;
115-
infoStreamPrint(OMC_LOG_DEBUG, 0, "Loaded embedded server");
111+
112+
// ISO C forbids assignment between function pointer and ‘void *’ [-Wpedantic]
113+
// dlsym with -pedantic is tricky, the following workaround is from the dlsym man page but still a hack
114+
*(void**)(&embedded_server_init) = funcInit;
115+
*(void**)(&wait_for_step) = funcWaitForStep;
116+
*(void**)(&embedded_server_deinit) = funcDeinit;
117+
*(void**)(&embedded_server_update) = funcUpdate;
118+
116119
return dll;
117120
}
118121

OMCompiler/SimulationRuntime/c/simulation/solver/gbode_main.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ typedef struct DATA_GBODEF{
123123

124124
/* statistics */
125125
SOLVERSTATS stats;
126-
int fastStateUpdateCount; /* number of fast state updates */
127-
int additionalFullODEEvaluations; /* number of overhead fODE evaluations without selection in fast steps */
126+
unsigned int fastStateUpdateCount; /* number of fast state updates */
127+
unsigned int additionalFullODEEvaluations; /* number of overhead fODE evaluations without selection in fast steps */
128128
} DATA_GBODEF;
129129

130130
typedef struct DATA_GBODE{

0 commit comments

Comments
 (0)