Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | // Copyright 2021 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
dan sinclair | d3b1369 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 15 | #include "src/tint/lang/wgsl/sem/info.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 16 | |
dan sinclair | d3b1369 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 17 | #include "src/tint/lang/wgsl/sem/function.h" |
| 18 | #include "src/tint/lang/wgsl/sem/module.h" |
| 19 | #include "src/tint/lang/wgsl/sem/statement.h" |
| 20 | #include "src/tint/lang/wgsl/sem/value_expression.h" |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 21 | #include "src/tint/utils/rtti/switch.h" |
James Price | 5853205 | 2023-01-25 01:24:46 +0000 | [diff] [blame] | 22 | |
dan sinclair | c990b3c | 2022-04-07 16:04:35 +0000 | [diff] [blame] | 23 | namespace tint::sem { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 24 | |
| 25 | Info::Info() = default; |
| 26 | |
| 27 | Info::Info(Info&&) = default; |
| 28 | |
| 29 | Info::~Info() = default; |
| 30 | |
| 31 | Info& Info::operator=(Info&&) = default; |
| 32 | |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 33 | core::DiagnosticSeverity Info::DiagnosticSeverity(const ast::Node* ast_node, |
| 34 | core::DiagnosticRule rule) const { |
James Price | 5853205 | 2023-01-25 01:24:46 +0000 | [diff] [blame] | 35 | // Get the diagnostic severity modification for a node. |
| 36 | auto check = [&](auto* node) { |
| 37 | auto& severities = node->DiagnosticSeverities(); |
| 38 | auto itr = severities.find(rule); |
| 39 | if (itr != severities.end()) { |
| 40 | return itr->second; |
| 41 | } |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 42 | return core::DiagnosticSeverity::kUndefined; |
James Price | 5853205 | 2023-01-25 01:24:46 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | // Get the diagnostic severity modification for a function. |
| 46 | auto check_func = [&](const sem::Function* func) { |
| 47 | auto severity = check(func); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 48 | if (severity != core::DiagnosticSeverity::kUndefined) { |
James Price | 5853205 | 2023-01-25 01:24:46 +0000 | [diff] [blame] | 49 | return severity; |
| 50 | } |
| 51 | |
| 52 | // No severity set on the function, so check the module instead. |
| 53 | return check(module_); |
| 54 | }; |
| 55 | |
| 56 | // Get the diagnostic severity modification for a statement. |
| 57 | auto check_stmt = [&](const sem::Statement* stmt) { |
| 58 | // Walk up the statement hierarchy, checking for diagnostic severity modifications. |
| 59 | while (true) { |
| 60 | auto severity = check(stmt); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 61 | if (severity != core::DiagnosticSeverity::kUndefined) { |
James Price | 5853205 | 2023-01-25 01:24:46 +0000 | [diff] [blame] | 62 | return severity; |
| 63 | } |
| 64 | if (!stmt->Parent()) { |
| 65 | break; |
| 66 | } |
| 67 | stmt = stmt->Parent(); |
| 68 | } |
| 69 | |
| 70 | // No severity set on the statement, so check the function instead. |
| 71 | return check_func(stmt->Function()); |
| 72 | }; |
| 73 | |
| 74 | // Query the diagnostic severity from the semantic node that corresponds to the AST node. |
| 75 | auto* sem = Get(ast_node); |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 76 | TINT_ASSERT(sem != nullptr); |
James Price | 5853205 | 2023-01-25 01:24:46 +0000 | [diff] [blame] | 77 | auto severity = Switch( |
| 78 | sem, // |
Ben Clayton | 3fb9a3f | 2023-02-04 21:20:26 +0000 | [diff] [blame] | 79 | [&](const sem::ValueExpression* expr) { return check_stmt(expr->Stmt()); }, |
James Price | 5853205 | 2023-01-25 01:24:46 +0000 | [diff] [blame] | 80 | [&](const sem::Statement* stmt) { return check_stmt(stmt); }, |
| 81 | [&](const sem::Function* func) { return check_func(func); }, |
| 82 | [&](Default) { |
| 83 | // Use the global severity set on the module. |
| 84 | return check(module_); |
| 85 | }); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 86 | TINT_ASSERT(severity != core::DiagnosticSeverity::kUndefined); |
James Price | 5853205 | 2023-01-25 01:24:46 +0000 | [diff] [blame] | 87 | return severity; |
| 88 | } |
| 89 | |
dan sinclair | c990b3c | 2022-04-07 16:04:35 +0000 | [diff] [blame] | 90 | } // namespace tint::sem |