blob: 575fd749170680e1803f98bc971ccf52b45a5c28 [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");
ethannicholasb3058bd2016-07-01 08:22:01 -0700248}
249
250DEF_TEST(SkSLCallNonFunction, r) {
251 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400252 "void main() { float x = 3; x(); }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700253 "error: 1: 'x' is not a function\n1 error\n");
254}
255
256DEF_TEST(SkSLInvalidUnary, r) {
257 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400258 "void main() { float4x4 x = float4x4(1); ++x; }",
259 "error: 1: '++' cannot operate on 'float4x4'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700260 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400261 "void main() { float3 x = float3(1); --x; }",
262 "error: 1: '--' cannot operate on 'float3'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700263 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,
270 "void main() { int x = !12; }",
271 "error: 1: '!' cannot operate on 'int'\n1 error\n");
272 test_failure(r,
273 "struct foo { } bar; void main() { foo x = +bar; }",
274 "error: 1: '+' cannot operate on 'foo'\n1 error\n");
275 test_failure(r,
276 "struct foo { } bar; void main() { foo x = -bar; }",
277 "error: 1: '-' cannot operate on 'foo'\n1 error\n");
278 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400279 "void main() { float2 x = float2(1, 1); x = +x; x = -x; }");
ethannicholasb3058bd2016-07-01 08:22:01 -0700280}
281
282DEF_TEST(SkSLInvalidAssignment, r) {
283 test_failure(r,
284 "void main() { 1 = 2; }",
285 "error: 1: cannot assign to '1'\n1 error\n");
286 test_failure(r,
287 "uniform int x; void main() { x = 0; }",
288 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
289 test_failure(r,
290 "const int x; void main() { x = 0; }",
291 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
292}
293
294DEF_TEST(SkSLBadIndex, r) {
295 test_failure(r,
296 "void main() { int x = 2[0]; }",
297 "error: 1: expected array, but found 'int'\n1 error\n");
298 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400299 "void main() { float2 x = float2(0); int y = x[0][0]; }",
300 "error: 1: expected array, but found 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700301}
302
303DEF_TEST(SkSLTernaryMismatch, r) {
304 test_failure(r,
305 "void main() { int x = 5 > 2 ? true : 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400306 "error: 1: ternary operator result mismatch: 'bool', 'float'\n1 error\n");
Ethan Nicholas2be687a2017-01-03 16:44:39 -0500307 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400308 "void main() { int x = 5 > 2 ? float3(1) : 1.0; }",
309 "error: 1: ternary operator result mismatch: 'float3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700310}
311
312DEF_TEST(SkSLInterfaceBlockStorageModifiers, r) {
313 test_failure(r,
314 "uniform foo { out int x; };",
315 "error: 1: interface block fields may not have storage qualifiers\n1 error\n");
316}
ethannicholas22f939e2016-10-13 13:25:34 -0700317
318DEF_TEST(SkSLUseWithoutInitialize, r) {
319 test_failure(r,
320 "void main() { int x; if (5 == 2) x = 3; x++; }",
321 "error: 1: 'x' has not been assigned\n1 error\n");
322 test_failure(r,
323 "void main() { int x[2][2]; int i; x[i][1] = 4; }",
324 "error: 1: 'i' has not been assigned\n1 error\n");
325 test_failure(r,
326 "int main() { int r; return r; }",
327 "error: 1: 'r' has not been assigned\n1 error\n");
328 test_failure(r,
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000329 "void main() { int x; int y = x; }",
ethannicholas22f939e2016-10-13 13:25:34 -0700330 "error: 1: 'x' has not been assigned\n1 error\n");
331 test_failure(r,
332 "void main() { bool x; if (true && (false || x)) return; }",
333 "error: 1: 'x' has not been assigned\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500334 test_failure(r,
335 "void main() { int x; switch (3) { case 0: x = 0; case 1: x = 1; }"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400336 "sk_FragColor = float4(x); }",
Ethan Nicholasaf197692017-02-27 13:26:45 -0500337 "error: 1: 'x' has not been assigned\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700338}
339
340DEF_TEST(SkSLUnreachable, r) {
341 test_failure(r,
342 "void main() { return; return; }",
343 "error: 1: unreachable\n1 error\n");
344 test_failure(r,
345 "void main() { for (;;) { continue; int x = 1; } }",
346 "error: 1: unreachable\n1 error\n");
Ethan Nicholasd9fe7002017-05-30 10:15:34 -0400347/* test_failure(r,
ethannicholas22f939e2016-10-13 13:25:34 -0700348 "void main() { for (;;) { } return; }",
Ethan Nicholasd9fe7002017-05-30 10:15:34 -0400349 "error: 1: unreachable\n1 error\n");*/
ethannicholas22f939e2016-10-13 13:25:34 -0700350 test_failure(r,
351 "void main() { if (true) return; else discard; return; }",
352 "error: 1: unreachable\n1 error\n");
353 test_failure(r,
354 "void main() { return; while (true); }",
355 "error: 1: unreachable\n1 error\n");
356}
357
358DEF_TEST(SkSLNoReturn, r) {
359 test_failure(r,
360 "int foo() { if (2 > 5) return 3; }",
361 "error: 1: function can exit without returning a value\n1 error\n");
362}
363
364DEF_TEST(SkSLBreakOutsideLoop, r) {
365 test_failure(r,
366 "void foo() { while(true) {} if (true) break; }",
Ethan Nicholasaf197692017-02-27 13:26:45 -0500367 "error: 1: break statement must be inside a loop or switch\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700368}
369
370DEF_TEST(SkSLContinueOutsideLoop, r) {
371 test_failure(r,
372 "void foo() { for(;;); continue; }",
373 "error: 1: continue statement must be inside a loop\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500374 test_failure(r,
375 "void foo() { switch (1) { default: continue; } }",
376 "error: 1: continue statement must be inside a loop\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700377}
ethannicholas08a92112016-11-09 13:26:45 -0800378
379DEF_TEST(SkSLStaticIfError, r) {
380 // ensure eliminated branch of static if / ternary is still checked for errors
381 test_failure(r,
382 "void foo() { if (true); else x = 5; }",
383 "error: 1: unknown identifier 'x'\n1 error\n");
384 test_failure(r,
385 "void foo() { if (false) x = 5; }",
386 "error: 1: unknown identifier 'x'\n1 error\n");
387 test_failure(r,
388 "void foo() { true ? 5 : x; }",
389 "error: 1: unknown identifier 'x'\n1 error\n");
390 test_failure(r,
391 "void foo() { false ? x : 5; }",
392 "error: 1: unknown identifier 'x'\n1 error\n");
393}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500394
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500395DEF_TEST(SkSLBadCap, r) {
396 test_failure(r,
397 "bool b = sk_Caps.bugFreeDriver;",
398 "error: 1: unknown capability flag 'bugFreeDriver'\n1 error\n");
399}
400
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500401DEF_TEST(SkSLDivByZero, r) {
402 test_failure(r,
403 "int x = 1 / 0;",
404 "error: 1: division by zero\n1 error\n");
405 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400406 "float x = 1 / 0;",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500407 "error: 1: division by zero\n1 error\n");
408 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400409 "float x = 1.0 / 0.0;",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500410 "error: 1: division by zero\n1 error\n");
411 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400412 "float x = -67.0 / (3.0 - 3);",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500413 "error: 1: division by zero\n1 error\n");
414}
415
Ethan Nicholas38657112017-02-09 17:01:22 -0500416DEF_TEST(SkSLUnsupportedGLSLIdentifiers, r) {
417 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400418 "void main() { float x = gl_FragCoord.x; };",
Ethan Nicholas38657112017-02-09 17:01:22 -0500419 "error: 1: unknown identifier 'gl_FragCoord'\n1 error\n");
420 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400421 "void main() { float r = gl_FragColor.r; };",
Ethan Nicholas38657112017-02-09 17:01:22 -0500422 "error: 1: unknown identifier 'gl_FragColor'\n1 error\n");
423}
424
Ethan Nicholasaf197692017-02-27 13:26:45 -0500425DEF_TEST(SkSLWrongSwitchTypes, r) {
426 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400427 "void main() { switch (float2(1)) { case 1: break; } }",
428 "error: 1: expected 'int', but found 'float2'\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500429 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400430 "void main() { switch (1) { case float2(1): break; } }",
431 "error: 1: expected 'int', but found 'float2'\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500432}
433
434DEF_TEST(SkSLNonConstantCase, r) {
435 test_failure(r,
436 "void main() { int x = 1; switch (1) { case x: break; } }",
437 "error: 1: case value must be a constant\n1 error\n");
438}
439
440DEF_TEST(SkSLDuplicateCase, r) {
441 test_failure(r,
442 "void main() { switch (1) { case 0: case 1: case 0: break; } }",
443 "error: 1: duplicate case value\n1 error\n");
444}
445
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400446DEF_TEST(SkSLFieldAfterRuntimeArray, r) {
447 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400448 "buffer broken { float x[]; float y; };",
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400449 "error: 1: only the last entry in an interface block may be a runtime-sized "
450 "array\n1 error\n");
451}
452
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400453DEF_TEST(SkSLStaticIf, r) {
454 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400455 "void main() { float x = 5; float y = 10;"
456 "@if (x < y) { sk_FragColor = float4(1); } }");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400457 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400458 "void main() { float x = sqrt(25); float y = 10;"
459 "@if (x < y) { sk_FragColor = float4(1); } }",
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400460 "error: 1: static if has non-static test\n1 error\n");
461}
462
463DEF_TEST(SkSLStaticSwitch, r) {
464 test_success(r,
465 "void main() {"
466 "int x = 1;"
467 "@switch (x) {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400468 "case 1: sk_FragColor = float4(1); break;"
469 "default: sk_FragColor = float4(0);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400470 "}"
471 "}");
472 test_failure(r,
473 "void main() {"
474 "int x = int(sqrt(1));"
475 "@switch (x) {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400476 "case 1: sk_FragColor = float4(1); break;"
477 "default: sk_FragColor = float4(0);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400478 "}"
479 "}",
480 "error: 1: static switch has non-static test\n1 error\n");
481 test_failure(r,
482 "void main() {"
483 "int x = 1;"
484 "@switch (x) {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400485 "case 1: sk_FragColor = float4(1); if (sqrt(0) < sqrt(1)) break;"
486 "default: sk_FragColor = float4(0);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400487 "}"
488 "}",
489 "error: 1: static switch contains non-static conditional break\n1 error\n");
490}
491
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500492#endif