2

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

Lundin
  • 195,001
  • 40
  • 254
  • 396
Zimo93
  • 31
  • 4
  • 2
    When asking about pragmas, please always specify which compiler you are using, since they are always, per definition, compiler-specific. – Lundin Jan 20 '23 at 11:24
  • Same weird problem as in this question: https://stackoverflow.com/questions/75108342/how-to-properly-cast-null-according-to-misra. Is it related to that? Again, rule 10.5 doesn't have jack to do with null pointers. – Lundin Jan 20 '23 at 11:36
  • @Lundin No it is not. I just wanted to understand the logic and put any misra rule into the quotes. – Zimo93 Jan 20 '23 at 11:40
  • to be more precise, it's about rule 11.9 @Lundin but again, the question is not about the misra rule and null pointer, but about understanding #define exp1 (_Pragma("expr2") suffix _Pragma("expr2 restore")) – Zimo93 Jan 20 '23 at 11:42
  • 1
    Yeah I actually guessed it was something like that :) I posted an answer. – Lundin Jan 20 '23 at 11:44
  • Note well that `__pragma` and `_Pragma` are different things. Only the latter is defined by the C language specification, and this is the one that appears in the code excerpt, but the title and prose of this question talk about the former. – John Bollinger Jan 20 '23 at 14:32

2 Answers2

1

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++.

  • 3
    "_Pragma macro is a way to use the #pragma directive in a way that is portable across different compilers" No, pragmas are by their very definition not portable and compilers encountering an unknown pragma is required to ignore it. The purpose of `_Pragma` is to allow pragmas to be used inside macros - otherwise it is equivalent to `#pragma`. – Lundin Jan 20 '23 at 11:23
  • 1
    There is no _MISRA C_ tool - MISRA C is a set of guidelines, enforced by a tool such as LDRA Toolsuite, PRQA's QA-C, or PC-Lint (list not exclusive) – Andrew Jan 20 '23 at 12:58
  • @Andrew, there are static analysers enforcing MISRA C rules. See [https://www.bugseng.com/eclair](Eclair). – Idriss Riouak Jan 20 '23 at 14:24
  • With respect, Idriss, I know all about MISRA ;-) – Andrew Jan 22 '23 at 09:22
  • @Andrew Fair enough. I was a bit unclear in my explanation. – Idriss Riouak Jan 22 '23 at 10:24
1

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Also every implementation-defined behavior, such as the use of pragmas, must be documented for MISRA C compliance. So you should be able to read up about what these pragmas do in your MISRA documentation, if you for example have a company-wide coding standard for MISRA C. – Lundin Jan 20 '23 at 11:48
  • This will probably not work, as the violation will be reported on the expansion of the macro – Andrew Jan 20 '23 at 12:57
  • @Andrew Worse, it messes up the code, is distracting and makes it hard to read. I prefer to use a separate config file as per the specific tool's preference, telling it which rules to check for and which to ignore. – Lundin Jan 20 '23 at 13:04