blob: b84b1f3d8c649b4eb86409c72f62550e5acdb0e4 [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();
Ethan Nicholas00543112018-07-31 09:44:36 -040017 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
18 SkSL::String(src), settings);
19 if (!compiler.errorCount()) {
20 compiler.optimize(*program);
21 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -040022 SkSL::String skError(error);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050023 if (compiler.errorText() != skError) {
ethannicholasb3058bd2016-07-01 08:22:01 -070024 SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error,
25 compiler.errorText().c_str());
26 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050027 REPORTER_ASSERT(r, compiler.errorText() == skError);
ethannicholasb3058bd2016-07-01 08:22:01 -070028}
29
30static void test_success(skiatest::Reporter* r, const char* src) {
31 SkSL::Compiler compiler;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050032 SkSL::Program::Settings settings;
33 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
34 settings.fCaps = caps.get();
35 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
Brian Osman93ba0a42017-08-14 14:48:10 -040036 SkSL::String(src), settings);
Ethan Nicholas941e7e22016-12-12 15:33:30 -050037 REPORTER_ASSERT(r, program);
ethannicholasb3058bd2016-07-01 08:22:01 -070038}
39
40DEF_TEST(SkSLUndefinedSymbol, r) {
41 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040042 "void main() { x = float2(1); }",
ethannicholasb3058bd2016-07-01 08:22:01 -070043 "error: 1: unknown identifier 'x'\n1 error\n");
44}
45
46DEF_TEST(SkSLUndefinedFunction, r) {
47 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040048 "void main() { int x = foo(1); }",
ethannicholasb3058bd2016-07-01 08:22:01 -070049 "error: 1: unknown identifier 'foo'\n1 error\n");
50}
51
52DEF_TEST(SkSLGenericArgumentMismatch, r) {
53 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040054 "void main() { float x = sin(1, 2); }",
ethannicholas471e8942016-10-28 09:02:46 -070055 "error: 1: call to 'sin' expected 1 argument, but found 2\n1 error\n");
56 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040057 "void main() { float x = sin(true); }",
ethannicholas471e8942016-10-28 09:02:46 -070058 "error: 1: no match for sin(bool)\n1 error\n");
59 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040060 "void main() { float x = sin(1); }");
ethannicholasb3058bd2016-07-01 08:22:01 -070061}
62
63DEF_TEST(SkSLArgumentCountMismatch, r) {
64 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040065 "float foo(float x) { return x * x; }"
66 "void main() { float x = foo(1, 2); }",
ethannicholasb3058bd2016-07-01 08:22:01 -070067 "error: 1: call to 'foo' expected 1 argument, but found 2\n1 error\n");
68}
69
70DEF_TEST(SkSLArgumentMismatch, r) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -040071 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040072 "float foo(float x) { return x * x; }"
73 "void main() { float x = foo(true); }",
74 "error: 1: expected 'float', but found 'bool'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -070075}
76
77DEF_TEST(SkSLIfTypeMismatch, r) {
78 test_failure(r,
79 "void main() { if (3) { } }",
80 "error: 1: expected 'bool', but found 'int'\n1 error\n");
81}
82
83DEF_TEST(SkSLDoTypeMismatch, r) {
84 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040085 "void main() { do { } while (float2(1)); }",
86 "error: 1: expected 'bool', but found 'float2'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -070087}
88
89DEF_TEST(SkSLWhileTypeMismatch, r) {
90 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040091 "void main() { while (float3(1)) { } }",
92 "error: 1: expected 'bool', but found 'float3'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -070093}
94
95DEF_TEST(SkSLForTypeMismatch, r) {
96 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040097 "void main() { for (int x = 0; x; x++) { } }",
ethannicholasb3058bd2016-07-01 08:22:01 -070098 "error: 1: expected 'bool', but found 'int'\n1 error\n");
99}
100
101DEF_TEST(SkSLConstructorTypeMismatch, r) {
102 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400103 "void main() { float2 x = float2(1.0, false); }",
104 "error: 1: expected 'float', but found 'bool'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700105 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400106 "void main() { float2 x = float2(bool2(false)); }",
107 "error: 1: 'bool2' is not a valid parameter to 'float2' constructor\n1 error\n");
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400108 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400109 "void main() { bool2 x = bool2(float2(1)); }",
110 "error: 1: 'float2' is not a valid parameter to 'bool2' constructor\n1 error\n");
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400111 test_failure(r,
ethannicholasb3058bd2016-07-01 08:22:01 -0700112 "void main() { bool x = bool(1.0); }",
113 "error: 1: cannot construct 'bool'\n1 error\n");
114 test_failure(r,
115 "struct foo { int x; }; void main() { foo x = foo(5); }",
116 "error: 1: cannot construct 'foo'\n1 error\n");
117 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400118 "struct foo { int x; } foo; void main() { float x = float(foo); }",
119 "error: 1: invalid argument to 'float' constructor (expected a number or bool, but found 'foo')\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700120 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400121 "struct foo { int x; } foo; void main() { float2 x = float2(foo); }",
122 "error: 1: 'foo' is not a valid parameter to 'float2' constructor\n1 error\n");
Ethan Nicholas3deaeb22017-04-25 14:42:11 -0400123 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400124 "void main() { float2x2 x = float2x2(true); }",
125 "error: 1: expected 'float', but found 'bool'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700126}
127
128DEF_TEST(SkSLConstructorArgumentCount, r) {
129 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400130 "void main() { float3 x = float3(1.0, 2.0); }",
131 "error: 1: invalid arguments to 'float3' constructor (expected 3 scalars, but "
ethannicholasb3058bd2016-07-01 08:22:01 -0700132 "found 2)\n1 error\n");
Ethan Nicholas84645e32017-02-09 13:57:14 -0500133 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400134 "void main() { float3 x = float3(1.0, 2.0, 3.0, 4.0); }",
135 "error: 1: invalid arguments to 'float3' constructor (expected 3 scalars, but found "
Ethan Nicholas84645e32017-02-09 13:57:14 -0500136 "4)\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700137}
138
139DEF_TEST(SkSLSwizzleScalar, r) {
140 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400141 "void main() { float x = 1; float y = x.y; }",
142 "error: 1: cannot swizzle value of type 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700143}
144
145DEF_TEST(SkSLSwizzleMatrix, r) {
146 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400147 "void main() { float2x2 x = float2x2(1); float y = x.y; }",
148 "error: 1: cannot swizzle value of type 'float2x2'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700149}
150
151DEF_TEST(SkSLSwizzleOutOfBounds, r) {
152 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400153 "void main() { float3 test = float2(1).xyz; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700154 "error: 1: invalid swizzle component 'z'\n1 error\n");
155}
156
157DEF_TEST(SkSLSwizzleTooManyComponents, r) {
158 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400159 "void main() { float4 test = float2(1).xxxxx; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700160 "error: 1: too many components in swizzle mask 'xxxxx'\n1 error\n");
161}
162
163DEF_TEST(SkSLSwizzleDuplicateOutput, r) {
164 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400165 "void main() { float4 test = float4(1); test.xyyz = float4(1); }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700166 "error: 1: cannot write to the same swizzle field more than once\n1 error\n");
167}
ethannicholasea4567c2016-10-17 11:24:37 -0700168
ethannicholasb3058bd2016-07-01 08:22:01 -0700169DEF_TEST(SkSLAssignmentTypeMismatch, r) {
170 test_failure(r,
171 "void main() { int x = 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400172 "error: 1: expected 'int', but found 'float'\n1 error\n");
ethannicholasea4567c2016-10-17 11:24:37 -0700173 test_failure(r,
174 "void main() { int x; x = 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400175 "error: 1: type mismatch: '=' cannot operate on 'int', 'float'\n1 error\n");
ethannicholasea4567c2016-10-17 11:24:37 -0700176 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400177 "void main() { float3 x = float3(0); x *= 1.0; }");
ethannicholasea4567c2016-10-17 11:24:37 -0700178 test_failure(r,
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400179 "void main() { int3 x = int3(0); x *= 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400180 "error: 1: type mismatch: '*=' cannot operate on 'int3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700181}
182
183DEF_TEST(SkSLReturnFromVoid, r) {
184 test_failure(r,
185 "void main() { return true; }",
186 "error: 1: may not return a value from a void function\n1 error\n");
187}
188
189DEF_TEST(SkSLReturnMissingValue, r) {
190 test_failure(r,
191 "int foo() { return; } void main() { }",
192 "error: 1: expected function to return 'int'\n1 error\n");
193}
194
195DEF_TEST(SkSLReturnTypeMismatch, r) {
196 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400197 "int foo() { return 1.0; } void main() { }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400198 "error: 1: expected 'int', but found 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700199}
200
201DEF_TEST(SkSLDuplicateFunction, r) {
202 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400203 "void main() { } void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700204 "error: 1: duplicate definition of void main()\n1 error\n");
205 test_success(r,
206 "void main(); void main() { }");
207}
208
209DEF_TEST(SkSLUsingInvalidValue, r) {
210 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400211 "void main() { int x = int; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700212 "error: 1: expected '(' to begin constructor invocation\n1 error\n");
213 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400214 "int test() { return 1; } void main() { int x = test; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700215 "error: 1: expected '(' to begin function call\n1 error\n");
216}
217DEF_TEST(SkSLDifferentReturnType, r) {
218 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400219 "int main() { return 1; } void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700220 "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 "
221 "error\n");
222}
223
224DEF_TEST(SkSLDifferentModifiers, r) {
225 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400226 "void test(int x); void test(out int x) { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700227 "error: 1: modifiers on parameter 1 differ between declaration and definition\n1 "
228 "error\n");
229}
230
231DEF_TEST(SkSLDuplicateSymbol, r) {
232 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400233 "int main; void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700234 "error: 1: symbol 'main' was already defined\n1 error\n");
235
236 test_failure(r,
237 "int x; int x; void main() { }",
238 "error: 1: symbol 'x' was already defined\n1 error\n");
239
240 test_success(r, "int x; void main() { int x; }");
241}
242
243DEF_TEST(SkSLBinaryTypeMismatch, r) {
244 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400245 "void main() { float x = 3 * true; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700246 "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n1 error\n");
247 test_failure(r,
248 "void main() { bool x = 1 || 2.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400249 "error: 1: type mismatch: '||' cannot operate on 'int', 'float'\n1 error\n");
Ethan Nicholas23463002018-03-28 15:16:15 -0400250 test_failure(r,
251 "void main() { bool x = float2(0) == 0; }",
252 "error: 1: type mismatch: '==' cannot operate on 'float2', 'int'\n1 error\n");
253 test_failure(r,
254 "void main() { bool x = float2(0) != 0; }",
255 "error: 1: type mismatch: '!=' cannot operate on 'float2', 'int'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700256}
257
258DEF_TEST(SkSLCallNonFunction, r) {
259 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400260 "void main() { float x = 3; x(); }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700261 "error: 1: 'x' is not a function\n1 error\n");
262}
263
264DEF_TEST(SkSLInvalidUnary, r) {
265 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400266 "void main() { float4x4 x = float4x4(1); ++x; }",
267 "error: 1: '++' cannot operate on 'float4x4'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700268 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400269 "void main() { float3 x = float3(1); --x; }",
270 "error: 1: '--' cannot operate on 'float3'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700271 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400272 "void main() { float4x4 x = float4x4(1); x++; }",
273 "error: 1: '++' cannot operate on 'float4x4'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700274 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400275 "void main() { float3 x = float3(1); x--; }",
276 "error: 1: '--' cannot operate on 'float3'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700277 test_failure(r,
278 "void main() { int x = !12; }",
279 "error: 1: '!' cannot operate on 'int'\n1 error\n");
280 test_failure(r,
281 "struct foo { } bar; void main() { foo x = +bar; }",
282 "error: 1: '+' cannot operate on 'foo'\n1 error\n");
283 test_failure(r,
284 "struct foo { } bar; void main() { foo x = -bar; }",
285 "error: 1: '-' cannot operate on 'foo'\n1 error\n");
286 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400287 "void main() { float2 x = float2(1, 1); x = +x; x = -x; }");
ethannicholasb3058bd2016-07-01 08:22:01 -0700288}
289
290DEF_TEST(SkSLInvalidAssignment, r) {
291 test_failure(r,
292 "void main() { 1 = 2; }",
293 "error: 1: cannot assign to '1'\n1 error\n");
294 test_failure(r,
295 "uniform int x; void main() { x = 0; }",
296 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
297 test_failure(r,
298 "const int x; void main() { x = 0; }",
299 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
300}
301
302DEF_TEST(SkSLBadIndex, r) {
303 test_failure(r,
304 "void main() { int x = 2[0]; }",
305 "error: 1: expected array, but found 'int'\n1 error\n");
306 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400307 "void main() { float2 x = float2(0); int y = x[0][0]; }",
308 "error: 1: expected array, but found 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700309}
310
311DEF_TEST(SkSLTernaryMismatch, r) {
312 test_failure(r,
313 "void main() { int x = 5 > 2 ? true : 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400314 "error: 1: ternary operator result mismatch: 'bool', 'float'\n1 error\n");
Ethan Nicholas2be687a2017-01-03 16:44:39 -0500315 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400316 "void main() { int x = 5 > 2 ? float3(1) : 1.0; }",
317 "error: 1: ternary operator result mismatch: 'float3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700318}
319
320DEF_TEST(SkSLInterfaceBlockStorageModifiers, r) {
321 test_failure(r,
322 "uniform foo { out int x; };",
323 "error: 1: interface block fields may not have storage qualifiers\n1 error\n");
324}
ethannicholas22f939e2016-10-13 13:25:34 -0700325
326DEF_TEST(SkSLUseWithoutInitialize, r) {
327 test_failure(r,
328 "void main() { int x; if (5 == 2) x = 3; x++; }",
329 "error: 1: 'x' has not been assigned\n1 error\n");
330 test_failure(r,
331 "void main() { int x[2][2]; int i; x[i][1] = 4; }",
332 "error: 1: 'i' has not been assigned\n1 error\n");
333 test_failure(r,
334 "int main() { int r; return r; }",
335 "error: 1: 'r' has not been assigned\n1 error\n");
336 test_failure(r,
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000337 "void main() { int x; int y = x; }",
ethannicholas22f939e2016-10-13 13:25:34 -0700338 "error: 1: 'x' has not been assigned\n1 error\n");
339 test_failure(r,
340 "void main() { bool x; if (true && (false || x)) return; }",
341 "error: 1: 'x' has not been assigned\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500342 test_failure(r,
343 "void main() { int x; switch (3) { case 0: x = 0; case 1: x = 1; }"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400344 "sk_FragColor = float4(x); }",
Ethan Nicholasaf197692017-02-27 13:26:45 -0500345 "error: 1: 'x' has not been assigned\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700346}
347
348DEF_TEST(SkSLUnreachable, r) {
349 test_failure(r,
350 "void main() { return; return; }",
351 "error: 1: unreachable\n1 error\n");
352 test_failure(r,
353 "void main() { for (;;) { continue; int x = 1; } }",
354 "error: 1: unreachable\n1 error\n");
Ethan Nicholasd9fe7002017-05-30 10:15:34 -0400355/* test_failure(r,
ethannicholas22f939e2016-10-13 13:25:34 -0700356 "void main() { for (;;) { } return; }",
Ethan Nicholasd9fe7002017-05-30 10:15:34 -0400357 "error: 1: unreachable\n1 error\n");*/
ethannicholas22f939e2016-10-13 13:25:34 -0700358 test_failure(r,
359 "void main() { if (true) return; else discard; return; }",
360 "error: 1: unreachable\n1 error\n");
361 test_failure(r,
362 "void main() { return; while (true); }",
363 "error: 1: unreachable\n1 error\n");
364}
365
366DEF_TEST(SkSLNoReturn, r) {
367 test_failure(r,
368 "int foo() { if (2 > 5) return 3; }",
369 "error: 1: function can exit without returning a value\n1 error\n");
370}
371
372DEF_TEST(SkSLBreakOutsideLoop, r) {
373 test_failure(r,
374 "void foo() { while(true) {} if (true) break; }",
Ethan Nicholasaf197692017-02-27 13:26:45 -0500375 "error: 1: break statement must be inside a loop or switch\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700376}
377
378DEF_TEST(SkSLContinueOutsideLoop, r) {
379 test_failure(r,
380 "void foo() { for(;;); continue; }",
381 "error: 1: continue statement must be inside a loop\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500382 test_failure(r,
383 "void foo() { switch (1) { default: continue; } }",
384 "error: 1: continue statement must be inside a loop\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700385}
ethannicholas08a92112016-11-09 13:26:45 -0800386
387DEF_TEST(SkSLStaticIfError, r) {
388 // ensure eliminated branch of static if / ternary is still checked for errors
389 test_failure(r,
390 "void foo() { if (true); else x = 5; }",
391 "error: 1: unknown identifier 'x'\n1 error\n");
392 test_failure(r,
393 "void foo() { if (false) x = 5; }",
394 "error: 1: unknown identifier 'x'\n1 error\n");
395 test_failure(r,
396 "void foo() { true ? 5 : x; }",
397 "error: 1: unknown identifier 'x'\n1 error\n");
398 test_failure(r,
399 "void foo() { false ? x : 5; }",
400 "error: 1: unknown identifier 'x'\n1 error\n");
401}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500402
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500403DEF_TEST(SkSLBadCap, r) {
404 test_failure(r,
405 "bool b = sk_Caps.bugFreeDriver;",
406 "error: 1: unknown capability flag 'bugFreeDriver'\n1 error\n");
407}
408
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500409DEF_TEST(SkSLDivByZero, r) {
410 test_failure(r,
411 "int x = 1 / 0;",
412 "error: 1: division by zero\n1 error\n");
413 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400414 "float x = 1 / 0;",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500415 "error: 1: division by zero\n1 error\n");
416 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400417 "float x = 1.0 / 0.0;",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500418 "error: 1: division by zero\n1 error\n");
419 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400420 "float x = -67.0 / (3.0 - 3);",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500421 "error: 1: division by zero\n1 error\n");
422}
423
Ethan Nicholas38657112017-02-09 17:01:22 -0500424DEF_TEST(SkSLUnsupportedGLSLIdentifiers, r) {
425 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400426 "void main() { float x = gl_FragCoord.x; };",
Ethan Nicholas38657112017-02-09 17:01:22 -0500427 "error: 1: unknown identifier 'gl_FragCoord'\n1 error\n");
428 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400429 "void main() { float r = gl_FragColor.r; };",
Ethan Nicholas38657112017-02-09 17:01:22 -0500430 "error: 1: unknown identifier 'gl_FragColor'\n1 error\n");
431}
432
Ethan Nicholasaf197692017-02-27 13:26:45 -0500433DEF_TEST(SkSLWrongSwitchTypes, r) {
434 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400435 "void main() { switch (float2(1)) { case 1: break; } }",
436 "error: 1: expected 'int', but found 'float2'\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500437 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400438 "void main() { switch (1) { case float2(1): break; } }",
439 "error: 1: expected 'int', but found 'float2'\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500440}
441
442DEF_TEST(SkSLNonConstantCase, r) {
443 test_failure(r,
444 "void main() { int x = 1; switch (1) { case x: break; } }",
445 "error: 1: case value must be a constant\n1 error\n");
446}
447
448DEF_TEST(SkSLDuplicateCase, r) {
449 test_failure(r,
450 "void main() { switch (1) { case 0: case 1: case 0: break; } }",
451 "error: 1: duplicate case value\n1 error\n");
452}
453
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400454DEF_TEST(SkSLFieldAfterRuntimeArray, r) {
455 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400456 "buffer broken { float x[]; float y; };",
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400457 "error: 1: only the last entry in an interface block may be a runtime-sized "
458 "array\n1 error\n");
459}
460
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400461DEF_TEST(SkSLStaticIf, r) {
462 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400463 "void main() { float x = 5; float y = 10;"
464 "@if (x < y) { sk_FragColor = float4(1); } }");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400465 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400466 "void main() { float x = sqrt(25); float y = 10;"
467 "@if (x < y) { sk_FragColor = float4(1); } }",
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400468 "error: 1: static if has non-static test\n1 error\n");
469}
470
471DEF_TEST(SkSLStaticSwitch, r) {
472 test_success(r,
473 "void main() {"
474 "int x = 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 test_failure(r,
481 "void main() {"
482 "int x = int(sqrt(1));"
483 "@switch (x) {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400484 "case 1: sk_FragColor = float4(1); break;"
485 "default: sk_FragColor = float4(0);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400486 "}"
487 "}",
488 "error: 1: static switch has non-static test\n1 error\n");
489 test_failure(r,
490 "void main() {"
491 "int x = 1;"
492 "@switch (x) {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400493 "case 1: sk_FragColor = float4(1); if (sqrt(0) < sqrt(1)) break;"
494 "default: sk_FragColor = float4(0);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400495 "}"
496 "}",
497 "error: 1: static switch contains non-static conditional break\n1 error\n");
498}
499
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500500DEF_TEST(SkSLInterfaceBlockScope, r) {
501 test_failure(r,
502 "uniform testBlock {"
503 "float x;"
504 "} test[x];",
505 "error: 1: unknown identifier 'x'\n1 error\n");
506}
507
Ethan Nicholas6c942712018-03-16 09:45:11 -0400508DEF_TEST(SkSLDuplicateOutput, r) {
509 test_failure(r,
510 "layout (location=0, index=0) out half4 duplicateOutput;",
511 "error: 1: out location=0, index=0 is reserved for sk_FragColor\n1 error\n");
512}