jamieschmeiser created this revision. jamieschmeiser added reviewers: anarazel, efriedma. Herald added a project: All. jamieschmeiser requested review of this revision. Herald added a project: clang.
Change the warning produced for subtraction from (or with) a null pointer to only be produced when the code path is live. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D126816 Files: clang/lib/Sema/SemaExpr.cpp clang/test/Sema/pointer-subtraction.c Index: clang/test/Sema/pointer-subtraction.c =================================================================== --- clang/test/Sema/pointer-subtraction.c +++ clang/test/Sema/pointer-subtraction.c @@ -11,6 +11,16 @@ 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 Index: clang/lib/Sema/SemaExpr.cpp =================================================================== --- clang/lib/Sema/SemaExpr.cpp +++ clang/lib/Sema/SemaExpr.cpp @@ -10845,8 +10845,10 @@ 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.
Index: clang/test/Sema/pointer-subtraction.c =================================================================== --- clang/test/Sema/pointer-subtraction.c +++ clang/test/Sema/pointer-subtraction.c @@ -11,6 +11,16 @@ 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 Index: clang/lib/Sema/SemaExpr.cpp =================================================================== --- clang/lib/Sema/SemaExpr.cpp +++ clang/lib/Sema/SemaExpr.cpp @@ -10845,8 +10845,10 @@ 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.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits