Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added range check of error codes #4249

Merged
merged 4 commits into from
Nov 16, 2023
Merged

Conversation

dmatousek
Copy link
Contributor

@dmatousek dmatousek commented Nov 15, 2023

The PR adds named constants for min and max values of error, warning, and info message codes to be used in backends. Also, the ErrorCatalog::add method now expects a code type so that it can perform range check. E.g.:

ErrorCatalog::getCatalog().add<ErrorMessage::MessageType::Info, INFO_USER_CODE>("user-code");

If the range check fails, a bug is triggered.

In addition, a new info code INFO_PROGRESS is added.

/// @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) {
bool add(MessageType type, int errorCode, const char *name, bool forceReplace = false) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These all appear like compile-time constants. Maybe it is possible to make this a compile-time range check using constexpr?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would require actually calling add in compile-time context. However map::emplace is not constexpr (not even in C++23). Or it would need to be a template, somewhat like this:

template<MessageType type, int errCode>
bool add(const char *name, bool forceReplace = flase) {
   static_assert(type !=  MessageType::Error ||
            (errorCode >= ErrorType::ERR_MIN_BACKEND && errorCode < ErrorType::ERR_MAX),
            "Error code out of range");
   ...
}

That would work too (I believe). It would be called like this:

errorCatalog.add<MessageType::Error, Err::ERR_FOO>("foo");

Which is not super-nice, but I'd say it is worth the added safety.

For this to work, the constants would need to be declared in the .h file. Probably as

static constexpr int ERR_MIN_BACKEND = ...;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll let you decide whether you consider it worth implementing. I think it'd be nice, since runtime BUGs are a persistence source of annoyance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your suggestions, they really make sense!

/// @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) {
bool add(MessageType type, int errorCode, const char *name, bool forceReplace = false) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll let you decide whether you consider it worth implementing. I think it'd be nice, since runtime BUGs are a persistence source of annoyance.

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 ||
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat, does this work errorcode or should we make explicit that it is a constexpr? Same for all the error codes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, what do you mean?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the integers are const not constexpr. Does this work with static_assert? Or does the compiler convert them automatically to compile time constants?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it works. The compilation fails with:

lib/error_catalog.h:112:50: error: static assertion failed
  112 |         static_assert(type != MessageType::Error ||
      |                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
  113 |             (errorCode >= ErrorType::ERR_MIN_BACKEND && errorCode <= ErrorType::ERR_MAX));
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I believe that for an integral type const implies consexpr.

@dmatousek
Copy link
Contributor Author

What do I have to do to be able to merge p4c PRs?

@fruffy
Copy link
Collaborator

fruffy commented Nov 16, 2023

I think we need to add you as a committer. Working on that. I can merge it for you.

@dmatousek
Copy link
Contributor Author

Thank you!

@fruffy fruffy merged commit cd3c93a into p4lang:main Nov 16, 2023
13 checks passed
@fruffy
Copy link
Collaborator

fruffy commented Nov 17, 2023

There should be a person at Intel that can add you to p4lang, is that not the case anymore?

fruffy pushed a commit that referenced this pull request Nov 21, 2023
* Added range check of error codes

* Added INFO_PROGRESS error code

* Fixed isError range

* Made range checks compile-time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants