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