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

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

<details>
<summary>Changes</summary>

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.


---
Full diff: https://github.com/llvm/llvm-project/pull/124849.diff


2 Files Affected:

- (modified) libc/cmake/modules/LLVMLibCTestRules.cmake (+1) 
- (modified) libc/include/llvm-libc-macros/containerof-macro.h (+2-2) 


``````````diff
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

``````````

</details>


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


More information about the libc-commits mailing list