diff --git a/modules/juce_audio_basics/buffers/juce_AudioDataConverters.cpp b/modules/juce_audio_basics/buffers/juce_AudioDataConverters.cpp index 2f751c5c19..b54c587820 100644 --- a/modules/juce_audio_basics/buffers/juce_AudioDataConverters.cpp +++ b/modules/juce_audio_basics/buffers/juce_AudioDataConverters.cpp @@ -32,7 +32,7 @@ void AudioDataConverters::convertFloatToInt16LE (const float* source, void* dest { for (int i = 0; i < numSamples; ++i) { - *reinterpret_cast (intData) = ByteOrder::swapIfBigEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i]))); + *unalignedPointerCast (intData) = ByteOrder::swapIfBigEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i]))); intData += destBytesPerSample; } } @@ -43,7 +43,7 @@ void AudioDataConverters::convertFloatToInt16LE (const float* source, void* dest for (int i = numSamples; --i >= 0;) { intData -= destBytesPerSample; - *reinterpret_cast (intData) = ByteOrder::swapIfBigEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i]))); + *unalignedPointerCast (intData) = ByteOrder::swapIfBigEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i]))); } } } @@ -57,7 +57,7 @@ void AudioDataConverters::convertFloatToInt16BE (const float* source, void* dest { for (int i = 0; i < numSamples; ++i) { - *reinterpret_cast (intData) = ByteOrder::swapIfLittleEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i]))); + *unalignedPointerCast (intData) = ByteOrder::swapIfLittleEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i]))); intData += destBytesPerSample; } } @@ -68,7 +68,7 @@ void AudioDataConverters::convertFloatToInt16BE (const float* source, void* dest for (int i = numSamples; --i >= 0;) { intData -= destBytesPerSample; - *reinterpret_cast (intData) = ByteOrder::swapIfLittleEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i]))); + *unalignedPointerCast (intData) = ByteOrder::swapIfLittleEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i]))); } } } @@ -132,7 +132,7 @@ void AudioDataConverters::convertFloatToInt32LE (const float* source, void* dest { for (int i = 0; i < numSamples; ++i) { - *reinterpret_cast (intData) = ByteOrder::swapIfBigEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i]))); + *unalignedPointerCast (intData) = ByteOrder::swapIfBigEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i]))); intData += destBytesPerSample; } } @@ -143,7 +143,7 @@ void AudioDataConverters::convertFloatToInt32LE (const float* source, void* dest for (int i = numSamples; --i >= 0;) { intData -= destBytesPerSample; - *reinterpret_cast (intData) = ByteOrder::swapIfBigEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i]))); + *unalignedPointerCast (intData) = ByteOrder::swapIfBigEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i]))); } } } @@ -157,7 +157,7 @@ void AudioDataConverters::convertFloatToInt32BE (const float* source, void* dest { for (int i = 0; i < numSamples; ++i) { - *reinterpret_cast (intData) = ByteOrder::swapIfLittleEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i]))); + *unalignedPointerCast (intData) = ByteOrder::swapIfLittleEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i]))); intData += destBytesPerSample; } } @@ -168,7 +168,7 @@ void AudioDataConverters::convertFloatToInt32BE (const float* source, void* dest for (int i = numSamples; --i >= 0;) { intData -= destBytesPerSample; - *reinterpret_cast (intData) = ByteOrder::swapIfLittleEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i]))); + *unalignedPointerCast (intData) = ByteOrder::swapIfLittleEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i]))); } } } @@ -181,10 +181,10 @@ void AudioDataConverters::convertFloatToFloat32LE (const float* source, void* de for (int i = 0; i < numSamples; ++i) { - *reinterpret_cast (d) = source[i]; + *unalignedPointerCast (d) = source[i]; #if JUCE_BIG_ENDIAN - *reinterpret_cast (d) = ByteOrder::swap (*reinterpret_cast (d)); + *unalignedPointerCast (d) = ByteOrder::swap (*unalignedPointerCast (d)); #endif d += destBytesPerSample; @@ -199,10 +199,10 @@ void AudioDataConverters::convertFloatToFloat32BE (const float* source, void* de for (int i = 0; i < numSamples; ++i) { - *reinterpret_cast (d) = source[i]; + *unalignedPointerCast (d) = source[i]; #if JUCE_LITTLE_ENDIAN - *reinterpret_cast (d) = ByteOrder::swap (*reinterpret_cast (d)); + *unalignedPointerCast (d) = ByteOrder::swap (*unalignedPointerCast (d)); #endif d += destBytesPerSample; @@ -219,7 +219,7 @@ void AudioDataConverters::convertInt16LEToFloat (const void* source, float* dest { for (int i = 0; i < numSamples; ++i) { - dest[i] = scale * (short) ByteOrder::swapIfBigEndian (*reinterpret_cast (intData)); + dest[i] = scale * (short) ByteOrder::swapIfBigEndian (*unalignedPointerCast (intData)); intData += srcBytesPerSample; } } @@ -230,7 +230,7 @@ void AudioDataConverters::convertInt16LEToFloat (const void* source, float* dest for (int i = numSamples; --i >= 0;) { intData -= srcBytesPerSample; - dest[i] = scale * (short) ByteOrder::swapIfBigEndian (*reinterpret_cast (intData)); + dest[i] = scale * (short) ByteOrder::swapIfBigEndian (*unalignedPointerCast (intData)); } } } @@ -244,7 +244,7 @@ void AudioDataConverters::convertInt16BEToFloat (const void* source, float* dest { for (int i = 0; i < numSamples; ++i) { - dest[i] = scale * (short) ByteOrder::swapIfLittleEndian (*reinterpret_cast (intData)); + dest[i] = scale * (short) ByteOrder::swapIfLittleEndian (*unalignedPointerCast (intData)); intData += srcBytesPerSample; } } @@ -255,7 +255,7 @@ void AudioDataConverters::convertInt16BEToFloat (const void* source, float* dest for (int i = numSamples; --i >= 0;) { intData -= srcBytesPerSample; - dest[i] = scale * (short) ByteOrder::swapIfLittleEndian (*reinterpret_cast (intData)); + dest[i] = scale * (short) ByteOrder::swapIfLittleEndian (*unalignedPointerCast (intData)); } } } @@ -319,7 +319,7 @@ void AudioDataConverters::convertInt32LEToFloat (const void* source, float* dest { for (int i = 0; i < numSamples; ++i) { - dest[i] = scale * (float) ByteOrder::swapIfBigEndian (*reinterpret_cast (intData)); + dest[i] = scale * (float) ByteOrder::swapIfBigEndian (*unalignedPointerCast (intData)); intData += srcBytesPerSample; } } @@ -330,7 +330,7 @@ void AudioDataConverters::convertInt32LEToFloat (const void* source, float* dest for (int i = numSamples; --i >= 0;) { intData -= srcBytesPerSample; - dest[i] = scale * (float) ByteOrder::swapIfBigEndian (*reinterpret_cast (intData)); + dest[i] = scale * (float) ByteOrder::swapIfBigEndian (*unalignedPointerCast (intData)); } } } @@ -344,7 +344,7 @@ void AudioDataConverters::convertInt32BEToFloat (const void* source, float* dest { for (int i = 0; i < numSamples; ++i) { - dest[i] = scale * (float) ByteOrder::swapIfLittleEndian (*reinterpret_cast (intData)); + dest[i] = scale * (float) ByteOrder::swapIfLittleEndian (*unalignedPointerCast (intData)); intData += srcBytesPerSample; } } @@ -355,7 +355,7 @@ void AudioDataConverters::convertInt32BEToFloat (const void* source, float* dest for (int i = numSamples; --i >= 0;) { intData -= srcBytesPerSample; - dest[i] = scale * (float) ByteOrder::swapIfLittleEndian (*reinterpret_cast (intData)); + dest[i] = scale * (float) ByteOrder::swapIfLittleEndian (*unalignedPointerCast (intData)); } } } @@ -366,10 +366,10 @@ void AudioDataConverters::convertFloat32LEToFloat (const void* source, float* de for (int i = 0; i < numSamples; ++i) { - dest[i] = *reinterpret_cast (s); + dest[i] = *unalignedPointerCast (s); #if JUCE_BIG_ENDIAN - auto d = reinterpret_cast (dest + i); + auto d = unalignedPointerCast (dest + i); *d = ByteOrder::swap (*d); #endif @@ -383,10 +383,10 @@ void AudioDataConverters::convertFloat32BEToFloat (const void* source, float* de for (int i = 0; i < numSamples; ++i) { - dest[i] = *reinterpret_cast (s); + dest[i] = *unalignedPointerCast (s); #if JUCE_LITTLE_ENDIAN - auto d = reinterpret_cast (dest + i); + auto d = unalignedPointerCast (dest + i); *d = ByteOrder::swap (*d); #endif diff --git a/modules/juce_audio_basics/buffers/juce_AudioSampleBuffer.h b/modules/juce_audio_basics/buffers/juce_AudioSampleBuffer.h index 4f4161bcfe..acd165ff01 100644 --- a/modules/juce_audio_basics/buffers/juce_AudioSampleBuffer.h +++ b/modules/juce_audio_basics/buffers/juce_AudioSampleBuffer.h @@ -404,8 +404,8 @@ public: auto numSamplesToCopy = (size_t) jmin (newNumSamples, size); - auto newChannels = reinterpret_cast (newData.get()); - auto newChan = reinterpret_cast (newData + channelListSize); + auto newChannels = unalignedPointerCast (newData.get()); + auto newChan = unalignedPointerCast (newData + channelListSize); for (int j = 0; j < newNumChannels; ++j) { @@ -437,10 +437,10 @@ public: { allocatedBytes = newTotalBytes; allocatedData.allocate (newTotalBytes, clearExtraSpace || isClear); - channels = reinterpret_cast (allocatedData.get()); + channels = unalignedPointerCast (allocatedData.get()); } - auto* chan = reinterpret_cast (allocatedData + channelListSize); + auto* chan = unalignedPointerCast (allocatedData + channelListSize); for (int i = 0; i < newNumChannels; ++i) { @@ -1137,8 +1137,8 @@ private: allocatedBytes = (size_t) numChannels * (size_t) size * sizeof (Type) + channelListSize + 32; allocatedData.malloc (allocatedBytes); - channels = reinterpret_cast (allocatedData.get()); - auto chan = reinterpret_cast (allocatedData + channelListSize); + channels = unalignedPointerCast (allocatedData.get()); + auto chan = unalignedPointerCast (allocatedData + channelListSize); for (int i = 0; i < numChannels; ++i) { @@ -1162,7 +1162,7 @@ private: else { allocatedData.malloc (numChannels + 1, sizeof (Type*)); - channels = reinterpret_cast (allocatedData.get()); + channels = unalignedPointerCast (allocatedData.get()); } for (int i = 0; i < numChannels; ++i) diff --git a/modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp b/modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp index d103dba285..5649ccd5de 100644 --- a/modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp +++ b/modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp @@ -48,7 +48,8 @@ namespace OggVorbisNamespace "-Wswitch-default", "-Wredundant-decls", "-Wmisleading-indentation", - "-Wmissing-prototypes") + "-Wmissing-prototypes", + "-Wcast-align") #include "oggvorbis/vorbisenc.h" #include "oggvorbis/codec.h" diff --git a/modules/juce_audio_processors/format_types/juce_VST3Headers.h b/modules/juce_audio_processors/format_types/juce_VST3Headers.h index 2fd842e295..b9615db4cd 100644 --- a/modules/juce_audio_processors/format_types/juce_VST3Headers.h +++ b/modules/juce_audio_processors/format_types/juce_VST3Headers.h @@ -56,7 +56,8 @@ JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wnon-virtual-dtor", "-Wpedantic", "-Wextra", "-Wclass-memaccess", - "-Wmissing-prototypes") + "-Wmissing-prototypes", + "-Wtype-limits") #undef DEVELOPMENT #define DEVELOPMENT 0 // This avoids a Clang warning in Steinberg code about unused values diff --git a/modules/juce_core/containers/juce_Variant.cpp b/modules/juce_core/containers/juce_Variant.cpp index 72fbcdaa90..6e92b955bf 100644 --- a/modules/juce_core/containers/juce_Variant.cpp +++ b/modules/juce_core/containers/juce_Variant.cpp @@ -257,8 +257,8 @@ public: } private: - static const String* getString (const ValueUnion& data) noexcept { return reinterpret_cast (data.stringValue); } - static String* getString (ValueUnion& data) noexcept { return reinterpret_cast (data.stringValue); } + static const String* getString (const ValueUnion& data) noexcept { return unalignedPointerCast (data.stringValue); } + static String* getString (ValueUnion& data) noexcept { return unalignedPointerCast (data.stringValue); } }; //============================================================================== diff --git a/modules/juce_core/memory/juce_Memory.h b/modules/juce_core/memory/juce_Memory.h index c19b9d5315..03c0de3ceb 100644 --- a/modules/juce_core/memory/juce_Memory.h +++ b/modules/juce_core/memory/juce_Memory.h @@ -39,13 +39,6 @@ inline void zerostruct (Type& structure) noexcept { memset ((v template inline void deleteAndZero (Type& pointer) { delete pointer; pointer = nullptr; } -/** A handy function which adds a number of bytes to any type of pointer and returns the result. - This can be useful to avoid casting pointers to a char* and back when you want to move them by - a specific number of bytes, -*/ -template -inline Type* addBytesToPointer (Type* basePointer, IntegerType bytes) noexcept { return reinterpret_cast (const_cast (reinterpret_cast (basePointer)) + bytes); } - /** A handy function to round up a pointer to the nearest multiple of a given number of bytes. alignmentBytes must be a power of two. */ template @@ -83,6 +76,53 @@ inline void writeUnaligned (void* dstPtr, Type value) noexcept memcpy (dstPtr, &value, sizeof (Type)); } +//============================================================================== +/** Casts a pointer to another type via `void*`, which suppresses the cast-align + warning which sometimes arises when casting pointers to types with different + alignment. + You should only use this when you know for a fact that the input pointer points + to a region that has suitable alignment for `Type`, e.g. regions returned from + malloc/calloc that should be suitable for any non-over-aligned type. +*/ +template ::value, int>::type = 0> +inline Type unalignedPointerCast (void* ptr) noexcept +{ + return reinterpret_cast (ptr); +} + +/** Casts a pointer to another type via `void*`, which suppresses the cast-align + warning which sometimes arises when casting pointers to types with different + alignment. + You should only use this when you know for a fact that the input pointer points + to a region that has suitable alignment for `Type`, e.g. regions returned from + malloc/calloc that should be suitable for any non-over-aligned type. +*/ +template ::value, int>::type = 0> +inline Type unalignedPointerCast (const void* ptr) noexcept +{ + return reinterpret_cast (ptr); +} + +/** A handy function which adds a number of bytes to any type of pointer and returns the result. + This can be useful to avoid casting pointers to a char* and back when you want to move them by + a specific number of bytes, +*/ +template +inline Type* addBytesToPointer (Type* basePointer, IntegerType bytes) noexcept +{ + return unalignedPointerCast (reinterpret_cast (basePointer) + bytes); +} + +/** A handy function which adds a number of bytes to any type of pointer and returns the result. + This can be useful to avoid casting pointers to a char* and back when you want to move them by + a specific number of bytes, +*/ +template +inline const Type* addBytesToPointer (const Type* basePointer, IntegerType bytes) noexcept +{ + return unalignedPointerCast (reinterpret_cast (basePointer) + bytes); +} + //============================================================================== #if JUCE_MAC || JUCE_IOS || DOXYGEN diff --git a/modules/juce_core/native/juce_posix_IPAddress.h b/modules/juce_core/native/juce_posix_IPAddress.h index b6f3432aa5..41c83cd374 100644 --- a/modules/juce_core/native/juce_posix_IPAddress.h +++ b/modules/juce_core/native/juce_posix_IPAddress.h @@ -71,8 +71,8 @@ namespace { if (ifa->ifa_addr->sa_family == AF_INET) { - auto interfaceAddressInfo = reinterpret_cast (ifa->ifa_addr); - auto broadcastAddressInfo = reinterpret_cast (ifa->ifa_dstaddr); + auto interfaceAddressInfo = unalignedPointerCast (ifa->ifa_addr); + auto broadcastAddressInfo = unalignedPointerCast (ifa->ifa_dstaddr); if (interfaceAddressInfo->sin_addr.s_addr != INADDR_NONE) { @@ -83,8 +83,8 @@ namespace } else if (ifa->ifa_addr->sa_family == AF_INET6) { - interfaceInfo.interfaceAddress = makeAddress (reinterpret_cast (ifa->ifa_addr)); - interfaceInfo.broadcastAddress = makeAddress (reinterpret_cast (ifa->ifa_dstaddr)); + interfaceInfo.interfaceAddress = makeAddress (unalignedPointerCast (ifa->ifa_addr)); + interfaceInfo.broadcastAddress = makeAddress (unalignedPointerCast (ifa->ifa_dstaddr)); return true; } } diff --git a/modules/juce_core/text/juce_String.cpp b/modules/juce_core/text/juce_String.cpp index bb1795be2b..a685e6a611 100644 --- a/modules/juce_core/text/juce_String.cpp +++ b/modules/juce_core/text/juce_String.cpp @@ -68,7 +68,7 @@ public: static CharPointerType createUninitialisedBytes (size_t numBytes) { numBytes = (numBytes + 3) & ~(size_t) 3; - auto s = reinterpret_cast (new char [sizeof (StringHolder) - sizeof (CharType) + numBytes]); + auto s = unalignedPointerCast (new char [sizeof (StringHolder) - sizeof (CharType) + numBytes]); s->refCount.value = 0; s->allocatedNumBytes = numBytes; return CharPointerType (s->text); @@ -210,7 +210,7 @@ private: static StringHolder* bufferFromText (const CharPointerType text) noexcept { // (Can't use offsetof() here because of warnings about this not being a POD) - return reinterpret_cast (reinterpret_cast (text.getAddress()) + return unalignedPointerCast (reinterpret_cast (text.getAddress()) - (reinterpret_cast (reinterpret_cast (128)->text) - 128)); } @@ -1991,7 +1991,7 @@ String String::createStringFromData (const void* const unknownData, int size) StringCreationHelper builder ((size_t) numChars); - auto src = reinterpret_cast (data + 2); + auto src = unalignedPointerCast (data + 2); if (CharPointer_UTF16::isByteOrderMarkBigEndian (data)) { @@ -2061,19 +2061,19 @@ struct StringEncodingConverter template <> struct StringEncodingConverter { - static CharPointer_UTF8 convert (const String& source) noexcept { return CharPointer_UTF8 (reinterpret_cast (source.getCharPointer().getAddress())); } + static CharPointer_UTF8 convert (const String& source) noexcept { return CharPointer_UTF8 (unalignedPointerCast (source.getCharPointer().getAddress())); } }; template <> struct StringEncodingConverter { - static CharPointer_UTF16 convert (const String& source) noexcept { return CharPointer_UTF16 (reinterpret_cast (source.getCharPointer().getAddress())); } + static CharPointer_UTF16 convert (const String& source) noexcept { return CharPointer_UTF16 (unalignedPointerCast (source.getCharPointer().getAddress())); } }; template <> struct StringEncodingConverter { - static CharPointer_UTF32 convert (const String& source) noexcept { return CharPointer_UTF32 (reinterpret_cast (source.getCharPointer().getAddress())); } + static CharPointer_UTF32 convert (const String& source) noexcept { return CharPointer_UTF32 (unalignedPointerCast (source.getCharPointer().getAddress())); } }; CharPointer_UTF8 String::toUTF8() const { return StringEncodingConverter::convert (*this); } diff --git a/modules/juce_dsp/containers/juce_AudioBlock.h b/modules/juce_dsp/containers/juce_AudioBlock.h index a83e5a3c06..35cdaa1597 100644 --- a/modules/juce_dsp/containers/juce_AudioBlock.h +++ b/modules/juce_dsp/containers/juce_AudioBlock.h @@ -124,10 +124,10 @@ public: heapBlockToUseForAllocation.malloc (channelListBytes + extraBytes + channelSize * numberOfChannels); - auto* chanArray = reinterpret_cast (heapBlockToUseForAllocation.getData()); + auto* chanArray = unalignedPointerCast (heapBlockToUseForAllocation.getData()); channels = chanArray; - auto* data = reinterpret_cast (addBytesToPointer (chanArray, channelListBytes)); + auto* data = unalignedPointerCast (addBytesToPointer (chanArray, channelListBytes)); data = snapPointerToAlignment (data, alignmentInBytes); for (ChannelCountType i = 0; i < numChannels; ++i) diff --git a/modules/juce_dsp/frequency/juce_FFT.cpp b/modules/juce_dsp/frequency/juce_FFT.cpp index ec7c91a468..3d0c608ac4 100644 --- a/modules/juce_dsp/frequency/juce_FFT.cpp +++ b/modules/juce_dsp/frequency/juce_FFT.cpp @@ -140,7 +140,7 @@ struct FFTFallback : public FFT::Instance else { HeapBlock heapSpace (scratchSize); - performRealOnlyForwardTransform (reinterpret_cast*> (heapSpace.getData()), d); + performRealOnlyForwardTransform (unalignedPointerCast*> (heapSpace.getData()), d); } } @@ -158,7 +158,7 @@ struct FFTFallback : public FFT::Instance else { HeapBlock heapSpace (scratchSize); - performRealOnlyInverseTransform (reinterpret_cast*> (heapSpace.getData()), d); + performRealOnlyInverseTransform (unalignedPointerCast*> (heapSpace.getData()), d); } } diff --git a/modules/juce_gui_basics/native/x11/juce_linux_XWindowSystem.cpp b/modules/juce_gui_basics/native/x11/juce_linux_XWindowSystem.cpp index e7297ec7d4..917fc747b8 100644 --- a/modules/juce_gui_basics/native/x11/juce_linux_XWindowSystem.cpp +++ b/modules/juce_gui_basics/native/x11/juce_linux_XWindowSystem.cpp @@ -1165,7 +1165,7 @@ namespace ClipboardHelpers numDataItems = 2; propertyFormat = 32; // atoms are 32-bit data.calloc (numDataItems * 4); - Atom* atoms = reinterpret_cast (data.getData()); + Atom* atoms = unalignedPointerCast (data.getData()); atoms[0] = XWindowSystem::getInstance()->getAtoms().utf8String; atoms[1] = XA_STRING; @@ -1224,7 +1224,7 @@ ComponentPeer* getPeerFor (::Window windowH) X11Symbols::getInstance()->xFindContext (display, (XID) windowH, windowHandleXContext, &peer); } - return reinterpret_cast (peer); + return unalignedPointerCast (peer); } //============================================================================== @@ -2998,7 +2998,7 @@ void XWindowSystem::handleKeyPressEvent (LinuxComponentPeer<::Window>* peer, XKe if (sym >= XK_F1 && sym <= XK_F35) { keyPressed = true; - keyCode = (sym & 0xff) | Keys::extendedKeyModifier; + keyCode = static_cast ((sym & 0xff) | Keys::extendedKeyModifier); } break; }