Skip to content

Commit

Permalink
Error handling updates
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
sjoelund committed Apr 10, 2014
1 parent df5e737 commit 9c895fb
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Compiler/Main/Main.mo
Expand Up @@ -1022,12 +1022,13 @@ algorithm
// call GC_init() the first thing we do!
System.initGarbageCollector();
Global.initialize();

ErrorExt.registerModelicaFormatError();

// cheat the function generation that this function is used
// this is needed because this function is used in the parser
// for the bootstrapped compiler
_ = Absyn.isDerCref(Absyn.INTEGER(0));

System.realtimeTick(GlobalScript.RT_CLOCK_SIMULATE_TOTAL);
args_1 = Flags.new(args);
System.gettextInit(Util.if_(Config.getRunningTestsuite(),"C",Flags.getConfigString(Flags.LOCALE_FLAG)));
Expand Down
7 changes: 7 additions & 0 deletions Compiler/Util/ErrorExt.mo
Expand Up @@ -43,6 +43,13 @@ encapsulated package ErrorExt

public import Error;

public function registerModelicaFormatError
external "C" Error_registerModelicaFormatError() annotation(Documentation(info="<html>
<p>Registers the ModelicaFormatError function to output messages in the Error buffer instead of the default standard output.</p>
<p>Note: Only works in the bootstrapped compiler!</p>
</html>"),Library = "omcruntime");
end registerModelicaFormatError;

public function updateCurrentComponent
input String str;
input Boolean writeable;
Expand Down
2 changes: 2 additions & 0 deletions Compiler/runtime/ErrorMessage.cpp
Expand Up @@ -137,6 +137,8 @@ std::string ErrorMessage::getMessage_(int warningsAsErrors)
std::string positionInfo = str.str();
ret_msg = positionInfo + message_;
}
// trim trailing whitespace
ret_msg.erase(ret_msg.find_last_not_of(" \n\r\t")+1);
return ret_msg;
}

Expand Down
24 changes: 23 additions & 1 deletion Compiler/runtime/Error_omc.cpp
Expand Up @@ -36,14 +36,36 @@ extern "C" {
#include "rml_compatibility.h"
#define ADD_METARECORD_DEFINITIONS static
#include "OpenModelicaBootstrappingHeader.h"
#include "ModelicaUtilities.h"
#include "ModelicaUtilitiesExtra.h"

}

#include "errorext.cpp"

extern "C" {

/* Handle ModelicaFormatError called during translation (external Modelica packages, print to Error.mo instead of stdout) */
void OpenModelica_ErrorModule_ModelicaVFormatError(const char *fmt, va_list ap)
{
char *str;
vasprintf(&str, fmt, ap);
c_add_message(NULL,0,ErrorType_runtime,ErrorLevel_error,str,NULL,0);
free(str);
MMC_THROW();
}

void OpenModelica_ErrorModule_ModelicaError(const char *str)
{
c_add_message(NULL,0,ErrorType_runtime,ErrorLevel_error,str,NULL,0);
MMC_THROW();
}

void Error_registerModelicaFormatError()
{
OpenModelica_ModelicaError = OpenModelica_ErrorModule_ModelicaError;
OpenModelica_ModelicaVFormatError = OpenModelica_ErrorModule_ModelicaVFormatError;
}

void Error_addMessage(threadData_t *threadData,int errorID, void *msg_type, void *severity, const char* message, modelica_metatype tokenlst)
{
ErrorMessage::TokenList tokens;
Expand Down
6 changes: 6 additions & 0 deletions Compiler/runtime/Error_rml.cpp
Expand Up @@ -193,4 +193,10 @@ RML_BEGIN_LABEL(ErrorExt__setShowErrorMessages)
RML_TAILCALLK(rmlSC);
}

RML_BEGIN_LABEL(ErrorExt__registerModelicaFormatError)
{
/* Bootstrapped compiler only */
RML_TAILCALLK(rmlSC);
}

} //extern "C"
17 changes: 14 additions & 3 deletions SimulationRuntime/c/util/ModelicaUtilities.c
Expand Up @@ -55,22 +55,33 @@ void ModelicaFormatMessage(const char* string,...) {
va_end(args);
}

void ModelicaError(const char* string) {
void OpenModelica_Simulation_ModelicaError(const char* string) {
fputs(string, stderr);
fflush(stderr);
omc_throw(NULL);
}

void ModelicaVFormatError(const char*string, va_list args) {
void OpenModelica_Simulation_ModelicaVFormatError(const char*string, va_list args) {
vfprintf(stderr, string, args);
fflush(stderr);
omc_throw(NULL);
}

void (*OpenModelica_ModelicaError)(const char*) = OpenModelica_Simulation_ModelicaError;
void (*OpenModelica_ModelicaVFormatError)(const char*,va_list) = OpenModelica_Simulation_ModelicaVFormatError;

void ModelicaError(const char* string) {
OpenModelica_ModelicaError(string);
}

void ModelicaVFormatError(const char*string, va_list args) {
OpenModelica_ModelicaVFormatError(string,args);
}

void ModelicaFormatError(const char* string, ...) {
va_list args;
va_start(args, string);
ModelicaVFormatError(string, args);
OpenModelica_ModelicaVFormatError(string,args);
va_end(args);
}

Expand Down

0 comments on commit 9c895fb

Please sign in to comment.