blob: 432f35bff5496b3c4d7e8abdcd8a7afca3c7263d [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;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050014 SkSL::Program::Settings settings;
15 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
16 settings.fCaps = caps.get();
Brian Osman93ba0a42017-08-14 14:48:10 -040017 compiler.convertProgram(SkSL::Program::kFragment_Kind, SkSL::String(src), settings);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040018 SkSL::String skError(error);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050019 if (compiler.errorText() != skError) {
ethannicholasb3058bd2016-07-01 08:22:01 -070020 SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error,
21 compiler.errorText().c_str());
22 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050023 REPORTER_ASSERT(r, compiler.errorText() == skError);
ethannicholasb3058bd2016-07-01 08:22:01 -070024}
25
26static void test_success(skiatest::Reporter* r, const char* src) {
27 SkSL::Compiler compiler;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050028 SkSL::Program::Settings settings;
29 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
30 settings.fCaps = caps.get();
31 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
Brian Osman93ba0a42017-08-14 14:48:10 -040032 SkSL::String(src), settings);
Ethan Nicholas941e7e22016-12-12 15:33:30 -050033 REPORTER_ASSERT(r, program);
ethannicholasb3058bd2016-07-01 08:22:01 -070034}
35
36DEF_TEST(SkSLUndefinedSymbol, r) {
37 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040038 "void main() { x = float2(1); }",
ethannicholasb3058bd2016-07-01 08:22:01 -070039 "error: 1: unknown identifier 'x'\n1 error\n");
40}
41
42DEF_TEST(SkSLUndefinedFunction, r) {
43 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040044 "void main() { int x = foo(1); }",
ethannicholasb3058bd2016-07-01 08:22:01 -070045 "error: 1: unknown identifier 'foo'\n1 error\n");
46}
47
48DEF_TEST(SkSLGenericArgumentMismatch, r) {
49 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040050 "void main() { float x = sin(1, 2); }",
ethannicholas471e8942016-10-28 09:02:46 -070051 "error: 1: call to 'sin' expected 1 argument, but found 2\n1 error\n");
52 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040053 "void main() { float x = sin(true); }",
ethannicholas471e8942016-10-28 09:02:46 -070054 "error: 1: no match for sin(bool)\n1 error\n");
55 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040056 "void main() { float x = sin(1); }");
ethannicholasb3058bd2016-07-01 08:22:01 -070057}
58
59DEF_TEST(SkSLArgumentCountMismatch, r) {
60 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040061 "float foo(float x) { return x * x; }"
62 "void main() { float x = foo(1, 2); }",
ethannicholasb3058bd2016-07-01 08:22:01 -070063 "error: 1: call to 'foo' expected 1 argument, but found 2\n1 error\n");
64}
65
66DEF_TEST(SkSLArgumentMismatch, r) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -040067 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040068 "float foo(float x) { return x * x; }"
69 "void main() { float x = foo(true); }",
70 "error: 1: expected 'float', but found 'bool'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -070071}
72
73DEF_TEST(SkSLIfTypeMismatch, r) {
74 test_failure(r,
75 "void main() { if (3) { } }",
76 "error: 1: expected 'bool', but found 'int'\n1 error\n");
77}
78
79DEF_TEST(SkSLDoTypeMismatch, r) {
80 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040081 "void main() { do { } while (float2(1)); }",
82 "error: 1: expected 'bool', but found 'float2'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -070083}
84
85DEF_TEST(SkSLWhileTypeMismatch, r) {
86 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040087 "void main() { while (float3(1)) { } }",
88 "error: 1: expected 'bool', but found 'float3'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -070089}
90
91DEF_TEST(SkSLForTypeMismatch, r) {
92 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040093 "void main() { for (int x = 0; x; x++) { } }",
ethannicholasb3058bd2016-07-01 08:22:01 -070094 "error: 1: expected 'bool', but found 'int'\n1 error\n");
95}
96
97DEF_TEST(SkSLConstructorTypeMismatch, r) {
98 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040099 "void main() { float2 x = float2(1.0, false); }",
100 "error: 1: expected 'float', but found 'bool'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700101 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400102 "void main() { float2 x = float2(bool2(false)); }",
103 "error: 1: 'bool2' is not a valid parameter to 'float2' constructor\n1 error\n");
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400104 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400105 "void main() { bool2 x = bool2(float2(1)); }",
106 "error: 1: 'float2' is not a valid parameter to 'bool2' constructor\n1 error\n");
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400107 test_failure(r,
ethannicholasb3058bd2016-07-01 08:22:01 -0700108 "void main() { bool x = bool(1.0); }",
109 "error: 1: cannot construct 'bool'\n1 error\n");
110 test_failure(r,
111 "struct foo { int x; }; void main() { foo x = foo(5); }",
112 "error: 1: cannot construct 'foo'\n1 error\n");
113 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400114 "struct foo { int x; } foo; void main() { float x = float(foo); }",
115 "error: 1: invalid argument to 'float' constructor (expected a number or bool, but found 'foo')\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700116 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400117 "struct foo { int x; } foo; void main() { float2 x = float2(foo); }",
118 "error: 1: 'foo' is not a valid parameter to 'float2' constructor\n1 error\n");
Ethan Nicholas3deaeb22017-04-25 14:42:11 -0400119 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400120 "void main() { float2x2 x = float2x2(true); }",
121 "error: 1: expected 'float', but found 'bool'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700122}
123
124DEF_TEST(SkSLConstructorArgumentCount, r) {
125 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400126 "void main() { float3 x = float3(1.0, 2.0); }",
127 "error: 1: invalid arguments to 'float3' constructor (expected 3 scalars, but "
ethannicholasb3058bd2016-07-01 08:22:01 -0700128 "found 2)\n1 error\n");
Ethan Nicholas84645e32017-02-09 13:57:14 -0500129 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400130 "void main() { float3 x = float3(1.0, 2.0, 3.0, 4.0); }",
131 "error: 1: invalid arguments to 'float3' constructor (expected 3 scalars, but found "
Ethan Nicholas84645e32017-02-09 13:57:14 -0500132 "4)\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700133}
134
135DEF_TEST(SkSLSwizzleScalar, r) {
136 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400137 "void main() { float x = 1; float y = x.y; }",
138 "error: 1: cannot swizzle value of type 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700139}
140
141DEF_TEST(SkSLSwizzleMatrix, r) {
142 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400143 "void main() { float2x2 x = float2x2(1); float y = x.y; }",
144 "error: 1: cannot swizzle value of type 'float2x2'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700145}
146
147DEF_TEST(SkSLSwizzleOutOfBounds, r) {
148 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400149 "void main() { float3 test = float2(1).xyz; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700150 "error: 1: invalid swizzle component 'z'\n1 error\n");
151}
152
153DEF_TEST(SkSLSwizzleTooManyComponents, r) {
154 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400155 "void main() { float4 test = float2(1).xxxxx; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700156 "error: 1: too many components in swizzle mask 'xxxxx'\n1 error\n");
157}
158
159DEF_TEST(SkSLSwizzleDuplicateOutput, r) {
160 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400161 "void main() { float4 test = float4(1); test.xyyz = float4(1); }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700162 "error: 1: cannot write to the same swizzle field more than once\n1 error\n");
163}
ethannicholasea4567c2016-10-17 11:24:37 -0700164
ethannicholasb3058bd2016-07-01 08:22:01 -0700165DEF_TEST(SkSLAssignmentTypeMismatch, r) {
166 test_failure(r,
167 "void main() { int x = 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400168 "error: 1: expected 'int', but found 'float'\n1 error\n");
ethannicholasea4567c2016-10-17 11:24:37 -0700169 test_failure(r,
170 "void main() { int x; x = 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400171 "error: 1: type mismatch: '=' cannot operate on 'int', 'float'\n1 error\n");
ethannicholasea4567c2016-10-17 11:24:37 -0700172 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400173 "void main() { float3 x = float3(0); x *= 1.0; }");
ethannicholasea4567c2016-10-17 11:24:37 -0700174 test_failure(r,
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400175 "void main() { int3 x = int3(0); x *= 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400176 "error: 1: type mismatch: '*=' cannot operate on 'int3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700177}
178
179DEF_TEST(SkSLReturnFromVoid, r) {
180 test_failure(r,
181 "void main() { return true; }",
182 "error: 1: may not return a value from a void function\n1 error\n");
183}
184
185DEF_TEST(SkSLReturnMissingValue, r) {
186 test_failure(r,
187 "int foo() { return; } void main() { }",
188 "error: 1: expected function to return 'int'\n1 error\n");
189}
190
191DEF_TEST(SkSLReturnTypeMismatch, r) {
192 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400193 "int foo() { return 1.0; } void main() { }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400194 "error: 1: expected 'int', but found 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700195}
196
197DEF_TEST(SkSLDuplicateFunction, r) {
198 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400199 "void main() { } void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700200 "error: 1: duplicate definition of void main()\n1 error\n");
201 test_success(r,
202 "void main(); void main() { }");
203}
204
205DEF_TEST(SkSLUsingInvalidValue, r) {
206 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400207 "void main() { int x = int; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700208 "error: 1: expected '(' to begin constructor invocation\n1 error\n");
209 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400210 "int test() { return 1; } void main() { int x = test; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700211 "error: 1: expected '(' to begin function call\n1 error\n");
212}
213DEF_TEST(SkSLDifferentReturnType, r) {
214 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400215 "int main() { return 1; } void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700216 "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 "
217 "error\n");
218}
219
220DEF_TEST(SkSLDifferentModifiers, r) {
221 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400222 "void test(int x); void test(out int x) { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700223 "error: 1: modifiers on parameter 1 differ between declaration and definition\n1 "
224 "error\n");
225}
226
227DEF_TEST(SkSLDuplicateSymbol, r) {
228 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400229 "int main; void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700230 "error: 1: symbol 'main' was already defined\n1 error\n");
231
232 test_failure(r,
233 "int x; int x; void main() { }",
234 "error: 1: symbol 'x' was already defined\n1 error\n");
235
236 test_success(r, "int x; void main() { int x; }");
237}
238
239DEF_TEST(SkSLBinaryTypeMismatch, r) {
240 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400241 "void main() { float x = 3 * true; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700242 "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n1 error\n");
243 test_failure(r,
244 "void main() { bool x = 1 || 2.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400245 "error: 1: type mismatch: '||' cannot operate on 'int', 'float'\n1 error\n");
Ethan Nicholas23463002018-03-28 15:16:15 -0400246 test_failure(r,
247 "void main() { bool x = float2(0) == 0; }",
248 "error: 1: type mismatch: '==' cannot operate on 'float2', 'int'\n1 error\n");
249 test_failure(r,
250 "void main() { bool x = float2(0) != 0; }",
251 "error: 1: type mismatch: '!=' cannot operate on 'float2', 'int'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700252}
253
254DEF_TEST(SkSLCallNonFunction, r) {
255 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400256 "void main() { float x = 3; x(); }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700257 "error: 1: 'x' is not a function\n1 error\n");
258}
259
260DEF_TEST(SkSLInvalidUnary, r) {
261 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400262 "void main() { float4x4 x = float4x4(1); ++x; }",
263 "error: 1: '++' cannot operate on 'float4x4'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700264 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400265 "void main() { float3 x = float3(1); --x; }",
266 "error: 1: '--' cannot operate on 'float3'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700267 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400268 "void main() { float4x4 x = float4x4(1); x++; }",
269 "error: 1: '++' cannot operate on 'float4x4'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700270 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400271 "void main() { float3 x = float3(1); x--; }",
272 "error: 1: '--' cannot operate on 'float3'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700273 test_failure(r,
274 "void main() { int x = !12; }",
275 "error: 1: '!' cannot operate on 'int'\n1 error\n");
276 test_failure(r,
277 "struct foo { } bar; void main() { foo x = +bar; }",
278 "error: 1: '+' cannot operate on 'foo'\n1 error\n");
279 test_failure(r,
280 "struct foo { } bar; void main() { foo x = -bar; }",
281 "error: 1: '-' cannot operate on 'foo'\n1 error\n");
282 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400283 "void main() { float2 x = float2(1, 1); x = +x; x = -x; }");
ethannicholasb3058bd2016-07-01 08:22:01 -0700284}
285
286DEF_TEST(SkSLInvalidAssignment, r) {
287 test_failure(r,
288 "void main() { 1 = 2; }",
289 "error: 1: cannot assign to '1'\n1 error\n");
290 test_failure(r,
291 "uniform int x; void main() { x = 0; }",
292 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
293 test_failure(r,
294 "const int x; void main() { x = 0; }",
295 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
296}
297
298DEF_TEST(SkSLBadIndex, r) {
299 test_failure(r,
300 "void main() { int x = 2[0]; }",
301 "error: 1: expected array, but found 'int'\n1 error\n");
302 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400303 "void main() { float2 x = float2(0); int y = x[0][0]; }",
304 "error: 1: expected array, but found 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700305}
306
307DEF_TEST(SkSLTernaryMismatch, r) {
308 test_failure(r,
309 "void main() { int x = 5 > 2 ? true : 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400310 "error: 1: ternary operator result mismatch: 'bool', 'float'\n1 error\n");
Ethan Nicholas2be687a2017-01-03 16:44:39 -0500311 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400312 "void main() { int x = 5 > 2 ? float3(1) : 1.0; }",
313 "error: 1: ternary operator result mismatch: 'float3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700314}
315
316DEF_TEST(SkSLInterfaceBlockStorageModifiers, r) {
317 test_failure(r,
318 "uniform foo { out int x; };",
319 "error: 1: interface block fields may not have storage qualifiers\n1 error\n");
320}
ethannicholas22f939e2016-10-13 13:25:34 -0700321
322DEF_TEST(SkSLUseWithoutInitialize, r) {
323 test_failure(r,
324 "void main() { int x; if (5 == 2) x = 3; x++; }",
325 "error: 1: 'x' has not been assigned\n1 error\n");
326 test_failure(r,
327 "void main() { int x[2][2]; int i; x[i][1] = 4; }",
328 "error: 1: 'i' has not been assigned\n1 error\n");
329 test_failure(r,
330 "int main() { int r; return r; }",
331 "error: 1: 'r' has not been assigned\n1 error\n");
332 test_failure(r,
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000333 "void main() { int x; int y = x; }",
ethannicholas22f939e2016-10-13 13:25:34 -0700334 "error: 1: 'x' has not been assigned\n1 error\n");
335 test_failure(r,
336 "void main() { bool x; if (true && (false || x)) return; }",
337 "error: 1: 'x' has not been assigned\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500338 test_failure(r,
339 "void main() { int x; switch (3) { case 0: x = 0; case 1: x = 1; }"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400340 "sk_FragColor = float4(x); }",
Ethan Nicholasaf197692017-02-27 13:26:45 -0500341 "error: 1: 'x' has not been assigned\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700342}
343
344DEF_TEST(SkSLUnreachable, r) {
345 test_failure(r,
346 "void main() { return; return; }",
347 "error: 1: unreachable\n1 error\n");
348 test_failure(r,
349 "void main() { for (;;) { continue; int x = 1; } }",
350 "error: 1: unreachable\n1 error\n");
Ethan Nicholasd9fe7002017-05-30 10:15:34 -0400351/* test_failure(r,
ethannicholas22f939e2016-10-13 13:25:34 -0700352 "void main() { for (;;) { } return; }",
Ethan Nicholasd9fe7002017-05-30 10:15:34 -0400353 "error: 1: unreachable\n1 error\n");*/
ethannicholas22f939e2016-10-13 13:25:34 -0700354 test_failure(r,
355 "void main() { if (true) return; else discard; return; }",
356 "error: 1: unreachable\n1 error\n");
357 test_failure(r,
358 "void main() { return; while (true); }",
359 "error: 1: unreachable\n1 error\n");
360}
361
362DEF_TEST(SkSLNoReturn, r) {
363 test_failure(r,
364 "int foo() { if (2 > 5) return 3; }",
365 "error: 1: function can exit without returning a value\n1 error\n");
366}
367
368DEF_TEST(SkSLBreakOutsideLoop, r) {
369 test_failure(r,
370 "void foo() { while(true) {} if (true) break; }",
Ethan Nicholasaf197692017-02-27 13:26:45 -0500371 "error: 1: break statement must be inside a loop or switch\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700372}
373
374DEF_TEST(SkSLContinueOutsideLoop, r) {
375 test_failure(r,
376 "void foo() { for(;;); continue; }",
377 "error: 1: continue statement must be inside a loop\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500378 test_failure(r,
379 "void foo() { switch (1) { default: continue; } }",
380 "error: 1: continue statement must be inside a loop\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700381}
ethannicholas08a92112016-11-09 13:26:45 -0800382
383DEF_TEST(SkSLStaticIfError, r) {
384 // ensure eliminated branch of static if / ternary is still checked for errors
385 test_failure(r,
386 "void foo() { if (true); else x = 5; }",
387 "error: 1: unknown identifier 'x'\n1 error\n");
388 test_failure(r,
389 "void foo() { if (false) x = 5; }",
390 "error: 1: unknown identifier 'x'\n1 error\n");
391 test_failure(r,
392 "void foo() { true ? 5 : x; }",
393 "error: 1: unknown identifier 'x'\n1 error\n");
394 test_failure(r,
395 "void foo() { false ? x : 5; }",
396 "error: 1: unknown identifier 'x'\n1 error\n");
397}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500398
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500399DEF_TEST(SkSLBadCap, r) {
400 test_failure(r,
401 "bool b = sk_Caps.bugFreeDriver;",
402 "error: 1: unknown capability flag 'bugFreeDriver'\n1 error\n");
403}
404
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500405DEF_TEST(SkSLDivByZero, r) {
406 test_failure(r,
407 "int x = 1 / 0;",
408 "error: 1: division by zero\n1 error\n");
409 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400410 "float x = 1 / 0;",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500411 "error: 1: division by zero\n1 error\n");
412 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400413 "float x = 1.0 / 0.0;",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500414 "error: 1: division by zero\n1 error\n");
415 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400416 "float x = -67.0 / (3.0 - 3);",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500417 "error: 1: division by zero\n1 error\n");
418}
419
Ethan Nicholas38657112017-02-09 17:01:22 -0500420DEF_TEST(SkSLUnsupportedGLSLIdentifiers, r) {
421 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400422 "void main() { float x = gl_FragCoord.x; };",
Ethan Nicholas38657112017-02-09 17:01:22 -0500423 "error: 1: unknown identifier 'gl_FragCoord'\n1 error\n");
424 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400425 "void main() { float r = gl_FragColor.r; };",
Ethan Nicholas38657112017-02-09 17:01:22 -0500426 "error: 1: unknown identifier 'gl_FragColor'\n1 error\n");
427}
428
Ethan Nicholasaf197692017-02-27 13:26:45 -0500429DEF_TEST(SkSLWrongSwitchTypes, r) {
430 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400431 "void main() { switch (float2(1)) { case 1: break; } }",
432 "error: 1: expected 'int', but found 'float2'\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500433 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400434 "void main() { switch (1) { case float2(1): break; } }",
435 "error: 1: expected 'int', but found 'float2'\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500436}
437
438DEF_TEST(SkSLNonConstantCase, r) {
439 test_failure(r,
440 "void main() { int x = 1; switch (1) { case x: break; } }",
441 "error: 1: case value must be a constant\n1 error\n");
442}
443
444DEF_TEST(SkSLDuplicateCase, r) {
445 test_failure(r,
446 "void main() { switch (1) { case 0: case 1: case 0: break; } }",
447 "error: 1: duplicate case value\n1 error\n");
448}
449
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400450DEF_TEST(SkSLFieldAfterRuntimeArray, r) {
451 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400452 "buffer broken { float x[]; float y; };",
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400453 "error: 1: only the last entry in an interface block may be a runtime-sized "
454 "array\n1 error\n");
455}
456
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400457DEF_TEST(SkSLStaticIf, r) {
458 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400459 "void main() { float x = 5; float y = 10;"
460 "@if (x < y) { sk_FragColor = float4(1); } }");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400461 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400462 "void main() { float x = sqrt(25); float y = 10;"
463 "@if (x < y) { sk_FragColor = float4(1); } }",
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400464 "error: 1: static if has non-static test\n1 error\n");
465}
466
467DEF_TEST(SkSLStaticSwitch, r) {
468 test_success(r,
469 "void main() {"
470 "int x = 1;"
471 "@switch (x) {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400472 "case 1: sk_FragColor = float4(1); break;"
473 "default: sk_FragColor = float4(0);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400474 "}"
475 "}");
476 test_failure(r,
477 "void main() {"
478 "int x = int(sqrt(1));"
479 "@switch (x) {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400480 "case 1: sk_FragColor = float4(1); break;"
481 "default: sk_FragColor = float4(0);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400482 "}"
483 "}",
484 "error: 1: static switch has non-static test\n1 error\n");
485 test_failure(r,
486 "void main() {"
487 "int x = 1;"
488 "@switch (x) {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400489 "case 1: sk_FragColor = float4(1); if (sqrt(0) < sqrt(1)) break;"
490 "default: sk_FragColor = float4(0);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400491 "}"
492 "}",
493 "error: 1: static switch contains non-static conditional break\n1 error\n");
494}
495
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500496DEF_TEST(SkSLInterfaceBlockScope, r) {
497 test_failure(r,
498 "uniform testBlock {"
499 "float x;"
500 "} test[x];",
501 "error: 1: unknown identifier 'x'\n1 error\n");
502}
503
Ethan Nicholas6c942712018-03-16 09:45:11 -0400504DEF_TEST(SkSLDuplicateOutput, r) {
505 test_failure(r,
506 "layout (location=0, index=0) out half4 duplicateOutput;",
507 "error: 1: out location=0, index=0 is reserved for sk_FragColor\n1 error\n");
508}