Skip to content

Commit

Permalink
Add ITAssertWithMessage and use it in iTermCharacterSource
Browse files Browse the repository at this point in the history
  • Loading branch information
gnachman committed Oct 17, 2018
1 parent 1f2b5d2 commit f10713a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
9 changes: 9 additions & 0 deletions sources/DebugLogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ extern BOOL gDebugLogging;
#define ITConservativeBetaAssert(condition, args...)
#endif

#define ITAssertWithMessage(condition, args...) \
do { \
if (!(condition)) { \
DLog(@"Crashing because %s from:\n%@", #condition, [NSThread callStackSymbols]); \
ELog(args); \
__assert_rtn(__func__, __FILE__, __LINE__, [[NSString stringWithFormat:@#condition ": " args] UTF8String]); \
} \
} while (0)

void ToggleDebugLogging(void);
int DebugLogImpl(const char *file, int line, const char *function, NSString* value);
void LogForNextCrash(const char *file, int line, const char *function, NSString* value, BOOL force);
Expand Down
38 changes: 20 additions & 18 deletions sources/Metal/Support/iTermCharacterSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ + (instancetype)sharedInstance {
}

instance = [[self alloc] init];
ITAssertWithMessage(instance, @"-[iTermBitmapContextPool init] returned nil");
threadDict[key] = instance;
return instance;
}
Expand Down Expand Up @@ -77,14 +78,14 @@ + (CGColorSpaceRef)colorSpace {
static CGColorSpaceRef colorSpace;
dispatch_once(&onceToken, ^{
colorSpace = CGColorSpaceCreateDeviceRGB();
assert(colorSpace);
ITAssertWithMessage(colorSpace, @"Colorspace is %@", colorSpace);
});
return colorSpace;
}

- (CGContextRef)allocateBitmapContextOfSize:(CGSize)size {
CGContextRef context = [self internalAllocateBitmapContextOfSize:size];
assert(context);
ITAssertWithMessage(context, @"nil context with size %@", NSStringFromSize(size));
CGContextSaveGState(context);
return context;
}
Expand All @@ -109,20 +110,20 @@ - (CGContextRef)internalAllocateBitmapContextOfSize:(CGSize)size {
if (_array.count) {
CGContextRef first = (__bridge CGContextRef)(_array.firstObject);
[_array removeObjectAtIndex:0];
assert(first);
ITAssertWithMessage(first, @"first array element is nil. Array length is now %@", @(_array.count));
return first;
}
assert(size.width > 0);
assert(size.width * 4 > 0);
assert(size.height > 0);
ITAssertWithMessage(size.width > 0, @"size is %@", NSStringFromSize(size));
ITAssertWithMessage(size.width * 4 > 0, @"size is %@", NSStringFromSize(size));
ITAssertWithMessage(size.height > 0, @"size is %@", NSStringFromSize(size));
CGContextRef context = CGBitmapContextCreate(NULL,
size.width,
size.height,
8,
size.width * 4,
[iTermBitmapContextPool colorSpace],
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
assert(context);
ITAssertWithMessage(context, @"nil CGContextRef. size is %@. colorspace is %@", NSStringFromSize(size), [iTermBitmapContextPool colorSpace]);
return context;
}
@end
Expand Down Expand Up @@ -232,9 +233,10 @@ - (instancetype)initWithCharacter:(NSString *)string
fakeItalic:(BOOL)fakeItalic
antialiased:(BOOL)antialiased
radius:(int)radius {
ITDebugAssert(font);
ITDebugAssert(size.width > 0 && size.height > 0);
ITDebugAssert(scale > 0);
assert(font);
assert(size.width > 0);
assert(size.height > 0);
assert(scale > 0);

if (string.length == 0) {
return nil;
Expand Down Expand Up @@ -282,10 +284,10 @@ - (void)dealloc {
}

- (CGContextRef)contextForIteration:(NSInteger)iteration {
assert(iteration >= 0 && iteration < 4);
assert(iteration < _numberOfIterationsNeeded);
ITAssertWithMessage(iteration >= 0 && iteration < 4, @"requested iteration %@ out of %@", @(iteration), @(_numberOfIterationsNeeded));
ITAssertWithMessage(iteration < _numberOfIterationsNeeded, @"requested iteration %@ out of %@", @(iteration), @(_numberOfIterationsNeeded));
CGContextRef context = _contexts[iteration];
assert(context);
ITAssertWithMessage(context, @"nil context for iteration %@/%@", @(iteration), @(_numberOfIterationsNeeded));
return context;
}

Expand Down Expand Up @@ -323,9 +325,9 @@ - (void)performPostProcessing {

// Sanity checks to ensure we don't traipse off the end of the allocated region.
const size_t bytesPerRow = CGBitmapContextGetBytesPerRow(context);
assert(bytesPerRow >= _size.width * 4);
ITAssertWithMessage(bytesPerRow >= _size.width * 4, @"bytesPerRow is %@ too big for size %@", @(bytesPerRow), NSStringFromSize(_size));
const size_t height = CGBitmapContextGetHeight(context);
assert(height >= _size.height);
ITAssertWithMessage(height >= _size.height, @"bitmap height is %@ for context %@, too big for size %@", @(height), context, NSStringFromSize(_size));
}

// i indexes into the array of pixels, always to the red value.
Expand Down Expand Up @@ -561,7 +563,7 @@ - (void)initializeStateIfNeededWithFont:(CTFontRef)runFont {
}
for (int i = 0; i < _numberOfIterationsNeeded; i++) {
_contexts[i] = [iTermCharacterSource newBitmapContextOfSize:_size];
assert(_contexts[i]);
ITAssertWithMessage(_contexts[i], @"context %@/%@ is null for size %@", @(i), @(_numberOfIterationsNeeded), NSStringFromSize(_size));
}
}

Expand All @@ -575,7 +577,7 @@ - (void)drawBackgroundIfNeededForIteration:(NSInteger)iteration
}

- (void)setTextColorForIteration:(NSInteger)iteration context:(CGContextRef)context {
assert(context);
ITAssertWithMessage(context, @"nil context for iteration %@/%@", @(iteration), @(_numberOfIterationsNeeded));
CGColorRef color = [[self textColorForIteration:iteration] CGColor];
CGContextSetFillColorWithColor(context, color);
CGContextSetStrokeColorWithColor(context, color);
Expand Down Expand Up @@ -693,7 +695,7 @@ - (NSColor *)textColorForIteration:(NSInteger)iteration {
case 3:
return [NSColor colorWithSRGBRed:1 green:1 blue:1 alpha:1];
}
assert(NO);
ITAssertWithMessage(NO, @"bogus iteration %@", @(iteration));
}
return [NSColor blackColor];
}
Expand Down

0 comments on commit f10713a

Please sign in to comment.