19#ifndef LIB_EXCEPTIONS_H_
20#define LIB_EXCEPTIONS_H_
26#include <boost/format.hpp>
28#include "absl/strings/str_cat.h"
29#include "lib/bug_helper.h"
35constexpr char ANSI_RED[] =
"\33[31m";
36constexpr char ANSI_BLUE[] =
"\33[34m";
37constexpr char ANSI_CLR[] =
"\33[0m";
41inline bool is_cerr_redirected() {
42 static bool initialized(
false);
46 is_redir = ttyname(fileno(stderr)) ==
nullptr;
52inline const char *cerr_colorize(
const char *color) {
53 if (is_cerr_redirected()) {
60inline const char *cerr_clear_colors() {
61 if (is_cerr_redirected()) {
73 void traceCreation() {}
76 template <
typename... Args>
79 boost::format fmt(format);
82 message = ::P4::bug_helper(fmt,
"",
"", std::forward<Args>(args)...);
85 const char *what()
const noexcept {
return message.c_str(); }
91 template <
typename... Args>
92 explicit CompilerBug(
const char *format, Args &&...args)
96 message = absl::StrCat(cerr_colorize(ANSI_RED),
"Compiler Bug", cerr_clear_colors(),
":\n",
100 template <
typename... Args>
101 CompilerBug(
int line,
const char *file,
const char *format, Args &&...args)
103 message = absl::StrCat(
"In file: ", file,
":", line,
"\n", cerr_colorize(ANSI_RED),
104 "Compiler Bug", cerr_clear_colors(),
": ", message);
111 template <
typename... Args>
115 message = absl::StrCat(cerr_colorize(ANSI_BLUE),
"Not yet implemented", cerr_clear_colors(),
119 template <
typename... Args>
123 absl::StrCat(
"In file: ", file,
":", line,
"\n", cerr_colorize(ANSI_BLUE),
124 "Unimplemented compiler support", cerr_clear_colors(),
": ", message);
132 template <
typename... Args>
139 throw P4::Util::CompilerBug(__LINE__, __FILE__, __VA_ARGS__); \
141#define BUG_CHECK(e, ...) \
143 if (!(e)) BUG(__VA_ARGS__); \
145#define P4C_UNIMPLEMENTED(...) \
147 throw P4::Util::CompilerUnimplemented(__LINE__, __FILE__, __VA_ARGS__); \
153#define FATAL_ERROR(...) \
155 throw P4::Util::CompilationError(__VA_ARGS__); \
Definition lib/exceptions.h:130
This class indicates a bug in the compiler.
Definition lib/exceptions.h:89
This class indicates an unimplemented feature in the compiler.
Definition lib/exceptions.h:109
Definition lib/exceptions.h:70