|
BDE 4.14.0 Production release
|
Macros | |
| #define | BSLA_NONNULLARG(...) |
| #define | BSLA_NONNULLARGS |
Provide macros to hint at null arguments to functions.
BSLA_NONNULLARGS is activeBSLA_NONNULLARG is activeThis component provides preprocessor macros that define compiler-specific compile-time annotations. These macros instruct the compiler to warn if null is passed to certain arguments to a function, or, on platforms where the feature is not supported, expand to nothing.
Note that the annotations cause warnings to be emitted if the covered argument is passed:
NULLstatic_cast<TYPE *>(0)nullptr (with C++11)const variable known to be 0 (clang only, no warning with g++)BSLA_NONNULLARGS: This annotation indicates that a compiler warning is to be issued if any of the pointer arguments to this function are passed null.
BSLA_NONNULLARG(...): This annotation, passed a variable-length list of positive integers, indicates that a compiler warning is to be issued if null is passed to a pointer argument at any of the specified indices, where the first argument of the annotated function has an index of 1. Note that for non-static member functions, the implicit this argument has index 1.
BSLA_NONNULLARG_IS_ACTIVE: The macro BSLA_NONNULLARGS_IS_ACTIVE is defined if BSLA_NONNULLARGS expands to something with the desired effect; otherwise BSLA_NONNULLARGS_IS_ACTIVE is not defined and BSLA_NONNULLARGS expands to nothing.
BSLS_NONNULLARGS_IS_ACTIVE: The macro BSLA_NONNULLARG_IS_ACTIVE is defined if BSLA_NONNULLARG expands to something with the desired effect; otherwise BSLA_NONNULLARG_IS_ACTIVE is not defined and BSLA_NONNULLARG expands to nothing.
This section illustrates intended use of this component.
First, we define a function, usagePrint1, annotated such that a compiler warning will occur if the first argument of the annotated function is passed 0, NULL, nullptr, or (on clang) a null pointer constant expression:
Then, we define a nearly identical function annotated with BSLA_NONNULLARGS instead. Note that only pointer arguments are affected by this annotation – repetition is not affected and may be passed 0 without a warning being emitted:
So the two different annotations on these functions have an identical effect – affecting the string argument but not the repetition argument.
Next, in main, we call both functions with a non-null first argument, and observe that no warning occurs. Note that even though 0 is passed to the integer argument to usagePrint2 and the BSLA_NONNULLARGS annotation was used, non-pointer arguments are not affected by that annotation:
Then, we call both functions passing the first argument a variable whose value is known by the compiler to be null, but since np1 is a non-const variable, no warning is issued:
Now, we call both functions passing various forms of constant null pointer expressions to the first argument:
Finally, we observe that the above calls result in the following warnings with clang w/C++11 support:
| #define BSLA_NONNULLARG | ( | ... | ) |
| #define BSLA_NONNULLARGS |