[RFC] Throwing an exception with null pointers
Derek Fawcus
dfawcus+dlang at employees.org
Wed Apr 16 22:43:24 UTC 2025
On Wednesday, 16 April 2025 at 21:30:58 UTC, Richard (Rikki)
Andrew Cattermole wrote:
> From a link made previously in this thread, the state of the
> art annotation of nullability in C++:
> https://clang.llvm.org/docs/analyzer/developer-docs/nullability.html
An example using it:
```C
// Compile with -Wnullable-to-nonnull-conversion
#if defined(__clang__)
#define ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull
begin")
#define ASSUME_NONNULL_END _Pragma("clang assume_nonnull end")
#else
#define ASSUME_NONNULL_BEGIN
#define ASSUME_NONNULL_END
#define __has_feature(x) (0)
#endif /* clang */
#if __has_feature (nullability)
#define NONNULL _Nonnull
#define NULLABLE _Nullable
#define NULL_UNSPECIFIED _Null_unspecified
#else
#define NONNULL
#define NULLABLE
#define NULL_UNSPECIFIED
#endif /* nullability */
ASSUME_NONNULL_BEGIN
int *someNonNull(int *);
int *someNullable(int * NULLABLE);
int foo(int * arg1, int * NULL_UNSPECIFIED arg2, int * NULLABLE
arg3) {
int a = 0;
int * NONNULL ptr;
a += *someNullable(arg1);
a += *someNullable(arg2);
a += *someNullable(arg3);
ptr = arg1;
ptr = arg2;
ptr = arg3;
a += *someNonNull(arg1);
a += *someNonNull(arg2);
a += *someNonNull(arg3);
return a;
}
ASSUME_NONNULL_END
```
```
$ clang-14 -Wall -Wnullable-to-nonnull-conversion -c ttt.c
ttt.c:37:8: warning: implicit conversion from nullable pointer
'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'
[-Wnullable-to-nonnull-conversion]
ptr = arg3;
^
ttt.c:41:20: warning: implicit conversion from nullable pointer
'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'
[-Wnullable-to-nonnull-conversion]
a += *someNonNull(arg3);
^
ttt.c:29:16: warning: variable 'ptr' set but not used
[-Wunused-but-set-variable]
int * NONNULL ptr;
^
3 warnings generated.
```
More information about the Digitalmars-d
mailing list