Skip to content

[libc] fix -Wcast-qual in containerof macro #124849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nickdesaulniers
Copy link
Member

@nickdesaulniers nickdesaulniers commented Jan 28, 2025

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.

Link: #124854

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 llvm#124036, so enable
-Wcast-qual for GCC while we're at it.
@llvmbot
Copy link
Member

llvmbot commented Jan 28, 2025

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants