1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 // This file is part of GCC.
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
19 // rust-diagnostics.h -- interface to diagnostic reporting -*- C++ -*-
21 #ifndef RUST_DIAGNOSTICS_H
22 #define RUST_DIAGNOSTICS_H
24 #include "rust-linemap.h"
26 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
27 #define RUST_ATTRIBUTE_GCC_DIAG(m, n) \
28 __attribute__ ((__format__ (__gcc_tdiag__, m, n))) \
29 __attribute__ ((__nonnull__ (m)))
31 #define RUST_ATTRIBUTE_GCC_DIAG(m, n)
34 // These declarations define the interface through which the frontend
35 // reports errors and warnings. These functions accept printf-like
36 // format specifiers (e.g. %d, %f, %s, etc), with the following additional
39 // 1. 'q' qualifier may be applied to a specifier to add quoting, e.g.
40 // %qd produces a quoted decimal output, %qs a quoted string output.
41 // [This extension is supported only with single-character format
44 // 2. %m specifier outputs value of "strerror(errno)" at time of call.
46 // 3. %< outputs an opening quote, %> a closing quote.
48 // All other format specifiers are as defined by 'sprintf'. The final resulting
49 // message is then sent to the back end via rust_be_error_at/rust_be_warning_at.
56 explicit ErrorCode (const char *str
) : m_str (str
)
59 gcc_assert (str
[0] == 'E');
66 rust_internal_error_at (const Location
, const char *fmt
, ...)
67 RUST_ATTRIBUTE_GCC_DIAG (2, 3)
68 RUST_ATTRIBUTE_NORETURN
;
70 rust_error_at (const Location
, const char *fmt
, ...)
71 RUST_ATTRIBUTE_GCC_DIAG (2, 3);
73 rust_warning_at (const Location
, int opt
, const char *fmt
, ...)
74 RUST_ATTRIBUTE_GCC_DIAG (3, 4);
76 rust_fatal_error (const Location
, const char *fmt
, ...)
77 RUST_ATTRIBUTE_GCC_DIAG (2, 3)
78 RUST_ATTRIBUTE_NORETURN
;
80 rust_inform (const Location
, const char *fmt
, ...)
81 RUST_ATTRIBUTE_GCC_DIAG (2, 3);
85 rust_error_at (const RichLocation
&, const char *fmt
, ...)
86 RUST_ATTRIBUTE_GCC_DIAG (2, 3);
88 rust_error_at (const RichLocation
&, const ErrorCode
, const char *fmt
, ...)
89 RUST_ATTRIBUTE_GCC_DIAG (3, 4);
92 // These interfaces provide a way for the front end to ask for
93 // the open/close quote characters it should use when formatting
94 // diagnostics (warnings, errors).
100 // These interfaces are used by utilities above to pass warnings and
101 // errors (once format specifiers have been expanded) to the back end,
102 // and to determine quoting style. Avoid calling these routines directly;
103 // instead use the equivalent routines above. The back end is required to
104 // implement these routines.
108 rust_be_internal_error_at (const Location
, const std::string
&errmsg
)
109 RUST_ATTRIBUTE_NORETURN
;
111 rust_be_error_at (const Location
, const std::string
&errmsg
);
113 rust_be_error_at (const RichLocation
&, const std::string
&errmsg
);
115 rust_be_error_at (const RichLocation
&, const ErrorCode
,
116 const std::string
&errmsg
);
118 rust_be_warning_at (const Location
, int opt
, const std::string
&warningmsg
);
120 rust_be_fatal_error (const Location
, const std::string
&errmsg
)
121 RUST_ATTRIBUTE_NORETURN
;
123 rust_be_inform (const Location
, const std::string
&infomsg
);
125 rust_be_get_quotechars (const char **open_quote
, const char **close_quote
);
127 rust_be_debug_p (void);
131 /* A structure used to represent an error. Useful for enabling
132 * errors to be ignored, e.g. if backtracking. */
145 // TODO: store more stuff? e.g. node id?
147 Error (Kind kind
, Location locus
, std::string message
)
148 : kind (kind
), locus (locus
), message (std::move (message
))
150 message
.shrink_to_fit ();
153 Error (Location locus
, std::string message
)
155 Error (Kind::Err
, locus
, std::move (message
));
158 static Error
Hint (Location locus
, std::string message
)
160 return Error (Kind::Hint
, locus
, std::move (message
));
163 static Error
Fatal (Location locus
, std::string message
)
165 return Error (Kind::FatalErr
, locus
, std::move (message
));
168 // TODO: the attribute part might be incorrect
169 Error (Location locus
, const char *fmt
,
170 ...) /*RUST_ATTRIBUTE_GCC_DIAG (2, 3)*/ RUST_ATTRIBUTE_GCC_DIAG (3, 4);
173 * printf-like overload of Error::Hint
175 static Error
Hint (Location locus
, const char *fmt
, ...)
176 RUST_ATTRIBUTE_GCC_DIAG (2, 3);
179 * printf-like overload of Error::Fatal
181 static Error
Fatal (Location locus
, const char *fmt
, ...)
182 RUST_ATTRIBUTE_GCC_DIAG (2, 3);
189 rust_inform (locus
, "%s", message
.c_str ());
192 rust_error_at (locus
, "%s", message
.c_str ());
195 rust_fatal_error (locus
, "%s", message
.c_str ());
202 // rust_debug uses normal printf formatting, not GCC diagnostic formatting.
203 #define rust_debug(...) rust_debug_loc (Location (), __VA_ARGS__)
205 // rust_sorry_at wraps GCC diagnostic "sorry_at" to accept "Location" instead of
207 #define rust_sorry_at(location, ...) \
208 sorry_at (location.gcc_location (), __VA_ARGS__)
211 rust_debug_loc (const Location location
, const char *fmt
,
212 ...) ATTRIBUTE_PRINTF_2
;
214 #endif // !defined(RUST_DIAGNOSTICS_H)