I was recently looking at some code where clang generated warnings due to -Wtautological-pointer-compare.
The code could be simplified to something like:
void foo(const char*s) __attribute__((nonnull)) {
if (s) { /* Test added just in case*/
if (s[0]=='a') s[0]='b'; /* Dummy code using the pointer */
}
}
Obviously if we trust the attributes then s cannot be null and the warning is redundant. However, to me it seems best to handle the null-case in function (since we cannot trust that the calling code is compiled with these warnings, or that people read warnings) - while still detecting other null-pointer issues in the code.
Thus disabling this warning (using a pragma for the entire function) seems sub-optimal.
In Visual Studio with SAL it seems you can use _In_ _Pre_defensive_ to handle
this case.
In that case,
_In_ _Pre_defensive_is preferred at a trust boundary to indicate that although a caller will get an error if it attempts to pass NULL, the function body will be analyzed as if the parameter might be NULL, and any attempts to de-reference the pointer without first checking it for NULL will be flagged.
Is something similar possible with clang?