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