Require target lra in gcc.dg/pr108095.c
[official-gcc.git] / gcc / rust / rust-diagnostics.h
blobd198bd5736fd4da3b7cb5148daeea2bde4a5dab7
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
8 // version.
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
13 // for more details.
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)))
30 #else
31 #define RUST_ATTRIBUTE_GCC_DIAG(m, n)
32 #endif
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
37 // extensions:
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
42 // specifiers].
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.
51 // clang-format off
52 // simple location
54 struct ErrorCode
56 explicit ErrorCode (const char *str) : m_str (str)
58 gcc_assert (str);
59 gcc_assert (str[0] == 'E');
62 const char *m_str;
65 extern void
66 rust_internal_error_at (const Location, const char *fmt, ...)
67 RUST_ATTRIBUTE_GCC_DIAG (2, 3)
68 RUST_ATTRIBUTE_NORETURN;
69 extern void
70 rust_error_at (const Location, const char *fmt, ...)
71 RUST_ATTRIBUTE_GCC_DIAG (2, 3);
72 extern void
73 rust_warning_at (const Location, int opt, const char *fmt, ...)
74 RUST_ATTRIBUTE_GCC_DIAG (3, 4);
75 extern void
76 rust_fatal_error (const Location, const char *fmt, ...)
77 RUST_ATTRIBUTE_GCC_DIAG (2, 3)
78 RUST_ATTRIBUTE_NORETURN;
79 extern void
80 rust_inform (const Location, const char *fmt, ...)
81 RUST_ATTRIBUTE_GCC_DIAG (2, 3);
83 // rich locations
84 extern void
85 rust_error_at (const RichLocation &, const char *fmt, ...)
86 RUST_ATTRIBUTE_GCC_DIAG (2, 3);
87 extern void
88 rust_error_at (const RichLocation &, const ErrorCode, const char *fmt, ...)
89 RUST_ATTRIBUTE_GCC_DIAG (3, 4);
90 // clang-format on
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).
95 extern const char *
96 rust_open_quote ();
97 extern const char *
98 rust_close_quote ();
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.
106 // clang-format off
107 extern void
108 rust_be_internal_error_at (const Location, const std::string &errmsg)
109 RUST_ATTRIBUTE_NORETURN;
110 extern void
111 rust_be_error_at (const Location, const std::string &errmsg);
112 extern void
113 rust_be_error_at (const RichLocation &, const std::string &errmsg);
114 extern void
115 rust_be_error_at (const RichLocation &, const ErrorCode,
116 const std::string &errmsg);
117 extern void
118 rust_be_warning_at (const Location, int opt, const std::string &warningmsg);
119 extern void
120 rust_be_fatal_error (const Location, const std::string &errmsg)
121 RUST_ATTRIBUTE_NORETURN;
122 extern void
123 rust_be_inform (const Location, const std::string &infomsg);
124 extern void
125 rust_be_get_quotechars (const char **open_quote, const char **close_quote);
126 extern bool
127 rust_be_debug_p (void);
128 // clang-format on
130 namespace Rust {
131 /* A structure used to represent an error. Useful for enabling
132 * errors to be ignored, e.g. if backtracking. */
133 struct Error
135 enum class Kind
137 Hint,
138 Err,
139 FatalErr,
142 Kind kind;
143 Location locus;
144 std::string message;
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);
184 void emit () const
186 switch (kind)
188 case Kind::Hint:
189 rust_inform (locus, "%s", message.c_str ());
190 break;
191 case Kind::Err:
192 rust_error_at (locus, "%s", message.c_str ());
193 break;
194 case Kind::FatalErr:
195 rust_fatal_error (locus, "%s", message.c_str ());
196 break;
200 } // namespace Rust
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
206 // "location_t"
207 #define rust_sorry_at(location, ...) \
208 sorry_at (location.gcc_location (), __VA_ARGS__)
210 void
211 rust_debug_loc (const Location location, const char *fmt,
212 ...) ATTRIBUTE_PRINTF_2;
214 #endif // !defined(RUST_DIAGNOSTICS_H)