[clang] d8c227b - Revert "Produce warning for performing pointer arithmetic on a null pointer."

Nico Weber via cfe-commits cfe-commits at lists.llvm.org
Wed May 12 11:54:07 PDT 2021


Author: Nico Weber
Date: 2021-05-12T14:53:50-04:00
New Revision: d8c227ba05d065046bdb8671f1df73dabaffa222

URL: https://github.com/llvm/llvm-project/commit/d8c227ba05d065046bdb8671f1df73dabaffa222
DIFF: https://github.com/llvm/llvm-project/commit/d8c227ba05d065046bdb8671f1df73dabaffa222.diff

LOG: Revert "Produce warning for performing pointer arithmetic on a null pointer."

This reverts commit dfc1e31d49fe1380c9bab43373995df5fed15e6d.
See discussion on https://reviews.llvm.org/D98798

Added: 
    

Modified: 
    clang/lib/Sema/SemaExpr.cpp
    clang/test/Sema/pointer-addition.c

Removed: 
    clang/test/Sema/pointer-addition.cpp


################################################################################
diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 719161fb9ba1e..b8be6732b758f 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -10779,17 +10779,7 @@ QualType Sema::CheckSubtractionOperands(ExprResult &LHS, ExprResult &RHS,
                                                LHS.get(), RHS.get()))
         return QualType();
 
-      bool LHSIsNullPtr = LHS.get()->IgnoreParenCasts()->isNullPointerConstant(
-          Context, Expr::NPC_ValueDependentIsNotNull);
-      bool RHSIsNullPtr = RHS.get()->IgnoreParenCasts()->isNullPointerConstant(
-          Context, Expr::NPC_ValueDependentIsNotNull);
-
-      // Subtracting nullptr or from nullptr should produce
-      // a warning expect nullptr - nullptr is valid in C++ [expr.add]p7
-      if (LHSIsNullPtr && (!getLangOpts().CPlusPlus || !RHSIsNullPtr))
-        diagnoseArithmeticOnNullPointer(*this, Loc, LHS.get(), false);
-      if (RHSIsNullPtr && (!getLangOpts().CPlusPlus || !LHSIsNullPtr))
-        diagnoseArithmeticOnNullPointer(*this, Loc, RHS.get(), false);
+      // FIXME: Add warnings for nullptr - ptr.
 
       // The pointee type may have zero size.  As an extension, a structure or
       // union may have zero size or an array may have zero length.  In this

diff  --git a/clang/test/Sema/pointer-addition.c b/clang/test/Sema/pointer-addition.c
index ab641bd132363..562f05340f7cb 100644
--- a/clang/test/Sema/pointer-addition.c
+++ b/clang/test/Sema/pointer-addition.c
@@ -29,7 +29,4 @@ void a(S* b, void* c) {
   // Cases that don't match the GNU inttoptr idiom get a 
diff erent warning.
   f = (char*)0 - i; // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
   int *g = (int*)0 + i; // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
-  f = (char*)((char*)0 - f); // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
-  f = (char*)(f - (char*)0); // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
-  f = (char*)((char*)0 - (char*)0); // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}} expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
 }

diff  --git a/clang/test/Sema/pointer-addition.cpp b/clang/test/Sema/pointer-addition.cpp
deleted file mode 100644
index 5e09034ed9651..0000000000000
--- a/clang/test/Sema/pointer-addition.cpp
+++ /dev/null
@@ -1,8 +0,0 @@
-// RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic -Wextra -std=c++11
-
-void a() {
-  char *f = (char*)0;
-  f = (char*)((char*)0 - f); // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
-  f = (char*)(f - (char*)0); // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
-  f = (char*)((char*)0 - (char*)0); // valid in C++
-}


        


More information about the cfe-commits mailing list