MicroModelicaCCompiler  4.5.3
error.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  This file is part of QSS Solver.
4 
5  QSS Solver is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  QSS Solver is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QSS Solver. If not, see <http://www.gnu.org/licenses/>.
17 
18  ******************************************************************************/
19 
20 #include "error.hpp"
21 
22 #include <stdarg.h>
23 #include <iostream>
24 #include <cstdio>
25 #include <cstdlib>
26 #include <sstream>
27 
28 using namespace std;
29 
30 namespace MicroModelica {
31 namespace Util {
32 Error::Error() : _errors(), _warnings(), _num_errors(0), _num_warnings(0), _std_def() {}
33 
34 void Error::setFile(string s) { _std_def = s; }
35 
36 string Error::file() { return _std_def; }
37 
38 void Error::setClassName(string class_name) { _class_name = class_name; }
39 
40 string Error::className() { return _class_name; }
41 
42 void Error::add(int pos, unsigned int code, ER_Type t, const string message, ...)
43 {
44  va_list ap;
45  ostringstream msg;
46  char local[1024];
47  int size = 100;
48  msg << "Line: " << pos << " Class: " << _class_name << endl;
49  msg << typeString(t) << "(" << hex << uppercase << code << ")" << endl;
50  va_start(ap, message);
51  vsnprintf((char *)local, size, message.c_str(), ap);
52  va_end(ap);
53  msg << printCode(code) << endl;
54  msg << local << endl;
55  if (t == ER_Error) {
56  _errors[_num_errors++] = msg.str();
57  } else {
58  _warnings[_num_warnings++] = msg.str();
59  }
60  if (t == ER_Fatal) {
61  show();
62  exit(-1);
63  }
64 }
65 
67 {
68  if (!_errors.empty() || !_warnings.empty()) {
69  cout << "File: " << _std_def << endl;
70  }
71  map<unsigned int, string>::iterator it;
72  for (it = _warnings.begin(); it != _warnings.end(); it++) {
73  cout << it->second;
74  }
75  for (it = _errors.begin(); it != _errors.end(); it++) {
76  cout << it->second;
77  }
78 }
79 
81 {
82  switch (t) {
83  case ER_Warning:
84  return "WARNING: ";
85  case ER_Error:
86  return "ERROR: ";
87  case ER_Fatal:
88  return "FATAL ERROR: ";
89  }
90  return "";
91 }
92 
93 string Error::printCode(int code)
94 {
95  int module_code = code & EM_MODULE;
96  int message_code = code & EM_MESSAGE;
97  string message;
98  switch (module_code) {
99  case EM_AST:
100  message.append("Abstract Syntax Tree: ");
101  break;
102  case EM_IR:
103  message.append("Intermediate Representation: ");
104  break;
105  case EM_CG:
106  message.append("Code Generator: ");
107  break;
108  case EM_PP:
109  message.append("Pretty Printer: ");
110  break;
111  case EM_ERROR:
112  message.append("Error: ");
113  break;
114  case EM_GRM:
115  message.append("Parser: ");
116  break;
117  }
118  switch (message_code) {
120  message.append("Variable not found.\n");
121  break;
123  message.append("Variable not defined.\n");
124  break;
125  case EM_CANT_OPEN_FILE:
126  message.append("Can not open file.\n");
127  break;
128  case EM_PARSE_FILE:
129  message.append("Can not parse file.\n");
130  break;
131  case EM_WRONG_TYPE:
132  message.append("Wrong variable type.\n");
133  break;
134  case EM_WRONG_EXP:
135  message.append("Wrong expression type.\n");
136  break;
137  case EM_WRONG_SCANNER:
138  message.append("Unknown scanner type.\n");
139  break;
140  case EM_UNKNOWN_OPER:
141  message.append("Unknown operator type.\n");
142  break;
144  message.append("Unknown operation.\n");
145  break;
146  case EM_UNKNOWN_EXP:
147  message.append("Unknown expression.\n");
148  break;
149  case EM_UNKNOWN_TYPE:
150  message.append("Unknown type.\n");
151  break;
152  case EM_ARRAY_INIT:
153  message.append("Array initialization not implemented.\n");
154  break;
155  case EM_ARRAY_INIT_VAL:
156  message.append("Array wrong number of init values.\n");
157  break;
158  case EM_ARRAY_CTE:
159  message.append("Array of type CONSTANT not allowed.\n");
160  break;
161  case EM_ARRAY_SIZE:
162  message.append("Array size expected.\n");
163  break;
164  case EM_ALG_INIT:
165  message.append("Can not assign initial values to algebraic variables.\n");
166  break;
167  case EM_ALG_INDEX:
168  message.append("Algebraic variables definitions must use previously defined variables.\n");
169  break;
170  case EM_OUTPUT_INIT:
171  message.append("Can not assign initial values to output variables.\n");
172  break;
173  case EM_FOR_VAR:
174  message.append("Can not assign a simple variable inside a for loop.\n");
175  break;
176  case EM_FOR_DEF:
177  message.append("Can not define a new variable inside a for loop.\n");
178  break;
179  case EM_CALL_EXP:
180  message.append("Can not use a function call in an index defintion.\n");
181  break;
182  case EM_RANGE_EXP:
183  message.append("Can not use a range expression in an index defintion.\n");
184  break;
185  case EM_INDEX_RANGE:
186  message.append("Index out of range.\n");
187  break;
188  case EM_SIZE_ZERO:
189  message.append("Algebraic variable size is zero.\n");
190  break;
191  case EM_GEN_DEF:
192  message.append("Expecting generic event definition.\n");
193  break;
194  case EM_EVENT_DEF:
195  message.append("Event not defined.\n");
196  break;
197  case EM_SYMBOL:
198  message.append("Symbol not recognized.\n");
199  break;
200  case EM_SYM_DER:
201  message.append("Symbolic derivative can not be generated (External functions?).\nTry using numerical integration.\n");
202  break;
203  case EM_EQ_DEF:
204  message.append("Equation not defined.\n");
205  break;
206  case EM_NO_EQ:
207  message.append("No equation defined in generic definition.\n");
208  break;
209  case EM_UNKNOWN_ODE:
210  message.append("Unkown equation type.\n");
211  break;
212  case EM_EVENT_FOUND:
213  message.append("Event not found.\n");
214  break;
215  case EM_UNKNOWN_ERROR:
216  message.append("Unknown error.\n");
217  break;
218  case EM_OUTPUT_DEF:
219  message.append("Missing output variable definition.\n");
220  break;
221  case EM_SAMPLED_DEF:
222  message.append("Missing sample period definition.\n");
223  break;
224  case EM_ARGUMENTS:
225  message.append("Function call, wrong number of arguments.\n");
226  break;
227  case EM_SAMPLED_OUT:
228  message.append("Sampled output, can not output generic expressions, only state or dicrete variables are allowed.\n");
229  break;
230  case EM_HANDLER:
231  message.append("Handler redefinition.\n");
232  break;
233  case EM_CONSTANT_FOR:
234  message.append("Constant variable definition inside for loop.\n");
235  break;
237  message.append("Definition not allowed in MicroModelica.\n");
238  break;
239  case EM_CLASS_DEFINITION:
240  message.append("Class definition.\n");
241  break;
243  message.append("Annotation not found.\n");
244  break;
246  message.append("Function definition not found.\n");
247  break;
249  message.append("Wrong variable type.\n");
250  break;
251  default:
252  break;
253  }
254  return message;
255 }
256 
257 int Error::errors() { return _num_errors; }
258 } // namespace Util
259 } // namespace MicroModelica
EM_DEFINITION_NOT_ALLOWED
#define EM_DEFINITION_NOT_ALLOWED
Definition: error.hpp:110
EM_PP
#define EM_PP
Definition: error.hpp:68
EM_WRONG_TYPE
#define EM_WRONG_TYPE
Definition: error.hpp:76
EM_ANNOTATION_NOT_FOUND
#define EM_ANNOTATION_NOT_FOUND
Definition: error.hpp:117
EM_OUTPUT_DEF
#define EM_OUTPUT_DEF
Definition: error.hpp:105
EM_ARRAY_CTE
#define EM_ARRAY_CTE
Definition: error.hpp:84
EM_UNKNOWN_OPER
#define EM_UNKNOWN_OPER
Definition: error.hpp:78
EM_EVENT_DEF
#define EM_EVENT_DEF
Definition: error.hpp:96
EM_FOR_VAR
#define EM_FOR_VAR
Definition: error.hpp:89
MicroModelica::Util::Error::_errors
std::map< unsigned int, std::string > _errors
Definition: error.hpp:147
EM_CALL_EXP
#define EM_CALL_EXP
Definition: error.hpp:91
EM_ARRAY_SIZE
#define EM_ARRAY_SIZE
Definition: error.hpp:85
MicroModelica::Util::Error::setFile
void setFile(std::string s)
Definition: error.cpp:34
EM_UNKNOWN_TYPE
#define EM_UNKNOWN_TYPE
Definition: error.hpp:81
MicroModelica::Util::Error::file
std::string file()
Definition: error.cpp:36
MicroModelica::Util::Error::_std_def
std::string _std_def
Definition: error.hpp:151
EM_VARIABLE_NOT_DEFINED
#define EM_VARIABLE_NOT_DEFINED
Definition: error.hpp:75
EM_ARGUMENTS
#define EM_ARGUMENTS
Definition: error.hpp:106
EM_ALG_INIT
#define EM_ALG_INIT
Definition: error.hpp:86
MicroModelica::Util::ER_Fatal
@ ER_Fatal
ER_Fatal.
Definition: error.hpp:123
MicroModelica::Util::Error::_warnings
std::map< unsigned int, std::string > _warnings
Definition: error.hpp:148
EM_RANGE_EXP
#define EM_RANGE_EXP
Definition: error.hpp:92
EM_CG
#define EM_CG
Definition: error.hpp:67
MicroModelica::Util::Error::className
std::string className()
Definition: error.cpp:40
EM_ARRAY_INIT
#define EM_ARRAY_INIT
Definition: error.hpp:82
EM_NO_EQ
#define EM_NO_EQ
Definition: error.hpp:100
EM_HANDLER
#define EM_HANDLER
Definition: error.hpp:108
EM_PARSE_FILE
#define EM_PARSE_FILE
Definition: error.hpp:73
MicroModelica::Util::Error::_num_warnings
int _num_warnings
Definition: error.hpp:150
EM_INDEX_RANGE
#define EM_INDEX_RANGE
Definition: error.hpp:93
MicroModelica::Util::ER_Error
@ ER_Error
ER_Error.
Definition: error.hpp:122
EM_CLASS_DEFINITION
#define EM_CLASS_DEFINITION
Definition: error.hpp:111
EM_UNKNOWN_EXP
#define EM_UNKNOWN_EXP
Definition: error.hpp:80
EM_MODULE
#define EM_MODULE
Definition: error.hpp:63
EM_EVENT_FOUND
#define EM_EVENT_FOUND
Definition: error.hpp:102
MicroModelica::Util::Error::printCode
std::string printCode(int code)
Definition: error.cpp:93
MicroModelica::Util::Error::_class_name
std::string _class_name
Definition: error.hpp:152
MicroModelica::Util::Error::show
void show()
Definition: error.cpp:66
MicroModelica::Util::ER_Warning
@ ER_Warning
ER_Warning.
Definition: error.hpp:121
MicroModelica::Util::Error::setClassName
void setClassName(std::string class_name)
Definition: error.cpp:38
MicroModelica::Util::Error::add
void add(int pos, unsigned int code, ER_Type t, const std::string message,...)
Definition: error.cpp:42
EM_WRONG_VARIABLE_TYPE
#define EM_WRONG_VARIABLE_TYPE
Definition: error.hpp:114
MicroModelica::Util::Error::errors
int errors()
Definition: error.cpp:257
EM_VARIABLE_NOT_FOUND
#define EM_VARIABLE_NOT_FOUND
Definition: error.hpp:71
MicroModelica::Util::Error::_num_errors
int _num_errors
Definition: error.hpp:149
EM_CANT_OPEN_FILE
#define EM_CANT_OPEN_FILE
Definition: error.hpp:72
EM_SAMPLED_DEF
#define EM_SAMPLED_DEF
Definition: error.hpp:104
MicroModelica::Util::ER_Type
ER_Type
Definition: error.hpp:120
EM_FOR_DEF
#define EM_FOR_DEF
Definition: error.hpp:90
EM_UNKNOWN_ERROR
#define EM_UNKNOWN_ERROR
Definition: error.hpp:103
EM_MESSAGE
#define EM_MESSAGE
Definition: error.hpp:64
EM_FUNCTION_NOT_FOUND
#define EM_FUNCTION_NOT_FOUND
Definition: error.hpp:116
EM_SYMBOL
#define EM_SYMBOL
Definition: error.hpp:97
EM_IR
#define EM_IR
Definition: error.hpp:66
MicroModelica
Definition: files.cpp:45
EM_GRM
#define EM_GRM
Definition: error.hpp:70
EM_EQ_DEF
#define EM_EQ_DEF
Definition: error.hpp:99
EM_ALG_INDEX
#define EM_ALG_INDEX
Definition: error.hpp:87
MicroModelica::Util::Error::typeString
std::string typeString(ER_Type t)
Definition: error.cpp:80
EM_UNKNOWN_ODE
#define EM_UNKNOWN_ODE
Definition: error.hpp:101
EM_WRONG_EXP
#define EM_WRONG_EXP
Definition: error.hpp:77
EM_AST
#define EM_AST
Definition: error.hpp:65
EM_ERROR
#define EM_ERROR
Definition: error.hpp:69
EM_SYM_DER
#define EM_SYM_DER
Definition: error.hpp:98
EM_SAMPLED_OUT
#define EM_SAMPLED_OUT
Definition: error.hpp:107
EM_WRONG_SCANNER
#define EM_WRONG_SCANNER
Definition: error.hpp:74
EM_SIZE_ZERO
#define EM_SIZE_ZERO
Definition: error.hpp:94
EM_UNKNOWN_OPERATION
#define EM_UNKNOWN_OPERATION
Definition: error.hpp:79
EM_CONSTANT_FOR
#define EM_CONSTANT_FOR
Definition: error.hpp:109
EM_GEN_DEF
#define EM_GEN_DEF
Definition: error.hpp:95
error.hpp
EM_ARRAY_INIT_VAL
#define EM_ARRAY_INIT_VAL
Definition: error.hpp:83
EM_OUTPUT_INIT
#define EM_OUTPUT_INIT
Definition: error.hpp:88