Skip to content

Commit e238cde

Browse files
johnstiles-googleSkCQ
authored andcommitted
Fix warnings about sending nullable pointers to non-null APIs.
Using the latest Xcode 14.2, these calls were generating warnings. (It's a valid report; if the buffer we send is not UTF-8, -init will in fact return null.) Explicitly casting the pointers to _Nonnull works around it; I also added proper null checks. Change-Id: I54d6c7e6b1b9728d522084250044f2f1f5cbb16b Reviewed-on: https://skia-review.googlesource.com/c/skia/+/641056 Auto-Submit: John Stiles <johnstiles@google.com> Reviewed-by: Jim Van Verth <jvanverth@google.com> Commit-Queue: Jim Van Verth <jvanverth@google.com> Commit-Queue: John Stiles <johnstiles@google.com>
1 parent db81bd2 commit e238cde

2 files changed

Lines changed: 30 additions & 20 deletions

File tree

src/gpu/ganesh/mtl/GrMtlUtil.mm

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,13 @@ bool GrSkSLToMSL(const GrMtlGpu* gpu,
100100
const std::string& msl,
101101
GrContextOptions::ShaderErrorHandler* errorHandler) {
102102
TRACE_EVENT0("skia.shaders", "driver_compile_shader");
103-
auto nsSource = [[NSString alloc] initWithBytesNoCopy:const_cast<char*>(msl.c_str())
104-
length:msl.size()
105-
encoding:NSUTF8StringEncoding
106-
freeWhenDone:NO];
103+
NSString* nsSource = [[NSString alloc] initWithBytesNoCopy:const_cast<char*>(msl.c_str())
104+
length:msl.size()
105+
encoding:NSUTF8StringEncoding
106+
freeWhenDone:NO];
107+
if (!nsSource) {
108+
return nil;
109+
}
107110
MTLCompileOptions* options = [[MTLCompileOptions alloc] init];
108111
// array<> is supported in MSL 2.0 on MacOS 10.13+ and iOS 11+,
109112
// and in MSL 1.2 on iOS 10+ (but not MacOS).
@@ -119,11 +122,12 @@ bool GrSkSLToMSL(const GrMtlGpu* gpu,
119122
NSError* error = nil;
120123
id<MTLLibrary> compiledLibrary;
121124
if (@available(macOS 10.15, *)) {
122-
compiledLibrary = [gpu->device() newLibraryWithSource:nsSource
125+
compiledLibrary = [gpu->device() newLibraryWithSource:(NSString* _Nonnull)nsSource
123126
options:options
124127
error:&error];
125128
} else {
126-
compiledLibrary = GrMtlNewLibraryWithSource(gpu->device(), nsSource, options, &error);
129+
compiledLibrary = GrMtlNewLibraryWithSource(gpu->device(), (NSString* _Nonnull)nsSource,
130+
options, &error);
127131
}
128132
if (!compiledLibrary) {
129133
errorHandler->compileError(msl.c_str(), error.debugDescription.UTF8String);
@@ -135,15 +139,17 @@ bool GrSkSLToMSL(const GrMtlGpu* gpu,
135139

136140
void GrPrecompileMtlShaderLibrary(const GrMtlGpu* gpu,
137141
const std::string& msl) {
138-
auto nsSource = [[NSString alloc] initWithBytesNoCopy:const_cast<char*>(msl.c_str())
139-
length:msl.size()
140-
encoding:NSUTF8StringEncoding
141-
freeWhenDone:NO];
142+
NSString* nsSource = [[NSString alloc] initWithBytesNoCopy:const_cast<char*>(msl.c_str())
143+
length:msl.size()
144+
encoding:NSUTF8StringEncoding
145+
freeWhenDone:NO];
146+
if (!nsSource) {
147+
return;
148+
}
142149
// Do nothing after completion for now.
143150
// TODO: cache the result somewhere so we can use it later.
144-
MTLNewLibraryCompletionHandler completionHandler =
145-
^(id<MTLLibrary> library, NSError* error) {};
146-
[gpu->device() newLibraryWithSource:nsSource
151+
MTLNewLibraryCompletionHandler completionHandler = ^(id<MTLLibrary> library, NSError* error) {};
152+
[gpu->device() newLibraryWithSource:(NSString* _Nonnull)nsSource
147153
options:nil
148154
completionHandler:completionHandler];
149155
}

src/gpu/graphite/mtl/MtlUtils.mm

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,13 @@ bool SkSLToMSL(SkSL::Compiler* compiler,
148148
const std::string& msl,
149149
ShaderErrorHandler* errorHandler) {
150150
TRACE_EVENT0("skia.shaders", "driver_compile_shader");
151-
auto nsSource = [[NSString alloc] initWithBytesNoCopy:const_cast<char*>(msl.c_str())
152-
length:msl.size()
153-
encoding:NSUTF8StringEncoding
154-
freeWhenDone:NO];
151+
NSString* nsSource = [[NSString alloc] initWithBytesNoCopy:const_cast<char*>(msl.c_str())
152+
length:msl.size()
153+
encoding:NSUTF8StringEncoding
154+
freeWhenDone:NO];
155+
if (!nsSource) {
156+
return nil;
157+
}
155158
MTLCompileOptions* options = [[MTLCompileOptions alloc] init];
156159
// array<> is supported in MSL 2.0 on MacOS 10.13+ and iOS 11+,
157160
// and in MSL 1.2 on iOS 10+ (but not MacOS).
@@ -165,9 +168,10 @@ bool SkSLToMSL(SkSL::Compiler* compiler,
165168

166169
NSError* error = nil;
167170
// TODO: do we need a version with a timeout?
168-
sk_cfp<id<MTLLibrary>> compiledLibrary([sharedContext->device() newLibraryWithSource:nsSource
169-
options:options
170-
error:&error]);
171+
sk_cfp<id<MTLLibrary>> compiledLibrary(
172+
[sharedContext->device() newLibraryWithSource:(NSString* _Nonnull)nsSource
173+
options:options
174+
error:&error]);
171175
if (!compiledLibrary) {
172176
errorHandler->compileError(msl.c_str(), error.debugDescription.UTF8String);
173177
return nil;

0 commit comments

Comments
 (0)