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 | } |
| 142 | DEF_TEST(SkSLAssignmentTypeMismatch, r) { |
| 143 | test_failure(r, |
| 144 | "void main() { int x = 1.0; }", |
| 145 | "error: 1: expected 'int', but found 'float'\n1 error\n"); |
| 146 | } |
| 147 | |
| 148 | DEF_TEST(SkSLReturnFromVoid, r) { |
| 149 | test_failure(r, |
| 150 | "void main() { return true; }", |
| 151 | "error: 1: may not return a value from a void function\n1 error\n"); |
| 152 | } |
| 153 | |
| 154 | DEF_TEST(SkSLReturnMissingValue, r) { |
| 155 | test_failure(r, |
| 156 | "int foo() { return; } void main() { }", |
| 157 | "error: 1: expected function to return 'int'\n1 error\n"); |
| 158 | } |
| 159 | |
| 160 | DEF_TEST(SkSLReturnTypeMismatch, r) { |
| 161 | test_failure(r, |
| 162 | "int foo() { return 1.0; } void main() { }", |
| 163 | "error: 1: expected 'int', but found 'float'\n1 error\n"); |
| 164 | } |
| 165 | |
| 166 | DEF_TEST(SkSLDuplicateFunction, r) { |
| 167 | test_failure(r, |
| 168 | "void main() { } void main() { }", |
| 169 | "error: 1: duplicate definition of void main()\n1 error\n"); |
| 170 | test_success(r, |
| 171 | "void main(); void main() { }"); |
| 172 | } |
| 173 | |
| 174 | DEF_TEST(SkSLUsingInvalidValue, r) { |
| 175 | test_failure(r, |
| 176 | "void main() { int x = int; }", |
| 177 | "error: 1: expected '(' to begin constructor invocation\n1 error\n"); |
| 178 | test_failure(r, |
| 179 | "int test() { return 1; } void main() { int x = test; }", |
| 180 | "error: 1: expected '(' to begin function call\n1 error\n"); |
| 181 | } |
| 182 | DEF_TEST(SkSLDifferentReturnType, r) { |
| 183 | test_failure(r, |
| 184 | "int main() { } void main() { }", |
| 185 | "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 " |
| 186 | "error\n"); |
| 187 | } |
| 188 | |
| 189 | DEF_TEST(SkSLDifferentModifiers, r) { |
| 190 | test_failure(r, |
| 191 | "void test(int x); void test(out int x) { }", |
| 192 | "error: 1: modifiers on parameter 1 differ between declaration and definition\n1 " |
| 193 | "error\n"); |
| 194 | } |
| 195 | |
| 196 | DEF_TEST(SkSLDuplicateSymbol, r) { |
| 197 | test_failure(r, |
| 198 | "int main; void main() { }", |
| 199 | "error: 1: symbol 'main' was already defined\n1 error\n"); |
| 200 | |
| 201 | test_failure(r, |
| 202 | "int x; int x; void main() { }", |
| 203 | "error: 1: symbol 'x' was already defined\n1 error\n"); |
| 204 | |
| 205 | test_success(r, "int x; void main() { int x; }"); |
| 206 | } |
| 207 | |
| 208 | DEF_TEST(SkSLBinaryTypeMismatch, r) { |
| 209 | test_failure(r, |
| 210 | "void main() { float x = 3 * true; }", |
| 211 | "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n1 error\n"); |
| 212 | test_failure(r, |
| 213 | "void main() { bool x = 1 || 2.0; }", |
| 214 | "error: 1: type mismatch: '||' cannot operate on 'int', 'float'\n1 error\n"); |
| 215 | } |
| 216 | |
| 217 | DEF_TEST(SkSLCallNonFunction, r) { |
| 218 | test_failure(r, |
| 219 | "void main() { float x = 3; x(); }", |
| 220 | "error: 1: 'x' is not a function\n1 error\n"); |
| 221 | } |
| 222 | |
| 223 | DEF_TEST(SkSLInvalidUnary, r) { |
| 224 | test_failure(r, |
| 225 | "void main() { mat4 x = mat4(1); ++x; }", |
| 226 | "error: 1: '++' cannot operate on 'mat4'\n1 error\n"); |
| 227 | test_failure(r, |
| 228 | "void main() { vec3 x = vec3(1); --x; }", |
| 229 | "error: 1: '--' cannot operate on 'vec3'\n1 error\n"); |
| 230 | test_failure(r, |
| 231 | "void main() { mat4 x = mat4(1); x++; }", |
| 232 | "error: 1: '++' cannot operate on 'mat4'\n1 error\n"); |
| 233 | test_failure(r, |
| 234 | "void main() { vec3 x = vec3(1); x--; }", |
| 235 | "error: 1: '--' cannot operate on 'vec3'\n1 error\n"); |
| 236 | test_failure(r, |
| 237 | "void main() { int x = !12; }", |
| 238 | "error: 1: '!' cannot operate on 'int'\n1 error\n"); |
| 239 | test_failure(r, |
| 240 | "struct foo { } bar; void main() { foo x = +bar; }", |
| 241 | "error: 1: '+' cannot operate on 'foo'\n1 error\n"); |
| 242 | test_failure(r, |
| 243 | "struct foo { } bar; void main() { foo x = -bar; }", |
| 244 | "error: 1: '-' cannot operate on 'foo'\n1 error\n"); |
| 245 | test_success(r, |
| 246 | "void main() { vec2 x = vec2(1, 1); x = +x; x = -x; }"); |
| 247 | } |
| 248 | |
| 249 | DEF_TEST(SkSLInvalidAssignment, r) { |
| 250 | test_failure(r, |
| 251 | "void main() { 1 = 2; }", |
| 252 | "error: 1: cannot assign to '1'\n1 error\n"); |
| 253 | test_failure(r, |
| 254 | "uniform int x; void main() { x = 0; }", |
| 255 | "error: 1: cannot modify immutable variable 'x'\n1 error\n"); |
| 256 | test_failure(r, |
| 257 | "const int x; void main() { x = 0; }", |
| 258 | "error: 1: cannot modify immutable variable 'x'\n1 error\n"); |
| 259 | } |
| 260 | |
| 261 | DEF_TEST(SkSLBadIndex, r) { |
| 262 | test_failure(r, |
| 263 | "void main() { int x = 2[0]; }", |
| 264 | "error: 1: expected array, but found 'int'\n1 error\n"); |
| 265 | test_failure(r, |
| 266 | "void main() { vec2 x = vec2(0); int y = x[0]; }", |
| 267 | "error: 1: expected array, but found 'vec2'\n1 error\n"); |
| 268 | } |
| 269 | |
| 270 | DEF_TEST(SkSLTernaryMismatch, r) { |
| 271 | test_failure(r, |
| 272 | "void main() { int x = 5 > 2 ? true : 1.0; }", |
| 273 | "error: 1: ternary operator result mismatch: 'bool', 'float'\n1 error\n"); |
| 274 | } |
| 275 | |
| 276 | DEF_TEST(SkSLInterfaceBlockStorageModifiers, r) { |
| 277 | test_failure(r, |
| 278 | "uniform foo { out int x; };", |
| 279 | "error: 1: interface block fields may not have storage qualifiers\n1 error\n"); |
| 280 | } |