1 /* -*- mode: C -*- */ 2 /* 3 IGraph library. 4 Copyright (C) 2003-2012 Gabor Csardi <csardi.gabor@gmail.com> 5 334 Harvard street, Cambridge, MA 02139 USA 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 2 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; if not, write to the Free Software 19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 20 02110-1301 USA 21 22 */ 23 24 #ifndef IGRAPH_ERROR_H 25 #define IGRAPH_ERROR_H 26 27 #include "igraph_decls.h" 28 29 #include <stdarg.h> 30 31 __BEGIN_DECLS 32 33 /* This file contains the igraph error handling. 34 * Most bits are taken literally from the GSL library (with the GSL_ 35 * prefix renamed to IGRAPH_), as I couldn't find a better way to do 36 * them. */ 37 38 /* IGRAPH_NORETURN indicates to the compiler that a function does not return. 39 * There are standard facilities for this, namely _Noreturn in C11 and [[noreturn]] in C++11. 40 * However, since igraph is currently compiled with older standards, and since 41 * the standard 'noreturn' specification would need to be diferent between C and C++, 42 * we do not use these facilities. 43 */ 44 #if defined(__GNUC__) 45 /* Compilers that support the GNU C syntax. Use __noreturn__ instead of 'noreturn' as the latter is a macro in C11. */ 46 #define IGRAPH_NORETURN __attribute__((__noreturn__)) 47 #elif defined(_MSC_VER) 48 /* Compilers that support the MSVC syntax. */ 49 #define IGRAPH_NORETURN __declspec(noreturn) 50 #else 51 #define IGRAPH_NORETURN 52 #endif 53 54 /** 55 * \section error_handling_basics Error handling basics 56 * 57 * <para>\a igraph functions can run into various problems preventing them 58 * from normal operation. The user might have supplied invalid arguments, 59 * e.g. a non-square matrix when a square-matrix was expected, or the program 60 * has run out of memory while some more memory allocation is required, etc. 61 * </para> 62 * 63 * <para>By default \a igraph aborts the program when it runs into an 64 * error. While this behavior might be good enough for smaller programs, 65 * it is without doubt avoidable in larger projects. Please read further 66 * if your project requires more sophisticated error handling. You can 67 * safely skip the rest of this chapter otherwise. 68 * </para> 69 */ 70 71 /** 72 * \section error_handlers Error handlers 73 * 74 * <para> 75 * If \a igraph runs into an error - an invalid argument was supplied 76 * to a function, or we've ran out of memory - the control is 77 * transferred to the \emb error handler \eme function. 78 * </para><para> 79 * The default error handler is \ref igraph_error_handler_abort which 80 * prints an error message and aborts the program. 81 * </para> 82 * <para> 83 * The \ref igraph_set_error_handler() function can be used to set a new 84 * error handler function of type \ref igraph_error_handler_t; see the 85 * documentation of this type for details. 86 * </para> 87 * <para> 88 * There are two other predefined error handler functions, 89 * \ref igraph_error_handler_ignore and \ref igraph_error_handler_printignore. 90 * These deallocate the temporarily allocated memory (more about this 91 * later) and return with the error code. The latter also prints an 92 * error message. If you use these error handlers you need to take 93 * care about possible errors yourself by checking the return value of 94 * (almost) every non-void \a igraph function. 95 * </para><para> 96 * Independently of the error handler installed, all functions in the 97 * library do their best to leave their arguments 98 * \em semantically unchanged if an error 99 * happens. By semantically we mean that the implementation of an 100 * object supplied as an argument might change, but its 101 * \quote meaning \endquote in most cases does not. The rare occasions 102 * when this rule is violated are documented in this manual. 103 * </para> 104 */ 105 106 /** 107 * \section error_codes Error codes 108 * 109 * <para>Every \a igraph function which can fail return a 110 * single integer error code. Some functions are very simple and 111 * cannot run into any error, these may return other types, or 112 * \type void as well. The error codes are defined by the 113 * \ref igraph_error_type_t enumeration. 114 * </para> 115 */ 116 117 /** 118 * \section writing_error_handlers Writing error handlers 119 * 120 * <para> 121 * The contents of the rest of this chapter might be useful only 122 * for those who want to create an interface to \a igraph from another 123 * language. Most readers can safely skip to the next chapter. 124 * </para> 125 * 126 * <para> 127 * You can write and install error handlers simply by defining a 128 * function of type \ref igraph_error_handler_t and calling 129 * \ref igraph_set_error_handler(). This feature is useful for interface 130 * writers, as \a igraph will have the chance to 131 * signal errors the appropriate way, e.g. the R interface defines an 132 * error handler which calls the <function>error()</function> 133 * function, as required by R, while the Python interface has an error 134 * handler which raises an exception according to the Python way. 135 * </para> 136 * <para> 137 * If you want to write an error handler, your error handler should 138 * call \ref IGRAPH_FINALLY_FREE() to deallocate all temporary memory to 139 * prevent memory leaks. 140 * </para> 141 */ 142 143 /** 144 * \section error_handling_internals Error handling internals 145 * 146 * <para> 147 * If an error happens, the functions in the library call the 148 * \ref IGRAPH_ERROR() macro with a textual description of the error and an 149 * \a igraph error code. This macro calls (through the \ref 150 * igraph_error() function) the installed error handler. Another useful 151 * macro is \ref IGRAPH_CHECK(). This checks the return value of its 152 * argument, which is normally a function call, and calls \ref 153 * IGRAPH_ERROR() if it is not \c IGRAPH_SUCCESS. 154 * </para> 155 */ 156 157 /** 158 * \section deallocating_memory Deallocating memory 159 * 160 * <para> 161 * If a function runs into an error (and the program is not aborted) 162 * the error handler should deallocate all temporary memory. This is 163 * done by storing the address and the destroy function of all temporary 164 * objects in a stack. The \ref IGRAPH_FINALLY function declares an object as 165 * temporary by placing its address in the stack. If an \a igraph function returns 166 * with success it calls \ref IGRAPH_FINALLY_CLEAN() with the 167 * number of objects to remove from the stack. If an error happens 168 * however, the error handler should call \ref IGRAPH_FINALLY_FREE() to 169 * deallocate each object added to the stack. This means that the 170 * temporary objects allocated in the calling function (and etc.) will 171 * be freed as well. 172 * </para> 173 */ 174 175 /** 176 * \section writing_functions_error_handling Writing \a igraph functions with 177 * proper error handling 178 * 179 * <para> 180 * There are some simple rules to keep in order to have functions 181 * behaving well in erroneous situations. First, check the arguments 182 * of the functions and call \ref IGRAPH_ERROR() if they are invalid. Second, 183 * call \ref IGRAPH_FINALLY on each dynamically allocated object and call 184 * \ref IGRAPH_FINALLY_CLEAN() with the proper argument before returning. Third, use 185 * \ref IGRAPH_CHECK on all \a igraph function calls which can generate errors. 186 * </para> 187 * <para> 188 * The size of the stack used for this bookkeeping is fixed, and 189 * small. If you want to allocate several objects, write a destroy 190 * function which can deallocate all of these. See the 191 * <filename>adjlist.c</filename> file in the 192 * \a igraph source for an example. 193 * </para> 194 * <para> 195 * For some functions these mechanisms are simply not flexible 196 * enough. These functions should define their own error handlers and 197 * restore the error handler before they return. 198 * </para> 199 */ 200 201 /** 202 * \section error_handling_threads Error handling and threads 203 * 204 * <para> 205 * It is likely that the \a igraph error handling 206 * method is \em not thread-safe, mainly because of 207 * the static global stack which is used to store the address of the 208 * temporarily allocated objects. This issue might be addressed in a 209 * later version of \a igraph. 210 * </para> 211 */ 212 213 /** 214 * \typedef igraph_error_handler_t 215 * \brief The type of error handler functions. 216 * 217 * This is the type of the error handler functions. 218 * \param reason Textual description of the error. 219 * \param file The source file in which the error is noticed. 220 * \param line The number of the line in the source file which triggered 221 * the error 222 * \param igraph_errno The \a igraph error code. 223 */ 224 225 typedef void igraph_error_handler_t (const char * reason, const char * file, 226 int line, int igraph_errno); 227 228 /** 229 * \var igraph_error_handler_abort 230 * \brief Abort program in case of error. 231 * 232 * The default error handler, prints an error message and aborts the 233 * program. 234 */ 235 236 IGRAPH_EXPORT igraph_error_handler_t igraph_error_handler_abort; 237 238 /** 239 * \var igraph_error_handler_ignore 240 * \brief Ignore errors. 241 * 242 * This error handler frees the temporarily allocated memory and returns 243 * with the error code. 244 */ 245 246 IGRAPH_EXPORT igraph_error_handler_t igraph_error_handler_ignore; 247 248 /** 249 * \var igraph_error_handler_printignore 250 * \brief Print and ignore errors. 251 * 252 * Frees temporarily allocated memory, prints an error message to the 253 * standard error and returns with the error code. 254 */ 255 256 IGRAPH_EXPORT igraph_error_handler_t igraph_error_handler_printignore; 257 258 /** 259 * \function igraph_set_error_handler 260 * \brief Sets a new error handler. 261 * 262 * Installs a new error handler. If called with 0, it installs the 263 * default error handler (which is currently 264 * \ref igraph_error_handler_abort). 265 * \param new_handler The error handler function to install. 266 * \return The old error handler function. This should be saved and 267 * restored if \p new_handler is not needed any 268 * more. 269 */ 270 271 IGRAPH_EXPORT igraph_error_handler_t* igraph_set_error_handler(igraph_error_handler_t* new_handler); 272 273 /** 274 * \typedef igraph_error_type_t 275 * \brief Error code type. 276 * These are the possible values returned by \a igraph functions. 277 * Note that these are interesting only if you defined an error handler 278 * with \ref igraph_set_error_handler(). Otherwise the program is aborted 279 * and the function causing the error never returns. 280 * 281 * \enumval IGRAPH_SUCCESS The function successfully completed its task. 282 * \enumval IGRAPH_FAILURE Something went wrong. You'll almost never 283 * meet this error as normally more specific error codes are used. 284 * \enumval IGRAPH_ENOMEM There wasn't enough memory to allocate 285 * on the heap. 286 * \enumval IGRAPH_PARSEERROR A parse error was found in a file. 287 * \enumval IGRAPH_EINVAL A parameter's value is invalid. E.g. negative 288 * number was specified as the number of vertices. 289 * \enumval IGRAPH_EXISTS A graph/vertex/edge attribute is already 290 * installed with the given name. 291 * \enumval IGRAPH_EINVEVECTOR Invalid vector of vertex ids. A vertex id 292 * is either negative or bigger than the number of vertices minus one. 293 * \enumval IGRAPH_EINVVID Invalid vertex id, negative or too big. 294 * \enumval IGRAPH_NONSQUARE A non-square matrix was received while a 295 * square matrix was expected. 296 * \enumval IGRAPH_EINVMODE Invalid mode parameter. 297 * \enumval IGRAPH_EFILE A file operation failed. E.g. a file doesn't exist, 298 * or the user has no rights to open it. 299 * \enumval IGRAPH_UNIMPLEMENTED Attempted to call an unimplemented or 300 * disabled (at compile-time) function. 301 * \enumval IGRAPH_DIVERGED A numeric algorithm failed to converge. 302 * \enumval IGRAPH_ARPACK_PROD Matrix-vector product failed. 303 * \enumval IGRAPH_ARPACK_NPOS N must be positive. 304 * \enumval IGRAPH_ARPACK_NEVNPOS NEV must be positive. 305 * \enumval IGRAPH_ARPACK_NCVSMALL NCV must be bigger. 306 * \enumval IGRAPH_ARPACK_NONPOSI Maximum number of iterations should be positive. 307 * \enumval IGRAPH_ARPACK_WHICHINV Invalid WHICH parameter. 308 * \enumval IGRAPH_ARPACK_BMATINV Invalid BMAT parameter. 309 * \enumval IGRAPH_ARPACK_WORKLSMALL WORKL is too small. 310 * \enumval IGRAPH_ARPACK_TRIDERR LAPACK error in tridiagonal eigenvalue calculation. 311 * \enumval IGRAPH_ARPACK_ZEROSTART Starting vector is zero. 312 * \enumval IGRAPH_ARPACK_MODEINV MODE is invalid. 313 * \enumval IGRAPH_ARPACK_MODEBMAT MODE and BMAT are not compatible. 314 * \enumval IGRAPH_ARPACK_ISHIFT ISHIFT must be 0 or 1. 315 * \enumval IGRAPH_ARPACK_NEVBE NEV and WHICH='BE' are incompatible. 316 * \enumval IGRAPH_ARPACK_NOFACT Could not build an Arnoldi factorization. 317 * \enumval IGRAPH_ARPACK_FAILED No eigenvalues to sufficient accuracy. 318 * \enumval IGRAPH_ARPACK_HOWMNY HOWMNY is invalid. 319 * \enumval IGRAPH_ARPACK_HOWMNYS HOWMNY='S' is not implemented. 320 * \enumval IGRAPH_ARPACK_EVDIFF Different number of converged Ritz values. 321 * \enumval IGRAPH_ARPACK_SHUR Error from calculation of a real Schur form. 322 * \enumval IGRAPH_ARPACK_LAPACK LAPACK (dtrevc) error for calculating eigenvectors. 323 * \enumval IGRAPH_ARPACK_UNKNOWN Unknown ARPACK error. 324 * \enumval IGRAPH_ENEGLOOP Negative loop detected while calculating shortest paths. 325 * \enumval IGRAPH_EINTERNAL Internal error, likely a bug in igraph. 326 * \enumval IGRAPH_EDIVZERO Big integer division by zero. 327 * \enumval IGRAPH_GLP_EBOUND GLPK error (GLP_EBOUND). 328 * \enumval IGRAPH_GLP_EROOT GLPK error (GLP_EROOT). 329 * \enumval IGRAPH_GLP_ENOPFS GLPK error (GLP_ENOPFS). 330 * \enumval IGRAPH_GLP_ENODFS GLPK error (GLP_ENODFS). 331 * \enumval IGRAPH_GLP_EFAIL GLPK error (GLP_EFAIL). 332 * \enumval IGRAPH_GLP_EMIPGAP GLPK error (GLP_EMIPGAP). 333 * \enumval IGRAPH_GLP_ETMLIM GLPK error (GLP_ETMLIM). 334 * \enumval IGRAPH_GLP_ESTOP GLPK error (GLP_ESTOP). 335 * \enumval IGRAPH_EATTRIBUTES Attribute handler error. The user is not 336 * expected to find this; it is signalled if some igraph function is 337 * not using the attribute handler interface properly. 338 * \enumval IGRAPH_EATTRCOMBINE Unimplemented attribute combination 339 * method for the given attribute type. 340 * \enumval IGRAPH_ELAPACK A LAPACK call resulted in an error. 341 * \enumval IGRAPH_EDRL Internal error in the DrL layout generator. 342 * \enumval IGRAPH_EOVERFLOW Integer or double overflow. 343 * \enumval IGRAPH_EGLP Internal GLPK error. 344 * \enumval IGRAPH_CPUTIME CPU time exceeded. 345 * \enumval IGRAPH_EUNDERFLOW Integer or double underflow. 346 * \enumval IGRAPH_ERWSTUCK Random walk got stuck. 347 */ 348 349 typedef enum { 350 IGRAPH_SUCCESS = 0, 351 IGRAPH_FAILURE = 1, 352 IGRAPH_ENOMEM = 2, 353 IGRAPH_PARSEERROR = 3, 354 IGRAPH_EINVAL = 4, 355 IGRAPH_EXISTS = 5, 356 IGRAPH_EINVEVECTOR = 6, 357 IGRAPH_EINVVID = 7, 358 IGRAPH_NONSQUARE = 8, 359 IGRAPH_EINVMODE = 9, 360 IGRAPH_EFILE = 10, 361 IGRAPH_UNIMPLEMENTED = 12, 362 IGRAPH_INTERRUPTED = 13, 363 IGRAPH_DIVERGED = 14, 364 IGRAPH_ARPACK_PROD = 15, 365 IGRAPH_ARPACK_NPOS = 16, 366 IGRAPH_ARPACK_NEVNPOS = 17, 367 IGRAPH_ARPACK_NCVSMALL = 18, 368 IGRAPH_ARPACK_NONPOSI = 19, 369 IGRAPH_ARPACK_WHICHINV = 20, 370 IGRAPH_ARPACK_BMATINV = 21, 371 IGRAPH_ARPACK_WORKLSMALL = 22, 372 IGRAPH_ARPACK_TRIDERR = 23, 373 IGRAPH_ARPACK_ZEROSTART = 24, 374 IGRAPH_ARPACK_MODEINV = 25, 375 IGRAPH_ARPACK_MODEBMAT = 26, 376 IGRAPH_ARPACK_ISHIFT = 27, 377 IGRAPH_ARPACK_NEVBE = 28, 378 IGRAPH_ARPACK_NOFACT = 29, 379 IGRAPH_ARPACK_FAILED = 30, 380 IGRAPH_ARPACK_HOWMNY = 31, 381 IGRAPH_ARPACK_HOWMNYS = 32, 382 IGRAPH_ARPACK_EVDIFF = 33, 383 IGRAPH_ARPACK_SHUR = 34, 384 IGRAPH_ARPACK_LAPACK = 35, 385 IGRAPH_ARPACK_UNKNOWN = 36, 386 IGRAPH_ENEGLOOP = 37, 387 IGRAPH_EINTERNAL = 38, 388 IGRAPH_ARPACK_MAXIT = 39, 389 IGRAPH_ARPACK_NOSHIFT = 40, 390 IGRAPH_ARPACK_REORDER = 41, 391 IGRAPH_EDIVZERO = 42, 392 IGRAPH_GLP_EBOUND = 43, 393 IGRAPH_GLP_EROOT = 44, 394 IGRAPH_GLP_ENOPFS = 45, 395 IGRAPH_GLP_ENODFS = 46, 396 IGRAPH_GLP_EFAIL = 47, 397 IGRAPH_GLP_EMIPGAP = 48, 398 IGRAPH_GLP_ETMLIM = 49, 399 IGRAPH_GLP_ESTOP = 50, 400 IGRAPH_EATTRIBUTES = 51, 401 IGRAPH_EATTRCOMBINE = 52, 402 IGRAPH_ELAPACK = 53, 403 IGRAPH_EDRL = 54, 404 IGRAPH_EOVERFLOW = 55, 405 IGRAPH_EGLP = 56, 406 IGRAPH_CPUTIME = 57, 407 IGRAPH_EUNDERFLOW = 58, 408 IGRAPH_ERWSTUCK = 59, 409 IGRAPH_STOP = 60 /* undocumented, used internally */ 410 } igraph_error_type_t; 411 /* Each enum value above must have a corresponding error string in 412 * igraph_i_error_strings[] in igraph_error.c 413 * 414 * Information on undocumented codes: 415 * - IGRAPH_STOP signals a request to stop in functions like igraph_i_maximal_cliques_bk() 416 */ 417 418 /* We use IGRAPH_FILE_BASENAME instead of __FILE__ to ensure that full 419 * paths don't leak into the library code. IGRAPH_FILE_BASENAME is set up 420 * by the build system when compiling the individual files. However, when 421 * including igraph_error.h in user code, this macro is not defined so we 422 * fall back to __FILE__ here 423 */ 424 #ifndef IGRAPH_FILE_BASENAME 425 # define IGRAPH_FILE_BASENAME __FILE__ 426 #endif 427 428 /** 429 * \define IGRAPH_ERROR 430 * \brief Trigger an error. 431 * 432 * \a igraph functions usually use this macro when they notice an error. 433 * It calls 434 * \ref igraph_error() with the proper parameters and if that returns 435 * the macro returns the "calling" function as well, with the error 436 * code. If for some (suspicious) reason you want to call the error 437 * handler without returning from the current function, call 438 * \ref igraph_error() directly. 439 * \param reason Textual description of the error. This should be 440 * something more descriptive than the text associated with the error 441 * code. E.g. if the error code is \c IGRAPH_EINVAL, 442 * its associated text (see \ref igraph_strerror()) is "Invalid 443 * value" and this string should explain which parameter was invalid 444 * and maybe why. 445 * \param igraph_errno The \a igraph error code. 446 */ 447 448 #define IGRAPH_ERROR(reason, igraph_errno) \ 449 do { \ 450 igraph_error (reason, IGRAPH_FILE_BASENAME, __LINE__, igraph_errno) ; \ 451 return igraph_errno ; \ 452 } while (0) 453 454 #define IGRAPH_ERROR_NO_RETURN(reason, igraph_errno) \ 455 do { \ 456 igraph_error (reason, IGRAPH_FILE_BASENAME, __LINE__, igraph_errno) ; \ 457 } while (0) 458 459 /** 460 * \function igraph_error 461 * \brief Triggers an error. 462 * 463 * \a igraph functions usually call this function (most often via the 464 * \ref IGRAPH_ERROR macro) if they notice an error. 465 * It calls the currently installed error handler function with the 466 * supplied arguments. 467 * 468 * \param reason Textual description of the error. 469 * \param file The source file in which the error was noticed. 470 * \param line The number of line in the source file which triggered the 471 * error. 472 * \param igraph_errno The \a igraph error code. 473 * \return the error code (if it returns) 474 * 475 * \sa igraph_errorf(). 476 */ 477 478 IGRAPH_EXPORT int igraph_error(const char *reason, const char *file, int line, 479 int igraph_errno); 480 481 /** 482 * \define IGRAPH_ERRORF 483 * \brief Triggers an error, with printf-like syntax. 484 * 485 * \a igraph functions can use this macro when they notice an error and 486 * want to pass on extra information to the user about what went wrong. 487 * It calls \ref igraph_errorf() with the proper parameters and if that 488 * returns the macro returns the "calling" function as well, with the 489 * error code. If for some (suspicious) reason you want to call the 490 * error handler without returning from the current function, call 491 * \ref igraph_errorf() directly. 492 * \param reason Textual description of the error, a template string 493 * with the same syntax as the standard printf C library function. 494 * This should be something more descriptive than the text associated 495 * with the error code. E.g. if the error code is \c IGRAPH_EINVAL, 496 * its associated text (see \ref igraph_strerror()) is "Invalid 497 * value" and this string should explain which parameter was invalid 498 * and maybe what was expected and what was recieved. 499 * \param igraph_errno The \a igraph error code. 500 * \param ... The additional arguments to be substituted into the 501 * template string. 502 */ 503 504 #define IGRAPH_ERRORF(reason, igraph_errno, ...) \ 505 do { \ 506 igraph_errorf(reason, IGRAPH_FILE_BASENAME, __LINE__, \ 507 igraph_errno, __VA_ARGS__) ; \ 508 return igraph_errno; \ 509 } while (0) 510 511 /** 512 * \function igraph_errorf 513 * \brief Triggers an error, printf-like version. 514 * 515 * \param reason Textual description of the error, interpreted as 516 * a \c printf format string. 517 * \param file The source file in which the error was noticed. 518 * \param line The line in the source file which triggered the error. 519 * \param igraph_errno The \a igraph error code. 520 * \param ... Additional parameters, the values to substitute into the 521 * format string. 522 * 523 * \sa igraph_error(). 524 */ 525 526 IGRAPH_EXPORT int igraph_errorf(const char *reason, const char *file, int line, 527 int igraph_errno, ...); 528 529 IGRAPH_EXPORT int igraph_errorvf(const char *reason, const char *file, int line, 530 int igraph_errno, va_list ap); 531 532 /** 533 * \function igraph_strerror 534 * \brief Textual description of an error. 535 * 536 * This is a simple utility function, it gives a short general textual 537 * description for an \a igraph error code. 538 * 539 * \param igraph_errno The \a igraph error code. 540 * \return pointer to the textual description of the error code. 541 */ 542 543 IGRAPH_EXPORT const char* igraph_strerror(const int igraph_errno); 544 545 #define IGRAPH_ERROR_SELECT_2(a,b) ((a) != IGRAPH_SUCCESS ? (a) : ((b) != IGRAPH_SUCCESS ? (b) : IGRAPH_SUCCESS)) 546 #define IGRAPH_ERROR_SELECT_3(a,b,c) ((a) != IGRAPH_SUCCESS ? (a) : IGRAPH_ERROR_SELECT_2(b,c)) 547 #define IGRAPH_ERROR_SELECT_4(a,b,c,d) ((a) != IGRAPH_SUCCESS ? (a) : IGRAPH_ERROR_SELECT_3(b,c,d)) 548 #define IGRAPH_ERROR_SELECT_5(a,b,c,d,e) ((a) != IGRAPH_SUCCESS ? (a) : IGRAPH_ERROR_SELECT_4(b,c,d,e)) 549 550 /* Now comes the more convenient error handling macro arsenal. 551 * Ideas taken from exception.{h,c} by Laurent Deniau see 552 * http://cern.ch/Laurent.Deniau/html/oopc/oopc.html#Exceptions for more 553 * information. We don't use the exception handling code though. */ 554 555 struct igraph_i_protectedPtr { 556 int all; 557 void *ptr; 558 void (*func)(void*); 559 }; 560 561 typedef void igraph_finally_func_t (void*); 562 563 IGRAPH_EXPORT void IGRAPH_FINALLY_REAL(void (*func)(void*), void* ptr); 564 565 /** 566 * \function IGRAPH_FINALLY_CLEAN 567 * \brief Signals clean deallocation of objects. 568 * 569 * Removes the specified number of objects from the stack of 570 * temporarily allocated objects. Most often this is called just 571 * before returning from a function. 572 * \param num The number of objects to remove from the bookkeeping 573 * stack. 574 */ 575 576 IGRAPH_EXPORT void IGRAPH_FINALLY_CLEAN(int num); 577 578 /** 579 * \function IGRAPH_FINALLY_FREE 580 * \brief Deallocates all registered objects. 581 * 582 * Calls the destroy function for all objects in the stack of 583 * temporarily allocated objects. This is usually called only from an 584 * error handler. It is \em not appropriate to use it 585 * instead of destroying each unneeded object of a function, as it 586 * destroys the temporary objects of the caller function (and so on) 587 * as well. 588 */ 589 590 IGRAPH_EXPORT void IGRAPH_FINALLY_FREE(void); 591 592 /** 593 * \function IGRAPH_FINALLY_STACK_SIZE 594 * \brief The number of registered objects. 595 * 596 * Returns the number of objects in the stack of temporarily allocated 597 * objects. This function is handy if you write an own igraph routine and 598 * you want to make sure it handles errors properly. A properly written 599 * igraph routine should not leave pointers to temporarily allocated objects 600 * in the finally stack, because otherwise an \ref IGRAPH_FINALLY_FREE call 601 * in another igraph function would result in freeing these objects as well 602 * (and this is really hard to debug, since the error will be not in that 603 * function that shows erroneous behaviour). Therefore, it is advised to 604 * write your own test cases and examine \ref IGRAPH_FINALLY_STACK_SIZE 605 * before and after your test cases - the numbers should be equal. 606 */ 607 IGRAPH_EXPORT int IGRAPH_FINALLY_STACK_SIZE(void); 608 609 /** 610 * \define IGRAPH_FINALLY_STACK_EMPTY 611 * \brief Returns true if there are no registered objects, false otherwise. 612 * 613 * This is just a shorthand notation for checking that 614 * \ref IGRAPH_FINALLY_STACK_SIZE() is zero. 615 */ 616 #define IGRAPH_FINALLY_STACK_EMPTY (IGRAPH_FINALLY_STACK_SIZE() == 0) 617 618 /** 619 * \define IGRAPH_FINALLY 620 * \brief Registers an object for deallocation. 621 * \param func The address of the function which is normally called to 622 * destroy the object. 623 * \param ptr Pointer to the object itself. 624 * 625 * This macro places the address of an object, together with the 626 * address of its destructor in a stack. This stack is used if an 627 * error happens to deallocate temporarily allocated objects to 628 * prevent memory leaks. 629 */ 630 631 #define IGRAPH_FINALLY(func, ptr) \ 632 do { \ 633 /* the following branch makes the compiler check the compatibility of \ 634 * func and ptr to detect cases when we are accidentally invoking an \ 635 * incorrect destructor function with the pointer */ \ 636 if (0) { func(ptr); } \ 637 IGRAPH_FINALLY_REAL((igraph_finally_func_t*)(func), (ptr)); \ 638 } while (0) 639 640 #if !defined(GCC_VERSION_MAJOR) && defined(__GNUC__) 641 #define GCC_VERSION_MAJOR __GNUC__ 642 #endif 643 644 #if defined(GCC_VERSION_MAJOR) && (GCC_VERSION_MAJOR >= 3) 645 #define IGRAPH_UNLIKELY(a) __builtin_expect((a), 0) 646 #define IGRAPH_LIKELY(a) __builtin_expect((a), 1) 647 #else 648 #define IGRAPH_UNLIKELY(a) a 649 #define IGRAPH_LIKELY(a) a 650 #endif 651 652 #if IGRAPH_VERIFY_FINALLY_STACK == 1 653 #define IGRAPH_CHECK(a) \ 654 do { \ 655 int enter_stack_size = IGRAPH_FINALLY_STACK_SIZE(); \ 656 int igraph_i_ret=(a); \ 657 if (IGRAPH_UNLIKELY(igraph_i_ret != 0)) {\ 658 IGRAPH_ERROR("", igraph_i_ret); \ 659 } \ 660 if (IGRAPH_UNLIKELY(enter_stack_size != IGRAPH_FINALLY_STACK_SIZE())) { \ 661 IGRAPH_ERROR("Non-matching number of IGRAPH_FINALLY and IGRAPH_FINALLY_CLEAN", IGRAPH_FAILURE); \ 662 } \ 663 } while (0) 664 #else 665 /** 666 * \define IGRAPH_CHECK 667 * \brief Checks the return value of a function call. 668 * 669 * \param a An expression, usually a function call. 670 * 671 * Executes the expression and checks its value. If this is not 672 * \c IGRAPH_SUCCESS, it calls \ref IGRAPH_ERROR with 673 * the value as the error code. Here is an example usage: 674 * \verbatim IGRAPH_CHECK(vector_push_back(&v, 100)); \endverbatim 675 * 676 * </para><para>There is only one reason to use this macro when writing 677 * \a igraph functions. If the user installs an error handler which 678 * returns to the auxiliary calling code (like \ref 679 * igraph_error_handler_ignore and \ref 680 * igraph_error_handler_printignore), and the \a igraph function 681 * signalling the error is called from another \a igraph function 682 * then we need to make sure that the error is propagated back to 683 * the auxiliary (i.e. non-igraph) calling function. This is achieved 684 * by using <function>IGRAPH_CHECK</function> on every \a igraph 685 * call which can return an error code. 686 */ 687 #define IGRAPH_CHECK(a) do { \ 688 int igraph_i_ret=(a); \ 689 if (IGRAPH_UNLIKELY(igraph_i_ret != 0)) {\ 690 IGRAPH_ERROR("", igraph_i_ret); \ 691 } } while (0) 692 #endif 693 694 695 696 /** 697 * \section about_igraph_warnings Warning messages 698 * 699 * <para> 700 * \a igraph also supports warning messages in addition to error 701 * messages. Warning messages typically do not terminate the 702 * program, but they are usually crucial to the user. 703 * </para> 704 * 705 * <para> 706 * \a igraph warnings are handled similarly to errors. There is a 707 * separate warning handler function that is called whenever 708 * an \a igraph function triggers a warning. This handler can be 709 * set by the \ref igraph_set_warning_handler() function. There are 710 * two predefined simple warning handlers, 711 * \ref igraph_warning_handler_ignore() and 712 * \ref igraph_warning_handler_print(), the latter being the default. 713 * </para> 714 * 715 * <para> 716 * To trigger a warning, \a igraph functions typically use the 717 * \ref IGRAPH_WARNING() macro, the \ref igraph_warning() function, 718 * or if more flexibility is needed, \ref igraph_warningf(). 719 * </para> 720 */ 721 722 /** 723 * \typedef igraph_warning_handler_t 724 * \brief The type of igraph warning handler functions. 725 * 726 * Currently it is defined to have the same type as 727 * \ref igraph_error_handler_t, although the last (error code) 728 * argument is not used. 729 */ 730 731 typedef igraph_error_handler_t igraph_warning_handler_t; 732 733 /** 734 * \function igraph_set_warning_handler 735 * \brief Installs a warning handler. 736 * 737 * Install the supplied warning handler function. 738 * \param new_handler The new warning handler function to install. 739 * Supply a null pointer here to uninstall the current 740 * warning handler, without installing a new one. 741 * \return The current warning handler function. 742 */ 743 744 IGRAPH_EXPORT igraph_warning_handler_t* igraph_set_warning_handler(igraph_warning_handler_t* new_handler); 745 746 IGRAPH_EXPORT extern igraph_warning_handler_t igraph_warning_handler_ignore; 747 IGRAPH_EXPORT extern igraph_warning_handler_t igraph_warning_handler_print; 748 749 /** 750 * \function igraph_warning 751 * \brief Triggers a warning. 752 * 753 * Call this function if you want to trigger a warning from within 754 * a function that uses \a igraph. 755 * \param reason Textual description of the warning. 756 * \param file The source file in which the warning was noticed. 757 * \param line The number of line in the source file which triggered the 758 * warning. 759 * \param igraph_errno Warnings could have potentially error codes as well, 760 * but this is currently not used in igraph. 761 * \return The supplied error code. 762 */ 763 764 IGRAPH_EXPORT int igraph_warning(const char *reason, const char *file, int line, 765 int igraph_errno); 766 767 /** 768 * \define IGRAPH_WARNINGF 769 * \brief Triggers a warning, with printf-like syntax. 770 * 771 * \a igraph functions can use this macro when they notice a warning and 772 * want to pass on extra information to the user about what went wrong. 773 * It calls \ref igraph_warningf() with the proper parameters and no 774 * error code. 775 * \param reason Textual description of the warning, a template string 776 * with the same syntax as the standard printf C library function. 777 * \param ... The additional arguments to be substituted into the 778 * template string. 779 */ 780 781 #define IGRAPH_WARNINGF(reason, ...) \ 782 do { \ 783 igraph_warningf(reason, IGRAPH_FILE_BASENAME, __LINE__, \ 784 -1, __VA_ARGS__); \ 785 } while (0) 786 787 788 789 /** 790 * \function igraph_warningf 791 * \brief Triggers a warning, printf-like version. 792 * 793 * This function is similar to \ref igraph_warning(), but 794 * uses a printf-like syntax. It substitutes the additional arguments 795 * into the \p reason template string and calls \ref igraph_warning(). 796 * \param reason Textual description of the warning, a template string 797 * with the same syntax as the standard printf C library function. 798 * \param file The source file in which the warning was noticed. 799 * \param line The number of line in the source file which triggered the 800 * warning. 801 * \param igraph_errno Warnings could have potentially error codes as well, 802 * but this is currently not used in igraph. 803 * \param ... The additional arguments to be substituted into the 804 * template string. 805 * \return The supplied error code. 806 */ 807 808 IGRAPH_EXPORT int igraph_warningf(const char *reason, const char *file, int line, 809 int igraph_errno, ...); 810 811 /** 812 * \define IGRAPH_WARNING 813 * \brief Triggers a warning. 814 * 815 * This is the usual way of triggering a warning from an igraph 816 * function. It calls \ref igraph_warning(). 817 * \param reason The warning message. 818 */ 819 820 #define IGRAPH_WARNING(reason) \ 821 do { \ 822 igraph_warning(reason, IGRAPH_FILE_BASENAME, __LINE__, -1); \ 823 } while (0) 824 825 826 /** 827 * \section fatal_error_handlers Fatal errors 828 * 829 * <para> 830 * In some rare situations, \a igraph may encounter an internal error 831 * that cannot be fully handled. In this case, it will call the 832 * current fatal error handler. The default fatal error handler 833 * simply prints the error and aborts the program. 834 * </para> 835 * 836 * <para> 837 * Fatal error handlers do not return. Typically, they might abort the 838 * the program immediately, or in the case of the high-level \a igraph 839 * interfaces, they might return to the top level using a 840 * <code>longjmp()</code>. The fatal error handler is only called when 841 * a serious error has occurred, and as a result igraph may be in an 842 * inconsistent state. The purpose of returning to the top level is to 843 * give the user a chance to save their work instead of aborting immediately. 844 * However, the program session should be restarted as soon as possible. 845 * </para> 846 * 847 * <para> 848 * Most projects that use \a igraph will use the default fatal error 849 * handler. 850 * </para> 851 */ 852 853 /** 854 * \typedef igraph_fatal_handler_t 855 * \brief The type of igraph fatal error handler functions. 856 * 857 * Functions of this type \em must not return. Typically they 858 * call <code>abort()</code> or do a <code>longjmp()</code>. 859 * 860 * \param reason Textual description of the error. 861 * \param file The source file in which the error is noticed. 862 * \param line The number of the line in the source file which triggered the error 863 */ 864 865 typedef void igraph_fatal_handler_t (const char *reason, const char *file, int line); 866 867 /** 868 * \function igraph_set_fatal_handler 869 * \brief Installs a fatal error handler. 870 * 871 * Installs the supplied fatal error handler function. 872 * 873 * </para><para> 874 * Fatal error handler functions \em must not return. Typically, the fatal 875 * error handler would either call <code>abort()</code> or <code>longjmp()</code>. 876 * 877 * \param new_handler The new fatal error handler function to install. 878 * Supply a null pointer here to uninstall the current 879 * fatal error handler, without installing a new one. 880 * \return The current fatal error handler function. 881 */ 882 883 IGRAPH_EXPORT igraph_fatal_handler_t* igraph_set_fatal_handler(igraph_fatal_handler_t* new_handler); 884 885 /** 886 * \var igraph_fatal_handler_abort 887 * \brief Abort program in case of fatal error. 888 * 889 * The default fatal error handler, prints an error message and aborts the program. 890 */ 891 892 IGRAPH_EXPORT igraph_fatal_handler_t igraph_fatal_handler_abort; 893 894 /** 895 * \function igraph_fatal 896 * \brief Triggers a fatal error. 897 * 898 * This function triggers a fatal error. Typically it is called indirectly through 899 * \ref IGRAPH_FATAL() or \ref IGRAPH_ASSERT(). 900 * 901 * \param reason Textual description of the error. 902 * \param file The source file in which the error was noticed. 903 * \param line The number of line in the source file which triggered the error. 904 */ 905 906 IGRAPH_EXPORT IGRAPH_NORETURN void igraph_fatal(const char *reason, const char *file, int line); 907 908 /** 909 * \function igraph_fatalf 910 * \brief Triggers a fatal error, printf-like syntax. 911 * 912 * This function is similar to \ref igraph_fatal(), but 913 * uses a printf-like syntax. It substitutes the additional arguments 914 * into the \p reason template string and calls \ref igraph_fatal(). 915 * 916 * \param reason Textual description of the error. 917 * \param file The source file in which the error was noticed. 918 * \param line The number of line in the source file which triggered the error. 919 * \param ... The additional arguments to be substituted into the template string. 920 */ 921 922 IGRAPH_EXPORT IGRAPH_NORETURN void igraph_fatalf(const char *reason, const char *file, int line, ...); 923 924 /** 925 * \define IGRAPH_FATALF 926 * \brief Triggers a fatal error, with printf-like syntax. 927 * 928 * \a igraph functions can use this macro when a fatal error occurs and 929 * want to pass on extra information to the user about what went wrong. 930 * It calls \ref igraph_fatalf() with the proper parameters. 931 * \param reason Textual description of the error, a template string 932 * with the same syntax as the standard printf C library function. 933 * \param ... The additional arguments to be substituted into the 934 * template string. 935 */ 936 937 #define IGRAPH_FATALF(reason, ...) \ 938 do { \ 939 igraph_fatalf(reason, IGRAPH_FILE_BASENAME, __LINE__, \ 940 __VA_ARGS__); \ 941 } while (0) 942 943 /** 944 * \define IGRAPH_FATAL 945 * \brief Triggers a fatal error. 946 * 947 * This is the usual way of triggering a fatal error from an igraph 948 * function. It calls \ref igraph_fatal(). 949 * 950 * </para><para> 951 * Use this macro only in situations where the error cannot be handled. 952 * The normal way to handle errors is \ref IGRAPH_ERROR(). 953 * 954 * \param reason The error message. 955 */ 956 957 #define IGRAPH_FATAL(reason) \ 958 do { \ 959 igraph_fatal(reason, IGRAPH_FILE_BASENAME, __LINE__); \ 960 } while (0) 961 962 /** 963 * \define IGRAPH_ASSERT 964 * \brief igraph-specific replacement for <code>assert()</code>. 965 * 966 * This macro is like the standard <code>assert()</code>, but instead of 967 * calling <code>abort()</code>, it calls \ref igraph_fatal(). This allows for returning 968 * the control to the calling program, e.g. returning to the top level in a high-level 969 * \a igraph interface. 970 * 971 * </para><para> 972 * Unlike <code>assert()</code>, <code>IGRAPH_ASSERT()</code> is not disabled 973 * when the \c NDEBUG macro is defined. 974 * 975 * </para><para> 976 * This macro is meant for internal use by \a igraph. 977 * 978 * </para><para> 979 * Since a typial fatal error handler does a <code>longjmp()</code>, avoid using this 980 * macro in C++ code. With most compilers, destructor will not be called when 981 * <code>longjmp()</code> leaves the current scope. 982 * 983 * \param condition The condition to be checked. 984 */ 985 986 #define IGRAPH_ASSERT(condition) \ 987 do { \ 988 if (!(condition)) { \ 989 igraph_fatal("Assertion failed: " #condition, IGRAPH_FILE_BASENAME, __LINE__); \ 990 } \ 991 } while (0) 992 993 __END_DECLS 994 995 #endif 996