Fix malformed CUDA version macro check
[alexxy/gromacs.git] / include / gmx_fatal.h
1
2 /*
3  * This file is part of the GROMACS molecular simulation package.
4  *
5  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
6  * Copyright (c) 2001-2004, The GROMACS development team,
7  * check out http://www.gromacs.org for more information.
8  * Copyright (c) 2012,2013, by the GROMACS development team, led by
9  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
10  * others, as listed in the AUTHORS file in the top-level source
11  * directory and at http://www.gromacs.org.
12  *
13  * GROMACS is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public License
15  * as published by the Free Software Foundation; either version 2.1
16  * of the License, or (at your option) any later version.
17  *
18  * GROMACS is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with GROMACS; if not, see
25  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
26  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
27  *
28  * If you want to redistribute modifications to GROMACS, please
29  * consider that scientific software is very special. Version
30  * control is crucial - bugs must be traceable. We will be happy to
31  * consider code for inclusion in the official distribution, but
32  * derived work must not be called official GROMACS. Details are found
33  * in the README & COPYING files - if they are missing, get the
34  * official version at http://www.gromacs.org.
35  *
36  * To help us fund GROMACS development, we humbly ask that you cite
37  * the research papers on the package. Check out http://www.gromacs.org.
38  */
39
40 #ifndef _fatal_h
41 #define _fatal_h
42 #include "visibility.h"
43
44 #include <stdio.h>
45 #include <stdarg.h>
46 #include <errno.h>
47 #include "types/simple.h"
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 #ifndef __has_feature      // Optional.
54 #define __has_feature(x) 0 // Compatibility with non-clang compilers.
55 #endif
56
57 /** \def GMX_ATTRIBUTE_NORETURN \brief Indicate that a function is not
58  * expected to return.
59  * WARNING: In general this flag should not be used for compiler
60  * optimizations, since set_gmx_error_handler can be set to a
61  * handler which does not quit.
62  */
63 #ifndef GMX_ATTRIBUTE_NORETURN
64 #if __has_feature(attribute_analyzer_noreturn)
65 #define GMX_ATTRIBUTE_NORETURN __attribute__((analyzer_noreturn))
66 #else
67 #define GMX_ATTRIBUTE_NORETURN
68 #endif
69 #endif
70
71 GMX_LIBGMX_EXPORT
72 void
73 _where(const char *file, int line);
74 #define where() _where(__FILE__, __LINE__)
75 /* Prints filename and line to stdlog and only on amba memvail */
76
77 void
78 _set_fatal_tmp_file(const char *fn, const char *file, int line);
79 #define set_fatal_tmp_file(fn) _set_fatal_tmp_file(fn, __FILE__, __LINE__)
80 /* set filename to be removed when fatal_error is called */
81
82 void
83 _unset_fatal_tmp_file(const char *fn, const char *file, int line);
84 #define unset_fatal_tmp_file(fn) _unset_fatal_tmp_file(fn, __FILE__, __LINE__)
85 /* unsets filename to be removed */
86
87 GMX_LIBGMX_EXPORT
88 void
89 gmx_fatal(int fatal_errno, const char *file, int line, const char *fmt, ...) GMX_ATTRIBUTE_NORETURN;
90 #define FARGS 0, __FILE__, __LINE__
91 /*
92  * Routine gmx_fatal prints
93  *
94  *  "fatal error file %s line %s \n\t "
95  *
96  * followed by the string specified by fmt and supplied parameters. If
97  * errno is 0, only the message and arguments are printed. If errno is
98  * a legal system errno or -1, a perror like message is printed after the
99  * first message, if errno is -1, the last system errno will be used.
100  * The format of fmt is that like printf etc, only %d, %x, %c, %f, %g and %s
101  * are allowed as format specifiers.
102  *
103  * In case all MPI processes want to stop with the same fatal error,
104  * use gmx_fatal_collective, declared in gmx_fatal_collective.h,
105  * to avoid having as many error messages as processes.
106  *
107  * Tip of the week:
108  * call this function using the FARGS macro:
109  * gmx_fatal(FARGS,fmt,...)
110  *
111  */
112
113 void
114 gmx_fatal_set_log_file(FILE *fp);
115 /* Set the log file for printing error messages */
116
117 GMX_LIBGMX_EXPORT
118 void
119 _invalid_case(const char *fn, int line);
120 #define invalid_case() _invalid_case(__FILE__, __LINE__)
121 /* Issue a warning stating 'Invalid case in switch' */
122
123 void _unexpected_eof(const char *fn, int line, const char *srcfn, int srcline);
124 #define unexpected_eof(fn, line) _unexpected_eof(fn, line, __FILE__, __LINE__)
125
126 /*
127  * Functions can write to this file for debug info
128  * Before writing to it, it should be checked whether
129  * the file is not NULL:
130  * if (debug) fprintf(debug,"%s","Hallo");
131  */
132 GMX_LIBGMX_EXPORT
133 extern FILE    *debug;
134 GMX_LIBGMX_EXPORT
135 extern gmx_bool gmx_debug_at;
136
137 void init_debug (const int dbglevel, const char *dbgfile);
138
139 GMX_LIBGMX_EXPORT
140 gmx_bool bDebugMode(void);
141 /* Return TRUE when the program was started in debug mode */
142
143 #if (defined __sgi && defined USE_SGI_FPE)
144 void doexceptions(void);
145 /* Set exception handlers for debugging */
146 #endif
147
148 /* warn_str is allowed to be NULL.
149  */
150 GMX_LIBGMX_EXPORT
151 void _range_check(int n, int n_min, int n_max, const char *warn_str,
152                   const char *var,
153                   const char *file, int line);
154
155 #define range_check_mesg(n, n_min, n_max, str) _range_check(n, n_min, n_max, str,#n, __FILE__, __LINE__)
156 /* Range check will terminate with an error message if not
157  * n E [ n_min, n_max >
158  * That is n_min is inclusive but not n_max.
159  */
160
161 #define range_check(n, n_min, n_max) _range_check(n, n_min, n_max, NULL,#n, __FILE__, __LINE__)
162 /* Range check will terminate with an error message if not
163  * n E [ n_min, n_max >
164  * That is n_min is inclusive but not n_max.
165  */
166
167 char *gmx_strerror(const char *key);
168 /* Return error message corresponding to the key.
169  * Maybe a multi-line message.
170  * The messages are stored in src/gmxlib/fatal.c
171  */
172
173 GMX_LIBGMX_EXPORT
174 void _gmx_error(const char *key, const char *msg, const char *file, int line) GMX_ATTRIBUTE_NORETURN;
175 #define gmx_error(key, msg) _gmx_error(key, msg, __FILE__, __LINE__)
176 /* Error msg of type key is generated and the program is
177  * terminated unless and error handle is set (see below)
178  */
179
180 /* Some common error types */
181 #define gmx_bug(msg)    gmx_error("bug", msg)
182 #define gmx_call(msg)   gmx_error("call", msg)
183 #define gmx_comm(msg)   gmx_error("comm", msg)
184 #define gmx_file(msg)   gmx_error("file", msg)
185 #define gmx_cmd(msg)    gmx_error("cmd", msg)
186 #define gmx_impl(msg)   gmx_error("impl", msg)
187 #define gmx_incons(msg) gmx_error("incons", msg)
188 #define gmx_input(msg)  gmx_error("input", msg)
189 #define gmx_mem(msg)    gmx_error("mem", msg)
190 #define gmx_open(fn)    gmx_error("open", fn)
191
192 void
193 set_gmx_error_handler(void (*func)(const char *msg));
194 /* An error function will be called that terminates the program
195  * with a fatal error, unless you override it with another function.
196  * i.e.:
197  * set_gmx_error_handler(my_func);
198  * where my_func is a function that takes a string as an argument.
199  * The string may be a multi-line string.
200  */
201
202 GMX_LIBGMX_EXPORT
203 void gmx_warning(const char *fmt, ...);
204 /* Print a warning message to stderr.
205  * The format of fmt is that like printf etc, only %d, %x, %c, %f, %g and %s
206  * are allowed as format specifiers.
207  * The message string should NOT start with "WARNING"
208  * and should NOT end with a newline.
209  */
210
211
212 #ifdef __cplusplus
213 }
214 #endif
215
216 #endif  /* _fatal_h */