[PATCH] D135287: Disallow dereferencing of void* in C++.

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 6 06:56:20 PDT 2022


aaron.ballman added a comment.

In D135287#3837617 <https://reviews.llvm.org/D135287#3837617>, @erichkeane wrote:

> In D135287#3837584 <https://reviews.llvm.org/D135287#3837584>, @jrtc27 wrote:
>
>> What about `__typeof__(*p)`?
>
> Yes, that would also be an error in C++, as it is on all other compilers.

This actually relates to a use case I brought up in the RFC -- in an unevaluated context where the expression is only used to determine a type, it doesn't seem particularly harmful to allow the dereference. There might be generic programming cases where this comes up for people who exclusively use Clang, but I'm not 100% certain.



================
Comment at: clang/docs/ReleaseNotes.rst:89-98
+- Clang now diagnoses a Warning-As-Default-Error on indirection of void* in C++
+  mode for ISO C++, GCC, ICC, and MSVC Compatibility. This is also now a SFINAE
+  error so constraint checking and SFINAE checking can be compatible with other
+  compilers. It is likely that this will be made a permanent error in Clang 17.
+
+  .. code-block:: c
+
----------------
Wordsmithing


================
Comment at: clang/include/clang/Basic/DiagnosticSemaKinds.td:6934-6935
   "indirection requires pointer operand (%0 invalid)">;
 def ext_typecheck_indirection_through_void_pointer : ExtWarn<
   "ISO %select{C|C++}0 does not allow indirection on operand of type %1">,
   InGroup<DiagGroup<"void-ptr-dereference">>;
----------------
We no longer use this diagnostic in C++, so might as well simplify it.


================
Comment at: clang/include/clang/Basic/DiagnosticSemaKinds.td:6939-6942
+      // Note: This uses a different diagnostics group than the C diagnostic
+      // so that projects that have disabled the above will get this diagnostic,
+      // and be aware of the deprecation.
+      InGroup<DiagGroup<"void-ptr-dereference-cpp">>,
----------------
Based on the code search for people using that diagnostic flag, I don't think we need to do this -- it seems like only a very small number of projects disable that warning (at least from a code search on sourcegraph). We still need a separate diagnostic (because of the `DefaultError`, but I think we can re-use the old diagnostic group. WDYT?


================
Comment at: clang/include/clang/Basic/DiagnosticSemaKinds.td:6942-6944
+      InGroup<DiagGroup<"void-ptr-dereference-cpp">>,
+      DefaultError,
+      SFINAEFailure;
----------------



================
Comment at: clang/test/SemaCXX/disallow_void_deref.cpp:1
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
+
----------------
You should also add a RUN line showing the behavior when the diagnostic is not an error and is disabled entirely.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D135287/new/

https://reviews.llvm.org/D135287



More information about the cfe-commits mailing list