1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkSLCompiler.h"
9
10 #include "Test.h"
11
test_failure(skiatest::Reporter * r,const char * src,const char * error)12 static void test_failure(skiatest::Reporter* r, const char* src, const char* error) {
13 SkSL::Compiler compiler;
14 SkSL::Program::Settings settings;
15 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
16 settings.fCaps = caps.get();
17 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
18 SkSL::String(src), settings);
19 if (!compiler.errorCount()) {
20 compiler.optimize(*program);
21 }
22 SkSL::String skError(error);
23 if (compiler.errorText() != skError) {
24 SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error,
25 compiler.errorText().c_str());
26 }
27 REPORTER_ASSERT(r, compiler.errorText() == skError);
28 }
29
test_success(skiatest::Reporter * r,const char * src)30 static void test_success(skiatest::Reporter* r, const char* src) {
31 SkSL::Compiler compiler;
32 SkSL::Program::Settings settings;
33 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
34 settings.fCaps = caps.get();
35 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
36 SkSL::String(src), settings);
37 REPORTER_ASSERT(r, program);
38 }
39
DEF_TEST(SkSLUndefinedSymbol,r)40 DEF_TEST(SkSLUndefinedSymbol, r) {
41 test_failure(r,
42 "void main() { x = float2(1); }",
43 "error: 1: unknown identifier 'x'\n1 error\n");
44 }
45
DEF_TEST(SkSLUndefinedFunction,r)46 DEF_TEST(SkSLUndefinedFunction, r) {
47 test_failure(r,
48 "void main() { int x = foo(1); }",
49 "error: 1: unknown identifier 'foo'\n1 error\n");
50 }
51
DEF_TEST(SkSLGenericArgumentMismatch,r)52 DEF_TEST(SkSLGenericArgumentMismatch, r) {
53 test_failure(r,
54 "void main() { float x = sin(1, 2); }",
55 "error: 1: no match for sin(int, int)\n1 error\n");
56 test_failure(r,
57 "void main() { float x = sin(true); }",
58 "error: 1: no match for sin(bool)\n1 error\n");
59 test_success(r,
60 "void main() { float x = sin(1); }");
61 }
62
DEF_TEST(SkSLArgumentCountMismatch,r)63 DEF_TEST(SkSLArgumentCountMismatch, r) {
64 test_failure(r,
65 "float foo(float x) { return x * x; }"
66 "void main() { float x = foo(1, 2); }",
67 "error: 1: call to 'foo' expected 1 argument, but found 2\n1 error\n");
68 }
69
DEF_TEST(SkSLArgumentMismatch,r)70 DEF_TEST(SkSLArgumentMismatch, r) {
71 test_failure(r,
72 "float foo(float x) { return x * x; }"
73 "void main() { float x = foo(true); }",
74 "error: 1: expected 'float', but found 'bool'\n1 error\n");
75 }
76
DEF_TEST(SkSLIfTypeMismatch,r)77 DEF_TEST(SkSLIfTypeMismatch, r) {
78 test_failure(r,
79 "void main() { if (3) { } }",
80 "error: 1: expected 'bool', but found 'int'\n1 error\n");
81 }
82
DEF_TEST(SkSLDoTypeMismatch,r)83 DEF_TEST(SkSLDoTypeMismatch, r) {
84 test_failure(r,
85 "void main() { do { } while (float2(1)); }",
86 "error: 1: expected 'bool', but found 'float2'\n1 error\n");
87 }
88
DEF_TEST(SkSLWhileTypeMismatch,r)89 DEF_TEST(SkSLWhileTypeMismatch, r) {
90 test_failure(r,
91 "void main() { while (float3(1)) { } }",
92 "error: 1: expected 'bool', but found 'float3'\n1 error\n");
93 }
94
DEF_TEST(SkSLForTypeMismatch,r)95 DEF_TEST(SkSLForTypeMismatch, r) {
96 test_failure(r,
97 "void main() { for (int x = 0; x; x++) { } }",
98 "error: 1: expected 'bool', but found 'int'\n1 error\n");
99 }
100
DEF_TEST(SkSLConstructorTypeMismatch,r)101 DEF_TEST(SkSLConstructorTypeMismatch, r) {
102 test_failure(r,
103 "void main() { float2 x = float2(1.0, false); }",
104 "error: 1: expected 'float', but found 'bool'\n1 error\n");
105 test_failure(r,
106 "void main() { float2 x = float2(bool2(false)); }",
107 "error: 1: 'bool2' is not a valid parameter to 'float2' constructor\n1 error\n");
108 test_failure(r,
109 "void main() { bool2 x = bool2(float2(1)); }",
110 "error: 1: 'float2' is not a valid parameter to 'bool2' constructor\n1 error\n");
111 test_failure(r,
112 "void main() { bool x = bool(1.0); }",
113 "error: 1: cannot construct 'bool'\n1 error\n");
114 test_failure(r,
115 "struct foo { int x; }; void main() { foo x = foo(5); }",
116 "error: 1: cannot construct 'foo'\n1 error\n");
117 test_failure(r,
118 "struct foo { int x; } foo; void main() { float x = float(foo); }",
119 "error: 1: invalid argument to 'float' constructor (expected a number or bool, but found 'foo')\n1 error\n");
120 test_failure(r,
121 "struct foo { int x; } foo; void main() { float2 x = float2(foo); }",
122 "error: 1: 'foo' is not a valid parameter to 'float2' constructor\n1 error\n");
123 test_failure(r,
124 "void main() { float2x2 x = float2x2(true); }",
125 "error: 1: expected 'float', but found 'bool'\n1 error\n");
126 }
127
DEF_TEST(SkSLConstructorArgumentCount,r)128 DEF_TEST(SkSLConstructorArgumentCount, r) {
129 test_failure(r,
130 "void main() { float3 x = float3(1.0, 2.0); }",
131 "error: 1: invalid arguments to 'float3' constructor (expected 3 scalars, but "
132 "found 2)\n1 error\n");
133 test_failure(r,
134 "void main() { float3 x = float3(1.0, 2.0, 3.0, 4.0); }",
135 "error: 1: invalid arguments to 'float3' constructor (expected 3 scalars, but found "
136 "4)\n1 error\n");
137 }
138
DEF_TEST(SkSLSwizzleScalar,r)139 DEF_TEST(SkSLSwizzleScalar, r) {
140 test_failure(r,
141 "void main() { float x = 1; float y = x.y; }",
142 "error: 1: cannot swizzle value of type 'float'\n1 error\n");
143 }
144
DEF_TEST(SkSLSwizzleMatrix,r)145 DEF_TEST(SkSLSwizzleMatrix, r) {
146 test_failure(r,
147 "void main() { float2x2 x = float2x2(1); float y = x.y; }",
148 "error: 1: cannot swizzle value of type 'float2x2'\n1 error\n");
149 }
150
DEF_TEST(SkSLSwizzleOutOfBounds,r)151 DEF_TEST(SkSLSwizzleOutOfBounds, r) {
152 test_failure(r,
153 "void main() { float3 test = float2(1).xyz; }",
154 "error: 1: invalid swizzle component 'z'\n1 error\n");
155 }
156
DEF_TEST(SkSLSwizzleTooManyComponents,r)157 DEF_TEST(SkSLSwizzleTooManyComponents, r) {
158 test_failure(r,
159 "void main() { float4 test = float2(1).xxxxx; }",
160 "error: 1: too many components in swizzle mask 'xxxxx'\n1 error\n");
161 }
162
DEF_TEST(SkSLSwizzleDuplicateOutput,r)163 DEF_TEST(SkSLSwizzleDuplicateOutput, r) {
164 test_failure(r,
165 "void main() { float4 test = float4(1); test.xyyz = float4(1); }",
166 "error: 1: cannot write to the same swizzle field more than once\n1 error\n");
167 }
168
DEF_TEST(SkSLAssignmentTypeMismatch,r)169 DEF_TEST(SkSLAssignmentTypeMismatch, r) {
170 test_failure(r,
171 "void main() { int x = 1.0; }",
172 "error: 1: expected 'int', but found 'float'\n1 error\n");
173 test_failure(r,
174 "void main() { int x; x = 1.0; }",
175 "error: 1: type mismatch: '=' cannot operate on 'int', 'float'\n1 error\n");
176 test_success(r,
177 "void main() { float3 x = float3(0); x *= 1.0; }");
178 test_failure(r,
179 "void main() { int3 x = int3(0); x *= 1.0; }",
180 "error: 1: type mismatch: '*=' cannot operate on 'int3', 'float'\n1 error\n");
181 }
182
DEF_TEST(SkSLReturnFromVoid,r)183 DEF_TEST(SkSLReturnFromVoid, r) {
184 test_failure(r,
185 "void main() { return true; }",
186 "error: 1: may not return a value from a void function\n1 error\n");
187 }
188
DEF_TEST(SkSLReturnMissingValue,r)189 DEF_TEST(SkSLReturnMissingValue, r) {
190 test_failure(r,
191 "int foo() { return; } void main() { }",
192 "error: 1: expected function to return 'int'\n1 error\n");
193 }
194
DEF_TEST(SkSLReturnTypeMismatch,r)195 DEF_TEST(SkSLReturnTypeMismatch, r) {
196 test_failure(r,
197 "int foo() { return 1.0; } void main() { }",
198 "error: 1: expected 'int', but found 'float'\n1 error\n");
199 }
200
DEF_TEST(SkSLDuplicateFunction,r)201 DEF_TEST(SkSLDuplicateFunction, r) {
202 test_failure(r,
203 "void main() { } void main() { }",
204 "error: 1: duplicate definition of void main()\n1 error\n");
205 test_success(r,
206 "void main(); void main() { }");
207 }
208
DEF_TEST(SkSLUsingInvalidValue,r)209 DEF_TEST(SkSLUsingInvalidValue, r) {
210 test_failure(r,
211 "void main() { int x = int; }",
212 "error: 1: expected '(' to begin constructor invocation\n1 error\n");
213 test_failure(r,
214 "int test() { return 1; } void main() { int x = test; }",
215 "error: 1: expected '(' to begin function call\n1 error\n");
216 }
DEF_TEST(SkSLDifferentReturnType,r)217 DEF_TEST(SkSLDifferentReturnType, r) {
218 test_failure(r,
219 "int main() { return 1; } void main() { }",
220 "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 "
221 "error\n");
222 }
223
DEF_TEST(SkSLDifferentModifiers,r)224 DEF_TEST(SkSLDifferentModifiers, r) {
225 test_failure(r,
226 "void test(int x); void test(out int x) { }",
227 "error: 1: modifiers on parameter 1 differ between declaration and definition\n1 "
228 "error\n");
229 }
230
DEF_TEST(SkSLDuplicateSymbol,r)231 DEF_TEST(SkSLDuplicateSymbol, r) {
232 test_failure(r,
233 "int main; void main() { }",
234 "error: 1: symbol 'main' was already defined\n1 error\n");
235
236 test_failure(r,
237 "int x; int x; void main() { }",
238 "error: 1: symbol 'x' was already defined\n1 error\n");
239
240 test_success(r, "int x; void main() { int x; }");
241 }
242
DEF_TEST(SkSLBinaryTypeMismatch,r)243 DEF_TEST(SkSLBinaryTypeMismatch, r) {
244 test_failure(r,
245 "void main() { float x = 3 * true; }",
246 "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n1 error\n");
247 test_failure(r,
248 "void main() { bool x = 1 || 2.0; }",
249 "error: 1: type mismatch: '||' cannot operate on 'int', 'float'\n1 error\n");
250 test_failure(r,
251 "void main() { bool x = float2(0) == 0; }",
252 "error: 1: type mismatch: '==' cannot operate on 'float2', 'int'\n1 error\n");
253 test_failure(r,
254 "void main() { bool x = float2(0) != 0; }",
255 "error: 1: type mismatch: '!=' cannot operate on 'float2', 'int'\n1 error\n");
256 }
257
DEF_TEST(SkSLCallNonFunction,r)258 DEF_TEST(SkSLCallNonFunction, r) {
259 test_failure(r,
260 "void main() { float x = 3; x(); }",
261 "error: 1: 'x' is not a function\n1 error\n");
262 }
263
DEF_TEST(SkSLInvalidUnary,r)264 DEF_TEST(SkSLInvalidUnary, r) {
265 test_failure(r,
266 "void main() { float4x4 x = float4x4(1); ++x; }",
267 "error: 1: '++' cannot operate on 'float4x4'\n1 error\n");
268 test_failure(r,
269 "void main() { float3 x = float3(1); --x; }",
270 "error: 1: '--' cannot operate on 'float3'\n1 error\n");
271 test_failure(r,
272 "void main() { float4x4 x = float4x4(1); x++; }",
273 "error: 1: '++' cannot operate on 'float4x4'\n1 error\n");
274 test_failure(r,
275 "void main() { float3 x = float3(1); x--; }",
276 "error: 1: '--' cannot operate on 'float3'\n1 error\n");
277 test_failure(r,
278 "void main() { int x = !12; }",
279 "error: 1: '!' cannot operate on 'int'\n1 error\n");
280 test_failure(r,
281 "struct foo { } bar; void main() { foo x = +bar; }",
282 "error: 1: '+' cannot operate on 'foo'\n1 error\n");
283 test_failure(r,
284 "struct foo { } bar; void main() { foo x = -bar; }",
285 "error: 1: '-' cannot operate on 'foo'\n1 error\n");
286 test_success(r,
287 "void main() { float2 x = float2(1, 1); x = +x; x = -x; }");
288 }
289
DEF_TEST(SkSLInvalidAssignment,r)290 DEF_TEST(SkSLInvalidAssignment, r) {
291 test_failure(r,
292 "void main() { 1 = 2; }",
293 "error: 1: cannot assign to '1'\n1 error\n");
294 test_failure(r,
295 "uniform int x; void main() { x = 0; }",
296 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
297 test_failure(r,
298 "const int x; void main() { x = 0; }",
299 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
300 }
301
DEF_TEST(SkSLBadIndex,r)302 DEF_TEST(SkSLBadIndex, r) {
303 test_failure(r,
304 "void main() { int x = 2[0]; }",
305 "error: 1: expected array, but found 'int'\n1 error\n");
306 test_failure(r,
307 "void main() { float2 x = float2(0); int y = x[0][0]; }",
308 "error: 1: expected array, but found 'float'\n1 error\n");
309 }
310
DEF_TEST(SkSLTernaryMismatch,r)311 DEF_TEST(SkSLTernaryMismatch, r) {
312 test_failure(r,
313 "void main() { int x = 5 > 2 ? true : 1.0; }",
314 "error: 1: ternary operator result mismatch: 'bool', 'float'\n1 error\n");
315 test_failure(r,
316 "void main() { int x = 5 > 2 ? float3(1) : 1.0; }",
317 "error: 1: ternary operator result mismatch: 'float3', 'float'\n1 error\n");
318 }
319
DEF_TEST(SkSLInterfaceBlockStorageModifiers,r)320 DEF_TEST(SkSLInterfaceBlockStorageModifiers, r) {
321 test_failure(r,
322 "uniform foo { out int x; };",
323 "error: 1: interface block fields may not have storage qualifiers\n1 error\n");
324 }
325
DEF_TEST(SkSLUseWithoutInitialize,r)326 DEF_TEST(SkSLUseWithoutInitialize, r) {
327 test_failure(r,
328 "void main() { int x; if (5 == 2) x = 3; x++; }",
329 "error: 1: 'x' has not been assigned\n1 error\n");
330 test_failure(r,
331 "void main() { int x[2][2]; int i; x[i][1] = 4; }",
332 "error: 1: 'i' has not been assigned\n1 error\n");
333 test_failure(r,
334 "int main() { int r; return r; }",
335 "error: 1: 'r' has not been assigned\n1 error\n");
336 test_failure(r,
337 "void main() { int x; int y = x; }",
338 "error: 1: 'x' has not been assigned\n1 error\n");
339 test_failure(r,
340 "void main() { bool x; if (true && (false || x)) return; }",
341 "error: 1: 'x' has not been assigned\n1 error\n");
342 test_failure(r,
343 "void main() { int x; switch (3) { case 0: x = 0; case 1: x = 1; }"
344 "sk_FragColor = half4(x); }",
345 "error: 1: 'x' has not been assigned\n1 error\n");
346 }
347
DEF_TEST(SkSLUnreachable,r)348 DEF_TEST(SkSLUnreachable, r) {
349 test_failure(r,
350 "void main() { return; return; }",
351 "error: 1: unreachable\n1 error\n");
352 test_failure(r,
353 "void main() { for (;;) { continue; int x = 1; } }",
354 "error: 1: unreachable\n1 error\n");
355 /* test_failure(r,
356 "void main() { for (;;) { } return; }",
357 "error: 1: unreachable\n1 error\n");*/
358 test_failure(r,
359 "void main() { if (true) return; else discard; return; }",
360 "error: 1: unreachable\n1 error\n");
361 test_failure(r,
362 "void main() { return; while (true); }",
363 "error: 1: unreachable\n1 error\n");
364 }
365
DEF_TEST(SkSLNoReturn,r)366 DEF_TEST(SkSLNoReturn, r) {
367 test_failure(r,
368 "int foo() { if (2 > 5) return 3; }",
369 "error: 1: function can exit without returning a value\n1 error\n");
370 }
371
DEF_TEST(SkSLBreakOutsideLoop,r)372 DEF_TEST(SkSLBreakOutsideLoop, r) {
373 test_failure(r,
374 "void foo() { while(true) {} if (true) break; }",
375 "error: 1: break statement must be inside a loop or switch\n1 error\n");
376 }
377
DEF_TEST(SkSLContinueOutsideLoop,r)378 DEF_TEST(SkSLContinueOutsideLoop, r) {
379 test_failure(r,
380 "void foo() { for(;;); continue; }",
381 "error: 1: continue statement must be inside a loop\n1 error\n");
382 test_failure(r,
383 "void foo() { switch (1) { default: continue; } }",
384 "error: 1: continue statement must be inside a loop\n1 error\n");
385 }
386
DEF_TEST(SkSLStaticIfError,r)387 DEF_TEST(SkSLStaticIfError, r) {
388 // ensure eliminated branch of static if / ternary is still checked for errors
389 test_failure(r,
390 "void foo() { if (true); else x = 5; }",
391 "error: 1: unknown identifier 'x'\n1 error\n");
392 test_failure(r,
393 "void foo() { if (false) x = 5; }",
394 "error: 1: unknown identifier 'x'\n1 error\n");
395 test_failure(r,
396 "void foo() { true ? 5 : x; }",
397 "error: 1: unknown identifier 'x'\n1 error\n");
398 test_failure(r,
399 "void foo() { false ? x : 5; }",
400 "error: 1: unknown identifier 'x'\n1 error\n");
401 }
402
DEF_TEST(SkSLBadCap,r)403 DEF_TEST(SkSLBadCap, r) {
404 test_failure(r,
405 "bool b = sk_Caps.bugFreeDriver;",
406 "error: 1: unknown capability flag 'bugFreeDriver'\n1 error\n");
407 }
408
DEF_TEST(SkSLDivByZero,r)409 DEF_TEST(SkSLDivByZero, r) {
410 test_failure(r,
411 "int x = 1 / 0;",
412 "error: 1: division by zero\n1 error\n");
413 test_failure(r,
414 "float x = 1 / 0;",
415 "error: 1: division by zero\n1 error\n");
416 test_failure(r,
417 "float x = 1.0 / 0.0;",
418 "error: 1: division by zero\n1 error\n");
419 test_failure(r,
420 "float x = -67.0 / (3.0 - 3);",
421 "error: 1: division by zero\n1 error\n");
422 }
423
DEF_TEST(SkSLUnsupportedGLSLIdentifiers,r)424 DEF_TEST(SkSLUnsupportedGLSLIdentifiers, r) {
425 test_failure(r,
426 "void main() { float x = gl_FragCoord.x; };",
427 "error: 1: unknown identifier 'gl_FragCoord'\n1 error\n");
428 test_failure(r,
429 "void main() { float r = gl_FragColor.r; };",
430 "error: 1: unknown identifier 'gl_FragColor'\n1 error\n");
431 }
432
DEF_TEST(SkSLWrongSwitchTypes,r)433 DEF_TEST(SkSLWrongSwitchTypes, r) {
434 test_failure(r,
435 "void main() { switch (float2(1)) { case 1: break; } }",
436 "error: 1: expected 'int', but found 'float2'\n1 error\n");
437 test_failure(r,
438 "void main() { switch (1) { case float2(1): break; } }",
439 "error: 1: expected 'int', but found 'float2'\n1 error\n");
440 }
441
DEF_TEST(SkSLNonConstantCase,r)442 DEF_TEST(SkSLNonConstantCase, r) {
443 test_failure(r,
444 "void main() { int x = 1; switch (1) { case x: break; } }",
445 "error: 1: case value must be a constant\n1 error\n");
446 }
447
DEF_TEST(SkSLDuplicateCase,r)448 DEF_TEST(SkSLDuplicateCase, r) {
449 test_failure(r,
450 "void main() { switch (1) { case 0: case 1: case 0: break; } }",
451 "error: 1: duplicate case value\n1 error\n");
452 }
453
DEF_TEST(SkSLFieldAfterRuntimeArray,r)454 DEF_TEST(SkSLFieldAfterRuntimeArray, r) {
455 test_failure(r,
456 "buffer broken { float x[]; float y; };",
457 "error: 1: only the last entry in an interface block may be a runtime-sized "
458 "array\n1 error\n");
459 }
460
DEF_TEST(SkSLStaticIf,r)461 DEF_TEST(SkSLStaticIf, r) {
462 test_success(r,
463 "void main() { float x = 5; float y = 10;"
464 "@if (x < y) { sk_FragColor = half4(1); } }");
465 test_failure(r,
466 "void main() { float x = sqrt(25); float y = 10;"
467 "@if (x < y) { sk_FragColor = half4(1); } }",
468 "error: 1: static if has non-static test\n1 error\n");
469 }
470
DEF_TEST(SkSLStaticSwitch,r)471 DEF_TEST(SkSLStaticSwitch, r) {
472 test_success(r,
473 "void main() {"
474 "int x = 1;"
475 "@switch (x) {"
476 "case 1: sk_FragColor = half4(1); break;"
477 "default: sk_FragColor = half4(0);"
478 "}"
479 "}");
480 test_failure(r,
481 "void main() {"
482 "int x = int(sqrt(1));"
483 "@switch (x) {"
484 "case 1: sk_FragColor = half4(1); break;"
485 "default: sk_FragColor = half4(0);"
486 "}"
487 "}",
488 "error: 1: static switch has non-static test\n1 error\n");
489 test_failure(r,
490 "void main() {"
491 "int x = 1;"
492 "@switch (x) {"
493 "case 1: sk_FragColor = half4(1); if (sqrt(0) < sqrt(1)) break;"
494 "default: sk_FragColor = half4(0);"
495 "}"
496 "}",
497 "error: 1: static switch contains non-static conditional break\n1 error\n");
498 }
499
DEF_TEST(SkSLInterfaceBlockScope,r)500 DEF_TEST(SkSLInterfaceBlockScope, r) {
501 test_failure(r,
502 "uniform testBlock {"
503 "float x;"
504 "} test[x];",
505 "error: 1: unknown identifier 'x'\n1 error\n");
506 }
507
DEF_TEST(SkSLDuplicateOutput,r)508 DEF_TEST(SkSLDuplicateOutput, r) {
509 test_failure(r,
510 "layout (location=0, index=0) out half4 duplicateOutput;",
511 "error: 1: out location=0, index=0 is reserved for sk_FragColor\n1 error\n");
512 }
513
DEF_TEST(SkSLConstantSwizzleNotLast,r)514 DEF_TEST(SkSLConstantSwizzleNotLast, r) {
515 test_failure(r,
516 "void main() { sk_FragColor = half4(1).rg00; }",
517 "error: 1: only the last swizzle component can be a constant\n1 error\n");
518 }
519