https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102136
Bug ID: 102136 Summary: bogus warnings for function calls in unevaluated contexts Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- GCC implements a number of warnings in the front ends that are intended to detect bugs that would occur during the evaluation of the checked expression. But in contexts where the expression is not evaluated such as in operands to _Alignof or sizeof the warnings are false positives. Using the result of a function call in such a context might be way of determining the size of the return type of the function. In most cases it's easy to avoid these false positives by providing a valid argument. Where it's harder is in calls to pure virtual member functions as in the test case below: $ cat t.C && gcc -S -Wall t.C struct A { virtual int f () = 0; }; enum { e0 = sizeof __builtin_strlen (0), e1 = sizeof (__typeof__ (__builtin_strlen (0))), e2 = __alignof__ __builtin_printf ("%s", 0), e3 = sizeof ((A*)0)->f () }; t.C:4:32: warning: argument 1 null where non-null expected [-Wnonnull] 4 | e0 = sizeof __builtin_strlen (0), | ~~~~~~~~~~~~~~~~~^~~ <built-in>: note: in a call to function ‘long unsigned int __builtin_strlen(const char*)’ declared ‘nonnull’ t.C:5:45: warning: argument 1 null where non-null expected [-Wnonnull] 5 | e1 = sizeof (__typeof__ (__builtin_strlen (0))), | ~~~~~~~~~~~~~~~~~^~~ <built-in>: note: in a call to function ‘long unsigned int __builtin_strlen(const char*)’ declared ‘nonnull’ t.C:6:40: warning: format ‘%s’ expects argument of type ‘char*’, but argument 2 has type ‘int’ [-Wformat=] 6 | e2 = __alignof__ __builtin_printf ("%s", 0), | ~^ ~ | | | | | int | char* | %d t.C:7:26: warning: ‘this’ pointer is null [-Wnonnull] 7 | e3 = sizeof ((A*)0)->f () | ~~~~~~~~~~~^~ t.C:1:24: note: in a call to non-static member function ‘virtual int A::f()’ 1 | struct A { virtual int f () = 0; }; | ^