xref: /aosp_15_r20/external/skia/src/sksl/SkSLErrorReporter.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #ifndef SKSL_ERROR_REPORTER
9 #define SKSL_ERROR_REPORTER
10 
11 #include "include/core/SkTypes.h"
12 
13 #include <string_view>
14 
15 namespace SkSL {
16 
17 class Position;
18 
19 /**
20  * Class which is notified in the event of an error.
21  */
22 class ErrorReporter {
23 public:
ErrorReporter()24     ErrorReporter() {}
25 
~ErrorReporter()26     virtual ~ErrorReporter() {}
27 
28     void error(Position position, std::string_view msg);
29 
source()30     std::string_view source() const { return fSource; }
31 
setSource(std::string_view source)32     void setSource(std::string_view source) { fSource = source; }
33 
errorCount()34     int errorCount() const {
35         return fErrorCount;
36     }
37 
resetErrorCount()38     void resetErrorCount() {
39         fErrorCount = 0;
40     }
41 
42 protected:
43     /**
44      * Called when an error is reported.
45      */
46     virtual void handleError(std::string_view msg, Position position) = 0;
47 
48 private:
49     Position position(int offset) const;
50 
51     std::string_view fSource;
52     int fErrorCount = 0;
53 };
54 
55 /**
56  * Error reporter for tests that need an SkSL context; aborts immediately if an error is reported.
57  */
58 class TestingOnly_AbortErrorReporter : public ErrorReporter {
59 public:
60     void handleError(std::string_view msg, Position pos) override;
61 };
62 
63 } // namespace SkSL
64 
65 #endif
66