blob: 3afb64fe6167a5f9378234b14e34f05c18e762fe [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,
97 "void main() { bool x = bool(1.0); }",
98 "error: 1: cannot construct 'bool'\n1 error\n");
99 test_failure(r,
100 "struct foo { int x; }; void main() { foo x = foo(5); }",
101 "error: 1: cannot construct 'foo'\n1 error\n");
102 test_failure(r,
103 "struct foo { int x; } foo; void main() { float x = float(foo); }",
104 "error: 1: invalid argument to 'float' constructor (expected a number or bool, but found 'foo')\n1 error\n");
105 test_failure(r,
106 "struct foo { int x; } foo; void main() { vec2 x = vec2(foo); }",
107 "error: 1: 'foo' is not a valid parameter to 'vec2' constructor\n1 error\n");
108}
109
110DEF_TEST(SkSLConstructorArgumentCount, r) {
111 test_failure(r,
112 "void main() { vec3 x = vec3(1.0, 2.0); }",
113 "error: 1: invalid arguments to 'vec3' constructor (expected 3 scalars, but "
114 "found 2)\n1 error\n");
115 test_success(r, "void main() { vec3 x = vec3(1.0, 2.0, 3.0, 4.0); }");
116}
117
118DEF_TEST(SkSLSwizzleScalar, r) {
119 test_failure(r,
120 "void main() { float x = 1; float y = x.y; }",
121 "error: 1: cannot swizzle value of type 'float'\n1 error\n");
122}
123
124DEF_TEST(SkSLSwizzleMatrix, r) {
125 test_failure(r,
126 "void main() { mat2 x = mat2(1); float y = x.y; }",
127 "error: 1: cannot swizzle value of type 'mat2'\n1 error\n");
128}
129
130DEF_TEST(SkSLSwizzleOutOfBounds, r) {
131 test_failure(r,
132 "void main() { vec3 test = vec2(1).xyz; }",
133 "error: 1: invalid swizzle component 'z'\n1 error\n");
134}
135
136DEF_TEST(SkSLSwizzleTooManyComponents, r) {
137 test_failure(r,
138 "void main() { vec4 test = vec2(1).xxxxx; }",
139 "error: 1: too many components in swizzle mask 'xxxxx'\n1 error\n");
140}
141
142DEF_TEST(SkSLSwizzleDuplicateOutput, r) {
143 test_failure(r,
144 "void main() { vec4 test = vec4(1); test.xyyz = vec4(1); }",
145 "error: 1: cannot write to the same swizzle field more than once\n1 error\n");
146}
ethannicholasea4567c2016-10-17 11:24:37 -0700147
ethannicholasb3058bd2016-07-01 08:22:01 -0700148DEF_TEST(SkSLAssignmentTypeMismatch, r) {
149 test_failure(r,
150 "void main() { int x = 1.0; }",
151 "error: 1: expected 'int', but found 'float'\n1 error\n");
ethannicholasea4567c2016-10-17 11:24:37 -0700152 test_failure(r,
153 "void main() { int x; x = 1.0; }",
154 "error: 1: type mismatch: '=' cannot operate on 'int', 'float'\n1 error\n");
155 test_success(r,
156 "void main() { vec3 x = vec3(0); x *= 1.0; }");
157 test_failure(r,
158 "void main() { ivec3 x = ivec3(0); x *= 1.0; }",
159 "error: 1: type mismatch: '*=' cannot operate on 'ivec3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700160}
161
162DEF_TEST(SkSLReturnFromVoid, r) {
163 test_failure(r,
164 "void main() { return true; }",
165 "error: 1: may not return a value from a void function\n1 error\n");
166}
167
168DEF_TEST(SkSLReturnMissingValue, r) {
169 test_failure(r,
170 "int foo() { return; } void main() { }",
171 "error: 1: expected function to return 'int'\n1 error\n");
172}
173
174DEF_TEST(SkSLReturnTypeMismatch, r) {
175 test_failure(r,
176 "int foo() { return 1.0; } void main() { }",
177 "error: 1: expected 'int', but found 'float'\n1 error\n");
178}
179
180DEF_TEST(SkSLDuplicateFunction, r) {
181 test_failure(r,
182 "void main() { } void main() { }",
183 "error: 1: duplicate definition of void main()\n1 error\n");
184 test_success(r,
185 "void main(); void main() { }");
186}
187
188DEF_TEST(SkSLUsingInvalidValue, r) {
189 test_failure(r,
190 "void main() { int x = int; }",
191 "error: 1: expected '(' to begin constructor invocation\n1 error\n");
192 test_failure(r,
193 "int test() { return 1; } void main() { int x = test; }",
194 "error: 1: expected '(' to begin function call\n1 error\n");
195}
196DEF_TEST(SkSLDifferentReturnType, r) {
197 test_failure(r,
ethannicholas22f939e2016-10-13 13:25:34 -0700198 "int main() { return 1; } void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700199 "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 "
200 "error\n");
201}
202
203DEF_TEST(SkSLDifferentModifiers, r) {
204 test_failure(r,
205 "void test(int x); void test(out int x) { }",
206 "error: 1: modifiers on parameter 1 differ between declaration and definition\n1 "
207 "error\n");
208}
209
210DEF_TEST(SkSLDuplicateSymbol, r) {
211 test_failure(r,
212 "int main; void main() { }",
213 "error: 1: symbol 'main' was already defined\n1 error\n");
214
215 test_failure(r,
216 "int x; int x; void main() { }",
217 "error: 1: symbol 'x' was already defined\n1 error\n");
218
219 test_success(r, "int x; void main() { int x; }");
220}
221
222DEF_TEST(SkSLBinaryTypeMismatch, r) {
223 test_failure(r,
224 "void main() { float x = 3 * true; }",
225 "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n1 error\n");
226 test_failure(r,
227 "void main() { bool x = 1 || 2.0; }",
228 "error: 1: type mismatch: '||' cannot operate on 'int', 'float'\n1 error\n");
229}
230
231DEF_TEST(SkSLCallNonFunction, r) {
232 test_failure(r,
233 "void main() { float x = 3; x(); }",
234 "error: 1: 'x' is not a function\n1 error\n");
235}
236
237DEF_TEST(SkSLInvalidUnary, r) {
238 test_failure(r,
239 "void main() { mat4 x = mat4(1); ++x; }",
240 "error: 1: '++' cannot operate on 'mat4'\n1 error\n");
241 test_failure(r,
242 "void main() { vec3 x = vec3(1); --x; }",
243 "error: 1: '--' cannot operate on 'vec3'\n1 error\n");
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() { int x = !12; }",
252 "error: 1: '!' cannot operate on 'int'\n1 error\n");
253 test_failure(r,
254 "struct foo { } bar; void main() { foo x = +bar; }",
255 "error: 1: '+' cannot operate on 'foo'\n1 error\n");
256 test_failure(r,
257 "struct foo { } bar; void main() { foo x = -bar; }",
258 "error: 1: '-' cannot operate on 'foo'\n1 error\n");
259 test_success(r,
260 "void main() { vec2 x = vec2(1, 1); x = +x; x = -x; }");
261}
262
263DEF_TEST(SkSLInvalidAssignment, r) {
264 test_failure(r,
265 "void main() { 1 = 2; }",
266 "error: 1: cannot assign to '1'\n1 error\n");
267 test_failure(r,
268 "uniform int x; void main() { x = 0; }",
269 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
270 test_failure(r,
271 "const int x; void main() { x = 0; }",
272 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
273}
274
275DEF_TEST(SkSLBadIndex, r) {
276 test_failure(r,
277 "void main() { int x = 2[0]; }",
278 "error: 1: expected array, but found 'int'\n1 error\n");
279 test_failure(r,
ethannicholas5961bc92016-10-12 06:39:56 -0700280 "void main() { vec2 x = vec2(0); int y = x[0][0]; }",
281 "error: 1: expected array, but found 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700282}
283
284DEF_TEST(SkSLTernaryMismatch, r) {
285 test_failure(r,
286 "void main() { int x = 5 > 2 ? true : 1.0; }",
287 "error: 1: ternary operator result mismatch: 'bool', 'float'\n1 error\n");
288}
289
290DEF_TEST(SkSLInterfaceBlockStorageModifiers, r) {
291 test_failure(r,
292 "uniform foo { out int x; };",
293 "error: 1: interface block fields may not have storage qualifiers\n1 error\n");
294}
ethannicholas22f939e2016-10-13 13:25:34 -0700295
296DEF_TEST(SkSLUseWithoutInitialize, r) {
297 test_failure(r,
298 "void main() { int x; if (5 == 2) x = 3; x++; }",
299 "error: 1: 'x' has not been assigned\n1 error\n");
300 test_failure(r,
301 "void main() { int x[2][2]; int i; x[i][1] = 4; }",
302 "error: 1: 'i' has not been assigned\n1 error\n");
303 test_failure(r,
304 "int main() { int r; return r; }",
305 "error: 1: 'r' has not been assigned\n1 error\n");
306 test_failure(r,
307 "void main() { int x; int y = x; }",
308 "error: 1: 'x' has not been assigned\n1 error\n");
309 test_failure(r,
310 "void main() { bool x; if (true && (false || x)) return; }",
311 "error: 1: 'x' has not been assigned\n1 error\n");
312}
313
314DEF_TEST(SkSLUnreachable, r) {
315 test_failure(r,
316 "void main() { return; return; }",
317 "error: 1: unreachable\n1 error\n");
318 test_failure(r,
319 "void main() { for (;;) { continue; int x = 1; } }",
320 "error: 1: unreachable\n1 error\n");
321 test_failure(r,
322 "void main() { for (;;) { } return; }",
323 "error: 1: unreachable\n1 error\n");
324 test_failure(r,
325 "void main() { if (true) return; else discard; return; }",
326 "error: 1: unreachable\n1 error\n");
327 test_failure(r,
328 "void main() { return; while (true); }",
329 "error: 1: unreachable\n1 error\n");
330}
331
332DEF_TEST(SkSLNoReturn, r) {
333 test_failure(r,
334 "int foo() { if (2 > 5) return 3; }",
335 "error: 1: function can exit without returning a value\n1 error\n");
336}
337
338DEF_TEST(SkSLBreakOutsideLoop, r) {
339 test_failure(r,
340 "void foo() { while(true) {} if (true) break; }",
341 "error: 1: break statement must be inside a loop\n1 error\n");
342}
343
344DEF_TEST(SkSLContinueOutsideLoop, r) {
345 test_failure(r,
346 "void foo() { for(;;); continue; }",
347 "error: 1: continue statement must be inside a loop\n1 error\n");
348}