File: custom_error_reporter.cpp

package info (click to toggle)
halide 21.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,752 kB
  • sloc: cpp: 289,334; ansic: 22,751; python: 7,486; makefile: 4,299; sh: 2,508; java: 1,549; javascript: 282; pascal: 207; xml: 127; asm: 9
file content (67 lines) | stat: -rw-r--r-- 2,053 bytes parent folder | download | duplicates (2)
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
#include "Halide.h"
#include <cstdio>
#include <cstdlib>

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;
}

struct MyCustomErrorReporter final : CompileTimeErrorReporter {
    int errors_occurred{0};
    int warnings_occurred{0};

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

    [[noreturn]] 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 the 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.
        const auto msg_safe = Internal::replace_all(msg, ":", "(colon)");
        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 the compiler (probably) doesn't.
    _halide_user_assert(argc > 0) << should_never_be_evaluated();

    MyCustomErrorReporter reporter;
    set_custom_compile_time_error_reporter(&reporter);

    user_warning << "Here is a warning.";

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

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