Disabling "drop from pointer to smaller uint32_t type" in Clang

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

Passing through them one at a time would be very tiring, since this is an experimental project, I am happy to agree to a โ€œhacker" solution to the problem. So instead, I changed the implementation of malloc to ensure that it never allocates memory above the 4GB limit. Technically, these throws must therefore be valid.

The question is how to explain this to the Clan? The error I get is: error: cast from pointer to smaller type 'uint32_t' (aka 'unsigned int') loses information . Is there any way to disable it?

Thanks David

+7
c ++ pointers clang compiler-errors
source share
5 answers

I agree that you must bite the bullet and fix the code in order to use the correct integer type. But to answer your question: No , you cannot turn it off, although you can get around it.

Many errors come from warnings. A good thing at all, but if you want to turn off the warning, just do it. Since the culprit is probably something like -Wall , which allows a lot of warnings that you must continue, you must selectively disable this one warning. The error message refers to the diagnostics responsible for the error message, for example. ... [-Wextra-tokens] (if not, remove the -fno-diagnostics-show-option flag). You can completely disable this diagnostic by adding -Wno-extra-tokens (again, the warning โ€œextra token signsโ€) or turn it into a non-fatal warning using -Wno-error=extra-tokens .

However, this particular error is not caused by a warning, and I cannot find any option to disable errors (it makes sense, since most errors are fatal).

But just truncate the integer value and not fix all the misuse of uint32_t so far you can use static_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr)) . Needless to say, this would still be a mistake.

+5
source share

How about using uintptr_t, most of your pointer arithmetic can still work.

0
source share

Save this piece of code as mycast.hpp and add -include mycast.hpp to your Makefile .

 #include <cstdint> template<typename U, typename T> U Reinterpret_cast(T *x) { return (U)(uintptr_t)x; } template<typename U, typename T> U Reinterpret_cast(T &x) { return *(U*)&x; } #define reinterpret_cast Reinterpret_cast 

They should do their job if your code is too complex.

Your strategy will not work for objects related to the stack, be careful! You can insert some debugging / logging logic into Reinterpret_cast , if necessary.

0
source share

I hit the same problem in a project without C ++ 11 and worked around it like this:

 inline int PtrToInt(void* ptr) { void* clang[1]; clang[0] = ptr; return *(int*)clang; } 
0
source share

I was able to disable this with -fms-extensions after getting this from someone from 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 SemaCastcpp in cases: one of them suggests that it is sensitive to MS extensions, https://github.com/llvm-mirror/clang/blob/release_50/lib/Sema/SemaCast.cpp#L2112 You can try -fms-extensions (I hope not -fms-compatibility ), but it will lead to the fact that all this will be with her.

0
source share

All Articles