Skip to content

Commit cfff413

Browse files
compnerdphausler
authored andcommitted
CoreFoundation: use C99 macro expansion (swiftlang#379)
Rather than defining the various flavours of CFAssert requiring the user to count the number of substituted parameters, collapse them to CFAssert. The C99 variadic macro expansion permits us to ensure that the arguments are all passable. At the same time, the compiler validation of the printf conversion will still catch the missing arguments (which could still be possible to have in the previous case by just using the wrong count).
1 parent 132d1cb commit cfff413

29 files changed

+286
-293
lines changed

Diff for: CoreFoundation/Base.subproj/CFBase.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ void CFAllocatorGetContext(CFAllocatorRef allocator, CFAllocatorContext *context
762762
#else
763763
__CFGenericValidateType(allocator, __kCFAllocatorTypeID);
764764
#endif
765-
CFAssert1(0 == context->version, __kCFLogAssertion, "%s(): context version not initialized to 0", __PRETTY_FUNCTION__);
765+
CFAssert(0 == context->version, __kCFLogAssertion, "%s(): context version not initialized to 0", __PRETTY_FUNCTION__);
766766
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
767767
if (allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t *
768768
return;

Diff for: CoreFoundation/Base.subproj/CFInternal.h

+14-21
Original file line numberDiff line numberDiff line change
@@ -175,28 +175,21 @@ CF_PRIVATE CFIndex __CFActiveProcessorCount();
175175
#endif
176176

177177
#if defined(DEBUG)
178-
#define __CFAssert(cond, prio, desc, a1, a2, a3, a4, a5) \
179-
do { \
180-
if (!(cond)) { \
181-
CFLog(prio, CFSTR(desc), a1, a2, a3, a4, a5); \
182-
HALT; \
183-
} \
184-
} while (0)
178+
#define __CFAssert(cond, prio, desc, ...) \
179+
do { \
180+
if (!(cond)) { \
181+
CFLog(prio, CFSTR(desc), __VA_ARGS__); \
182+
HALT; \
183+
} \
184+
} while (0)
185185
#else
186-
#define __CFAssert(cond, prio, desc, a1, a2, a3, a4, a5) \
187-
do {} while (0)
188-
#endif
189-
190-
#define CFAssert(condition, priority, description) \
191-
__CFAssert((condition), (priority), description, 0, 0, 0, 0, 0)
192-
#define CFAssert1(condition, priority, description, a1) \
193-
__CFAssert((condition), (priority), description, (a1), 0, 0, 0, 0)
194-
#define CFAssert2(condition, priority, description, a1, a2) \
195-
__CFAssert((condition), (priority), description, (a1), (a2), 0, 0, 0)
196-
#define CFAssert3(condition, priority, description, a1, a2, a3) \
197-
__CFAssert((condition), (priority), description, (a1), (a2), (a3), 0, 0)
198-
#define CFAssert4(condition, priority, description, a1, a2, a3, a4) \
199-
__CFAssert((condition), (priority), description, (a1), (a2), (a3), (a4), 0)
186+
#define __CFAssert(cond, prio, desc, ...) \
187+
do { \
188+
} while (0)
189+
#endif
190+
191+
#define CFAssert(condition, priority, description, ...) \
192+
__CFAssert((condition), (priority), description, __VA_ARGS__)
200193

201194
#define __kCFLogAssertion 3
202195

Diff for: CoreFoundation/Base.subproj/CFPlatform.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ CF_PRIVATE const wchar_t *_CFDLLPath(void) {
7272
CFAssert(ourModule, __kCFLogAssertion, "GetModuleHandle failed");
7373

7474
DWORD wResult = GetModuleFileNameW(ourModule, cachedPath, MAX_PATH+1);
75-
CFAssert1(wResult > 0, __kCFLogAssertion, "GetModuleFileName failed: %d", GetLastError());
76-
CFAssert1(wResult < MAX_PATH+1, __kCFLogAssertion, "GetModuleFileName result truncated: %s", cachedPath);
75+
CFAssert(wResult > 0, __kCFLogAssertion, "GetModuleFileName failed: %d", GetLastError());
76+
CFAssert(wResult < MAX_PATH+1, __kCFLogAssertion, "GetModuleFileName result truncated: %s", cachedPath);
7777

7878
// strip off last component, the DLL name
7979
CFIndex idx;

Diff for: CoreFoundation/Base.subproj/CFRuntime.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ CFTypeRef _CFRuntimeCreateInstance(CFAllocatorRef allocator, CFTypeID typeID, CF
349349
return memory;
350350
#else
351351
if (__CFRuntimeClassTableSize <= typeID) HALT;
352-
CFAssert1(typeID != _kCFRuntimeNotATypeID, __kCFLogAssertion, "%s(): Uninitialized type id", __PRETTY_FUNCTION__);
352+
CFAssert(typeID != _kCFRuntimeNotATypeID, __kCFLogAssertion, "%s(): Uninitialized type id", __PRETTY_FUNCTION__);
353353
CFRuntimeClass *cls = __CFRuntimeClassTable[typeID];
354354
if (NULL == cls) {
355355
return NULL;
@@ -426,7 +426,7 @@ CFTypeRef _CFRuntimeCreateInstance(CFAllocatorRef allocator, CFTypeID typeID, CF
426426
#if DEPLOYMENT_RUNTIME_SWIFT
427427
#else
428428
void _CFRuntimeInitStaticInstance(void *ptr, CFTypeID typeID) {
429-
CFAssert1(typeID != _kCFRuntimeNotATypeID, __kCFLogAssertion, "%s(): Uninitialized type id", __PRETTY_FUNCTION__);
429+
CFAssert(typeID != _kCFRuntimeNotATypeID, __kCFLogAssertion, "%s(): Uninitialized type id", __PRETTY_FUNCTION__);
430430
if (__CFRuntimeClassTableSize <= typeID) HALT;
431431
CFRuntimeClass *cfClass = __CFRuntimeClassTable[typeID];
432432
Boolean customRC = !!(cfClass->version & _kCFRuntimeCustomRefCount);
@@ -555,12 +555,12 @@ CF_PRIVATE void __CFGenericValidateType_(CFTypeRef cf, CFTypeID type, const char
555555
if (cf && CF_IS_SWIFT(type, (CFSwiftRef)cf)) return;
556556
#endif
557557
if (cf && CF_IS_OBJC(type, cf)) return;
558-
CFAssert2((cf != NULL) && (NULL != __CFRuntimeClassTable[__CFGenericTypeID_inline(cf)]) && (__kCFNotATypeTypeID != __CFGenericTypeID_inline(cf)) && (__kCFTypeTypeID != __CFGenericTypeID_inline(cf)), __kCFLogAssertion, "%s(): pointer %p is not a CF object", func, cf); \
559-
CFAssert3(__CFGenericTypeID_inline(cf) == type, __kCFLogAssertion, "%s(): pointer %p is not a %s", func, cf, __CFRuntimeClassTable[type]->className); \
558+
CFAssert((cf != NULL) && (NULL != __CFRuntimeClassTable[__CFGenericTypeID_inline(cf)]) && (__kCFNotATypeTypeID != __CFGenericTypeID_inline(cf)) && (__kCFTypeTypeID != __CFGenericTypeID_inline(cf)), __kCFLogAssertion, "%s(): pointer %p is not a CF object", func, cf); \
559+
CFAssert(__CFGenericTypeID_inline(cf) == type, __kCFLogAssertion, "%s(): pointer %p is not a %s", func, cf, __CFRuntimeClassTable[type]->className); \
560560
}
561561

562562
#define __CFGenericAssertIsCF(cf) \
563-
CFAssert2(cf != NULL && (NULL != __CFRuntimeClassTable[__CFGenericTypeID_inline(cf)]) && (__kCFNotATypeTypeID != __CFGenericTypeID_inline(cf)) && (__kCFTypeTypeID != __CFGenericTypeID_inline(cf)), __kCFLogAssertion, "%s(): pointer %p is not a CF object", __PRETTY_FUNCTION__, cf);
563+
CFAssert(cf != NULL && (NULL != __CFRuntimeClassTable[__CFGenericTypeID_inline(cf)]) && (__kCFNotATypeTypeID != __CFGenericTypeID_inline(cf)) && (__kCFTypeTypeID != __CFGenericTypeID_inline(cf)), __kCFLogAssertion, "%s(): pointer %p is not a CF object", __PRETTY_FUNCTION__, cf);
564564

565565

566566
#define CFTYPE_IS_OBJC(obj) (false)
@@ -590,7 +590,7 @@ CFTypeID CFGetTypeID(CFTypeRef cf) {
590590
}
591591

592592
CFStringRef CFCopyTypeIDDescription(CFTypeID type) {
593-
CFAssert2((NULL != __CFRuntimeClassTable[type]) && __kCFNotATypeTypeID != type && __kCFTypeTypeID != type, __kCFLogAssertion, "%s(): type %d is not a CF type ID", __PRETTY_FUNCTION__, type);
593+
CFAssert((NULL != __CFRuntimeClassTable[type]) && __kCFNotATypeTypeID != type && __kCFTypeTypeID != type, __kCFLogAssertion, "%s(): type %d is not a CF type ID", __PRETTY_FUNCTION__, type);
594594
return CFStringCreateWithCString(kCFAllocatorSystemDefault, __CFRuntimeClassTable[type]->className, kCFStringEncodingASCII);
595595
}
596596

@@ -1259,8 +1259,8 @@ static CFBundleRef RegisterCoreFoundationBundle(void) {
12591259
CFAssert(ourModule, __kCFLogAssertion, "GetModuleHandle failed");
12601260

12611261
wResult = GetModuleFileNameW(ourModule, path, MAX_PATH+1);
1262-
CFAssert1(wResult > 0, __kCFLogAssertion, "GetModuleFileName failed: %d", GetLastError());
1263-
CFAssert1(wResult < MAX_PATH+1, __kCFLogAssertion, "GetModuleFileName result truncated: %s", path);
1262+
CFAssert(wResult > 0, __kCFLogAssertion, "GetModuleFileName failed: %d", GetLastError());
1263+
CFAssert(wResult < MAX_PATH+1, __kCFLogAssertion, "GetModuleFileName result truncated: %s", path);
12641264

12651265
// strip off last component, the DLL name
12661266
for (idx = wResult - 1; idx; idx--) {

0 commit comments

Comments
 (0)