Documentation of CSL
error.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
23 #ifndef ERROR_H_INCLUDED
24 #define ERROR_H_INCLUDED
25 
26 #include <iostream>
27 #include <string_view>
28 #include <csignal>
29 #include "options.h"
30 
31 namespace cslError{
32 
36  enum Error{
72  };
73 
77  enum Warning{
88  };
89 
90 }
91 
92 namespace csl {
93 
94 #ifdef DEBUG
95 
100 constexpr static const bool STOP_ERROR_DEBUG_MODE = true;
101 #else
102 constexpr static const bool STOP_ERROR_DEBUG_MODE = true;
103 #endif
104 
114 template<typename T>
115 inline void callError(cslError::Error error, const std::string& caller, T spec)
116 {
117  using namespace cslError;
118  std::cout<<"Error "<<(int)error<<": ";
119  switch(error)
120  {
121  case AbstractFuncCalled:
122  std::cout<<"Abstract function \""<<caller<<"\" called. Should not be, virtual function.\n"; break;
123  case BadCopyConstructor:
124  std::cout<<"Copy constructor called with wrong type in "<<caller<<": "<<spec<<".\n"; break;
125  case OutOfBounds:
126  std::cout<<"Index "<<spec<<" out of bounds in "<<caller<<".\n"; break;
127  case ElementSequence:
128  std::cout<<"Initializing a vector element with a sequence in "<<caller<<".\n"; break;
129  case EvalNotValued:
130  std::cout<<"Evaluating the value of non valued Abstract in "<<caller<<".\n"; break;
131  case UndefinedBehaviour:
132  std::cout<<"Undefined behaviour in "<<caller<<"\n."; break;
133  case ContractDummy:
134  std::cout<<"Contracting dummy index in "<<caller<<": "<<spec<<std::endl; break;
135  case ContractionMismatch:
136  std::cout<<"Mismatch in contraction ("<<caller<<"): "<<spec<<std::endl; break;
137  case SymmetryMismatch:
138  std::cout<<"Mismatch in symmetry in "<<caller<<".\n"; break;
139  case UnknownProperty:
140  std::cout<<"Unknown property in "<<caller<<": "<<spec<<std::endl; break;
141  case InvalidITensor:
142  std::cout<<"Bad set of indices in creation of TensorElement in "<<caller<<std::endl; break;
144  std::cout<<"Invalid initialization of TensorParent in "<<caller<<": bad tensor.\n"; break;
145  case InvalidIndicialSum:
146  std::cout<<"Invalid sum of indicial objects in "<<caller<<std::endl; break;
147  case BadDependency:
148  std::cout<<"Bad dependency set for "<<spec<<" in "<<caller<<"\n"; break;
149  case BadType:
150  std::cout<<"Bad type of expression sent in "<<caller<<": requires "<<spec<<".\n"; break;
151  case BadSymmetry :
152  std::cout<<"Bad symmetry set in TensorParent (wrong dimension) in "<<caller<<".\n"; break;
153  case BadContraction:
154  std::cout<<"Bad contraction of indices in "<<caller<<": "<<spec<<".\n"; break;
155  case UnknownEquation:
156  std::cout<<"Unknown type of equation in "<<caller<<": "<<spec<<".\n"; break;
157  case BadPropertySetting:
158  std::cout<<"Setting a property with something else than an equality in "<<caller<<": "<<spec<<".\n"; break;
159  case ArbitraryEvaluated:
160  std::cout<<"An arbitrary abstract has been evaluated from "<<caller<<". Should not be.\n"; break;
161  case ComparingNotDummy:
162  std::cout<<"Comparing non-dummy Abstract in "<<caller<<".\n"; break;
163  case BadCommutationProp:
164  std::cout<<"Setting commutability property of an object for which it is not possible in "<<caller<<".\n"; break;
166  std::cout<<"Negative dimension encountered in "<<caller<<": "<<spec<<std::endl; break;
168  std::cout<<"Calling a special contraction of TensorElement in "<<caller<<", but there is no contraction.\n"; break;
169  case BadSelfContraction:
170  std::cout<<"Initializing badly a self-contraction property of TensorParent in "<<caller<<".\n"; break;
172  std::cout<<"Bas initialization of field in "<<caller<<": need a vector of the right space.\n"; break;
173 
175  std::cout<<"Rules of the operator are not implemented in "<<caller<<".\n"; break;
176  case BadOperator:
177  std::cout<<"Bad initialization of operator in "<<caller<<".\n"; break;
178 
179  default:
180  std::cout<<"SmError "<<(int)error<<" not recognized.\n";
181  }
182  if (STOP_ERROR_DEBUG_MODE)
183  std::raise(SIGSEGV);
184  else if (option::errorStopsProgram)
185  exit((int)error);
186 }
190 void callError(cslError::Error error, const std::string& caller);
191 
201 template<typename T>
202 inline void callWarning(cslError::Warning warning, const std::string& caller, T spec)
203 {
204  using namespace cslError;
205  std::cout<<"Warning "<<(int)warning<<": ";
206  switch(warning)
207  {
208  case UnknownType:
209  std::cout<<"ScmType "<<" unknown in function \""<<caller<<"\".\n"; break;
210  case InvalidDimension:
211  std::cout<<"Invalid dimension "<<spec<<" in function "<<caller<<".\n"; break;
212  case FactorialFloat:
213  std::cout<<"Float value encountered in Factorial. Replacing by rounded value.\n"; break;
214  case NotValued:
215  std::cout<<"Evaluating non valued in "<<caller<<". Replacing by 0.\n"; break;
216  case Infinity:
217  std::cout<<"Infinite result found in "<<caller<<".\n"; break;
219  std::cout<<"Copying building block in "<<caller<<": "<<spec<<"\n"; break;
221  std::cout<<"The function getNCharacters() is not implemented for type "<<spec<<" in "<<caller<<".\n"; break;
222 
223  default:
224  std::cout<<"SmWarning "<<(int)warning<<" not recognized.\n";
225  }
226 }
227 
228 } // End of namespace csl
229 
230 
231 enum class CSLError{
232 
233  AbstractCallError, //Raised when an abstract (not implemented) fucntion is called.
234  AttributeError, //Raised when attribute assignment or reference fails.
235  EOFError, //Raised when the input() functions hits end-of-file condition.
236  FloatingPointError, //Raised when a floating point operation fails.
237  GeneratorExit, //Raise when a generator's close() method is called.
238  IndexError, //Raised when index of a sequence is out of range.
239  IOError, //Raised when a file is not found.
240  KeyError, //Raised when a key is not found in a dictionary.
241  MathError,
242  NameError, //Raised when a variable is not found in local or global scope.
243  NotImplementedError,//Raised by abstract methods.
244  OverflowError, //Raised when result of an arithmetic operation is too large to be represented.
245  RuntimeError, //Raised when an error does not fall under any other category.
246  SyntaxError, //Raised by parser when syntax error is encountered.
247  TypeError, //Raised when a function or operation is applied to an object of incorrect type.
248  ValueError, //Raised when a function gets argument of correct type but improper value.
249  ZeroDivisionError //Raised when second operand of division or modulo operation is zero.
250 };
251 
252 std::ostream& operator<<(std::ostream& fout, CSLError error);
253 
254 void CallErrorImpl(CSLError error,
255  std::string_view func,
256  std::string_view file,
257  int line);
258 
259 void CallErrorImpl(CSLError error,
260  std::string_view func,
261  std::string_view file,
262  int line,
263  std::string_view message);
264 
265 #define CSL_ASSERT(cond, error)\
266  if (not (cond)) {\
267  CallErrorImpl(error, __func__, __FILE__, __LINE__);\
268  }
269 #define CSL_ASSERT_SPEC(cond, error, message)\
270  if (not (cond)) {\
271  CallErrorImpl(error, __func__, __FILE__, __LINE__, message);\
272  }
273 
274 #define CALL_SMERROR(error)\
275  CallErrorImpl(error, __func__, __FILE__, __LINE__)
276 
277 #define CALL_SMERROR_SPEC(error, message)\
278  CallErrorImpl(error, __func__, __FILE__, __LINE__, message)
279 
280 
281 #endif
Definition: error.h:47
Definition: error.h:84
Namespace for csl library.
Definition: abreviation.h:34
Definition: error.h:37
Definition: error.h:40
Definition: error.h:69
Definition: error.h:80
Definition: error.h:65
Definition: error.h:83
Definition: error.h:46
Definition: error.h:59
Definition: error.h:54
Definition: error.h:67
Definition: error.h:58
Definition: error.h:61
Definition: error.h:56
Definition: error.h:31
Definition: error.h:81
Definition: error.h:45
Definition: error.h:44
Definition: error.h:53
Definition: error.h:52
Definition: error.h:82
Definition: error.h:70
Definition: error.h:64
Warning
List of all warning possible.
Definition: error.h:77
Definition: error.h:50
Definition: error.h:63
Definition: error.h:66
void callError(cslError::Error error, const std::string &caller)
Calls callError(cslError::Error, const std::string&,T) with no spec.
Definition: error.cpp:20
Definition: error.h:48
Definition: error.h:39
Definition: error.h:68
Definition: error.h:62
void callWarning(cslError::Warning warning, const std::string &caller, T spec)
Displays a warning message depending on the warning warning, the name of the caller function caller a...
Definition: error.h:202
Definition: error.h:78
Definition: error.h:57
Definition: error.h:42
Definition: error.h:60
Error
List all error possible.
Definition: error.h:36