blob: fc20fa2e6d03a79e60f16be096f7716bba90ef09 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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
12static void test_failure(skiatest::Reporter* r, const char* src, const char* error) {
13 SkSL::Compiler compiler;
14 std::stringstream out;
15 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, src, out);
16 if (compiler.errorText() != error) {
17 SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error,
18 compiler.errorText().c_str());
19 }
20 REPORTER_ASSERT(r, !result);
21 REPORTER_ASSERT(r, compiler.errorText() == error);
22}
23
24static void test_success(skiatest::Reporter* r, const char* src) {
25 SkSL::Compiler compiler;
26 std::stringstream out;
27 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, src, out);
28 REPORTER_ASSERT(r, result);
29}
30
31DEF_TEST(SkSLUndefinedSymbol, r) {
32 test_failure(r,
33 "void main() { x = vec2(1); }",
34 "error: 1: unknown identifier 'x'\n1 error\n");
35}
36
37DEF_TEST(SkSLUndefinedFunction, r) {
38 test_failure(r,
39 "void main() { int x = foo(1); }",
40 "error: 1: unknown identifier 'foo'\n1 error\n");
41}
42
43DEF_TEST(SkSLGenericArgumentMismatch, r) {
44 test_failure(r,
45 "void main() { float x = sin(1, 2); }",
ethannicholas471e8942016-10-28 09:02:46 -070046 "error: 1: call to 'sin' expected 1 argument, but found 2\n1 error\n");
47 test_failure(r,
48 "void main() { float x = sin(true); }",
49 "error: 1: no match for sin(bool)\n1 error\n");
50 test_success(r,
51 "void main() { float x = sin(1); }");
ethannicholasb3058bd2016-07-01 08:22:01 -070052}
53
54DEF_TEST(SkSLArgumentCountMismatch, r) {
55 test_failure(r,
56 "float foo(float x) { return x * x; }"
57 "void main() { float x = foo(1, 2); }",
58 "error: 1: call to 'foo' expected 1 argument, but found 2\n1 error\n");
59}
60
61DEF_TEST(SkSLArgumentMismatch, r) {
62 test_failure(r,
63 "float foo(float x) { return x * x; }"
64 "void main() { float x = foo(true); }",
65 "error: 1: expected 'float', but found 'bool'\n1 error\n");
66}
67
68DEF_TEST(SkSLIfTypeMismatch, r) {
69 test_failure(r,
70 "void main() { if (3) { } }",
71 "error: 1: expected 'bool', but found 'int'\n1 error\n");
72}
73
74DEF_TEST(SkSLDoTypeMismatch, r) {
75 test_failure(r,
76 "void main() { do { } while (vec2(1)); }",
77 "error: 1: expected 'bool', but found 'vec2'\n1 error\n");
78}
79
80DEF_TEST(SkSLWhileTypeMismatch, r) {
81 test_failure(r,
82 "void main() { while (vec3(1)) { } }",
83 "error: 1: expected 'bool', but found 'vec3'\n1 error\n");
84}
85
86DEF_TEST(SkSLForTypeMismatch, r) {
87 test_failure(r,
88 "void main() { for (int x = 0; x; x++) { } }",
89 "error: 1: expected 'bool', but found 'int'\n1 error\n");
90}
91
92DEF_TEST(SkSLConstructorTypeMismatch, r) {
93 test_failure(r,
94 "void main() { vec2 x = vec2(1.0, false); }",
95 "error: 1: expected 'float', but found 'bool'\n1 error\n");
96 test_failure(r,
Ethan Nicholas4578a8e2016-11-01 11:57:42 -040097 "void main() { vec2 x = vec2(bvec2(false)); }",
98 "error: 1: 'bvec2' is not a valid parameter to 'vec2' constructor\n1 error\n");
99 test_failure(r,
100 "void main() { bvec2 x = bvec2(vec2(1)); }",
101 "error: 1: 'vec2' is not a valid parameter to 'bvec2' constructor\n1 error\n");
102 test_failure(r,
ethannicholasb3058bd2016-07-01 08:22:01 -0700103 "void main() { bool x = bool(1.0); }",
104 "error: 1: cannot construct 'bool'\n1 error\n");
105 test_failure(r,
106 "struct foo { int x; }; void main() { foo x = foo(5); }",
107 "error: 1: cannot construct 'foo'\n1 error\n");
108 test_failure(r,
109 "struct foo { int x; } foo; void main() { float x = float(foo); }",
110 "error: 1: invalid argument to 'float' constructor (expected a number or bool, but found 'foo')\n1 error\n");
111 test_failure(r,
112 "struct foo { int x; } foo; void main() { vec2 x = vec2(foo); }",
113 "error: 1: 'foo' is not a valid parameter to 'vec2' constructor\n1 error\n");
114}
115
116DEF_TEST(SkSLConstructorArgumentCount, r) {
117 test_failure(r,
118 "void main() { vec3 x = vec3(1.0, 2.0); }",
119 "error: 1: invalid arguments to 'vec3' constructor (expected 3 scalars, but "
120 "found 2)\n1 error\n");
121 test_success(r, "void main() { vec3 x = vec3(1.0, 2.0, 3.0, 4.0); }");
122}
123
124DEF_TEST(SkSLSwizzleScalar, r) {
125 test_failure(r,
126 "void main() { float x = 1; float y = x.y; }",
127 "error: 1: cannot swizzle value of type 'float'\n1 error\n");
128}
129
130DEF_TEST(SkSLSwizzleMatrix, r) {
131 test_failure(r,
132 "void main() { mat2 x = mat2(1); float y = x.y; }",
133 "error: 1: cannot swizzle value of type 'mat2'\n1 error\n");
134}
135
136DEF_TEST(SkSLSwizzleOutOfBounds, r) {
137 test_failure(r,
138 "void main() { vec3 test = vec2(1).xyz; }",
139 "error: 1: invalid swizzle component 'z'\n1 error\n");
140}
141
142DEF_TEST(SkSLSwizzleTooManyComponents, r) {
143 test_failure(r,
144 "void main() { vec4 test = vec2(1).xxxxx; }",
145 "error: 1: too many components in swizzle mask 'xxxxx'\n1 error\n");
146}
147
148DEF_TEST(SkSLSwizzleDuplicateOutput, r) {
149 test_failure(r,
150 "void main() { vec4 test = vec4(1); test.xyyz = vec4(1); }",
151 "error: 1: cannot write to the same swizzle field more than once\n1 error\n");
152}
ethannicholasea4567c2016-10-17 11:24:37 -0700153
ethannicholasb3058bd2016-07-01 08:22:01 -0700154DEF_TEST(SkSLAssignmentTypeMismatch, r) {
155 test_failure(r,
156 "void main() { int x = 1.0; }",
157 "error: 1: expected 'int', but found 'float'\n1 error\n");
ethannicholasea4567c2016-10-17 11:24:37 -0700158 test_failure(r,
159 "void main() { int x; x = 1.0; }",
160 "error: 1: type mismatch: '=' cannot operate on 'int', 'float'\n1 error\n");
161 test_success(r,
162 "void main() { vec3 x = vec3(0); x *= 1.0; }");
163 test_failure(r,
164 "void main() { ivec3 x = ivec3(0); x *= 1.0; }",
165 "error: 1: type mismatch: '*=' cannot operate on 'ivec3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700166}
167
168DEF_TEST(SkSLReturnFromVoid, r) {
169 test_failure(r,
170 "void main() { return true; }",
171 "error: 1: may not return a value from a void function\n1 error\n");
172}
173
174DEF_TEST(SkSLReturnMissingValue, r) {
175 test_failure(r,
176 "int foo() { return; } void main() { }",
177 "error: 1: expected function to return 'int'\n1 error\n");
178}
179
180DEF_TEST(SkSLReturnTypeMismatch, r) {
181 test_failure(r,
182 "int foo() { return 1.0; } void main() { }",
183 "error: 1: expected 'int', but found 'float'\n1 error\n");
184}
185
186DEF_TEST(SkSLDuplicateFunction, r) {
187 test_failure(r,
188 "void main() { } void main() { }",
189 "error: 1: duplicate definition of void main()\n1 error\n");
190 test_success(r,
191 "void main(); void main() { }");
192}
193
194DEF_TEST(SkSLUsingInvalidValue, r) {
195 test_failure(r,
196 "void main() { int x = int; }",
197 "error: 1: expected '(' to begin constructor invocation\n1 error\n");
198 test_failure(r,
199 "int test() { return 1; } void main() { int x = test; }",
200 "error: 1: expected '(' to begin function call\n1 error\n");
201}
202DEF_TEST(SkSLDifferentReturnType, r) {
203 test_failure(r,
ethannicholas22f939e2016-10-13 13:25:34 -0700204 "int main() { return 1; } void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700205 "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 "
206 "error\n");
207}
208
209DEF_TEST(SkSLDifferentModifiers, r) {
210 test_failure(r,
211 "void test(int x); void test(out int x) { }",
212 "error: 1: modifiers on parameter 1 differ between declaration and definition\n1 "
213 "error\n");
214}
215
216DEF_TEST(SkSLDuplicateSymbol, r) {
217 test_failure(r,
218 "int main; void main() { }",
219 "error: 1: symbol 'main' was already defined\n1 error\n");
220
221 test_failure(r,
222 "int x; int x; void main() { }",
223 "error: 1: symbol 'x' was already defined\n1 error\n");
224
225 test_success(r, "int x; void main() { int x; }");
226}
227
228DEF_TEST(SkSLBinaryTypeMismatch, r) {
229 test_failure(r,
230 "void main() { float x = 3 * true; }",
231 "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n1 error\n");
232 test_failure(r,
233 "void main() { bool x = 1 || 2.0; }",
234 "error: 1: type mismatch: '||' cannot operate on 'int', 'float'\n1 error\n");
235}
236
237DEF_TEST(SkSLCallNonFunction, r) {
238 test_failure(r,
239 "void main() { float x = 3; x(); }",
240 "error: 1: 'x' is not a function\n1 error\n");
241}
242
243DEF_TEST(SkSLInvalidUnary, r) {
244 test_failure(r,
245 "void main() { mat4 x = mat4(1); ++x; }",
246 "error: 1: '++' cannot operate on 'mat4'\n1 error\n");
247 test_failure(r,
248 "void main() { vec3 x = vec3(1); --x; }",
249 "error: 1: '--' cannot operate on 'vec3'\n1 error\n");
250 test_failure(r,
251 "void main() { mat4 x = mat4(1); x++; }",
252 "error: 1: '++' cannot operate on 'mat4'\n1 error\n");
253 test_failure(r,
254 "void main() { vec3 x = vec3(1); x--; }",
255 "error: 1: '--' cannot operate on 'vec3'\n1 error\n");
256 test_failure(r,
257 "void main() { int x = !12; }",
258 "error: 1: '!' cannot operate on 'int'\n1 error\n");
259 test_failure(r,
260 "struct foo { } bar; void main() { foo x = +bar; }",
261 "error: 1: '+' cannot operate on 'foo'\n1 error\n");
262 test_failure(r,
263 "struct foo { } bar; void main() { foo x = -bar; }",
264 "error: 1: '-' cannot operate on 'foo'\n1 error\n");
265 test_success(r,
266 "void main() { vec2 x = vec2(1, 1); x = +x; x = -x; }");
267}
268
269DEF_TEST(SkSLInvalidAssignment, r) {
270 test_failure(r,
271 "void main() { 1 = 2; }",
272 "error: 1: cannot assign to '1'\n1 error\n");
273 test_failure(r,
274 "uniform int x; void main() { x = 0; }",
275 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
276 test_failure(r,
277 "const int x; void main() { x = 0; }",
278 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
279}
280
281DEF_TEST(SkSLBadIndex, r) {
282 test_failure(r,
283 "void main() { int x = 2[0]; }",
284 "error: 1: expected array, but found 'int'\n1 error\n");
285 test_failure(r,
ethannicholas5961bc92016-10-12 06:39:56 -0700286 "void main() { vec2 x = vec2(0); int y = x[0][0]; }",
287 "error: 1: expected array, but found 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700288}
289
290DEF_TEST(SkSLTernaryMismatch, r) {
291 test_failure(r,
292 "void main() { int x = 5 > 2 ? true : 1.0; }",
293 "error: 1: ternary operator result mismatch: 'bool', 'float'\n1 error\n");
294}
295
296DEF_TEST(SkSLInterfaceBlockStorageModifiers, r) {
297 test_failure(r,
298 "uniform foo { out int x; };",
299 "error: 1: interface block fields may not have storage qualifiers\n1 error\n");
300}
ethannicholas22f939e2016-10-13 13:25:34 -0700301
302DEF_TEST(SkSLUseWithoutInitialize, r) {
303 test_failure(r,
304 "void main() { int x; if (5 == 2) x = 3; x++; }",
305 "error: 1: 'x' has not been assigned\n1 error\n");
306 test_failure(r,
307 "void main() { int x[2][2]; int i; x[i][1] = 4; }",
308 "error: 1: 'i' has not been assigned\n1 error\n");
309 test_failure(r,
310 "int main() { int r; return r; }",
311 "error: 1: 'r' has not been assigned\n1 error\n");
312 test_failure(r,
313 "void main() { int x; int y = x; }",
314 "error: 1: 'x' has not been assigned\n1 error\n");
315 test_failure(r,
316 "void main() { bool x; if (true && (false || x)) return; }",
317 "error: 1: 'x' has not been assigned\n1 error\n");
318}
319
320DEF_TEST(SkSLUnreachable, r) {
321 test_failure(r,
322 "void main() { return; return; }",
323 "error: 1: unreachable\n1 error\n");
324 test_failure(r,
325 "void main() { for (;;) { continue; int x = 1; } }",
326 "error: 1: unreachable\n1 error\n");
327 test_failure(r,
328 "void main() { for (;;) { } return; }",
329 "error: 1: unreachable\n1 error\n");
330 test_failure(r,
331 "void main() { if (true) return; else discard; return; }",
332 "error: 1: unreachable\n1 error\n");
333 test_failure(r,
334 "void main() { return; while (true); }",
335 "error: 1: unreachable\n1 error\n");
336}
337
338DEF_TEST(SkSLNoReturn, r) {
339 test_failure(r,
340 "int foo() { if (2 > 5) return 3; }",
341 "error: 1: function can exit without returning a value\n1 error\n");
342}
343
344DEF_TEST(SkSLBreakOutsideLoop, r) {
345 test_failure(r,
346 "void foo() { while(true) {} if (true) break; }",
347 "error: 1: break statement must be inside a loop\n1 error\n");
348}
349
350DEF_TEST(SkSLContinueOutsideLoop, r) {
351 test_failure(r,
352 "void foo() { for(;;); continue; }",
353 "error: 1: continue statement must be inside a loop\n1 error\n");
354}
ethannicholas08a92112016-11-09 13:26:45 -0800355
356DEF_TEST(SkSLStaticIfError, r) {
357 // ensure eliminated branch of static if / ternary is still checked for errors
358 test_failure(r,
359 "void foo() { if (true); else x = 5; }",
360 "error: 1: unknown identifier 'x'\n1 error\n");
361 test_failure(r,
362 "void foo() { if (false) x = 5; }",
363 "error: 1: unknown identifier 'x'\n1 error\n");
364 test_failure(r,
365 "void foo() { true ? 5 : x; }",
366 "error: 1: unknown identifier 'x'\n1 error\n");
367 test_failure(r,
368 "void foo() { false ? x : 5; }",
369 "error: 1: unknown identifier 'x'\n1 error\n");
370}