Home > database >  what does two __pragma inside a define macro means?
what does two __pragma inside a define macro means?

Time:01-21

I am trying to understand one line of code having two __pragma operatores inside one define macro:

#define NULL    (_Pragma("nomisrac 10.5") 0 _Pragma("nomisrac restore"))

I know that pragma can be used in macros as of the c99 standard. But I only know it for a simple use case as given here Pragma in define macro

Can anyone explains what is the purpose of this and why we have double __pragma operators seperated by the suffix "0" inside of the define macro

CodePudding user response:

This appears to be an attempt of blocking MISRA diagnostics for rule 10.5. Which is about value conversions to the inappropriate essential type and not about (null) pointer conversions, so it is senseless. Except if this is an attempt to block incorrect diagnostics by a broken static analyser like this one.

There's a more relevant MISRA rule stating that NULL shall never be (re)defined by the application in the first place. And yet another rule that NULL is the only allowed form of a null pointer constant. Blocking those diagnostics would make more sense, especially if you are the one implementing the standard library stddef.h.

As for what the pragmas do, it is common that pragmas "stack" (push/pop) message settings locally, so that you can disable a certain message just at one single place instead of in the whole project or at every trailing line from the point of the pragma. The purpose of the second pragma is to restore the default message settings.

Something like this might not be feasible(?):

#pragma nomisrac 10.5
#define NULL 0
#pragma nomisrac restore

I'm just guessing now, but since there is not necessarily any relation between #pragma and #define as such, perhaps the compiler/static analyser will still complain when this macro is fetched into some translation unit and expanded there.

Or alternatively there is no rationale and the programmer just liked to smack everything into a single line.

CodePudding user response:

The code you provided defines the macro NULL as the result of two #pragma directives. The first _Pragma("nomisrac 10.5") likely sets a specific behavior for the "nomisrac" command, which is likely a specific rule set for the code analysis tool, such as the one from the tool "MISRA C". The second _Pragma("nomisrac restore") likely restores the previous behavior for the "nomisrac" command. The value of the NULL macro is then defined as 0, which is the standard value for a null pointer in C and C .

  • Related