Tidy: modernize-use-nullptr
[alexxy/gromacs.git] / src / gromacs / fileio / warninp.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016,2017, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #include "gmxpre.h"
38
39 #include "warninp.h"
40
41 #include <cstring>
42
43 #include "gromacs/utility/cstringutil.h"
44 #include "gromacs/utility/fatalerror.h"
45 #include "gromacs/utility/smalloc.h"
46
47 typedef struct warninp {
48     gmx_bool bAllowWarnings;
49     int      nwarn_note;
50     int      nwarn_warn;
51     int      nwarn_error;
52     int      maxwarn;
53     int      lineno;
54     char     filenm[256];
55 } t_warninp;
56
57 warninp_t init_warning(gmx_bool bAllowWarnings, int maxwarning)
58 {
59     warninp_t wi;
60
61     snew(wi, 1);
62
63     wi->bAllowWarnings = bAllowWarnings;
64     wi->maxwarn        = maxwarning;
65     wi->nwarn_note     = 0;
66     wi->nwarn_warn     = 0;
67     wi->nwarn_error    = 0;
68     strcpy(wi->filenm, "unknown");
69     wi->lineno         = 0;
70
71     return wi;
72 }
73
74 void set_warning_line(warninp_t wi, const char *s, int line)
75 {
76     if (s != nullptr)
77     {
78         std::strcpy(wi->filenm, s);
79     }
80     wi->lineno = line;
81 }
82
83 int get_warning_line(warninp_t wi)
84 {
85     return wi->lineno;
86 }
87
88 const char *get_warning_file(warninp_t wi)
89 {
90     return wi->filenm;
91 }
92
93 static void low_warning(warninp_t wi, const char *wtype, int n, const char *s)
94 {
95 #define indent 2
96     char *temp, *temp2;
97     int   i;
98
99     if (s == nullptr)
100     {
101         s = "Empty error message.";
102     }
103     snew(temp, std::strlen(s)+indent+1);
104     for (i = 0; i < indent; i++)
105     {
106         temp[i] = ' ';
107     }
108     temp[indent] = '\0';
109     std::strcat(temp, s);
110     temp2 = wrap_lines(temp, 78-indent, indent, FALSE);
111     if (std::strlen(wi->filenm) > 0)
112     {
113         if (wi->lineno != -1)
114         {
115             fprintf(stderr, "\n%s %d [file %s, line %d]:\n%s\n\n",
116                     wtype, n, wi->filenm, wi->lineno, temp2);
117         }
118         else
119         {
120             fprintf(stderr, "\n%s %d [file %s]:\n%s\n\n",
121                     wtype, n, wi->filenm, temp2);
122         }
123     }
124     else
125     {
126         fprintf(stderr, "\n%s %d:\n%s\n\n", wtype, n, temp2);
127     }
128     sfree(temp);
129     sfree(temp2);
130 }
131
132 void warning(warninp_t wi, const char *s)
133 {
134     if (wi->bAllowWarnings)
135     {
136         wi->nwarn_warn++;
137         low_warning(wi, "WARNING", wi->nwarn_warn, s);
138     }
139     else
140     {
141         warning_error(wi, s);
142     }
143 }
144
145 void warning_note(warninp_t wi, const char *s)
146 {
147     wi->nwarn_note++;
148     low_warning(wi, "NOTE", wi->nwarn_note, s);
149 }
150
151 void warning_error(warninp_t wi, const char *s)
152 {
153     wi->nwarn_error++;
154     low_warning(wi, "ERROR", wi->nwarn_error, s);
155 }
156
157 static void print_warn_count(const char *type, int n)
158 {
159     if (n > 0)
160     {
161         fprintf(stderr, "\nThere %s %d %s%s\n",
162                 (n == 1) ? "was" : "were", n, type, (n == 1) ? "" : "s");
163     }
164 }
165
166 // Note it is the caller's responsibility to ensure that exiting is correct behaviour
167 static gmx_noreturn void check_warning_error_impl(warninp_t wi, int f_errno, const char *file, int line)
168 {
169     print_warn_count("note", wi->nwarn_note);
170     print_warn_count("warning", wi->nwarn_warn);
171
172     gmx_fatal(f_errno, file, line, "There %s %d error%s in input file(s)",
173               (wi->nwarn_error == 1) ? "was" : "were", wi->nwarn_error,
174               (wi->nwarn_error == 1) ? ""    : "s");
175 }
176
177 void check_warning_error(warninp_t wi, int f_errno, const char *file, int line)
178 {
179     if (wi->nwarn_error > 0)
180     {
181         check_warning_error_impl(wi, f_errno, file, line);
182     }
183 }
184
185 void warning_error_and_exit(warninp_t wi, const char *s, int f_errno, const char *file, int line)
186 {
187     warning_error(wi, s);
188     check_warning_error_impl(wi, f_errno, file, line);
189 }
190
191 gmx_bool warning_errors_exist(warninp_t wi)
192 {
193     return (wi->nwarn_error > 0);
194 }
195
196 void done_warning(warninp_t wi, int f_errno, const char *file, int line)
197 {
198     // If we've had an error, then this will report the number of
199     // notes and warnings, and then exit.
200     check_warning_error(wi, f_errno, file, line);
201
202     // Otherwise, we report the number of notes and warnings.
203     print_warn_count("note", wi->nwarn_note);
204     print_warn_count("warning", wi->nwarn_warn);
205
206     if (wi->maxwarn >= 0 && wi->nwarn_warn > wi->maxwarn)
207     {
208         gmx_fatal(f_errno, file, line,
209                   "Too many warnings (%d).\n"
210                   "If you are sure all warnings are harmless, use the -maxwarn option.",
211                   wi->nwarn_warn);
212     }
213
214     free_warning(wi);
215 }
216
217 void free_warning(warninp_t wi)
218 {
219     sfree(wi);
220 }
221
222 void _too_few(warninp_t wi, const char *fn, int line)
223 {
224     char buf[STRLEN];
225
226     sprintf(buf,
227             "Too few parameters on line (source file %s, line %d)",
228             fn, line);
229     warning(wi, buf);
230 }
231
232 void _incorrect_n_param(warninp_t wi, const char *fn, int line)
233 {
234     char buf[STRLEN];
235
236     sprintf(buf,
237             "Incorrect number of parameters on line (source file %s, line %d)",
238             fn, line);
239     warning(wi, buf);
240 }