[PATCH] D37042: Teach clang to tolerate the 'p = nullptr + n' idiom used by glibc

Richard Smith - zygoloid via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 30 14:55:36 PDT 2017


rsmith added inline comments.


================
Comment at: lib/AST/Expr.cpp:1857
+  if (!PExp->IgnoreParenCasts()
+          ->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNull))
+    return false;
----------------
If we get here with a value-dependent expression, we should treat it as non-null (do not warn on `(char*)PtrTemplateParameter + N`).


================
Comment at: lib/Sema/SemaExpr.cpp:8804
+  if (PExp->IgnoreParenCasts()->isNullPointerConstant(
+          Context, Expr::NPC_ValueDependentIsNull)) {
+    // In C++ adding zero to a null pointer is defined.
----------------
Likewise here, we should treat a value-dependent expression as non-null.


================
Comment at: lib/Sema/SemaExpr.cpp:8877
     if (RHS.get()->getType()->isIntegerType()) {
+      // Subtracting from a null pointer should produce a warning.
+      // The last argument to the diagnose call says this doesn't match the
----------------
Subtracting zero from a null pointer should not warn in C++.

(Conversely, subtracting a non-null pointer from a pointer should warn in C++, and subtracting any pointer from a null pointer should warn in C.)


================
Comment at: lib/Sema/SemaExpr.cpp:8881
+      if (LHS.get()->IgnoreParenCasts()->isNullPointerConstant(Context,
+                                           Expr::NPC_ValueDependentIsNull))
+        diagnoseArithmeticOnNullPointer(*this, Loc, LHS.get(), false);
----------------
Likewise.


https://reviews.llvm.org/D37042





More information about the cfe-commits mailing list