File: custom_error_reporter.cpp

package info (click to toggle)
halide 19.0.0-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 55,096 kB
  • sloc: cpp: 286,920; ansic: 22,751; python: 5,821; makefile: 4,374; sh: 2,445; java: 1,549; javascript: 282; pascal: 212; xml: 127; asm: 9
file content (72 lines) | stat: -rw-r--r-- 2,263 bytes parent folder | download
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
#include "Halide.h"
#include <stdio.h>
#include <string.h>

using namespace Halide;

int evaluated = 0;

int should_be_evaluated() {
    printf("Should be evaluated\n");
    evaluated += 1;
    return 0;
}

int should_never_be_evaluated() {
    printf("Should never be evaluated\n");
    exit(1);
    return 0;
}

class MyCustomErrorReporter : public Halide::CompileTimeErrorReporter {
public:
    int errors_occurred;
    int warnings_occurred;

    MyCustomErrorReporter()
        : errors_occurred(0), warnings_occurred(0) {
    }

    void warning(const char *msg) override {
        auto msg_safe = Halide::Internal::replace_all(msg, ":", "(semicolon)");
        printf("Custom warn: %s\n", msg_safe.c_str());
        warnings_occurred++;
    }

    void error(const char *msg) override {
        // Emitting "error.*:" to stdout or stderr will cause CMake to report the
        // test as a failure on Windows, regardless of error code returned.
        // The error text we get from ErrorReport probably contains some variant
        // of this, so let's make sure it doesn't match that pattern.
        auto msg_safe = Halide::Internal::replace_all(msg, ":", "(semicolon)");
        printf("Custom err: %s\n", msg_safe.c_str());
        errors_occurred++;

        if (warnings_occurred != 1 || errors_occurred != 1 || evaluated != 1) {
            printf("There should have been 1 warning and 1 error and 1 evaluated assertion argument\n");
            exit(1);
        }

        // CompileTimeErrorReporter::error() must not return.
        printf("Success!\n");
        exit(0);
    }
};

int main(int argc, char **argv) {

    // Use argc here so that the compiler cannot optimize it away:
    // we know argc > 0 always, but compiler (probably) doesn't.
    _halide_user_assert(argc > 0) << should_never_be_evaluated();

    MyCustomErrorReporter reporter;
    set_custom_compile_time_error_reporter(&reporter);

    Halide::Internal::ErrorReport("", 0, nullptr, Halide::Internal::ErrorReport::User | Halide::Internal::ErrorReport::Warning) << "Here is a warning.";

    // This call should not return.
    _halide_user_assert(argc == 0) << should_be_evaluated();

    printf("CompileTimeErrorReporter::error() must not return.\n");
    return 1;
}