Skip to content

Commit 9c895fb

Browse files
committed
Error handling updates
- Strip trailing whitespace from error-messages (error-messages add a trailing newline by default; we do not want 2 of them) - Register Modelica*Error functions in ErrorExt.mo (for the bootstrapped compiler) - This means we can use Modelica libraries and simply treat ModelicaError in an external function as Error.addMessage + throw() git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@20075 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent df5e737 commit 9c895fb

File tree

6 files changed

+55
-6
lines changed

6 files changed

+55
-6
lines changed

Compiler/Main/Main.mo

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,12 +1022,13 @@ algorithm
10221022
// call GC_init() the first thing we do!
10231023
System.initGarbageCollector();
10241024
Global.initialize();
1025-
1025+
ErrorExt.registerModelicaFormatError();
1026+
10261027
// cheat the function generation that this function is used
10271028
// this is needed because this function is used in the parser
10281029
// for the bootstrapped compiler
10291030
_ = Absyn.isDerCref(Absyn.INTEGER(0));
1030-
1031+
10311032
System.realtimeTick(GlobalScript.RT_CLOCK_SIMULATE_TOTAL);
10321033
args_1 = Flags.new(args);
10331034
System.gettextInit(Util.if_(Config.getRunningTestsuite(),"C",Flags.getConfigString(Flags.LOCALE_FLAG)));

Compiler/Util/ErrorExt.mo

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ encapsulated package ErrorExt
4343

4444
public import Error;
4545

46+
public function registerModelicaFormatError
47+
external "C" Error_registerModelicaFormatError() annotation(Documentation(info="<html>
48+
<p>Registers the ModelicaFormatError function to output messages in the Error buffer instead of the default standard output.</p>
49+
<p>Note: Only works in the bootstrapped compiler!</p>
50+
</html>"),Library = "omcruntime");
51+
end registerModelicaFormatError;
52+
4653
public function updateCurrentComponent
4754
input String str;
4855
input Boolean writeable;

Compiler/runtime/ErrorMessage.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ std::string ErrorMessage::getMessage_(int warningsAsErrors)
137137
std::string positionInfo = str.str();
138138
ret_msg = positionInfo + message_;
139139
}
140+
// trim trailing whitespace
141+
ret_msg.erase(ret_msg.find_last_not_of(" \n\r\t")+1);
140142
return ret_msg;
141143
}
142144

Compiler/runtime/Error_omc.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,36 @@ extern "C" {
3636
#include "rml_compatibility.h"
3737
#define ADD_METARECORD_DEFINITIONS static
3838
#include "OpenModelicaBootstrappingHeader.h"
39-
#include "ModelicaUtilities.h"
39+
#include "ModelicaUtilitiesExtra.h"
4040

4141
}
4242

4343
#include "errorext.cpp"
4444

4545
extern "C" {
4646

47+
/* Handle ModelicaFormatError called during translation (external Modelica packages, print to Error.mo instead of stdout) */
48+
void OpenModelica_ErrorModule_ModelicaVFormatError(const char *fmt, va_list ap)
49+
{
50+
char *str;
51+
vasprintf(&str, fmt, ap);
52+
c_add_message(NULL,0,ErrorType_runtime,ErrorLevel_error,str,NULL,0);
53+
free(str);
54+
MMC_THROW();
55+
}
56+
57+
void OpenModelica_ErrorModule_ModelicaError(const char *str)
58+
{
59+
c_add_message(NULL,0,ErrorType_runtime,ErrorLevel_error,str,NULL,0);
60+
MMC_THROW();
61+
}
62+
63+
void Error_registerModelicaFormatError()
64+
{
65+
OpenModelica_ModelicaError = OpenModelica_ErrorModule_ModelicaError;
66+
OpenModelica_ModelicaVFormatError = OpenModelica_ErrorModule_ModelicaVFormatError;
67+
}
68+
4769
void Error_addMessage(threadData_t *threadData,int errorID, void *msg_type, void *severity, const char* message, modelica_metatype tokenlst)
4870
{
4971
ErrorMessage::TokenList tokens;

Compiler/runtime/Error_rml.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,10 @@ RML_BEGIN_LABEL(ErrorExt__setShowErrorMessages)
193193
RML_TAILCALLK(rmlSC);
194194
}
195195

196+
RML_BEGIN_LABEL(ErrorExt__registerModelicaFormatError)
197+
{
198+
/* Bootstrapped compiler only */
199+
RML_TAILCALLK(rmlSC);
200+
}
201+
196202
} //extern "C"

SimulationRuntime/c/util/ModelicaUtilities.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,33 @@ void ModelicaFormatMessage(const char* string,...) {
5555
va_end(args);
5656
}
5757

58-
void ModelicaError(const char* string) {
58+
void OpenModelica_Simulation_ModelicaError(const char* string) {
5959
fputs(string, stderr);
6060
fflush(stderr);
6161
omc_throw(NULL);
6262
}
6363

64-
void ModelicaVFormatError(const char*string, va_list args) {
64+
void OpenModelica_Simulation_ModelicaVFormatError(const char*string, va_list args) {
6565
vfprintf(stderr, string, args);
6666
fflush(stderr);
6767
omc_throw(NULL);
6868
}
6969

70+
void (*OpenModelica_ModelicaError)(const char*) = OpenModelica_Simulation_ModelicaError;
71+
void (*OpenModelica_ModelicaVFormatError)(const char*,va_list) = OpenModelica_Simulation_ModelicaVFormatError;
72+
73+
void ModelicaError(const char* string) {
74+
OpenModelica_ModelicaError(string);
75+
}
76+
77+
void ModelicaVFormatError(const char*string, va_list args) {
78+
OpenModelica_ModelicaVFormatError(string,args);
79+
}
80+
7081
void ModelicaFormatError(const char* string, ...) {
7182
va_list args;
7283
va_start(args, string);
73-
ModelicaVFormatError(string, args);
84+
OpenModelica_ModelicaVFormatError(string,args);
7485
va_end(args);
7586
}
7687

0 commit comments

Comments
 (0)