1 /*
2 * Copyright 2021 Google LLC.
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 "include/private/SkSLIRNode.h"
9 #include "include/sksl/DSL.h"
10 #include "src/gpu/GrDirectContextPriv.h"
11 #include "src/gpu/GrGpu.h"
12 #include "src/sksl/SkSLCompiler.h"
13 #include "src/sksl/SkSLThreadContext.h"
14 #include "src/sksl/dsl/priv/DSLWriter.h"
15
16 #include "tests/Test.h"
17
18 #include <limits>
19
20 using namespace SkSL::dsl;
21
22 #if defined(__GNUC__) || defined(__clang__)
23
24 class ExpectErrorLineNumber : public SkSL::ErrorReporter {
25 public:
ExpectErrorLineNumber(skiatest::Reporter * reporter,const char * msg,int line)26 ExpectErrorLineNumber(skiatest::Reporter* reporter, const char* msg, int line)
27 : fMsg(msg)
28 , fLine(line)
29 , fReporter(reporter)
30 , fOldReporter(&GetErrorReporter()) {
31 SetErrorReporter(this);
32 }
33
~ExpectErrorLineNumber()34 ~ExpectErrorLineNumber() override {
35 REPORTER_ASSERT(fReporter, !fMsg);
36 SetErrorReporter(fOldReporter);
37 }
38
handleError(skstd::string_view msg,SkSL::PositionInfo pos)39 void handleError(skstd::string_view msg, SkSL::PositionInfo pos) override {
40 REPORTER_ASSERT(fReporter, msg == fMsg,
41 "Error mismatch: expected:\n%sbut received:\n%.*s", fMsg, (int)msg.length(),
42 msg.data());
43 REPORTER_ASSERT(fReporter, pos.line() == fLine,
44 "Line number mismatch: expected %d, but received %d\n", fLine, pos.line());
45 SkSL::ThreadContext::Compiler().handleError(msg, pos);
46 fMsg = nullptr;
47 }
48
49 private:
50 const char* fMsg;
51 int fLine;
52 skiatest::Reporter* fReporter;
53 ErrorReporter* fOldReporter;
54 };
55
DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLErrorLineNumbers,r,ctxInfo)56 DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLErrorLineNumbers, r, ctxInfo) {
57 Start(ctxInfo.directContext()->priv().getGpu()->shaderCompiler());
58 {
59 ExpectErrorLineNumber error(r, "type mismatch: '+' cannot operate on 'float', 'bool'",
60 __LINE__ + 1);
61 (Float(1) + true).release();
62 }
63
64 {
65 Var a(kBool_Type);
66 DSLWriter::MarkDeclared(a);
67 ExpectErrorLineNumber error(r, "type mismatch: '=' cannot operate on 'bool', 'float'",
68 __LINE__ + 1);
69 (a = 5.0f).release();
70 }
71
72 {
73 Var a(Array(kInt_Type, 5));
74 DSLWriter::MarkDeclared(a);
75 ExpectErrorLineNumber error(r, "expected 'int', but found 'bool'", __LINE__ + 1);
76 (a[true]).release();
77 }
78
79 {
80 Var a(Array(kInt_Type, 5));
81 DSLWriter::MarkDeclared(a);
82 ExpectErrorLineNumber error(r, "'++' cannot operate on 'int[5]'", __LINE__ + 1);
83 (++a).release();
84 }
85
86 {
87 ExpectErrorLineNumber error(r, "expected 'bool', but found 'int'", __LINE__ + 1);
88 Do(Discard(), 5).release();
89 }
90
91 {
92 ExpectErrorLineNumber error(r, "expected 'bool', but found 'int'", __LINE__ + 1);
93 For(DSLStatement(), 5, DSLExpression(), Block()).release();
94 }
95
96 {
97 ExpectErrorLineNumber error(r, "expected 'bool', but found 'int'", __LINE__ + 1);
98 If(5, Discard()).release();
99 }
100
101 {
102 ExpectErrorLineNumber error(r, "expected 'bool', but found 'int'", __LINE__ + 1);
103 While(5, Discard()).release();
104 }
105
106 {
107 ExpectErrorLineNumber error(r, "no match for abs(bool)", __LINE__ + 1);
108 Abs(true).release();
109 }
110 End();
111 }
112
113 #endif // defined(__GNUC__) || defined(__clang__)
114