0

I want to ask if there is a way to suppress misra warnings using a custom function without having to write \\ Misra Warning Suppression (always the same derivation) everywhere.

I often use throw std::runtime_error("Custom Error") on many places in my code. Unfortunately, I have to do misra warning suppression. So I have to add \\ Misra Warning Suppression after every throw std::runtime_error("Custom Error");. In the code it is like this: throw std::runtime_error("Custom Error"); \\ Misra Warning Suppression. Since I literally have to use it everywhere with the same derivation, I thought about shortening it.

Here, the std::exception can be any error type. So I thought about using macros like this:

#define EXCEPTION_MISRA( TYPE, MESSAGE ) \
  throw TYPE( MESSAGE) // Use Misra Suppression


int main()
{
    EXCEPTION_MISRA( std::runtime_error, "Custom Error" );
    return 0;
}

Unfortunately, macros are not really the yellow from the egg for my use case. Is there any way to do the same using functions? Kind of making your own xyz::throw function, where in the function the \\ Misra Warning Suppression is added?

Thank you!

Lukas
  • 3
  • 1
  • 1
    Why do you use MISRA C++ and exceptions in the same project? That doesn't make any sense. Exception handling is completely unacceptable in safety-related software, period. It's one of the main reasons why C++ is regarded as unsuitable for embedded systems in general and safety-related systems in particular. – Lundin Apr 21 '22 at 06:46
  • Note: The violation will be for the use of a *function-like macro* (Required Rule 16-0-4), not (as described) a *custom function* – Andrew Apr 21 '22 at 07:49

1 Answers1

1

I don't use MISRA, but would a template work? Something like

template<class E> 
[[ noreturn ]] void exception_misra(const std::string& message)
{
    throw E(message); // Use Misra Suppression
} 

called like so:

exception_misra<std::runtime_error>("Custom Error");
AndyG
  • 39,700
  • 8
  • 109
  • 143
  • MISRA is all about protecting against undesirable behaviour... wrapping something undesirable, making your code less understandable, just to *shut up the checker* is a demonstration that you do not understand the context of what you are suggesting. If you want to do something, MISRA Compliance describes the appropriate approach, and tool configuration should support that. – Andrew Apr 21 '22 at 07:41