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