[libc-commits] [libc] [libc] fix -Wcast-qual in containerof macro (PR #124849)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Tue Jan 28 14:07:15 PST 2025


https://github.com/nickdesaulniers created https://github.com/llvm/llvm-project/pull/124849

Fixes the diagnostic observed with gcc-14:

    llvm-project/libc/include/llvm-libc-macros/containerof-macro.h:17:13:
    error: cast from type ‘const char*’ to type ‘void*’ casts away qualifiers
    [-Werror=cast-qual]
     17 |     (type *)(void *)((const char *)__ptr - offsetof(type, member));
        |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We're trying to turn on more warnings for the tests in #124036, so enable
-Wcast-qual for GCC while we're at it.


>From 82b4e51240f55b6eb8c40a0e8ffccfe34e33c145 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Tue, 28 Jan 2025 13:23:37 -0800
Subject: [PATCH] [libc] fix -Wcast-qual in containerof macro
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Fixes the diagnostic observed with gcc-14:

    llvm-project/libc/include/llvm-libc-macros/containerof-macro.h:17:13:
    error: cast from type ‘const char*’ to type ‘void*’ casts away qualifiers
    [-Werror=cast-qual]
     17 |     (type *)(void *)((const char *)__ptr - offsetof(type, member));
        |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We're trying to turn on more warnings for the tests in #124036, so enable
-Wcast-qual for GCC while we're at it.
---
 libc/cmake/modules/LLVMLibCTestRules.cmake        | 1 +
 libc/include/llvm-libc-macros/containerof-macro.h | 4 ++--
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 10bb9c9487d636..1b4a19e2745373 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -46,6 +46,7 @@ function(_get_common_test_compile_options output_var c_test flags)
       if(NOT c_test)
         list(APPEND compile_options "-fext-numeric-literals")
       endif()
+      list(APPEND compile_options "-Wcast-qual")
     else()
       list(APPEND compile_options "-Wno-c99-extensions")
       list(APPEND compile_options "-Wno-gnu-imaginary-constant")
diff --git a/libc/include/llvm-libc-macros/containerof-macro.h b/libc/include/llvm-libc-macros/containerof-macro.h
index debf441bf845e3..6322d8b46b0174 100644
--- a/libc/include/llvm-libc-macros/containerof-macro.h
+++ b/libc/include/llvm-libc-macros/containerof-macro.h
@@ -13,8 +13,8 @@
 
 #define __containerof(ptr, type, member)                                       \
   ({                                                                           \
-    const __typeof(((type *)0)->member) *__ptr = (ptr);                        \
-    (type *)(void *)((const char *)__ptr - offsetof(type, member));            \
+    typeof(((type *)0)->member) *__ptr = (ptr);                                \
+    (type *)(void *)((char *)__ptr - offsetof(type, member));                  \
   })
 
 #endif // LLVM_LIBC_MACROS_CONTAINEROF_MACRO_H



More information about the libc-commits mailing list