Disabling "cast from pointer to smaller type uint32_t" error in Clang

13,868

I was able to disable this with -fms-extensions after getting this from someone on the Cpplang Slack:

Looking at "DiagnosticSemaKinds.td" it shows up as err_bad_reinterpret_cast_small_int, https://github.com/llvm-mirror/clang/blob/release_50/include/clang/Basic/DiagnosticSemaKinds.td#L6193 There are two occurences in "SemaCast.cpp" -- one of which suggests it's sensitive to MS extensions, https://github.com/llvm-mirror/clang/blob/release_50/lib/Sema/SemaCast.cpp#L2112 One could try -fms-extensions (hopefully not -fms-compatibility), but that would bring all the shebang with it.

Share:
13,868

Related videos on Youtube

david.brazdil
Author by

david.brazdil

Updated on September 15, 2022

Comments

  • david.brazdil
    david.brazdil over 1 year

    I'm working on a school project that involves porting a large piece of C++ code on an experimental piece of hardware. Unfortunately, that hardware is 64-bit and the code contains many instances of pointer arithmetic that expects pointers to be 32-bit, i.e. it often does reinterpret_cast<uint32_t>(ptr).

    Going through them one by one would be very tedious and since this is an experimental project anyway, I'm happy to settle for a "hackish" workaround. So instead I modified the implementation of malloc to ensure it never allocates memory above the 4GB limit. Technically, these casts should therefore be valid.

    Question is, how do I explain this to Clang? The error I'm getting is: error: cast from pointer to smaller type 'uint32_t' (aka 'unsigned int') loses information. Is there a way to disable it?

    Thanks, David

    • Sean
      Sean over 10 years
      You should change your code to handle this. Just because you're altering malloc it doesn't mean you're covered. For example, someone might write a custom allocator that's used in a class you don't have access to, and it's allocating at an address above 4GB.
    • n. m.
      n. m. over 10 years
      replace uint32_t by uintptr_t wholesale, then fix remaining cases one by one.
  • anatolyg
    anatolyg over 10 years
    OP said he doesn't want it. I can easily imagine a situation where rewriting code using uintptr_t is not feasible (e.g. serializing pointers to 4 bytes and using hard-coded offsets).
  • Dries Staelens
    Dries Staelens about 5 years
    I had this error while trying to cross compile the <memory> header from libstdc++ 5.4.0 with clang 7.0.1 for ARM. The -fms-extensions flag resolved the issue.