10#ifndef LIB_EXCEPTIONS_H_
11#define LIB_EXCEPTIONS_H_
19#include "absl/strings/str_cat.h"
20#include "lib/format.h"
25constexpr char ANSI_RED[] =
"\33[31m";
26constexpr char ANSI_BLUE[] =
"\33[34m";
27constexpr char ANSI_CLR[] =
"\33[0m";
31inline bool is_cerr_redirected() {
32 static bool initialized(
false);
36 is_redir = ttyname(fileno(stderr)) ==
nullptr;
42inline const char *cerr_colorize(
const char *color) {
43 if (is_cerr_redirected()) {
50inline const char *cerr_clear_colors() {
51 if (is_cerr_redirected()) {
63 void traceCreation() {}
71 template <
typename... Args>
74 message = createBugMessage(format, args...);
77 [[nodiscard]]
const char *what() const noexcept
override {
return message.c_str(); }
88 message = absl::StrCat(cerr_colorize(ANSI_RED),
"Compiler Bug", cerr_clear_colors(),
":\n",
92 template <
typename... Args>
93 explicit CompilerBug(
const char *format, Args &&...args)
96 template <
typename... Args>
97 CompilerBug(
int line,
const char *file,
const char *format, Args &&...args)
99 message = absl::StrCat(
"In file: ", file,
":", line,
"\n", cerr_colorize(ANSI_RED),
100 "Compiler Bug", cerr_clear_colors(),
": ", message);
107 template <
typename... Args>
108 explicit CompilerUnimplemented(
const char *format, Args &&...args)
111 message = absl::StrCat(cerr_colorize(ANSI_BLUE),
"Not yet implemented", cerr_clear_colors(),
115 template <
typename... Args>
116 CompilerUnimplemented(
int line,
const char *file,
const char *format, Args &&...args)
119 absl::StrCat(
"In file: ", file,
":", line,
"\n", cerr_colorize(ANSI_BLUE),
120 "Unimplemented compiler support", cerr_clear_colors(),
": ", message);
128 template <
typename... Args>
129 explicit CompilationError(
const char *format, Args &&...args)
136 throw P4::Util::CompilerBug(__LINE__, __FILE__, __VA_ARGS__); \
138#define BUG_CHECK(e, ...) \
140 if (!(e)) BUG(__VA_ARGS__); \
142#define P4C_UNIMPLEMENTED(...) \
144 throw P4::Util::CompilerUnimplemented(__LINE__, __FILE__, __VA_ARGS__); \
150#define FATAL_ERROR(...) \
152 throw P4::Util::CompilationError(__VA_ARGS__); \
This class indicates a bug in the compiler.
Definition lib/exceptions.h:81
CompilerBug(std::string formattedMessage)
Also usable when the diagnostic formatter itself fails.
Definition lib/exceptions.h:84
P4CExceptionBase(std::string formattedMessage)
Use already-formatted text without invoking the diagnostic formatter.
Definition lib/exceptions.h:67