Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.DEFINE.BADEXP.CPP

Inappropriate macro expansion in a C++ source

MISRA-C++ Rule 16-2-2 (required): C++ macros shall only be used for include guards, type qualifiers, or storage class specifiers.

Rationale

These are the only permitted uses of macros. C++ offers const variable and function templates, which provide a type-safe alternative to the preprocessor.

Note that the use of macros for type qualifiers and storage class specifiers will break Rule 16—2—1.

Example

// The following are compliant
#define STOR extern                   // storage class specifier
                                      // Breaks Rule 16—2—1

// The following are non-compliant
#define CLOCK (xtal/16)               // Constant expression
#define PLUS2(X) ((X) + 2)            // Macro expanding to expression
#define PI 3.14159F                   // use const object instead
#define int32_t long                  // use typedef instead
#define STARTIF if(                   // language redefinition
#define INIT(value) {(value), 0, 0}   // braced initializer
#define HEADER "filename.h"           // string literal