P4C
The P4 Compiler
Loading...
Searching...
No Matches
error_catalog.h
1/*
2Copyright 2018-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#ifndef LIB_ERROR_CATALOG_H_
18#define LIB_ERROR_CATALOG_H_
19
20#include <map>
21#include <string>
22
23#include "cstring.h"
24#include "lib/error_message.h"
25#include "lib/exceptions.h"
26
27namespace P4 {
28
29using MessageType = ErrorMessage::MessageType;
30
31class ErrorReporter;
32
35class ErrorType {
36 public:
37 // -------- Errors -------------
38 // errors as initially defined with a format string
39 static constexpr int LEGACY_ERROR = 0;
40 static constexpr int ERR_UNKNOWN = 1; // unknown construct (in context)
41 static constexpr int ERR_UNSUPPORTED = 2; // unsupported construct
42 static constexpr int ERR_UNEXPECTED = 3; // unexpected construct
43 static constexpr int ERR_UNINITIALIZED = 4; // uninitialized reads/writes
44 static constexpr int ERR_EXPECTED = 5; // language, compiler expects a different construct
45 static constexpr int ERR_NOT_FOUND = 6; // A different way to say ERR_EXPECTED
46 static constexpr int ERR_INVALID = 7; // invalid construct
47 static constexpr int ERR_EXPRESSION = 8; // expression related errors
48 static constexpr int ERR_OVERLIMIT = 9; // program node exceeds target limits
49 static constexpr int ERR_INSUFFICIENT = 10; // program node does not have enough of ...
50 static constexpr int ERR_TYPE_ERROR = 11; // P4 type checking errors
51 static constexpr int ERR_UNSUPPORTED_ON_TARGET = 12; // target can not handle construct
52 static constexpr int ERR_DUPLICATE = 13; // duplicate objects
53 static constexpr int ERR_IO = 14; // IO error
54 static constexpr int ERR_UNREACHABLE = 15; // unreachable code
55 static constexpr int ERR_MODEL = 16; // something is wrong with the target model
56 static constexpr int ERR_TABLE_KEYS = 17; // something is wrong with a table key
57 static constexpr int ERR_RESERVED = 18; // Reserved for target use
58 // Backends should extend this class with additional errors in the range 500-999.
59 static constexpr int ERR_MIN_BACKEND = 500; // first allowed backend error code
60 static constexpr int ERR_MAX = 999; // last allowed error code
61
62 // -------- Warnings -----------
63 // warnings as initially defined with a format string
64 static constexpr int LEGACY_WARNING = ERR_MAX + 1;
65 static constexpr int WARN_FAILED = 1001; // non-fatal failure!
66 static constexpr int WARN_UNKNOWN = 1002; // unknown construct (in context)
67 static constexpr int WARN_INVALID = 1003; // invalid construct
68 static constexpr int WARN_UNSUPPORTED = 1004; // unsupported construct
69 static constexpr int WARN_DEPRECATED = 1005; // deprecated feature
70 static constexpr int WARN_UNINITIALIZED = 1006; // unitialized instance
71 static constexpr int WARN_UNINITIALIZED_USE = 1019; // use of uninitialized value
72 static constexpr int WARN_UNINITIALIZED_OUT_PARAM =
73 1018; // output parameter may be uninitialized
74 static constexpr int WARN_UNUSED = 1007; // unused instance
75 static constexpr int WARN_MISSING = 1008; // missing construct
76 static constexpr int WARN_ORDERING = 1009; // inconsistent statement ordering
77 static constexpr int WARN_MISMATCH = 1010; // mismatched constructs
78 static constexpr int WARN_OVERFLOW = 1011; // values do not fit
79 static constexpr int WARN_IGNORE_PROPERTY = 1012; // invalid property for object, ignored
80 static constexpr int WARN_TYPE_INFERENCE = 1013; // type inference can not infer, substitutes
81 static constexpr int WARN_PARSER_TRANSITION = 1014; // parser transition non-fatal issues
82 static constexpr int WARN_UNREACHABLE = 1015; // unreachable code
83 static constexpr int WARN_SHADOWING = 1016; // instance shadowing
84 static constexpr int WARN_IGNORE = 1017; // simply ignore
85 static constexpr int WARN_INVALID_HEADER = 1020; // access to fields of an invalid header
86 static constexpr int WARN_DUPLICATE_PRIORITIES = 1021; // two entries with the same priority
87 static constexpr int WARN_ENTRIES_OUT_OF_ORDER = 1022; // entries with priorities out of order
88 static constexpr int WARN_MULTI_HDR_EXTRACT =
89 1023; // same header may be extracted more than once
90 static constexpr int WARN_EXPRESSION = 1024; // expression related warnings
91 static constexpr int WARN_DUPLICATE = 1025; // duplicate objects
92 static constexpr int WARN_BRANCH_HINT = 1026; // branch frequency/likely hints
93 static constexpr int WARN_TABLE_KEYS = 1027; // something is wrong with a table key
94 // Backends should extend this class with additional warnings in the range 1500-2141.
95 static constexpr int WARN_MIN_BACKEND = 1500; // first allowed backend warning code
96 static constexpr int WARN_MAX = 2141; // last allowed warning code
97
98 // -------- Info messages -------------
99 // info messages as initially defined with a format string
100 static constexpr int INFO_INFERRED = WARN_MAX + 1; // information inferred by compiler
101 static constexpr int INFO_PROGRESS = 2143; // compilation progress
102 static constexpr int INFO_REMOVED = 2144; // instance removed
103
104 // Backends should extend this class with additional info messages in the range 3000-3999.
105 static constexpr int INFO_MIN_BACKEND = 3000; // first allowed backend info code
106 static constexpr int INFO_MAX = 3999; // last allowed info code
107};
108
109class ErrorCatalog {
110 public:
112 static ErrorCatalog &getCatalog() {
113 static ErrorCatalog instance;
114 return instance;
115 }
116
123 template <MessageType type, int errorCode>
124 bool add(const char *name, bool forceReplace = false) {
125 static_assert(type != MessageType::Error ||
126 (errorCode >= ErrorType::ERR_MIN_BACKEND && errorCode <= ErrorType::ERR_MAX));
127 static_assert(type != MessageType::Warning || (errorCode >= ErrorType::WARN_MIN_BACKEND &&
128 errorCode <= ErrorType::WARN_MAX));
129 static_assert(type != MessageType::Info || (errorCode >= ErrorType::INFO_MIN_BACKEND &&
130 errorCode <= ErrorType::INFO_MAX));
131 static_assert(type != MessageType::None);
132 if (forceReplace) errorCatalog.erase(errorCode);
133 auto it = errorCatalog.emplace(errorCode, name);
134 return it.second;
135 }
136
138 cstring getName(int errorCode) {
139 using namespace P4::literals;
140
141 if (errorCatalog.count(errorCode)) return errorCatalog.at(errorCode);
142 return "--unknown--"_cs;
143 }
144
145 bool isError(int errorCode) {
146 return errorCode >= ErrorType::LEGACY_ERROR && errorCode <= ErrorType::ERR_MAX;
147 }
148
150 bool isError(std::string_view name) {
151 cstring lookup(name);
152 // Some diagnostics might be both errors and warning/info
153 // (e.g. "invalid" -> both ERR_INVALID and WARN_INVALID).
154 bool error = false;
155 for (const auto &pair : errorCatalog) {
156 if (pair.second == lookup) {
157 if (!isError(pair.first)) return false;
158 error = true;
159 }
160 }
161
162 return error;
163 }
164
165 bool diagnosticExists(std::string_view name) {
166 cstring lookup(name);
167 for (const auto &pair : errorCatalog) {
168 if (pair.second == lookup) return true;
169 }
170 return false;
171 }
172
173 void initReporter(ErrorReporter &reporter);
174
175 private:
176 ErrorCatalog() {}
177
179 static std::map<int, cstring> errorCatalog;
180};
181
182} // namespace P4
183
184#endif /* LIB_ERROR_CATALOG_H_ */
Definition error_catalog.h:109
static ErrorCatalog & getCatalog()
Return the singleton object.
Definition error_catalog.h:112
bool add(const char *name, bool forceReplace=false)
Definition error_catalog.h:124
bool isError(std::string_view name)
return true if the given diagnostic can only be an error; false otherwise
Definition error_catalog.h:150
cstring getName(int errorCode)
retrieve the name for errorCode
Definition error_catalog.h:138
bool diagnosticExists(std::string_view name)
return true if the given diagnostic name exists in the catalog
Definition error_catalog.h:165
Definition error_reporter.h:49
Definition error_catalog.h:35
Definition cstring.h:85
Definition cstring.h:80
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:58