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