forked from OSchip/llvm-project
Only issue warning for subtraction involving null pointers on live code paths
Summary: Change the warning produced for subtraction from (or with) a null pointer to only be produced when the code path is live. https://github.com/llvm/llvm-project/issues/54570 Author: Jamie Schmeiser <schmeise@ca.ibm.com> Reviewed By: anarazel (Andres Freund) Differential Revision: https://reviews.llvm.org/D126816
This commit is contained in:
parent
5c902af572
commit
efbf0136b4
|
@ -10845,8 +10845,10 @@ static void diagnoseSubtractionOnNullPointer(Sema &S, SourceLocation Loc,
|
|||
if (S.Diags.getSuppressSystemWarnings() && S.SourceMgr.isInSystemMacro(Loc))
|
||||
return;
|
||||
|
||||
S.Diag(Loc, diag::warn_pointer_sub_null_ptr)
|
||||
<< S.getLangOpts().CPlusPlus << Pointer->getSourceRange();
|
||||
S.DiagRuntimeBehavior(Loc, Pointer,
|
||||
S.PDiag(diag::warn_pointer_sub_null_ptr)
|
||||
<< S.getLangOpts().CPlusPlus
|
||||
<< Pointer->getSourceRange());
|
||||
}
|
||||
|
||||
/// Diagnose invalid arithmetic on two function pointers.
|
||||
|
|
|
@ -11,6 +11,16 @@ void a(void) {
|
|||
f = (char *)(f - (char *)0); // expected-warning {{performing pointer subtraction with a null pointer has undefined behavior}}
|
||||
f = (char *)((char *)0 - (char *)0); // expected-warning {{performing pointer subtraction with a null pointer has undefined behavior}} expected-warning {{performing pointer subtraction with a null pointer has undefined behavior}}
|
||||
|
||||
if (1)
|
||||
f = (char *)((char *)0 - f); // expected-warning {{performing pointer subtraction with a null pointer has undefined behavior}}
|
||||
else
|
||||
f = (char *)((char *)0 - f);
|
||||
|
||||
if (0)
|
||||
f = (char *)((char *)0 - f);
|
||||
else
|
||||
f = (char *)((char *)0 - f); // expected-warning {{performing pointer subtraction with a null pointer has undefined behavior}}
|
||||
|
||||
#ifndef SYSTEM_WARNINGS
|
||||
SYSTEM_MACRO(f);
|
||||
#else
|
||||
|
|
|
@ -11,6 +11,16 @@ void a() {
|
|||
f = (char *)(f - (char *)0); // expected-warning {{performing pointer subtraction with a null pointer may have undefined behavior}}
|
||||
f = (char *)((char *)0 - (char *)0); // valid in C++
|
||||
|
||||
if (1)
|
||||
f = (char *)((char *)0 - f); // expected-warning {{performing pointer subtraction with a null pointer may have undefined behavior}}
|
||||
else
|
||||
f = (char *)((char *)0 - f);
|
||||
|
||||
if (0)
|
||||
f = (char *)((char *)0 - f);
|
||||
else
|
||||
f = (char *)((char *)0 - f); // expected-warning {{performing pointer subtraction with a null pointer may have undefined behavior}}
|
||||
|
||||
#ifndef SYSTEM_WARNINGS
|
||||
SYSTEM_MACRO(f);
|
||||
#else
|
||||
|
|
Loading…
Reference in New Issue