aboutsummaryrefslogtreecommitdiff
path: root/src/deps/skia/include/sksl/SkSLErrorReporter.h
blob: 48cc0958ec1738a0dd6f7469135105f522af2c8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright 2021 Google LLC.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SKSL_ERROR_REPORTER
#define SKSL_ERROR_REPORTER

#include "include/core/SkStringView.h"
#include "include/core/SkTypes.h"
#include "include/private/SkSLString.h"

#include <string>
#include <vector>

namespace SkSL {

#ifndef __has_builtin
    #define __has_builtin(x) 0
#endif

class PositionInfo {
public:
    PositionInfo(const char* file = nullptr, int line = -1)
        : fFile(file)
        , fLine(line) {}

#if __has_builtin(__builtin_FILE) && __has_builtin(__builtin_LINE)
    static PositionInfo Capture(const char* file = __builtin_FILE(), int line = __builtin_LINE()) {
        return PositionInfo(file, line);
    }
#else
    static PositionInfo Capture() { return PositionInfo(); }
#endif // __has_builtin(__builtin_FILE) && __has_builtin(__builtin_LINE)

    const char* file_name() const {
        return fFile;
    }

    int line() const {
        return fLine;
    }

private:
    const char* fFile = nullptr;
    int32_t fLine = -1;
};

/**
 * Class which is notified in the event of an error.
 */
class ErrorReporter {
public:
    ErrorReporter() {}

    virtual ~ErrorReporter() {
        SkASSERT(fPendingErrors.empty());
    }

    void error(skstd::string_view msg, PositionInfo position);

    /**
     * Reports an error message at the given line of the source text. Errors reported
     * with a line of -1 will be queued until line number information can be determined.
     */
    void error(int line, skstd::string_view msg);

    const char* source() const { return fSource; }

    void setSource(const char* source) { fSource = source; }

    void reportPendingErrors(PositionInfo pos) {
        for (const String& msg : fPendingErrors) {
            this->handleError(msg, pos);
        }
        fPendingErrors.clear();
    }

    int errorCount() const {
        return fErrorCount;
    }

    void resetErrorCount() {
        fErrorCount = 0;
    }

protected:
    /**
     * Called when an error is reported.
     */
    virtual void handleError(skstd::string_view msg, PositionInfo position) = 0;

private:
    PositionInfo position(int offset) const;

    const char* fSource = nullptr;
    std::vector<String> fPendingErrors;
    int fErrorCount = 0;
};

/**
 * Error reporter for tests that need an SkSL context; aborts immediately if an error is reported.
 */
class TestingOnly_AbortErrorReporter : public ErrorReporter {
public:
    void handleError(skstd::string_view msg, PositionInfo pos) override {
        SK_ABORT("%.*s", (int)msg.length(), msg.data());
    }
};

} // namespace SkSL

#endif