Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-958

Macros should not be #define'd or #undef'd within a block

    Details

    • Default Severity:
      Critical
    • Impact:
      High
    • Likelihood:
      Low
    • Legacy Key:
      PPDefineOrUndefFromBlock
    • Covered Languages:
      C, C++, Objective-C
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      10min
    • MISRA C 2004:
      19.5
    • MISRA C++ 2008:
      16-0-2
    • PC-Lint:
      9158, 9159

      Description

      While it is legal to place #define and #undef directives anywhere in a source file, placing them outside of the global namespace is misleading since their scope is not actually restricted. This may be inconsistent with developer expectations.

      Noncompliant Code Example

      namespace NS
      {
        #ifndef MY_HDR
        #define MY_HDR    /* Noncompliant */
        #undef FOO        /* Noncompliant */
        #endif
      }
      

      Compliant Solution

      #ifndef MY_HDR
      #define MY_HDR
      #undef FOO
      #endif
      

      See

      • MISRA C:2004, 19.5 - Macros shall not be #define'd or #undef'd within a block.
      • MISRA C++:2008, 16-0-2 - Macros shall only be #define'd or #undef'd in the global namespace.

        Attachments

          Issue Links

          1.
          C-Family RSPEC-4378 Language-Specification Active Unassigned

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                Anonymous
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated: