[clang] 89e56e7 - [Clang] Don't warn if deferencing void pointers in unevaluated context

Jun Zhang via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 27 21:30:30 PDT 2022


Author: Jun Zhang
Date: 2022-09-28T12:30:02+08:00
New Revision: 89e56e732d5e89d8715a501158793ac305bc4b70

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

LOG: [Clang] Don't warn if deferencing void pointers in unevaluated context

After https://reviews.llvm.org/D134461, Clang will diagnose a warning if
trying to deference void pointers in C mode. However, this causes a lot
of noises when compiling a 5.19.11 Linux kernel.

This patch reduces the warning by marking deferencing void pointers in
unevaluated context OK, like `sizeof(*void_ptr)`, `typeof(*void_ptr)`
and etc.

Fixes https://github.com/ClangBuiltLinux/linux/issues/1720

Signed-off-by: Jun Zhang <jun at junz.org>

Differential Revision: https://reviews.llvm.org/D134702

Added: 
    clang/test/Sema/no-warn-void-ptr-uneval.c

Modified: 
    clang/lib/Sema/SemaExpr.cpp
    clang/test/Analysis/misc-ps.m

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index acbf7d534d49f..fca9240362e76 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -14536,7 +14536,7 @@ static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK,
     //   [...] the expression to which [the unary * operator] is applied shall
     //   be a pointer to an object type, or a pointer to a function type
     LangOptions LO = S.getLangOpts();
-    if (LO.CPlusPlus || !(LO.C99 && IsAfterAmp))
+    if (LO.CPlusPlus || !(LO.C99 && (IsAfterAmp || S.isUnevaluatedContext())))
       S.Diag(OpLoc, diag::ext_typecheck_indirection_through_void_pointer)
           << LO.CPlusPlus << OpTy << Op->getSourceRange();
   }

diff  --git a/clang/test/Analysis/misc-ps.m b/clang/test/Analysis/misc-ps.m
index 4e3783dfc93ac..e9e56315eb268 100644
--- a/clang/test/Analysis/misc-ps.m
+++ b/clang/test/Analysis/misc-ps.m
@@ -133,7 +133,7 @@ void handle_sizeof_void(unsigned flag) {
   void* q;
   
   if (!flag) {
-    if (sizeof(*q) == 1) // expected-warning {{ISO C does not allow indirection on operand of type 'void *'}}
+    if (sizeof(*q) == 1)
       return;
     // Infeasibe.
     *p = 1; // no-warning

diff  --git a/clang/test/Sema/no-warn-void-ptr-uneval.c b/clang/test/Sema/no-warn-void-ptr-uneval.c
new file mode 100644
index 0000000000000..2aed1180ab0da
--- /dev/null
+++ b/clang/test/Sema/no-warn-void-ptr-uneval.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify %s
+
+// expected-no-diagnostics
+void foo(void *vp) {
+  sizeof(*vp);
+  sizeof(*(vp));
+  void inner(typeof(*vp));
+}


        


More information about the cfe-commits mailing list