[PATCH] D134461: [Clang] Warn when trying to deferencing void pointers in C

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 23 06:33:19 PDT 2022


aaron.ballman added inline comments.


================
Comment at: clang/include/clang/Basic/DiagnosticSemaKinds.td:6922
 def ext_typecheck_indirection_through_void_pointer : ExtWarn<
-  "ISO C++ does not allow indirection on operand of type %0">,
+  "ISO %select{C++|C}0 does not allow indirection on operand of type %1">,
   InGroup<DiagGroup<"void-ptr-dereference">>;
----------------
Swapping these so the logic is more straightforward elsewhere.


================
Comment at: clang/lib/Sema/SemaExpr.cpp:14538-14544
+    if (S.getLangOpts().CPlusPlus)
+      S.Diag(OpLoc, diag::ext_typecheck_indirection_through_void_pointer)
+          << /* C++ mode */ 0 << OpTy << Op->getSourceRange();
+    else if (!S.getLangOpts().CPlusPlus)
+      if (!(S.getLangOpts().C99 && IsAfterAmp))
+        S.Diag(OpLoc, diag::ext_typecheck_indirection_through_void_pointer)
+            << /* C mode */ 1 << OpTy << Op->getSourceRange();
----------------
Simplifying the logic a bit.


================
Comment at: clang/test/C/drs/dr1xx.c:140
   /* The behavior changed between C89 and C99. */
-  (void)&*p; /* c89only-warning {{ISO C forbids taking the address of an expression of type 'void'}} */
+  (void)&*p; /* c89only-warning {{ISO C forbids taking the address of an expression of type 'void'}} c89only-warning {{ISO C does not allow indirection on operand of type 'void *'}} */
   /* The behavior of all three of these is undefined. */
----------------
Can you switch all of the warning changes in this file to use this style where each expected diagnostic is on its own line? That makes it easier to notice which diagnostics happen on the line (it's easy to lose sight of the trailing expected diagnostics otherwise).


================
Comment at: clang/test/C/drs/dr1xx.c:143
+  (void)*p; /* expected-warning {{ISO C does not allow indirection on operand of type 'void *'}}*/
+  (void)&(*p); /* c89only-warning {{ISO C forbids taking the address of an expression of type 'void'}} expected-warning {{ISO C does not allow indirection on operand of type 'void *'}}*/
+  (void)(i ? *p : *p); /* expected-warning {{ISO C does not allow indirection on operand of type 'void *'}} expected-warning {{ISO C does not allow indirection on operand of type 'void *'}}*/
----------------
This looks wrong to me -- this should be an `expected-warning` instead of a `c89only-warning`, same as two lines above, right?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134461



More information about the cfe-commits mailing list