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 | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 16 | SkDynamicMemoryWStream out; |
| 17 | bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, SkString(src), out); |
| 18 | SkString skError(error); |
| 19 | if (compiler.errorText() != skError) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 20 | SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error, |
| 21 | compiler.errorText().c_str()); |
| 22 | } |
| 23 | REPORTER_ASSERT(r, !result); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 24 | REPORTER_ASSERT(r, compiler.errorText() == skError); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | static void test_success(skiatest::Reporter* r, const char* src) { |
| 28 | SkSL::Compiler compiler; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 29 | SkDynamicMemoryWStream out; |
| 30 | bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, SkString(src), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 31 | REPORTER_ASSERT(r, result); |
| 32 | } |
| 33 | |
| 34 | DEF_TEST(SkSLUndefinedSymbol, r) { |
| 35 | test_failure(r, |
| 36 | "void main() { x = vec2(1); }", |
| 37 | "error: 1: unknown identifier 'x'\n1 error\n"); |
| 38 | } |
| 39 | |
| 40 | DEF_TEST(SkSLUndefinedFunction, r) { |
| 41 | test_failure(r, |
| 42 | "void main() { int x = foo(1); }", |
| 43 | "error: 1: unknown identifier 'foo'\n1 error\n"); |
| 44 | } |
| 45 | |
| 46 | DEF_TEST(SkSLGenericArgumentMismatch, r) { |
| 47 | test_failure(r, |
| 48 | "void main() { float x = sin(1, 2); }", |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 49 | "error: 1: call to 'sin' expected 1 argument, but found 2\n1 error\n"); |
| 50 | test_failure(r, |
| 51 | "void main() { float x = sin(true); }", |
| 52 | "error: 1: no match for sin(bool)\n1 error\n"); |
| 53 | test_success(r, |
| 54 | "void main() { float x = sin(1); }"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | DEF_TEST(SkSLArgumentCountMismatch, r) { |
| 58 | test_failure(r, |
| 59 | "float foo(float x) { return x * x; }" |
| 60 | "void main() { float x = foo(1, 2); }", |
| 61 | "error: 1: call to 'foo' expected 1 argument, but found 2\n1 error\n"); |
| 62 | } |
| 63 | |
| 64 | DEF_TEST(SkSLArgumentMismatch, r) { |
| 65 | test_failure(r, |
| 66 | "float foo(float x) { return x * x; }" |
| 67 | "void main() { float x = foo(true); }", |
| 68 | "error: 1: expected 'float', but found 'bool'\n1 error\n"); |
| 69 | } |
| 70 | |
| 71 | DEF_TEST(SkSLIfTypeMismatch, r) { |
| 72 | test_failure(r, |
| 73 | "void main() { if (3) { } }", |
| 74 | "error: 1: expected 'bool', but found 'int'\n1 error\n"); |
| 75 | } |
| 76 | |
| 77 | DEF_TEST(SkSLDoTypeMismatch, r) { |
| 78 | test_failure(r, |
| 79 | "void main() { do { } while (vec2(1)); }", |
| 80 | "error: 1: expected 'bool', but found 'vec2'\n1 error\n"); |
| 81 | } |
| 82 | |
| 83 | DEF_TEST(SkSLWhileTypeMismatch, r) { |
| 84 | test_failure(r, |
| 85 | "void main() { while (vec3(1)) { } }", |
| 86 | "error: 1: expected 'bool', but found 'vec3'\n1 error\n"); |
| 87 | } |
| 88 | |
| 89 | DEF_TEST(SkSLForTypeMismatch, r) { |
| 90 | test_failure(r, |
| 91 | "void main() { for (int x = 0; x; x++) { } }", |
| 92 | "error: 1: expected 'bool', but found 'int'\n1 error\n"); |
| 93 | } |
| 94 | |
| 95 | DEF_TEST(SkSLConstructorTypeMismatch, r) { |
| 96 | test_failure(r, |
| 97 | "void main() { vec2 x = vec2(1.0, false); }", |
| 98 | "error: 1: expected 'float', but found 'bool'\n1 error\n"); |
| 99 | test_failure(r, |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 100 | "void main() { vec2 x = vec2(bvec2(false)); }", |
| 101 | "error: 1: 'bvec2' is not a valid parameter to 'vec2' constructor\n1 error\n"); |
| 102 | test_failure(r, |
| 103 | "void main() { bvec2 x = bvec2(vec2(1)); }", |
| 104 | "error: 1: 'vec2' is not a valid parameter to 'bvec2' constructor\n1 error\n"); |
| 105 | test_failure(r, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 106 | "void main() { bool x = bool(1.0); }", |
| 107 | "error: 1: cannot construct 'bool'\n1 error\n"); |
| 108 | test_failure(r, |
| 109 | "struct foo { int x; }; void main() { foo x = foo(5); }", |
| 110 | "error: 1: cannot construct 'foo'\n1 error\n"); |
| 111 | test_failure(r, |
| 112 | "struct foo { int x; } foo; void main() { float x = float(foo); }", |
| 113 | "error: 1: invalid argument to 'float' constructor (expected a number or bool, but found 'foo')\n1 error\n"); |
| 114 | test_failure(r, |
| 115 | "struct foo { int x; } foo; void main() { vec2 x = vec2(foo); }", |
| 116 | "error: 1: 'foo' is not a valid parameter to 'vec2' constructor\n1 error\n"); |
| 117 | } |
| 118 | |
| 119 | DEF_TEST(SkSLConstructorArgumentCount, r) { |
| 120 | test_failure(r, |
| 121 | "void main() { vec3 x = vec3(1.0, 2.0); }", |
| 122 | "error: 1: invalid arguments to 'vec3' constructor (expected 3 scalars, but " |
| 123 | "found 2)\n1 error\n"); |
| 124 | test_success(r, "void main() { vec3 x = vec3(1.0, 2.0, 3.0, 4.0); }"); |
| 125 | } |
| 126 | |
| 127 | DEF_TEST(SkSLSwizzleScalar, r) { |
| 128 | test_failure(r, |
| 129 | "void main() { float x = 1; float y = x.y; }", |
| 130 | "error: 1: cannot swizzle value of type 'float'\n1 error\n"); |
| 131 | } |
| 132 | |
| 133 | DEF_TEST(SkSLSwizzleMatrix, r) { |
| 134 | test_failure(r, |
| 135 | "void main() { mat2 x = mat2(1); float y = x.y; }", |
| 136 | "error: 1: cannot swizzle value of type 'mat2'\n1 error\n"); |
| 137 | } |
| 138 | |
| 139 | DEF_TEST(SkSLSwizzleOutOfBounds, r) { |
| 140 | test_failure(r, |
| 141 | "void main() { vec3 test = vec2(1).xyz; }", |
| 142 | "error: 1: invalid swizzle component 'z'\n1 error\n"); |
| 143 | } |
| 144 | |
| 145 | DEF_TEST(SkSLSwizzleTooManyComponents, r) { |
| 146 | test_failure(r, |
| 147 | "void main() { vec4 test = vec2(1).xxxxx; }", |
| 148 | "error: 1: too many components in swizzle mask 'xxxxx'\n1 error\n"); |
| 149 | } |
| 150 | |
| 151 | DEF_TEST(SkSLSwizzleDuplicateOutput, r) { |
| 152 | test_failure(r, |
| 153 | "void main() { vec4 test = vec4(1); test.xyyz = vec4(1); }", |
| 154 | "error: 1: cannot write to the same swizzle field more than once\n1 error\n"); |
| 155 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 156 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 157 | DEF_TEST(SkSLAssignmentTypeMismatch, r) { |
| 158 | test_failure(r, |
| 159 | "void main() { int x = 1.0; }", |
| 160 | "error: 1: expected 'int', but found 'float'\n1 error\n"); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 161 | test_failure(r, |
| 162 | "void main() { int x; x = 1.0; }", |
| 163 | "error: 1: type mismatch: '=' cannot operate on 'int', 'float'\n1 error\n"); |
| 164 | test_success(r, |
| 165 | "void main() { vec3 x = vec3(0); x *= 1.0; }"); |
| 166 | test_failure(r, |
| 167 | "void main() { ivec3 x = ivec3(0); x *= 1.0; }", |
| 168 | "error: 1: type mismatch: '*=' cannot operate on 'ivec3', 'float'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | DEF_TEST(SkSLReturnFromVoid, r) { |
| 172 | test_failure(r, |
| 173 | "void main() { return true; }", |
| 174 | "error: 1: may not return a value from a void function\n1 error\n"); |
| 175 | } |
| 176 | |
| 177 | DEF_TEST(SkSLReturnMissingValue, r) { |
| 178 | test_failure(r, |
| 179 | "int foo() { return; } void main() { }", |
| 180 | "error: 1: expected function to return 'int'\n1 error\n"); |
| 181 | } |
| 182 | |
| 183 | DEF_TEST(SkSLReturnTypeMismatch, r) { |
| 184 | test_failure(r, |
| 185 | "int foo() { return 1.0; } void main() { }", |
| 186 | "error: 1: expected 'int', but found 'float'\n1 error\n"); |
| 187 | } |
| 188 | |
| 189 | DEF_TEST(SkSLDuplicateFunction, r) { |
| 190 | test_failure(r, |
| 191 | "void main() { } void main() { }", |
| 192 | "error: 1: duplicate definition of void main()\n1 error\n"); |
| 193 | test_success(r, |
| 194 | "void main(); void main() { }"); |
| 195 | } |
| 196 | |
| 197 | DEF_TEST(SkSLUsingInvalidValue, r) { |
| 198 | test_failure(r, |
| 199 | "void main() { int x = int; }", |
| 200 | "error: 1: expected '(' to begin constructor invocation\n1 error\n"); |
| 201 | test_failure(r, |
| 202 | "int test() { return 1; } void main() { int x = test; }", |
| 203 | "error: 1: expected '(' to begin function call\n1 error\n"); |
| 204 | } |
| 205 | DEF_TEST(SkSLDifferentReturnType, r) { |
| 206 | test_failure(r, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 207 | "int main() { return 1; } void main() { }", |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 208 | "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 " |
| 209 | "error\n"); |
| 210 | } |
| 211 | |
| 212 | DEF_TEST(SkSLDifferentModifiers, r) { |
| 213 | test_failure(r, |
| 214 | "void test(int x); void test(out int x) { }", |
| 215 | "error: 1: modifiers on parameter 1 differ between declaration and definition\n1 " |
| 216 | "error\n"); |
| 217 | } |
| 218 | |
| 219 | DEF_TEST(SkSLDuplicateSymbol, r) { |
| 220 | test_failure(r, |
| 221 | "int main; void main() { }", |
| 222 | "error: 1: symbol 'main' was already defined\n1 error\n"); |
| 223 | |
| 224 | test_failure(r, |
| 225 | "int x; int x; void main() { }", |
| 226 | "error: 1: symbol 'x' was already defined\n1 error\n"); |
| 227 | |
| 228 | test_success(r, "int x; void main() { int x; }"); |
| 229 | } |
| 230 | |
| 231 | DEF_TEST(SkSLBinaryTypeMismatch, r) { |
| 232 | test_failure(r, |
| 233 | "void main() { float x = 3 * true; }", |
| 234 | "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n1 error\n"); |
| 235 | test_failure(r, |
| 236 | "void main() { bool x = 1 || 2.0; }", |
| 237 | "error: 1: type mismatch: '||' cannot operate on 'int', 'float'\n1 error\n"); |
| 238 | } |
| 239 | |
| 240 | DEF_TEST(SkSLCallNonFunction, r) { |
| 241 | test_failure(r, |
| 242 | "void main() { float x = 3; x(); }", |
| 243 | "error: 1: 'x' is not a function\n1 error\n"); |
| 244 | } |
| 245 | |
| 246 | DEF_TEST(SkSLInvalidUnary, r) { |
| 247 | test_failure(r, |
| 248 | "void main() { mat4 x = mat4(1); ++x; }", |
| 249 | "error: 1: '++' cannot operate on 'mat4'\n1 error\n"); |
| 250 | test_failure(r, |
| 251 | "void main() { vec3 x = vec3(1); --x; }", |
| 252 | "error: 1: '--' cannot operate on 'vec3'\n1 error\n"); |
| 253 | test_failure(r, |
| 254 | "void main() { mat4 x = mat4(1); x++; }", |
| 255 | "error: 1: '++' cannot operate on 'mat4'\n1 error\n"); |
| 256 | test_failure(r, |
| 257 | "void main() { vec3 x = vec3(1); x--; }", |
| 258 | "error: 1: '--' cannot operate on 'vec3'\n1 error\n"); |
| 259 | test_failure(r, |
| 260 | "void main() { int x = !12; }", |
| 261 | "error: 1: '!' cannot operate on 'int'\n1 error\n"); |
| 262 | test_failure(r, |
| 263 | "struct foo { } bar; void main() { foo x = +bar; }", |
| 264 | "error: 1: '+' cannot operate on 'foo'\n1 error\n"); |
| 265 | test_failure(r, |
| 266 | "struct foo { } bar; void main() { foo x = -bar; }", |
| 267 | "error: 1: '-' cannot operate on 'foo'\n1 error\n"); |
| 268 | test_success(r, |
| 269 | "void main() { vec2 x = vec2(1, 1); x = +x; x = -x; }"); |
| 270 | } |
| 271 | |
| 272 | DEF_TEST(SkSLInvalidAssignment, r) { |
| 273 | test_failure(r, |
| 274 | "void main() { 1 = 2; }", |
| 275 | "error: 1: cannot assign to '1'\n1 error\n"); |
| 276 | test_failure(r, |
| 277 | "uniform int x; void main() { x = 0; }", |
| 278 | "error: 1: cannot modify immutable variable 'x'\n1 error\n"); |
| 279 | test_failure(r, |
| 280 | "const int x; void main() { x = 0; }", |
| 281 | "error: 1: cannot modify immutable variable 'x'\n1 error\n"); |
| 282 | } |
| 283 | |
| 284 | DEF_TEST(SkSLBadIndex, r) { |
| 285 | test_failure(r, |
| 286 | "void main() { int x = 2[0]; }", |
| 287 | "error: 1: expected array, but found 'int'\n1 error\n"); |
| 288 | test_failure(r, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 289 | "void main() { vec2 x = vec2(0); int y = x[0][0]; }", |
| 290 | "error: 1: expected array, but found 'float'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | DEF_TEST(SkSLTernaryMismatch, r) { |
| 294 | test_failure(r, |
| 295 | "void main() { int x = 5 > 2 ? true : 1.0; }", |
| 296 | "error: 1: ternary operator result mismatch: 'bool', 'float'\n1 error\n"); |
| 297 | } |
| 298 | |
| 299 | DEF_TEST(SkSLInterfaceBlockStorageModifiers, r) { |
| 300 | test_failure(r, |
| 301 | "uniform foo { out int x; };", |
| 302 | "error: 1: interface block fields may not have storage qualifiers\n1 error\n"); |
| 303 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 304 | |
| 305 | DEF_TEST(SkSLUseWithoutInitialize, r) { |
| 306 | test_failure(r, |
| 307 | "void main() { int x; if (5 == 2) x = 3; x++; }", |
| 308 | "error: 1: 'x' has not been assigned\n1 error\n"); |
| 309 | test_failure(r, |
| 310 | "void main() { int x[2][2]; int i; x[i][1] = 4; }", |
| 311 | "error: 1: 'i' has not been assigned\n1 error\n"); |
| 312 | test_failure(r, |
| 313 | "int main() { int r; return r; }", |
| 314 | "error: 1: 'r' has not been assigned\n1 error\n"); |
| 315 | test_failure(r, |
| 316 | "void main() { int x; int y = x; }", |
| 317 | "error: 1: 'x' has not been assigned\n1 error\n"); |
| 318 | test_failure(r, |
| 319 | "void main() { bool x; if (true && (false || x)) return; }", |
| 320 | "error: 1: 'x' has not been assigned\n1 error\n"); |
| 321 | } |
| 322 | |
| 323 | DEF_TEST(SkSLUnreachable, r) { |
| 324 | test_failure(r, |
| 325 | "void main() { return; return; }", |
| 326 | "error: 1: unreachable\n1 error\n"); |
| 327 | test_failure(r, |
| 328 | "void main() { for (;;) { continue; int x = 1; } }", |
| 329 | "error: 1: unreachable\n1 error\n"); |
| 330 | test_failure(r, |
| 331 | "void main() { for (;;) { } return; }", |
| 332 | "error: 1: unreachable\n1 error\n"); |
| 333 | test_failure(r, |
| 334 | "void main() { if (true) return; else discard; return; }", |
| 335 | "error: 1: unreachable\n1 error\n"); |
| 336 | test_failure(r, |
| 337 | "void main() { return; while (true); }", |
| 338 | "error: 1: unreachable\n1 error\n"); |
| 339 | } |
| 340 | |
| 341 | DEF_TEST(SkSLNoReturn, r) { |
| 342 | test_failure(r, |
| 343 | "int foo() { if (2 > 5) return 3; }", |
| 344 | "error: 1: function can exit without returning a value\n1 error\n"); |
| 345 | } |
| 346 | |
| 347 | DEF_TEST(SkSLBreakOutsideLoop, r) { |
| 348 | test_failure(r, |
| 349 | "void foo() { while(true) {} if (true) break; }", |
| 350 | "error: 1: break statement must be inside a loop\n1 error\n"); |
| 351 | } |
| 352 | |
| 353 | DEF_TEST(SkSLContinueOutsideLoop, r) { |
| 354 | test_failure(r, |
| 355 | "void foo() { for(;;); continue; }", |
| 356 | "error: 1: continue statement must be inside a loop\n1 error\n"); |
| 357 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 358 | |
| 359 | DEF_TEST(SkSLStaticIfError, r) { |
| 360 | // ensure eliminated branch of static if / ternary is still checked for errors |
| 361 | test_failure(r, |
| 362 | "void foo() { if (true); else x = 5; }", |
| 363 | "error: 1: unknown identifier 'x'\n1 error\n"); |
| 364 | test_failure(r, |
| 365 | "void foo() { if (false) x = 5; }", |
| 366 | "error: 1: unknown identifier 'x'\n1 error\n"); |
| 367 | test_failure(r, |
| 368 | "void foo() { true ? 5 : x; }", |
| 369 | "error: 1: unknown identifier 'x'\n1 error\n"); |
| 370 | test_failure(r, |
| 371 | "void foo() { false ? x : 5; }", |
| 372 | "error: 1: unknown identifier 'x'\n1 error\n"); |
| 373 | } |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 374 | |
| 375 | #endif |