| OLD | NEW |
| (Empty) | |
| 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* err
or) { |
| 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("%s\n", compiler.errorText().c_str()); |
| 18 } |
| 19 REPORTER_ASSERT(r, !result); |
| 20 REPORTER_ASSERT(r, compiler.errorText() == error); |
| 21 } |
| 22 |
| 23 static void test_success(skiatest::Reporter* r, const char* src) { |
| 24 SkSL::Compiler compiler; |
| 25 std::stringstream out; |
| 26 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, src, out); |
| 27 REPORTER_ASSERT(r, result); |
| 28 } |
| 29 |
| 30 DEF_TEST(SkSLUndefinedSymbol, r) { |
| 31 test_failure(r, |
| 32 "void main() { x = vec2(1); }", |
| 33 "error: 1: unknown identifier 'x'\n1 error\n"); |
| 34 } |
| 35 |
| 36 DEF_TEST(SkSLUndefinedFunction, r) { |
| 37 test_failure(r, |
| 38 "void main() { int x = foo(1); }", |
| 39 "error: 1: unknown identifier 'foo'\n1 error\n"); |
| 40 } |
| 41 |
| 42 DEF_TEST(SkSLGenericArgumentMismatch, r) { |
| 43 test_failure(r, |
| 44 "void main() { float x = sin(1, 2); }", |
| 45 "error: 1: no match for sin(int, int)\n1 error\n"); |
| 46 } |
| 47 |
| 48 DEF_TEST(SkSLArgumentCountMismatch, r) { |
| 49 test_failure(r, |
| 50 "float foo(float x) { return x * x; }" |
| 51 "void main() { float x = foo(1, 2); }", |
| 52 "error: 1: call to 'foo' expected 1 argument, but found 2\n1 er
ror\n"); |
| 53 } |
| 54 |
| 55 DEF_TEST(SkSLArgumentMismatch, r) { |
| 56 test_failure(r, |
| 57 "float foo(float x) { return x * x; }" |
| 58 "void main() { float x = foo(true); }", |
| 59 "error: 1: expected 'float', but found 'bool'\n1 error\n"); |
| 60 } |
| 61 |
| 62 DEF_TEST(SkSLIfTypeMismatch, r) { |
| 63 test_failure(r, |
| 64 "void main() { if (3) { } }", |
| 65 "error: 1: expected 'bool', but found 'int'\n1 error\n"); |
| 66 } |
| 67 |
| 68 DEF_TEST(SkSLDoTypeMismatch, r) { |
| 69 test_failure(r, |
| 70 "void main() { do { } while (vec2(1)); }", |
| 71 "error: 1: expected 'bool', but found 'vec2'\n1 error\n"); |
| 72 } |
| 73 |
| 74 DEF_TEST(SkSLWhileTypeMismatch, r) { |
| 75 test_failure(r, |
| 76 "void main() { while (vec3(1)) { } }", |
| 77 "error: 1: expected 'bool', but found 'vec3'\n1 error\n"); |
| 78 } |
| 79 |
| 80 DEF_TEST(SkSLForTypeMismatch, r) { |
| 81 test_failure(r, |
| 82 "void main() { for (int x = 0; x; x++) { } }", |
| 83 "error: 1: expected 'bool', but found 'int'\n1 error\n"); |
| 84 } |
| 85 |
| 86 DEF_TEST(SkSLConstructorTypeMismatch, r) { |
| 87 test_failure(r, |
| 88 "void main() { vec2 x = vec2(1.0, false); }", |
| 89 "error: 1: expected 'float', but found 'bool'\n1 error\n"); |
| 90 test_failure(r, |
| 91 "void main() { bool x = bool(1.0); }", |
| 92 "error: 1: cannot construct 'bool'\n1 error\n"); |
| 93 test_failure(r, |
| 94 "struct foo { int x; }; void main() { foo x = foo(5); }", |
| 95 "error: 1: cannot construct 'foo'\n1 error\n"); |
| 96 test_failure(r, |
| 97 "struct foo { int x; } foo; void main() { float x = float(foo);
}", |
| 98 "error: 1: invalid argument to 'float' constructor (expected a
number or bool, but found 'foo')\n1 error\n"); |
| 99 test_failure(r, |
| 100 "struct foo { int x; } foo; void main() { vec2 x = vec2(foo); }
", |
| 101 "error: 1: 'foo' is not a valid parameter to 'vec2' constructor
\n1 error\n"); |
| 102 } |
| 103 |
| 104 DEF_TEST(SkSLConstructorArgumentCount, r) { |
| 105 test_failure(r, |
| 106 "void main() { vec3 x = vec3(1.0, 2.0); }", |
| 107 "error: 1: invalid arguments to 'vec3' constructor (expected 3
scalars, but " |
| 108 "found 2)\n1 error\n"); |
| 109 test_success(r, "void main() { vec3 x = vec3(1.0, 2.0, 3.0, 4.0); }"); |
| 110 } |
| 111 |
| 112 DEF_TEST(SkSLSwizzleScalar, r) { |
| 113 test_failure(r, |
| 114 "void main() { float x = 1; float y = x.y; }", |
| 115 "error: 1: cannot swizzle value of type 'float'\n1 error\n"); |
| 116 } |
| 117 |
| 118 DEF_TEST(SkSLSwizzleMatrix, r) { |
| 119 test_failure(r, |
| 120 "void main() { mat2 x = mat2(1); float y = x.y; }", |
| 121 "error: 1: cannot swizzle value of type 'mat2'\n1 error\n"); |
| 122 } |
| 123 |
| 124 DEF_TEST(SkSLSwizzleOutOfBounds, r) { |
| 125 test_failure(r, |
| 126 "void main() { vec3 test = vec2(1).xyz; }", |
| 127 "error: 1: invalid swizzle component 'z'\n1 error\n"); |
| 128 } |
| 129 |
| 130 DEF_TEST(SkSLSwizzleTooManyComponents, r) { |
| 131 test_failure(r, |
| 132 "void main() { vec4 test = vec2(1).xxxxx; }", |
| 133 "error: 1: too many components in swizzle mask 'xxxxx'\n1 error
\n"); |
| 134 } |
| 135 |
| 136 DEF_TEST(SkSLSwizzleDuplicateOutput, r) { |
| 137 test_failure(r, |
| 138 "void main() { vec4 test = vec4(1); test.xyyz = vec4(1); }", |
| 139 "error: 1: cannot write to the same swizzle field more than onc
e\n1 error\n"); |
| 140 } |
| 141 DEF_TEST(SkSLAssignmentTypeMismatch, r) { |
| 142 test_failure(r, |
| 143 "void main() { int x = 1.0; }", |
| 144 "error: 1: expected 'int', but found 'float'\n1 error\n"); |
| 145 } |
| 146 |
| 147 DEF_TEST(SkSLReturnFromVoid, r) { |
| 148 test_failure(r, |
| 149 "void main() { return true; }", |
| 150 "error: 1: may not return a value from a void function\n1 error
\n"); |
| 151 } |
| 152 |
| 153 DEF_TEST(SkSLReturnMissingValue, r) { |
| 154 test_failure(r, |
| 155 "int foo() { return; } void main() { }", |
| 156 "error: 1: expected function to return 'int'\n1 error\n"); |
| 157 } |
| 158 |
| 159 DEF_TEST(SkSLReturnTypeMismatch, r) { |
| 160 test_failure(r, |
| 161 "int foo() { return 1.0; } void main() { }", |
| 162 "error: 1: expected 'int', but found 'float'\n1 error\n"); |
| 163 } |
| 164 |
| 165 DEF_TEST(SkSLDuplicateFunction, r) { |
| 166 test_failure(r, |
| 167 "void main() { } void main() { }", |
| 168 "error: 1: duplicate definition of void main()\n1 error\n"); |
| 169 test_success(r, |
| 170 "void main(); void main() { }"); |
| 171 } |
| 172 |
| 173 DEF_TEST(SkSLUsingInvalidValue, r) { |
| 174 test_failure(r, |
| 175 "void main() { int x = int; }", |
| 176 "error: 1: expected '(' to begin constructor invocation\n1 erro
r\n"); |
| 177 test_failure(r, |
| 178 "int test() { return 1; } void main() { int x = test; }", |
| 179 "error: 1: expected '(' to begin function call\n1 error\n"); |
| 180 } |
| 181 DEF_TEST(SkSLDifferentReturnType, r) { |
| 182 test_failure(r, |
| 183 "int main() { } void main() { }", |
| 184 "error: 1: functions 'void main()' and 'int main()' differ only
in return type\n1 " |
| 185 "error\n"); |
| 186 } |
| 187 |
| 188 DEF_TEST(SkSLDifferentModifiers, r) { |
| 189 test_failure(r, |
| 190 "void test(int x); void test(out int x) { }", |
| 191 "error: 1: modifiers on parameter 1 differ between declaration
and definition\n1 " |
| 192 "error\n"); |
| 193 } |
| 194 |
| 195 DEF_TEST(SkSLDuplicateSymbol, r) { |
| 196 test_failure(r, |
| 197 "int main; void main() { }", |
| 198 "error: 1: symbol 'main' was already defined\n1 error\n"); |
| 199 |
| 200 test_failure(r, |
| 201 "int x; int x; void main() { }", |
| 202 "error: 1: symbol 'x' was already defined\n1 error\n"); |
| 203 |
| 204 test_success(r, "int x; void main() { int x; }"); |
| 205 } |
| 206 |
| 207 DEF_TEST(SkSLBinaryTypeMismatch, r) { |
| 208 test_failure(r, |
| 209 "void main() { float x = 3 * true; }", |
| 210 "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n
1 error\n"); |
| 211 test_failure(r, |
| 212 "void main() { bool x = 1 || 2.0; }", |
| 213 "error: 1: type mismatch: '||' cannot operate on 'int', 'float'
\n1 error\n"); |
| 214 } |
| 215 |
| 216 DEF_TEST(SkSLCallNonFunction, r) { |
| 217 test_failure(r, |
| 218 "void main() { float x = 3; x(); }", |
| 219 "error: 1: 'x' is not a function\n1 error\n"); |
| 220 } |
| 221 |
| 222 DEF_TEST(SkSLInvalidUnary, r) { |
| 223 test_failure(r, |
| 224 "void main() { mat4 x = mat4(1); ++x; }", |
| 225 "error: 1: '++' cannot operate on 'mat4'\n1 error\n"); |
| 226 test_failure(r, |
| 227 "void main() { vec3 x = vec3(1); --x; }", |
| 228 "error: 1: '--' cannot operate on 'vec3'\n1 error\n"); |
| 229 test_failure(r, |
| 230 "void main() { mat4 x = mat4(1); x++; }", |
| 231 "error: 1: '++' cannot operate on 'mat4'\n1 error\n"); |
| 232 test_failure(r, |
| 233 "void main() { vec3 x = vec3(1); x--; }", |
| 234 "error: 1: '--' cannot operate on 'vec3'\n1 error\n"); |
| 235 test_failure(r, |
| 236 "void main() { int x = !12; }", |
| 237 "error: 1: '!' cannot operate on 'int'\n1 error\n"); |
| 238 test_failure(r, |
| 239 "struct foo { } bar; void main() { foo x = +bar; }", |
| 240 "error: 1: '+' cannot operate on 'foo'\n1 error\n"); |
| 241 test_failure(r, |
| 242 "struct foo { } bar; void main() { foo x = -bar; }", |
| 243 "error: 1: '-' cannot operate on 'foo'\n1 error\n"); |
| 244 test_success(r, |
| 245 "void main() { vec2 x = vec2(1, 1); x = +x; x = -x; }"); |
| 246 } |
| 247 |
| 248 DEF_TEST(SkSLInvalidAssignment, r) { |
| 249 test_failure(r, |
| 250 "void main() { 1 = 2; }", |
| 251 "error: 1: cannot assign to '1'\n1 error\n"); |
| 252 test_failure(r, |
| 253 "uniform int x; void main() { x = 0; }", |
| 254 "error: 1: cannot modify immutable variable 'x'\n1 error\n"); |
| 255 test_failure(r, |
| 256 "const int x; void main() { x = 0; }", |
| 257 "error: 1: cannot modify immutable variable 'x'\n1 error\n"); |
| 258 } |
| 259 |
| 260 DEF_TEST(SkSlBadIndex, r) { |
| 261 test_failure(r, |
| 262 "void main() { int x = 2[0]; }", |
| 263 "error: 1: expected array, but found 'int'\n1 error\n"); |
| 264 test_failure(r, |
| 265 "void main() { vec2 x = vec2(0); int y = x[0]; }", |
| 266 "error: 1: expected array, but found 'vec2'\n1 error\n"); |
| 267 } |
| 268 |
| 269 DEF_TEST(SkSLTernaryMismatch, r) { |
| 270 test_failure(r, |
| 271 "void main() { int x = 5 > 2 ? true : 1.0; }", |
| 272 "error: 1: ternary operator result mismatch: 'bool', 'float'\n1
error\n"); |
| 273 } |
| 274 |
| 275 DEF_TEST(SkSLInterfaceBlockStorageModifiers, r) { |
| 276 test_failure(r, |
| 277 "uniform foo { out int x; };", |
| 278 "error: 1: interface block fields may not have storage qualifie
rs\n1 error\n"); |
| 279 } |
| OLD | NEW |