blob: c24a25d8320b42829d473c36ec0e10e2524d4b9c [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
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050012#if SK_SUPPORT_GPU
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050013
ethannicholasb3058bd2016-07-01 08:22:01 -070014static void test_failure(skiatest::Reporter* r, const char* src, const char* error) {
15 SkSL::Compiler compiler;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050016 SkSL::Program::Settings settings;
17 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
18 settings.fCaps = caps.get();
Brian Osman93ba0a42017-08-14 14:48:10 -040019 compiler.convertProgram(SkSL::Program::kFragment_Kind, SkSL::String(src), settings);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040020 SkSL::String skError(error);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050021 if (compiler.errorText() != skError) {
ethannicholasb3058bd2016-07-01 08:22:01 -070022 SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error,
23 compiler.errorText().c_str());
24 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050025 REPORTER_ASSERT(r, compiler.errorText() == skError);
ethannicholasb3058bd2016-07-01 08:22:01 -070026}
27
28static void test_success(skiatest::Reporter* r, const char* src) {
29 SkSL::Compiler compiler;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050030 SkSL::Program::Settings settings;
31 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
32 settings.fCaps = caps.get();
33 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
Brian Osman93ba0a42017-08-14 14:48:10 -040034 SkSL::String(src), settings);
Ethan Nicholas941e7e22016-12-12 15:33:30 -050035 REPORTER_ASSERT(r, program);
ethannicholasb3058bd2016-07-01 08:22:01 -070036}
37
38DEF_TEST(SkSLUndefinedSymbol, r) {
39 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040040 "void main() { x = float2(1); }",
ethannicholasb3058bd2016-07-01 08:22:01 -070041 "error: 1: unknown identifier 'x'\n1 error\n");
42}
43
44DEF_TEST(SkSLUndefinedFunction, r) {
45 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040046 "void main() { int x = foo(1); }",
ethannicholasb3058bd2016-07-01 08:22:01 -070047 "error: 1: unknown identifier 'foo'\n1 error\n");
48}
49
50DEF_TEST(SkSLGenericArgumentMismatch, r) {
51 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040052 "void main() { float x = sin(1, 2); }",
ethannicholas471e8942016-10-28 09:02:46 -070053 "error: 1: call to 'sin' expected 1 argument, but found 2\n1 error\n");
54 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040055 "void main() { float x = sin(true); }",
ethannicholas471e8942016-10-28 09:02:46 -070056 "error: 1: no match for sin(bool)\n1 error\n");
57 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040058 "void main() { float x = sin(1); }");
ethannicholasb3058bd2016-07-01 08:22:01 -070059}
60
61DEF_TEST(SkSLArgumentCountMismatch, r) {
62 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040063 "float foo(float x) { return x * x; }"
64 "void main() { float x = foo(1, 2); }",
ethannicholasb3058bd2016-07-01 08:22:01 -070065 "error: 1: call to 'foo' expected 1 argument, but found 2\n1 error\n");
66}
67
68DEF_TEST(SkSLArgumentMismatch, r) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -040069 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040070 "float foo(float x) { return x * x; }"
71 "void main() { float x = foo(true); }",
72 "error: 1: expected 'float', but found 'bool'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -070073}
74
75DEF_TEST(SkSLIfTypeMismatch, r) {
76 test_failure(r,
77 "void main() { if (3) { } }",
78 "error: 1: expected 'bool', but found 'int'\n1 error\n");
79}
80
81DEF_TEST(SkSLDoTypeMismatch, r) {
82 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040083 "void main() { do { } while (float2(1)); }",
84 "error: 1: expected 'bool', but found 'float2'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -070085}
86
87DEF_TEST(SkSLWhileTypeMismatch, r) {
88 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040089 "void main() { while (float3(1)) { } }",
90 "error: 1: expected 'bool', but found 'float3'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -070091}
92
93DEF_TEST(SkSLForTypeMismatch, r) {
94 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040095 "void main() { for (int x = 0; x; x++) { } }",
ethannicholasb3058bd2016-07-01 08:22:01 -070096 "error: 1: expected 'bool', but found 'int'\n1 error\n");
97}
98
99DEF_TEST(SkSLConstructorTypeMismatch, r) {
100 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400101 "void main() { float2 x = float2(1.0, false); }",
102 "error: 1: expected 'float', but found 'bool'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700103 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400104 "void main() { float2 x = float2(bool2(false)); }",
105 "error: 1: 'bool2' is not a valid parameter to 'float2' constructor\n1 error\n");
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400106 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400107 "void main() { bool2 x = bool2(float2(1)); }",
108 "error: 1: 'float2' is not a valid parameter to 'bool2' constructor\n1 error\n");
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400109 test_failure(r,
ethannicholasb3058bd2016-07-01 08:22:01 -0700110 "void main() { bool x = bool(1.0); }",
111 "error: 1: cannot construct 'bool'\n1 error\n");
112 test_failure(r,
113 "struct foo { int x; }; void main() { foo x = foo(5); }",
114 "error: 1: cannot construct 'foo'\n1 error\n");
115 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400116 "struct foo { int x; } foo; void main() { float x = float(foo); }",
117 "error: 1: invalid argument to 'float' constructor (expected a number or bool, but found 'foo')\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700118 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400119 "struct foo { int x; } foo; void main() { float2 x = float2(foo); }",
120 "error: 1: 'foo' is not a valid parameter to 'float2' constructor\n1 error\n");
Ethan Nicholas3deaeb22017-04-25 14:42:11 -0400121 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400122 "void main() { float2x2 x = float2x2(true); }",
123 "error: 1: expected 'float', but found 'bool'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700124}
125
126DEF_TEST(SkSLConstructorArgumentCount, r) {
127 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400128 "void main() { float3 x = float3(1.0, 2.0); }",
129 "error: 1: invalid arguments to 'float3' constructor (expected 3 scalars, but "
ethannicholasb3058bd2016-07-01 08:22:01 -0700130 "found 2)\n1 error\n");
Ethan Nicholas84645e32017-02-09 13:57:14 -0500131 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400132 "void main() { float3 x = float3(1.0, 2.0, 3.0, 4.0); }",
133 "error: 1: invalid arguments to 'float3' constructor (expected 3 scalars, but found "
Ethan Nicholas84645e32017-02-09 13:57:14 -0500134 "4)\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700135}
136
137DEF_TEST(SkSLSwizzleScalar, r) {
138 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400139 "void main() { float x = 1; float y = x.y; }",
140 "error: 1: cannot swizzle value of type 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700141}
142
143DEF_TEST(SkSLSwizzleMatrix, r) {
144 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400145 "void main() { float2x2 x = float2x2(1); float y = x.y; }",
146 "error: 1: cannot swizzle value of type 'float2x2'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700147}
148
149DEF_TEST(SkSLSwizzleOutOfBounds, r) {
150 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400151 "void main() { float3 test = float2(1).xyz; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700152 "error: 1: invalid swizzle component 'z'\n1 error\n");
153}
154
155DEF_TEST(SkSLSwizzleTooManyComponents, r) {
156 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400157 "void main() { float4 test = float2(1).xxxxx; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700158 "error: 1: too many components in swizzle mask 'xxxxx'\n1 error\n");
159}
160
161DEF_TEST(SkSLSwizzleDuplicateOutput, r) {
162 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400163 "void main() { float4 test = float4(1); test.xyyz = float4(1); }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700164 "error: 1: cannot write to the same swizzle field more than once\n1 error\n");
165}
ethannicholasea4567c2016-10-17 11:24:37 -0700166
ethannicholasb3058bd2016-07-01 08:22:01 -0700167DEF_TEST(SkSLAssignmentTypeMismatch, r) {
168 test_failure(r,
169 "void main() { int x = 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400170 "error: 1: expected 'int', but found 'float'\n1 error\n");
ethannicholasea4567c2016-10-17 11:24:37 -0700171 test_failure(r,
172 "void main() { int x; x = 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400173 "error: 1: type mismatch: '=' cannot operate on 'int', 'float'\n1 error\n");
ethannicholasea4567c2016-10-17 11:24:37 -0700174 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400175 "void main() { float3 x = float3(0); x *= 1.0; }");
ethannicholasea4567c2016-10-17 11:24:37 -0700176 test_failure(r,
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400177 "void main() { int3 x = int3(0); x *= 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400178 "error: 1: type mismatch: '*=' cannot operate on 'int3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700179}
180
181DEF_TEST(SkSLReturnFromVoid, r) {
182 test_failure(r,
183 "void main() { return true; }",
184 "error: 1: may not return a value from a void function\n1 error\n");
185}
186
187DEF_TEST(SkSLReturnMissingValue, r) {
188 test_failure(r,
189 "int foo() { return; } void main() { }",
190 "error: 1: expected function to return 'int'\n1 error\n");
191}
192
193DEF_TEST(SkSLReturnTypeMismatch, r) {
194 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400195 "int foo() { return 1.0; } void main() { }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400196 "error: 1: expected 'int', but found 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700197}
198
199DEF_TEST(SkSLDuplicateFunction, r) {
200 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400201 "void main() { } void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700202 "error: 1: duplicate definition of void main()\n1 error\n");
203 test_success(r,
204 "void main(); void main() { }");
205}
206
207DEF_TEST(SkSLUsingInvalidValue, r) {
208 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400209 "void main() { int x = int; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700210 "error: 1: expected '(' to begin constructor invocation\n1 error\n");
211 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400212 "int test() { return 1; } void main() { int x = test; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700213 "error: 1: expected '(' to begin function call\n1 error\n");
214}
215DEF_TEST(SkSLDifferentReturnType, r) {
216 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400217 "int main() { return 1; } void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700218 "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 "
219 "error\n");
220}
221
222DEF_TEST(SkSLDifferentModifiers, r) {
223 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400224 "void test(int x); void test(out int x) { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700225 "error: 1: modifiers on parameter 1 differ between declaration and definition\n1 "
226 "error\n");
227}
228
229DEF_TEST(SkSLDuplicateSymbol, r) {
230 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400231 "int main; void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700232 "error: 1: symbol 'main' was already defined\n1 error\n");
233
234 test_failure(r,
235 "int x; int x; void main() { }",
236 "error: 1: symbol 'x' was already defined\n1 error\n");
237
238 test_success(r, "int x; void main() { int x; }");
239}
240
241DEF_TEST(SkSLBinaryTypeMismatch, r) {
242 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400243 "void main() { float x = 3 * true; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700244 "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n1 error\n");
245 test_failure(r,
246 "void main() { bool x = 1 || 2.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400247 "error: 1: type mismatch: '||' cannot operate on 'int', 'float'\n1 error\n");
Ethan Nicholas23463002018-03-28 15:16:15 -0400248 test_failure(r,
249 "void main() { bool x = float2(0) == 0; }",
250 "error: 1: type mismatch: '==' cannot operate on 'float2', 'int'\n1 error\n");
251 test_failure(r,
252 "void main() { bool x = float2(0) != 0; }",
253 "error: 1: type mismatch: '!=' cannot operate on 'float2', 'int'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700254}
255
256DEF_TEST(SkSLCallNonFunction, r) {
257 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400258 "void main() { float x = 3; x(); }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700259 "error: 1: 'x' is not a function\n1 error\n");
260}
261
262DEF_TEST(SkSLInvalidUnary, r) {
263 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400264 "void main() { float4x4 x = float4x4(1); ++x; }",
265 "error: 1: '++' cannot operate on 'float4x4'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700266 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400267 "void main() { float3 x = float3(1); --x; }",
268 "error: 1: '--' cannot operate on 'float3'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700269 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400270 "void main() { float4x4 x = float4x4(1); x++; }",
271 "error: 1: '++' cannot operate on 'float4x4'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700272 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400273 "void main() { float3 x = float3(1); x--; }",
274 "error: 1: '--' cannot operate on 'float3'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700275 test_failure(r,
276 "void main() { int x = !12; }",
277 "error: 1: '!' cannot operate on 'int'\n1 error\n");
278 test_failure(r,
279 "struct foo { } bar; void main() { foo x = +bar; }",
280 "error: 1: '+' cannot operate on 'foo'\n1 error\n");
281 test_failure(r,
282 "struct foo { } bar; void main() { foo x = -bar; }",
283 "error: 1: '-' cannot operate on 'foo'\n1 error\n");
284 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400285 "void main() { float2 x = float2(1, 1); x = +x; x = -x; }");
ethannicholasb3058bd2016-07-01 08:22:01 -0700286}
287
288DEF_TEST(SkSLInvalidAssignment, r) {
289 test_failure(r,
290 "void main() { 1 = 2; }",
291 "error: 1: cannot assign to '1'\n1 error\n");
292 test_failure(r,
293 "uniform int x; void main() { x = 0; }",
294 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
295 test_failure(r,
296 "const int x; void main() { x = 0; }",
297 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
298}
299
300DEF_TEST(SkSLBadIndex, r) {
301 test_failure(r,
302 "void main() { int x = 2[0]; }",
303 "error: 1: expected array, but found 'int'\n1 error\n");
304 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400305 "void main() { float2 x = float2(0); int y = x[0][0]; }",
306 "error: 1: expected array, but found 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700307}
308
309DEF_TEST(SkSLTernaryMismatch, r) {
310 test_failure(r,
311 "void main() { int x = 5 > 2 ? true : 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400312 "error: 1: ternary operator result mismatch: 'bool', 'float'\n1 error\n");
Ethan Nicholas2be687a2017-01-03 16:44:39 -0500313 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400314 "void main() { int x = 5 > 2 ? float3(1) : 1.0; }",
315 "error: 1: ternary operator result mismatch: 'float3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700316}
317
318DEF_TEST(SkSLInterfaceBlockStorageModifiers, r) {
319 test_failure(r,
320 "uniform foo { out int x; };",
321 "error: 1: interface block fields may not have storage qualifiers\n1 error\n");
322}
ethannicholas22f939e2016-10-13 13:25:34 -0700323
324DEF_TEST(SkSLUseWithoutInitialize, r) {
325 test_failure(r,
326 "void main() { int x; if (5 == 2) x = 3; x++; }",
327 "error: 1: 'x' has not been assigned\n1 error\n");
328 test_failure(r,
329 "void main() { int x[2][2]; int i; x[i][1] = 4; }",
330 "error: 1: 'i' has not been assigned\n1 error\n");
331 test_failure(r,
332 "int main() { int r; return r; }",
333 "error: 1: 'r' has not been assigned\n1 error\n");
334 test_failure(r,
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000335 "void main() { int x; int y = x; }",
ethannicholas22f939e2016-10-13 13:25:34 -0700336 "error: 1: 'x' has not been assigned\n1 error\n");
337 test_failure(r,
338 "void main() { bool x; if (true && (false || x)) return; }",
339 "error: 1: 'x' has not been assigned\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500340 test_failure(r,
341 "void main() { int x; switch (3) { case 0: x = 0; case 1: x = 1; }"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400342 "sk_FragColor = float4(x); }",
Ethan Nicholasaf197692017-02-27 13:26:45 -0500343 "error: 1: 'x' has not been assigned\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700344}
345
346DEF_TEST(SkSLUnreachable, r) {
347 test_failure(r,
348 "void main() { return; return; }",
349 "error: 1: unreachable\n1 error\n");
350 test_failure(r,
351 "void main() { for (;;) { continue; int x = 1; } }",
352 "error: 1: unreachable\n1 error\n");
Ethan Nicholasd9fe7002017-05-30 10:15:34 -0400353/* test_failure(r,
ethannicholas22f939e2016-10-13 13:25:34 -0700354 "void main() { for (;;) { } return; }",
Ethan Nicholasd9fe7002017-05-30 10:15:34 -0400355 "error: 1: unreachable\n1 error\n");*/
ethannicholas22f939e2016-10-13 13:25:34 -0700356 test_failure(r,
357 "void main() { if (true) return; else discard; return; }",
358 "error: 1: unreachable\n1 error\n");
359 test_failure(r,
360 "void main() { return; while (true); }",
361 "error: 1: unreachable\n1 error\n");
362}
363
364DEF_TEST(SkSLNoReturn, r) {
365 test_failure(r,
366 "int foo() { if (2 > 5) return 3; }",
367 "error: 1: function can exit without returning a value\n1 error\n");
368}
369
370DEF_TEST(SkSLBreakOutsideLoop, r) {
371 test_failure(r,
372 "void foo() { while(true) {} if (true) break; }",
Ethan Nicholasaf197692017-02-27 13:26:45 -0500373 "error: 1: break statement must be inside a loop or switch\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700374}
375
376DEF_TEST(SkSLContinueOutsideLoop, r) {
377 test_failure(r,
378 "void foo() { for(;;); continue; }",
379 "error: 1: continue statement must be inside a loop\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500380 test_failure(r,
381 "void foo() { switch (1) { default: continue; } }",
382 "error: 1: continue statement must be inside a loop\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700383}
ethannicholas08a92112016-11-09 13:26:45 -0800384
385DEF_TEST(SkSLStaticIfError, r) {
386 // ensure eliminated branch of static if / ternary is still checked for errors
387 test_failure(r,
388 "void foo() { if (true); else x = 5; }",
389 "error: 1: unknown identifier 'x'\n1 error\n");
390 test_failure(r,
391 "void foo() { if (false) x = 5; }",
392 "error: 1: unknown identifier 'x'\n1 error\n");
393 test_failure(r,
394 "void foo() { true ? 5 : x; }",
395 "error: 1: unknown identifier 'x'\n1 error\n");
396 test_failure(r,
397 "void foo() { false ? x : 5; }",
398 "error: 1: unknown identifier 'x'\n1 error\n");
399}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500400
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500401DEF_TEST(SkSLBadCap, r) {
402 test_failure(r,
403 "bool b = sk_Caps.bugFreeDriver;",
404 "error: 1: unknown capability flag 'bugFreeDriver'\n1 error\n");
405}
406
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500407DEF_TEST(SkSLDivByZero, r) {
408 test_failure(r,
409 "int x = 1 / 0;",
410 "error: 1: division by zero\n1 error\n");
411 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400412 "float x = 1 / 0;",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500413 "error: 1: division by zero\n1 error\n");
414 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400415 "float x = 1.0 / 0.0;",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500416 "error: 1: division by zero\n1 error\n");
417 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400418 "float x = -67.0 / (3.0 - 3);",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500419 "error: 1: division by zero\n1 error\n");
420}
421
Ethan Nicholas38657112017-02-09 17:01:22 -0500422DEF_TEST(SkSLUnsupportedGLSLIdentifiers, r) {
423 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400424 "void main() { float x = gl_FragCoord.x; };",
Ethan Nicholas38657112017-02-09 17:01:22 -0500425 "error: 1: unknown identifier 'gl_FragCoord'\n1 error\n");
426 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400427 "void main() { float r = gl_FragColor.r; };",
Ethan Nicholas38657112017-02-09 17:01:22 -0500428 "error: 1: unknown identifier 'gl_FragColor'\n1 error\n");
429}
430
Ethan Nicholasaf197692017-02-27 13:26:45 -0500431DEF_TEST(SkSLWrongSwitchTypes, r) {
432 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400433 "void main() { switch (float2(1)) { case 1: break; } }",
434 "error: 1: expected 'int', but found 'float2'\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500435 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400436 "void main() { switch (1) { case float2(1): break; } }",
437 "error: 1: expected 'int', but found 'float2'\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500438}
439
440DEF_TEST(SkSLNonConstantCase, r) {
441 test_failure(r,
442 "void main() { int x = 1; switch (1) { case x: break; } }",
443 "error: 1: case value must be a constant\n1 error\n");
444}
445
446DEF_TEST(SkSLDuplicateCase, r) {
447 test_failure(r,
448 "void main() { switch (1) { case 0: case 1: case 0: break; } }",
449 "error: 1: duplicate case value\n1 error\n");
450}
451
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400452DEF_TEST(SkSLFieldAfterRuntimeArray, r) {
453 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400454 "buffer broken { float x[]; float y; };",
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400455 "error: 1: only the last entry in an interface block may be a runtime-sized "
456 "array\n1 error\n");
457}
458
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400459DEF_TEST(SkSLStaticIf, r) {
460 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400461 "void main() { float x = 5; float y = 10;"
462 "@if (x < y) { sk_FragColor = float4(1); } }");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400463 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400464 "void main() { float x = sqrt(25); float y = 10;"
465 "@if (x < y) { sk_FragColor = float4(1); } }",
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400466 "error: 1: static if has non-static test\n1 error\n");
467}
468
469DEF_TEST(SkSLStaticSwitch, r) {
470 test_success(r,
471 "void main() {"
472 "int x = 1;"
473 "@switch (x) {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400474 "case 1: sk_FragColor = float4(1); break;"
475 "default: sk_FragColor = float4(0);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400476 "}"
477 "}");
478 test_failure(r,
479 "void main() {"
480 "int x = int(sqrt(1));"
481 "@switch (x) {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400482 "case 1: sk_FragColor = float4(1); break;"
483 "default: sk_FragColor = float4(0);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400484 "}"
485 "}",
486 "error: 1: static switch has non-static test\n1 error\n");
487 test_failure(r,
488 "void main() {"
489 "int x = 1;"
490 "@switch (x) {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400491 "case 1: sk_FragColor = float4(1); if (sqrt(0) < sqrt(1)) break;"
492 "default: sk_FragColor = float4(0);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400493 "}"
494 "}",
495 "error: 1: static switch contains non-static conditional break\n1 error\n");
496}
497
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500498DEF_TEST(SkSLInterfaceBlockScope, r) {
499 test_failure(r,
500 "uniform testBlock {"
501 "float x;"
502 "} test[x];",
503 "error: 1: unknown identifier 'x'\n1 error\n");
504}
505
Ethan Nicholas6c942712018-03-16 09:45:11 -0400506DEF_TEST(SkSLDuplicateOutput, r) {
507 test_failure(r,
508 "layout (location=0, index=0) out half4 duplicateOutput;",
509 "error: 1: out location=0, index=0 is reserved for sk_FragColor\n1 error\n");
510}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500511#endif