ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 12 | #if SK_SUPPORT_GPU |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 13 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 14 | static void test_failure(skiatest::Reporter* r, const char* src, const char* error) { |
| 15 | SkSL::Compiler compiler; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 16 | SkSL::Program::Settings settings; |
| 17 | sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default(); |
| 18 | settings.fCaps = caps.get(); |
| 19 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind, |
| 20 | SkString(src), settings); |
| 21 | if (program) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 22 | SkSL::String ignored; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 23 | compiler.toSPIRV(*program, &ignored); |
| 24 | } |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 25 | SkSL::String skError(error); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 26 | if (compiler.errorText() != skError) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 27 | SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error, |
| 28 | compiler.errorText().c_str()); |
| 29 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 30 | REPORTER_ASSERT(r, compiler.errorText() == skError); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static void test_success(skiatest::Reporter* r, const char* src) { |
| 34 | SkSL::Compiler compiler; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 35 | SkSL::Program::Settings settings; |
| 36 | sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default(); |
| 37 | settings.fCaps = caps.get(); |
| 38 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind, |
| 39 | SkString(src), settings); |
| 40 | REPORTER_ASSERT(r, program); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 41 | SkSL::String ignored; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 42 | REPORTER_ASSERT(r, compiler.toSPIRV(*program, &ignored)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | DEF_TEST(SkSLUndefinedSymbol, r) { |
| 46 | test_failure(r, |
| 47 | "void main() { x = vec2(1); }", |
| 48 | "error: 1: unknown identifier 'x'\n1 error\n"); |
| 49 | } |
| 50 | |
| 51 | DEF_TEST(SkSLUndefinedFunction, r) { |
| 52 | test_failure(r, |
| 53 | "void main() { int x = foo(1); }", |
| 54 | "error: 1: unknown identifier 'foo'\n1 error\n"); |
| 55 | } |
| 56 | |
| 57 | DEF_TEST(SkSLGenericArgumentMismatch, r) { |
| 58 | test_failure(r, |
| 59 | "void main() { float x = sin(1, 2); }", |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 60 | "error: 1: call to 'sin' expected 1 argument, but found 2\n1 error\n"); |
| 61 | test_failure(r, |
| 62 | "void main() { float x = sin(true); }", |
| 63 | "error: 1: no match for sin(bool)\n1 error\n"); |
| 64 | test_success(r, |
| 65 | "void main() { float x = sin(1); }"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | DEF_TEST(SkSLArgumentCountMismatch, r) { |
| 69 | test_failure(r, |
| 70 | "float foo(float x) { return x * x; }" |
| 71 | "void main() { float x = foo(1, 2); }", |
| 72 | "error: 1: call to 'foo' expected 1 argument, but found 2\n1 error\n"); |
| 73 | } |
| 74 | |
| 75 | DEF_TEST(SkSLArgumentMismatch, r) { |
| 76 | test_failure(r, |
| 77 | "float foo(float x) { return x * x; }" |
| 78 | "void main() { float x = foo(true); }", |
| 79 | "error: 1: expected 'float', but found 'bool'\n1 error\n"); |
| 80 | } |
| 81 | |
| 82 | DEF_TEST(SkSLIfTypeMismatch, r) { |
| 83 | test_failure(r, |
| 84 | "void main() { if (3) { } }", |
| 85 | "error: 1: expected 'bool', but found 'int'\n1 error\n"); |
| 86 | } |
| 87 | |
| 88 | DEF_TEST(SkSLDoTypeMismatch, r) { |
| 89 | test_failure(r, |
| 90 | "void main() { do { } while (vec2(1)); }", |
| 91 | "error: 1: expected 'bool', but found 'vec2'\n1 error\n"); |
| 92 | } |
| 93 | |
| 94 | DEF_TEST(SkSLWhileTypeMismatch, r) { |
| 95 | test_failure(r, |
| 96 | "void main() { while (vec3(1)) { } }", |
| 97 | "error: 1: expected 'bool', but found 'vec3'\n1 error\n"); |
| 98 | } |
| 99 | |
| 100 | DEF_TEST(SkSLForTypeMismatch, r) { |
| 101 | test_failure(r, |
| 102 | "void main() { for (int x = 0; x; x++) { } }", |
| 103 | "error: 1: expected 'bool', but found 'int'\n1 error\n"); |
| 104 | } |
| 105 | |
| 106 | DEF_TEST(SkSLConstructorTypeMismatch, r) { |
| 107 | test_failure(r, |
| 108 | "void main() { vec2 x = vec2(1.0, false); }", |
| 109 | "error: 1: expected 'float', but found 'bool'\n1 error\n"); |
| 110 | test_failure(r, |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 111 | "void main() { vec2 x = vec2(bvec2(false)); }", |
| 112 | "error: 1: 'bvec2' is not a valid parameter to 'vec2' constructor\n1 error\n"); |
| 113 | test_failure(r, |
| 114 | "void main() { bvec2 x = bvec2(vec2(1)); }", |
| 115 | "error: 1: 'vec2' is not a valid parameter to 'bvec2' constructor\n1 error\n"); |
| 116 | test_failure(r, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 117 | "void main() { bool x = bool(1.0); }", |
| 118 | "error: 1: cannot construct 'bool'\n1 error\n"); |
| 119 | test_failure(r, |
| 120 | "struct foo { int x; }; void main() { foo x = foo(5); }", |
| 121 | "error: 1: cannot construct 'foo'\n1 error\n"); |
| 122 | test_failure(r, |
| 123 | "struct foo { int x; } foo; void main() { float x = float(foo); }", |
| 124 | "error: 1: invalid argument to 'float' constructor (expected a number or bool, but found 'foo')\n1 error\n"); |
| 125 | test_failure(r, |
| 126 | "struct foo { int x; } foo; void main() { vec2 x = vec2(foo); }", |
| 127 | "error: 1: 'foo' is not a valid parameter to 'vec2' constructor\n1 error\n"); |
| 128 | } |
| 129 | |
| 130 | DEF_TEST(SkSLConstructorArgumentCount, r) { |
| 131 | test_failure(r, |
| 132 | "void main() { vec3 x = vec3(1.0, 2.0); }", |
| 133 | "error: 1: invalid arguments to 'vec3' constructor (expected 3 scalars, but " |
| 134 | "found 2)\n1 error\n"); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 135 | test_failure(r, |
| 136 | "void main() { vec3 x = vec3(1.0, 2.0, 3.0, 4.0); }", |
| 137 | "error: 1: invalid arguments to 'vec3' constructor (expected 3 scalars, but found " |
| 138 | "4)\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | DEF_TEST(SkSLSwizzleScalar, r) { |
| 142 | test_failure(r, |
| 143 | "void main() { float x = 1; float y = x.y; }", |
| 144 | "error: 1: cannot swizzle value of type 'float'\n1 error\n"); |
| 145 | } |
| 146 | |
| 147 | DEF_TEST(SkSLSwizzleMatrix, r) { |
| 148 | test_failure(r, |
| 149 | "void main() { mat2 x = mat2(1); float y = x.y; }", |
| 150 | "error: 1: cannot swizzle value of type 'mat2'\n1 error\n"); |
| 151 | } |
| 152 | |
| 153 | DEF_TEST(SkSLSwizzleOutOfBounds, r) { |
| 154 | test_failure(r, |
| 155 | "void main() { vec3 test = vec2(1).xyz; }", |
| 156 | "error: 1: invalid swizzle component 'z'\n1 error\n"); |
| 157 | } |
| 158 | |
| 159 | DEF_TEST(SkSLSwizzleTooManyComponents, r) { |
| 160 | test_failure(r, |
| 161 | "void main() { vec4 test = vec2(1).xxxxx; }", |
| 162 | "error: 1: too many components in swizzle mask 'xxxxx'\n1 error\n"); |
| 163 | } |
| 164 | |
| 165 | DEF_TEST(SkSLSwizzleDuplicateOutput, r) { |
| 166 | test_failure(r, |
| 167 | "void main() { vec4 test = vec4(1); test.xyyz = vec4(1); }", |
| 168 | "error: 1: cannot write to the same swizzle field more than once\n1 error\n"); |
| 169 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 170 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 171 | DEF_TEST(SkSLAssignmentTypeMismatch, r) { |
| 172 | test_failure(r, |
| 173 | "void main() { int x = 1.0; }", |
| 174 | "error: 1: expected 'int', but found 'float'\n1 error\n"); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 175 | test_failure(r, |
| 176 | "void main() { int x; x = 1.0; }", |
| 177 | "error: 1: type mismatch: '=' cannot operate on 'int', 'float'\n1 error\n"); |
| 178 | test_success(r, |
| 179 | "void main() { vec3 x = vec3(0); x *= 1.0; }"); |
| 180 | test_failure(r, |
| 181 | "void main() { ivec3 x = ivec3(0); x *= 1.0; }", |
| 182 | "error: 1: type mismatch: '*=' cannot operate on 'ivec3', 'float'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | DEF_TEST(SkSLReturnFromVoid, r) { |
| 186 | test_failure(r, |
| 187 | "void main() { return true; }", |
| 188 | "error: 1: may not return a value from a void function\n1 error\n"); |
| 189 | } |
| 190 | |
| 191 | DEF_TEST(SkSLReturnMissingValue, r) { |
| 192 | test_failure(r, |
| 193 | "int foo() { return; } void main() { }", |
| 194 | "error: 1: expected function to return 'int'\n1 error\n"); |
| 195 | } |
| 196 | |
| 197 | DEF_TEST(SkSLReturnTypeMismatch, r) { |
| 198 | test_failure(r, |
| 199 | "int foo() { return 1.0; } void main() { }", |
| 200 | "error: 1: expected 'int', but found 'float'\n1 error\n"); |
| 201 | } |
| 202 | |
| 203 | DEF_TEST(SkSLDuplicateFunction, r) { |
| 204 | test_failure(r, |
| 205 | "void main() { } void main() { }", |
| 206 | "error: 1: duplicate definition of void main()\n1 error\n"); |
| 207 | test_success(r, |
| 208 | "void main(); void main() { }"); |
| 209 | } |
| 210 | |
| 211 | DEF_TEST(SkSLUsingInvalidValue, r) { |
| 212 | test_failure(r, |
| 213 | "void main() { int x = int; }", |
| 214 | "error: 1: expected '(' to begin constructor invocation\n1 error\n"); |
| 215 | test_failure(r, |
| 216 | "int test() { return 1; } void main() { int x = test; }", |
| 217 | "error: 1: expected '(' to begin function call\n1 error\n"); |
| 218 | } |
| 219 | DEF_TEST(SkSLDifferentReturnType, r) { |
| 220 | test_failure(r, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 221 | "int main() { return 1; } void main() { }", |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 222 | "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 " |
| 223 | "error\n"); |
| 224 | } |
| 225 | |
| 226 | DEF_TEST(SkSLDifferentModifiers, r) { |
| 227 | test_failure(r, |
| 228 | "void test(int x); void test(out int x) { }", |
| 229 | "error: 1: modifiers on parameter 1 differ between declaration and definition\n1 " |
| 230 | "error\n"); |
| 231 | } |
| 232 | |
| 233 | DEF_TEST(SkSLDuplicateSymbol, r) { |
| 234 | test_failure(r, |
| 235 | "int main; void main() { }", |
| 236 | "error: 1: symbol 'main' was already defined\n1 error\n"); |
| 237 | |
| 238 | test_failure(r, |
| 239 | "int x; int x; void main() { }", |
| 240 | "error: 1: symbol 'x' was already defined\n1 error\n"); |
| 241 | |
| 242 | test_success(r, "int x; void main() { int x; }"); |
| 243 | } |
| 244 | |
| 245 | DEF_TEST(SkSLBinaryTypeMismatch, r) { |
| 246 | test_failure(r, |
| 247 | "void main() { float x = 3 * true; }", |
| 248 | "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n1 error\n"); |
| 249 | test_failure(r, |
| 250 | "void main() { bool x = 1 || 2.0; }", |
| 251 | "error: 1: type mismatch: '||' cannot operate on 'int', 'float'\n1 error\n"); |
| 252 | } |
| 253 | |
| 254 | DEF_TEST(SkSLCallNonFunction, r) { |
| 255 | test_failure(r, |
| 256 | "void main() { float x = 3; x(); }", |
| 257 | "error: 1: 'x' is not a function\n1 error\n"); |
| 258 | } |
| 259 | |
| 260 | DEF_TEST(SkSLInvalidUnary, r) { |
| 261 | test_failure(r, |
| 262 | "void main() { mat4 x = mat4(1); ++x; }", |
| 263 | "error: 1: '++' cannot operate on 'mat4'\n1 error\n"); |
| 264 | test_failure(r, |
| 265 | "void main() { vec3 x = vec3(1); --x; }", |
| 266 | "error: 1: '--' cannot operate on 'vec3'\n1 error\n"); |
| 267 | test_failure(r, |
| 268 | "void main() { mat4 x = mat4(1); x++; }", |
| 269 | "error: 1: '++' cannot operate on 'mat4'\n1 error\n"); |
| 270 | test_failure(r, |
| 271 | "void main() { vec3 x = vec3(1); x--; }", |
| 272 | "error: 1: '--' cannot operate on 'vec3'\n1 error\n"); |
| 273 | 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, |
| 283 | "void main() { vec2 x = vec2(1, 1); x = +x; x = -x; }"); |
| 284 | } |
| 285 | |
| 286 | DEF_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 | |
| 298 | DEF_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, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 303 | "void main() { vec2 x = vec2(0); int y = x[0][0]; }", |
| 304 | "error: 1: expected array, but found 'float'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | DEF_TEST(SkSLTernaryMismatch, r) { |
| 308 | test_failure(r, |
| 309 | "void main() { int x = 5 > 2 ? true : 1.0; }", |
| 310 | "error: 1: ternary operator result mismatch: 'bool', 'float'\n1 error\n"); |
Ethan Nicholas | 2be687a | 2017-01-03 16:44:39 -0500 | [diff] [blame] | 311 | test_failure(r, |
| 312 | "void main() { int x = 5 > 2 ? vec3(1) : 1.0; }", |
| 313 | "error: 1: ternary operator result mismatch: 'vec3', 'float'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | DEF_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 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 321 | |
| 322 | DEF_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, |
| 333 | "void main() { int x; int y = x; }", |
| 334 | "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 Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 338 | test_failure(r, |
| 339 | "void main() { int x; switch (3) { case 0: x = 0; case 1: x = 1; }" |
| 340 | "sk_FragColor = vec4(x); }", |
| 341 | "error: 1: 'x' has not been assigned\n1 error\n"); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | DEF_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"); |
| 351 | test_failure(r, |
| 352 | "void main() { for (;;) { } return; }", |
| 353 | "error: 1: unreachable\n1 error\n"); |
| 354 | 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 | |
| 362 | DEF_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 | |
| 368 | DEF_TEST(SkSLBreakOutsideLoop, r) { |
| 369 | test_failure(r, |
| 370 | "void foo() { while(true) {} if (true) break; }", |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 371 | "error: 1: break statement must be inside a loop or switch\n1 error\n"); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | DEF_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 Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 378 | test_failure(r, |
| 379 | "void foo() { switch (1) { default: continue; } }", |
| 380 | "error: 1: continue statement must be inside a loop\n1 error\n"); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 381 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 382 | |
| 383 | DEF_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 Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 398 | |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 399 | DEF_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 Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 405 | DEF_TEST(SkSLBadOffset, r) { |
| 406 | test_failure(r, |
| 407 | "struct Bad { layout (offset = 5) int x; } bad; void main() { bad.x = 5; }", |
| 408 | "error: 1: offset of field 'x' must be a multiple of 4\n1 error\n"); |
| 409 | test_failure(r, |
| 410 | "struct Bad { int x; layout (offset = 0) int y; } bad; void main() { bad.x = 5; }", |
| 411 | "error: 1: offset of field 'y' must be at least 4\n1 error\n"); |
| 412 | } |
| 413 | |
Ethan Nicholas | 9a5610e | 2017-01-03 15:16:29 -0500 | [diff] [blame] | 414 | DEF_TEST(SkSLDivByZero, r) { |
| 415 | test_failure(r, |
| 416 | "int x = 1 / 0;", |
| 417 | "error: 1: division by zero\n1 error\n"); |
| 418 | test_failure(r, |
| 419 | "float x = 1 / 0;", |
| 420 | "error: 1: division by zero\n1 error\n"); |
| 421 | test_failure(r, |
| 422 | "float x = 1.0 / 0.0;", |
| 423 | "error: 1: division by zero\n1 error\n"); |
| 424 | test_failure(r, |
| 425 | "float x = -67.0 / (3.0 - 3);", |
| 426 | "error: 1: division by zero\n1 error\n"); |
| 427 | } |
| 428 | |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 429 | DEF_TEST(SkSLUnsupportedGLSLIdentifiers, r) { |
| 430 | test_failure(r, |
| 431 | "void main() { float x = gl_FragCoord.x; };", |
| 432 | "error: 1: unknown identifier 'gl_FragCoord'\n1 error\n"); |
| 433 | test_failure(r, |
| 434 | "void main() { float r = gl_FragColor.r; };", |
| 435 | "error: 1: unknown identifier 'gl_FragColor'\n1 error\n"); |
| 436 | } |
| 437 | |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 438 | DEF_TEST(SkSLWrongSwitchTypes, r) { |
| 439 | test_failure(r, |
| 440 | "void main() { switch (vec2(1)) { case 1: break; } }", |
| 441 | "error: 1: expected 'int', but found 'vec2'\n1 error\n"); |
| 442 | test_failure(r, |
| 443 | "void main() { switch (1) { case vec2(1): break; } }", |
| 444 | "error: 1: expected 'int', but found 'vec2'\n1 error\n"); |
| 445 | } |
| 446 | |
| 447 | DEF_TEST(SkSLNonConstantCase, r) { |
| 448 | test_failure(r, |
| 449 | "void main() { int x = 1; switch (1) { case x: break; } }", |
| 450 | "error: 1: case value must be a constant\n1 error\n"); |
| 451 | } |
| 452 | |
| 453 | DEF_TEST(SkSLDuplicateCase, r) { |
| 454 | test_failure(r, |
| 455 | "void main() { switch (1) { case 0: case 1: case 0: break; } }", |
| 456 | "error: 1: duplicate case value\n1 error\n"); |
| 457 | } |
| 458 | |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 459 | #endif |