1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/* d-diagnostics.cc -- D frontend interface to gcc diagnostics.
Copyright (C) 2017-2024 Free Software Foundation, Inc.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "dmd/globals.h"
#include "dmd/errors.h"
#include "tree.h"
#include "options.h"
#include "diagnostic.h"
#include "d-tree.h"
/* Rewrite the format string FORMAT to deal with any format extensions not
supported by pp_format().
The following format specifiers are handled:
`...`: text within backticks gets quoted as '%<...%>'.
%-10s: left-justify format flag is removed leaving '%s' remaining.
%02x: zero-padding format flag is removed leaving '%x' remaining.
%X: uppercase unsigned hexadecimals are rewritten as '%x'. */
static char *
expand_d_format (const char *format)
{
obstack buf;
bool inbacktick = false;
gcc_obstack_init (&buf);
for (const char *p = format; *p;)
{
while (*p != '\0' && *p != '\\' && *p != '%' && *p != '`')
{
obstack_1grow (&buf, *p);
p++;
}
if (*p == '\0')
break;
if (*p == '\\')
{
if (p[1] == '`')
{
/* Escaped backtick, don't expand it as a quoted string. */
obstack_1grow (&buf, '`');
p++;;
}
else
obstack_1grow (&buf, *p);
p++;
continue;
}
if (*p == '`')
{
/* Text enclosed by `...` are translated as a quoted string. */
if (inbacktick)
{
obstack_grow (&buf, "%>", 2);
inbacktick = false;
}
else
{
obstack_grow (&buf, "%<", 2);
inbacktick = true;
}
p++;
continue;
}
/* Check the conversion specification for unhandled flags. */
obstack_1grow (&buf, *p);
p++;
Lagain:
switch (*p)
{
case '\0':
/* Malformed format string. */
gcc_unreachable ();
case '-':
/* Remove whitespace formatting. */
p++;
while (ISDIGIT (*p))
p++;
goto Lagain;
case '0':
/* Remove zero padding from format string. */
while (ISDIGIT (*p))
p++;
goto Lagain;
case 'X':
/* Hex format only supports lower-case. */
obstack_1grow (&buf, 'x');
p++;
break;
default:
break;
}
}
gcc_assert (!inbacktick);
obstack_1grow (&buf, '\0');
return (char *) obstack_finish (&buf);
}
/* Rewrite the format string FORMAT to deal with any characters that require
escaping before expand_d_format expands it. */
static char *
escape_d_format (const char *format)
{
bool quoted = false;
size_t format_len = 0;
obstack buf;
gcc_obstack_init (&buf);
/* If the format string is enclosed by two '`' characters, then don't escape
the first and last characters. */
if (*format == '`')
{
format_len = strlen (format) - 1;
if (format_len && format[format_len] == '`')
quoted = true;
}
for (const char *p = format; *p; p++)
{
switch (*p)
{
case '%':
/* Escape `%' characters so that pp_format does not confuse them
for actual format specifiers. */
obstack_1grow (&buf, '%');
break;
case '`':
/* Escape '`' characters so that expand_d_format does not confuse them
for a quoted string. */
if (!quoted || (p != format && p != (format + format_len)))
obstack_1grow (&buf, '\\');
break;
default:
break;
}
obstack_1grow (&buf, *p);
}
obstack_1grow (&buf, '\0');
return (char *) obstack_finish (&buf);
}
/* Helper routine for all error routines. Reports a diagnostic specified by
KIND at the explicit location LOC. The message FORMAT comes from the dmd
front-end, which does not get translated by the gcc diagnostic routines. */
static void ATTRIBUTE_GCC_DIAG(3,0)
d_diagnostic_report_diagnostic (const Loc &loc, int opt, const char *format,
va_list ap, diagnostic_t kind, bool verbatim)
{
va_list argp;
va_copy (argp, ap);
if (loc.filename () || !verbatim)
{
rich_location rich_loc (line_table, make_location_t (loc));
diagnostic_info diagnostic;
char *xformat = expand_d_format (format);
diagnostic_set_info_translated (&diagnostic, xformat, &argp,
&rich_loc, kind);
if (opt != 0)
diagnostic.option_index = opt;
diagnostic_report_diagnostic (global_dc, &diagnostic);
}
else
{
/* Write verbatim messages with no location direct to stream. */
text_info text (expand_d_format (format), &argp, errno, nullptr);
pp_format_verbatim (global_dc->printer, &text);
pp_newline_and_flush (global_dc->printer);
}
va_end (argp);
}
/* Print a diagnostic message of type KIND with explicit location LOC with an
optional message prefix PREFIX1 and PREFIX2, increasing the global or gagged
error count depending on how KIND is treated. */
void D_ATTRIBUTE_FORMAT(2,0) ATTRIBUTE_GCC_DIAG(2,0)
verrorReport (const Loc& loc, const char *format, va_list ap, ErrorKind kind,
const char *prefix1, const char *prefix2)
{
diagnostic_t diag_kind = DK_UNSPECIFIED;
int opt = 0;
bool verbatim = false;
char *xformat;
if (kind == ErrorKind::error)
{
global.errors++;
if (global.gag)
global.gaggedErrors++;
if (global.gag && !global.params.v.showGaggedErrors)
return;
diag_kind = global.gag ? DK_ANACHRONISM : DK_ERROR;
}
else if (kind == ErrorKind::warning)
{
if (global.gag || global.params.warnings == DIAGNOSTICoff)
{
if (global.gag)
global.gaggedWarnings++;
return;
}
/* Warnings don't count if not treated as errors. */
if (global.params.warnings == DIAGNOSTICerror)
global.warnings++;
diag_kind = DK_WARNING;
}
else if (kind == ErrorKind::deprecation)
{
if (global.params.useDeprecated == DIAGNOSTICerror)
return verrorReport (loc, format, ap, ErrorKind::error, prefix1,
prefix2);
else if (global.gag || global.params.useDeprecated != DIAGNOSTICinform)
{
if (global.gag)
global.gaggedWarnings++;
return;
}
opt = OPT_Wdeprecated;
diag_kind = DK_WARNING;
}
else if (kind == ErrorKind::message)
{
diag_kind = DK_NOTE;
verbatim = true;
}
else if (kind == ErrorKind::tip)
{
if (global.gag)
return;
diag_kind = DK_DEBUG;
verbatim = true;
}
else
gcc_unreachable ();
/* Build string and emit. */
if (prefix2 != NULL)
xformat = xasprintf ("%s %s %s", escape_d_format (prefix1),
escape_d_format (prefix2), format);
else if (prefix1 != NULL)
xformat = xasprintf ("%s %s", escape_d_format (prefix1), format);
else
xformat = xasprintf ("%s", format);
d_diagnostic_report_diagnostic (loc, opt, xformat, ap, diag_kind, verbatim);
free (xformat);
}
/* Print supplementary message about the last diagnostic of type KIND, with
explicit location LOC. This doesn't increase the global error count. */
void D_ATTRIBUTE_FORMAT(2,0) ATTRIBUTE_GCC_DIAG(2,0)
verrorReportSupplemental (const Loc& loc, const char* format, va_list ap,
ErrorKind kind)
{
if (kind == ErrorKind::error)
{
if (global.gag && !global.params.v.showGaggedErrors)
return;
}
else if (kind == ErrorKind::warning)
{
if (global.params.warnings == DIAGNOSTICoff || global.gag)
return;
}
else if (kind == ErrorKind::deprecation)
{
if (global.params.useDeprecated == DIAGNOSTICerror)
return verrorReportSupplemental (loc, format, ap, ErrorKind::error);
else if (global.params.useDeprecated != DIAGNOSTICinform || global.gag)
return;
}
else
gcc_unreachable ();
d_diagnostic_report_diagnostic (loc, 0, format, ap, DK_NOTE, false);
}
/* Call this after printing out fatal error messages to clean up and
exit the compiler. */
void
fatal (void)
{
exit (FATAL_EXIT_CODE);
}
|