How to annotate functions to aid code analysis

58 views Asked by At

Consider the following functions:

constexpr bool IsNull(const int* arg)
{
    return arg == nullptr;
}

void Test()
{
    int x{ 42 };
    int* p = &x;    // C26429 : Symbol 'p' is never tested for nullness
    if (!IsNull(p))
        *p = 5;
}

Warning C26429 occurs because the compiler does not 'see' the pointer check inside of function IsNull().

MSVC has the __assume(exp) intrinsic function to tell the compiler that exp should be considered true. But is there any way to annotate function IsNull to indicate that a certain post condition has been met if it returns true? Similar to the SAL annotations?

I haven't found anything so I think I have to revert to converting IsNull into a macro function:

#define IsNull(P) (!(P))
0

There are 0 answers