Blender V5.0
mtl_shader_log.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_string_ref.hh"
10
11#include "GPU_platform.hh"
12
13#include "mtl_shader_log.hh"
14
15namespace blender::gpu {
16
17const char *MTLLogParser::parse_line(const char *source_combined,
18 const char *log_line,
19 GPULogItem &log_item)
20{
21 const char *name_start = log_line;
22 log_line = skip_name(log_line);
23 const char *name_end = log_line;
24 log_line = skip_separators(log_line, ":");
25
26 /* Parse error line & char numbers. */
27 if (at_number(log_line)) {
28 /* Reset skip line if two errors follow. */
29 parsed_error_ = false;
30 wrapper_error_ = false;
31
32 const char *error_line_number_end;
33
34 log_item.cursor.row = parse_number(log_line, &error_line_number_end);
35 log_line = error_line_number_end;
36 log_line = skip_separators(log_line, ": ");
37 log_item.cursor.column = parse_number(log_line, &error_line_number_end);
38 log_line = error_line_number_end;
39 /* Simply copy the start of the error line since it is already in the format we want. */
40 log_item.cursor.file_name_and_error_line = StringRef(name_start, error_line_number_end);
41
42 StringRef source_name(name_start, name_end);
43
44 if (source_name == "msl_wrapper_code") {
45 /* In this case the issue is in the wrapper. We cannot access it.
46 * So we still display the internal error lines for some more information. */
47 log_item.cursor.row = -1;
48 wrapper_error_ = true;
49 }
50 else if (!source_name.is_empty()) {
51 std::string needle = std::string("#line 1 \"") + source_name + "\"";
52
53 StringRefNull src(source_combined);
54 int64_t file_start = src.find(needle);
55 if (file_start == -1) {
56 /* Can be generated code or wrapper code outside of the main sources.
57 * But should be already caught by the above case. */
58 log_item.cursor.row = -1;
59 wrapper_error_ = true;
60 }
61 else {
62 StringRef previous_sources(source_combined, file_start);
63 for (const char c : previous_sources) {
64 if (c == '\n') {
65 log_item.cursor.row++;
66 }
67 }
68 /* The method above does not work with injected #line directives.
69 * Just output the raw error and forget about the formatting for now. */
70 log_item.cursor.row = -1;
71 log_item.cursor.file_name_and_error_line = "";
72 parsed_error_ = false;
73 wrapper_error_ = true;
74 return name_start;
75 }
76 }
77 }
78 else if (parsed_error_) {
79 /* Skip the redundant lines that we be outputted above the error. */
80 return skip_line(log_line);
81 }
82 else if (wrapper_error_) {
83 /* Display full lines of error in case of wrapper (non parsed) errors.
84 * Avoids weirdly aligned '^' and underlined suggestions. */
85 return name_start;
86 }
87 log_line = skip_separators(log_line, ": ");
88
89 /* Skip to message. Avoid redundant info. */
90 log_line = skip_severity_keyword(log_line, log_item);
91 log_line = skip_separators(log_line, ": ");
92
93 return log_line;
94}
95
96const char *MTLLogParser::skip_name(const char *log_line)
97{
98 return skip_until(log_line, ':');
99}
100
101const char *MTLLogParser::skip_severity_keyword(const char *log_line, GPULogItem &log_item)
102{
103 return skip_severity(log_line, log_item, "error", "warning", "note");
104}
105
106const char *MTLLogParser::skip_line(const char *cursor) const
107{
108 while (!ELEM(cursor[0], '\n', '\0')) {
109 cursor++;
110 }
111 return cursor;
112}
113
114} // namespace blender::gpu
#define ELEM(...)
long long int int64_t
constexpr int64_t find(char c, int64_t pos=0) const
constexpr bool is_empty() const
int parse_number(const char *log_line, const char **r_new_position) const
const char * skip_separators(const char *log_line, const StringRef separators) const
bool at_number(const char *log_line) const
const char * skip_until(const char *log_line, char stop_char) const
const char * skip_severity(const char *log_line, GPULogItem &log_item, const char *error_msg, const char *warning_msg, const char *note_msg) const
const char * parse_line(const char *source_combined, const char *log_line, GPULogItem &log_item) override
const char * skip_line(const char *cursor) const
const char * skip_name(const char *log_line)
const char * skip_severity_keyword(const char *log_line, GPULogItem &log_item)