Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit f3b9012

Browse files
Add magic to foundation.h to provide alignof everywhere
C++11 added alignof(x) to return the alignment of a type. Not all of our compilers support this but they do all have equivalents. Lots of ifdef magic has been added to foundation.h to support this.
1 parent 51f1402 commit f3b9012

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

libfoundation/include/foundation.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,40 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
413413
# define MC_DLLEXPORT_DEF __attribute__((__visibility__("default"), __used__))
414414
#endif
415415

416+
////////////////////////////////////////////////////////////////////////////////
417+
//
418+
// C++ COMPATIBILITY
419+
//
420+
421+
#ifndef __has_feature
422+
# define __has_feature(x) 0
423+
#endif
424+
#ifndef __has_extension
425+
# define __has_extension(x) __has_feature(x)
426+
#endif
427+
428+
// Ensure we have alignof(...) available
429+
#if defined(__cplusplus) && !__has_feature(cxx_alignof)
430+
// Testing __cplusplus isn't sufficient as some compilers changed the value before being fully-conforming
431+
# if defined(__clang__)
432+
// No need for a version check; Clang supports __has_feature as a built-in
433+
// so if we get here, it isn't supported
434+
# define alignof(x) __alignof__(x)
435+
# elif defined(__GNUC__)
436+
// GCC added C++11 alignof(x) in GCC 4.8
437+
# if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR < 8)
438+
# define alignof(x) __alignof__(x)
439+
# endif
440+
# elif defined(_MSC_VER)
441+
// MSVC added C++11 alignof(x) in Visual Studio 2012 (compiler version 11.0, _MSC_VER 1700)
442+
# if (_MSC_VER < 1700)
443+
# define alignof(x) __alignof(x)
444+
# endif
445+
# else
446+
# error Don't know how to get alignof(x) on this compiler
447+
# endif
448+
#endif
449+
416450
////////////////////////////////////////////////////////////////////////////////
417451
//
418452
// FIXED WIDTH INTEGER TYPES

libscript/src/script-execute.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class MCScriptForeignInvocation
116116
{
117117
if (m_argument_count == kMaxArguments ||
118118
!Allocate(sizeof(void *),
119-
__alignof__(void *),
119+
alignof(void *),
120120
m_argument_values[m_argument_count]))
121121
{
122122
return MCErrorThrowOutOfMemory();
@@ -208,13 +208,13 @@ __MCScriptComputeSlotAttributes(const MCResolvedTypeInfo& p_slot_type,
208208
MCHandlerTypeInfoIsForeign(p_slot_type.type))
209209
{
210210
r_slot_size = sizeof(void*);
211-
r_slot_align = __alignof__(void *);
211+
r_slot_align = alignof(void*);
212212
r_slot_drop = nil;
213213
return;
214214
}
215-
215+
216216
r_slot_size = sizeof(void*);
217-
r_slot_align = __alignof__(void*);
217+
r_slot_align = alignof(void*);
218218
r_slot_drop = __MCScriptDropValueRef;
219219
}
220220

0 commit comments

Comments
 (0)