Skip to content

Commit

Permalink
Added range check of error codes (#4249)
Browse files Browse the repository at this point in the history
* Added range check of error codes

* Added INFO_PROGRESS error code

* Fixed isError range

* Made range checks compile-time
  • Loading branch information
dmatousek authored and fruffy committed Nov 21, 2023
1 parent 1406aa6 commit fa38080
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
14 changes: 6 additions & 8 deletions lib/error_catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ const int ErrorType::ERR_IO = 14;
const int ErrorType::ERR_UNREACHABLE = 15;
const int ErrorType::ERR_MODEL = 16;
const int ErrorType::ERR_RESERVED = 17;
// If we specialize for 1000 error types we're good!
const int ErrorType::ERR_MAX_ERRORS = 999;

// ------ Warnings -----------
const int ErrorType::LEGACY_WARNING = ERR_MAX_ERRORS + 1;
const int ErrorType::LEGACY_WARNING = ERR_MAX + 1;
const int ErrorType::WARN_FAILED = 1001;
const int ErrorType::WARN_UNKNOWN = 1002;
const int ErrorType::WARN_INVALID = 1003;
Expand All @@ -65,11 +63,10 @@ const int ErrorType::WARN_UNINITIALIZED_USE = 1019;
const int ErrorType::WARN_INVALID_HEADER = 1020;
const int ErrorType::WARN_DUPLICATE_PRIORITIES = 1021;
const int ErrorType::WARN_ENTRIES_OUT_OF_ORDER = 1022;
const int ErrorType::WARN_MAX_WARNINGS = 2142;

// ------ Info message -----------
const int ErrorType::INFO_INFERRED = WARN_MAX_WARNINGS + 1;
const int ErrorType::INFO_MAX_INFOS = 3999;
// ------ Info messages -----------
const int ErrorType::INFO_INFERRED = WARN_MAX + 1;
const int ErrorType::INFO_PROGRESS = 2143;

// map from errorCode to ErrorSig
std::map<int, cstring> ErrorCatalog::errorCatalog = {
Expand Down Expand Up @@ -118,4 +115,5 @@ std::map<int, cstring> ErrorCatalog::errorCatalog = {
{ErrorType::WARN_ENTRIES_OUT_OF_ORDER, "entries_out_of_priority_order"},

// Info messages
{ErrorType::INFO_INFERRED, "inferred"}};
{ErrorType::INFO_INFERRED, "inferred"},
{ErrorType::INFO_PROGRESS, "progress"}};
35 changes: 25 additions & 10 deletions lib/error_catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ limitations under the License.
#include <string>

#include "cstring.h"
#include "lib/error_message.h"
#include "lib/exceptions.h"

using MessageType = ErrorMessage::MessageType;

/// enumerate supported errors
/// Backends should extend this class with additional errors in the range 500-999 and
/// warnings in the range 1500-2141.
/// It is a class and not an enum class because in C++11 you can't extend an enum class
class ErrorType {
public:
Expand All @@ -48,9 +50,9 @@ class ErrorType {
static const int ERR_UNREACHABLE; // unreachable parser state
static const int ERR_MODEL; // something is wrong with the target model
static const int ERR_RESERVED; // Reserved for target use

// If we specialize for 1000 error types we're good!
static const int ERR_MAX_ERRORS;
// Backends should extend this class with additional errors in the range 500-999.
static const int ERR_MIN_BACKEND = 500; // first allowed backend error code
static const int ERR_MAX = 999; // last allowed error code

// -------- Warnings -----------
// warnings as initially defined with a format string
Expand All @@ -77,14 +79,18 @@ class ErrorType {
static const int WARN_INVALID_HEADER; // access to fields of an invalid header
static const int WARN_DUPLICATE_PRIORITIES; // two entries with the same priority
static const int WARN_ENTRIES_OUT_OF_ORDER; // entries with priorities out of order

static const int WARN_MAX_WARNINGS;
// Backends should extend this class with additional warnings in the range 1500-2141.
static const int WARN_MIN_BACKEND = 1500; // first allowed backend warning code
static const int WARN_MAX = 2141; // last allowed warning code

// -------- Info messages -------------
// info messages as initially defined with a format string
static const int INFO_INFERRED; // information inferred by compiler
static const int INFO_PROGRESS; // compilation progress

static const int INFO_MAX_INFOS;
// Backends should extend this class with additional info messages in the range 3000-3999.
static const int INFO_MIN_BACKEND = 3000; // first allowed backend info code
static const int INFO_MAX = 3999; // last allowed info code
};

class ErrorCatalog {
Expand All @@ -97,10 +103,19 @@ class ErrorCatalog {

/// add to the catalog
/// returns false if the code already exists and forceReplace was not set to true
/// @param type - error/warning/info message
/// @param errorCode - integer value for the error/warning
/// @param name - name for the error. Used to enable/disable all errors of that type
/// @param forceReplace - override an existing error type in the catalog
bool add(int errorCode, const char *name, bool forceReplace = false) {
template <MessageType type, int errorCode>
bool add(const char *name, bool forceReplace = false) {
static_assert(type != MessageType::Error ||
(errorCode >= ErrorType::ERR_MIN_BACKEND && errorCode <= ErrorType::ERR_MAX));
static_assert(type != MessageType::Warning || (errorCode >= ErrorType::WARN_MIN_BACKEND &&
errorCode <= ErrorType::WARN_MAX));
static_assert(type != MessageType::Info || (errorCode >= ErrorType::INFO_MIN_BACKEND &&
errorCode <= ErrorType::INFO_MAX));
static_assert(type != MessageType::None);
if (forceReplace) errorCatalog.erase(errorCode);
auto it = errorCatalog.emplace(errorCode, name);
return it.second;
Expand All @@ -125,7 +140,7 @@ class ErrorCatalog {
bool isError(cstring name) {
int code = getCode(name);
if (code == -1) return false;
if (code >= ErrorType::ERR_MAX_ERRORS) return false;
if (code > ErrorType::ERR_MAX) return false;
return true;
}

Expand Down

0 comments on commit fa38080

Please sign in to comment.