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 // FIXME: make these constexpr
40 static const int LEGACY_ERROR;
41 static const int ERR_UNKNOWN; // unknown construct (in context)
42 static const int ERR_UNSUPPORTED; // unsupported construct
43 static const int ERR_UNEXPECTED; // unexpected construct
44 static const int ERR_UNINITIALIZED; // uninitialized reads/writes
45 static const int ERR_EXPECTED; // language, compiler expects a different construct
46 static const int ERR_NOT_FOUND; // A different way to say ERR_EXPECTED
47 static const int ERR_INVALID; // invalid construct
48 static const int ERR_EXPRESSION; // expression related errors
49 static const int ERR_OVERLIMIT; // program node exceeds target limits
50 static const int ERR_INSUFFICIENT; // program node does not have enough of ...
51 static const int ERR_TYPE_ERROR; // P4 type checking errors
52 static const int ERR_UNSUPPORTED_ON_TARGET; // target can not handle construct
53 static const int ERR_DUPLICATE; // duplicate objects
54 static const int ERR_IO; // IO error
55 static const int ERR_UNREACHABLE; // unreachable code
56 static const int ERR_MODEL; // something is wrong with the target model
57 static const int ERR_TABLE_KEYS; // something is wrong with a table key
58 static const int ERR_RESERVED; // Reserved for target use
59 // Backends should extend this class with additional errors in the range 500-999.
60 static const int ERR_MIN_BACKEND = 500; // first allowed backend error code
61 static const int ERR_MAX = 999; // last allowed error code
62
63 // -------- Warnings -----------
64 // warnings as initially defined with a format string
65 static const int LEGACY_WARNING;
66 static const int WARN_FAILED; // non-fatal failure!
67 static const int WARN_UNKNOWN; // unknown construct (in context)
68 static const int WARN_INVALID; // invalid construct
69 static const int WARN_UNSUPPORTED; // unsupported construct
70 static const int WARN_DEPRECATED; // deprecated feature
71 static const int WARN_UNINITIALIZED; // unitialized instance
72 static const int WARN_UNINITIALIZED_USE; // use of uninitialized value
73 static const int WARN_UNINITIALIZED_OUT_PARAM; // output parameter may be uninitialized
74 static const int WARN_UNUSED; // unused instance
75 static const int WARN_MISSING; // missing construct
76 static const int WARN_ORDERING; // inconsistent statement ordering
77 static const int WARN_MISMATCH; // mismatched constructs
78 static const int WARN_OVERFLOW; // values do not fit
79 static const int WARN_IGNORE_PROPERTY; // invalid property for object, ignored
80 static const int WARN_TYPE_INFERENCE; // type inference can not infer, substitutes
81 static const int WARN_PARSER_TRANSITION; // parser transition non-fatal issues
82 static const int WARN_UNREACHABLE; // unreachable code
83 static const int WARN_SHADOWING; // instance shadowing
84 static const int WARN_IGNORE; // simply ignore
85 static const int WARN_INVALID_HEADER; // access to fields of an invalid header
86 static const int WARN_DUPLICATE_PRIORITIES; // two entries with the same priority
87 static const int WARN_ENTRIES_OUT_OF_ORDER; // entries with priorities out of order
88 static const int WARN_MULTI_HDR_EXTRACT; // same header may be extracted more than once
89 static const int WARN_EXPRESSION; // expression related warnings
90 static const int WARN_DUPLICATE; // duplicate objects
91 static const int WARN_BRANCH_HINT; // branch frequency/likely hints
92 static const int WARN_TABLE_KEYS; // something is wrong with a table key
93 // Backends should extend this class with additional warnings in the range 1500-2141.
94 static const int WARN_MIN_BACKEND = 1500; // first allowed backend warning code
95 static const int WARN_MAX = 2141; // last allowed warning code
96
97 // -------- Info messages -------------
98 // info messages as initially defined with a format string
99 static const int INFO_INFERRED; // information inferred by compiler
100 static const int INFO_PROGRESS; // compilation progress
101 static const int INFO_REMOVED; // instance removed
102
103 // Backends should extend this class with additional info messages in the range 3000-3999.
104 static const int INFO_MIN_BACKEND = 3000; // first allowed backend info code
105 static const int INFO_MAX = 3999; // last allowed info code
106};
107
108class ErrorCatalog {
109 public:
111 static ErrorCatalog &getCatalog() {
112 static ErrorCatalog instance;
113 return instance;
114 }
115
122 template <MessageType type, int errorCode>
123 bool add(const char *name, bool forceReplace = false) {
124 static_assert(type != MessageType::Error ||
125 (errorCode >= ErrorType::ERR_MIN_BACKEND && errorCode <= ErrorType::ERR_MAX));
126 static_assert(type != MessageType::Warning || (errorCode >= ErrorType::WARN_MIN_BACKEND &&
127 errorCode <= ErrorType::WARN_MAX));
128 static_assert(type != MessageType::Info || (errorCode >= ErrorType::INFO_MIN_BACKEND &&
129 errorCode <= ErrorType::INFO_MAX));
130 static_assert(type != MessageType::None);
131 if (forceReplace) errorCatalog.erase(errorCode);
132 auto it = errorCatalog.emplace(errorCode, name);
133 return it.second;
134 }
135
137 cstring getName(int errorCode) {
138 using namespace P4::literals;
139
140 if (errorCatalog.count(errorCode)) return errorCatalog.at(errorCode);
141 return "--unknown--"_cs;
142 }
143
144 bool isError(int errorCode) {
145 return errorCode >= ErrorType::LEGACY_ERROR && errorCode <= ErrorType::ERR_MAX;
146 }
147
149 bool isError(std::string_view name) {
150 cstring lookup(name);
151 // Some diagnostics might be both errors and warning/info
152 // (e.g. "invalid" -> both ERR_INVALID and WARN_INVALID).
153 bool error = false;
154 for (const auto &pair : errorCatalog) {
155 if (pair.second == lookup) {
156 if (!isError(pair.first)) return false;
157 error = true;
158 }
159 }
160
161 return error;
162 }
163
164 void initReporter(ErrorReporter &reporter);
165
166 private:
167 ErrorCatalog() {}
168
170 static std::map<int, cstring> errorCatalog;
171};
172
173} // namespace P4
174
175#endif /* LIB_ERROR_CATALOG_H_ */
Definition error_catalog.h:108
static ErrorCatalog & getCatalog()
Return the singleton object.
Definition error_catalog.h:111
bool add(const char *name, bool forceReplace=false)
Definition error_catalog.h:123
bool isError(std::string_view name)
return true if the given diagnostic can only be an error; false otherwise
Definition error_catalog.h:149
cstring getName(int errorCode)
retrieve the name for errorCode
Definition error_catalog.h:137
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