P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
exceptions.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17/* -*-c++-*- */
18
19#ifndef LIB_EXCEPTIONS_H_
20#define LIB_EXCEPTIONS_H_
21
22#include <unistd.h>
23
24#include <exception>
25
26#include <boost/format.hpp>
27
28#include "lib/error_helper.h"
29
30namespace Util {
31
32// colors to pretty print messages
33// \e is non-standard escape sequence, use codepoint \33 instead
34constexpr char ANSI_RED[] = "\33[31m";
35constexpr char ANSI_BLUE[] = "\33[34m";
36constexpr char ANSI_CLR[] = "\33[0m";
37
40inline bool is_cerr_redirected() {
41 static bool initialized(false);
42 static bool is_redir;
43 if (!initialized) {
44 initialized = true;
45 is_redir = ttyname(fileno(stderr)) == nullptr; // NOLINT(runtime/threadsafe_fn)
46 }
47 return is_redir;
48}
49
51inline const char *cerr_colorize(const char *color) {
52 if (is_cerr_redirected()) {
53 return "";
54 }
55 return color;
56}
57
59inline const char *cerr_clear_colors() {
60 if (is_cerr_redirected()) {
61 return "";
62 }
63 return ANSI_CLR;
64}
65
69class P4CExceptionBase : public std::exception {
70 protected:
71 cstring message;
72 void traceCreation() {}
73
74 public:
75 template <typename... T>
76 P4CExceptionBase(const char *format, T... args) {
77 traceCreation();
78 boost::format fmt(format);
79 message = ::bug_helper(fmt, "", "", "", std::forward<T>(args)...);
80 }
81
82 const char *what() const noexcept { return message.c_str(); }
83};
84
86class CompilerBug final : public P4CExceptionBase {
87 public:
88 template <typename... T>
89 CompilerBug(const char *format, T... args) : P4CExceptionBase(format, args...) {
90 // Check if output is redirected and if so, then don't color text so that
91 // escape characters are not present
92 message = cstring(cerr_colorize(ANSI_RED)) + "Compiler Bug" + cerr_clear_colors() + ":\n" +
93 message;
94 }
95
96 template <typename... T>
97 CompilerBug(int line, const char *file, const char *format, T... args)
98 : P4CExceptionBase(format, args...) {
99 message = cstring("In file: ") + file + ":" + Util::toString(line) + "\n" +
100 cerr_colorize(ANSI_RED) + "Compiler Bug" + cerr_clear_colors() + ": " + message;
101 }
102};
103
106 public:
107 template <typename... T>
108 CompilerUnimplemented(const char *format, T... args) : P4CExceptionBase(format, args...) {
109 // Do not add colors when redirecting to stderr
110 message = cstring(cerr_colorize(ANSI_BLUE)) + "Not yet implemented" + cerr_clear_colors() +
111 ":\n" + message;
112 }
113
114 template <typename... T>
115 CompilerUnimplemented(int line, const char *file, const char *format, T... args)
116 : P4CExceptionBase(format, args...) {
117 message = cstring("In file: ") + file + ":" + Util::toString(line) + "\n" +
118 cerr_colorize(ANSI_BLUE) + "Unimplemented compiler support" +
119 cerr_clear_colors() + ": " + message;
120 }
121};
122
126 public:
127 template <typename... T>
128 CompilationError(const char *format, T... args) : P4CExceptionBase(format, args...) {}
129};
130
131#define BUG(...) \
132 do { \
133 throw Util::CompilerBug(__LINE__, __FILE__, __VA_ARGS__); \
134 } while (0)
135#define BUG_CHECK(e, ...) \
136 do { \
137 if (!(e)) BUG(__VA_ARGS__); \
138 } while (0)
139#define P4C_UNIMPLEMENTED(...) \
140 do { \
141 throw Util::CompilerUnimplemented(__LINE__, __FILE__, __VA_ARGS__); \
142 } while (0)
143
144} // namespace Util
145
147#define FATAL_ERROR(...) \
148 do { \
149 throw Util::CompilationError(__VA_ARGS__); \
150 } while (0)
151
152#endif /* LIB_EXCEPTIONS_H_ */
Definition exceptions.h:125
This class indicates a bug in the compiler.
Definition exceptions.h:86
This class indicates an unimplemented feature in the compiler.
Definition exceptions.h:105
Definition exceptions.h:69
Definition cstring.h:72