| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "sema/errors.h" | ||
| 2 | #include "ast/magic.h" | ||
| 3 | #include "ast/ops.h" | ||
| 4 | #include "lexer/lexer.h" | ||
| 5 | #include "parser/parsing_error.h" | ||
| 6 | #include "utilities/names.h" | ||
| 7 | #include "utilities/strings.h" | ||
| 8 | |||
| 9 | namespace lython { | ||
| 10 | |||
| 11 | std::string TypeError::message(String const& lhs_v, | ||
| 12 | String const& lhs_t, | ||
| 13 | String const& rhs_v, | ||
| 14 | String const& rhs_t) { | ||
| 15 |
3/5✓ Branch 1 taken 42 times.
✓ Branch 3 taken 42 times.
✓ Branch 4 taken 42 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
84 | Array<String> msg = {"TypeError: "}; |
| 16 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | if (!lhs_v.empty()) { |
| 17 |
2/2✓ Branch 2 taken 42 times.
✓ Branch 5 taken 42 times.
|
42 | msg.push_back("expression `"); |
| 18 |
1/1✓ Branch 1 taken 42 times.
|
42 | msg.push_back((lhs_v)); |
| 19 |
2/2✓ Branch 2 taken 42 times.
✓ Branch 5 taken 42 times.
|
42 | msg.push_back("` "); |
| 20 | } | ||
| 21 |
5/6✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 19 times.
✓ Branch 5 taken 23 times.
✓ Branch 6 taken 19 times.
✓ Branch 7 taken 23 times.
|
42 | if (!lhs_v.empty() && !lhs_t.empty()) { |
| 22 |
2/2✓ Branch 2 taken 19 times.
✓ Branch 5 taken 19 times.
|
19 | msg.push_back("of "); |
| 23 | } | ||
| 24 |
2/2✓ Branch 1 taken 19 times.
✓ Branch 2 taken 23 times.
|
42 | if (!lhs_t.empty()) { |
| 25 |
2/2✓ Branch 2 taken 19 times.
✓ Branch 5 taken 19 times.
|
19 | msg.push_back("type `"); |
| 26 |
1/1✓ Branch 1 taken 19 times.
|
19 | msg.push_back((lhs_t)); |
| 27 |
2/2✓ Branch 2 taken 19 times.
✓ Branch 5 taken 19 times.
|
19 | msg.push_back("` "); |
| 28 | } | ||
| 29 | |||
| 30 |
2/2✓ Branch 2 taken 42 times.
✓ Branch 5 taken 42 times.
|
42 | msg.push_back("is not compatible with "); |
| 31 |
2/2✓ Branch 1 taken 22 times.
✓ Branch 2 taken 20 times.
|
42 | if (!rhs_v.empty()) { |
| 32 |
2/2✓ Branch 2 taken 22 times.
✓ Branch 5 taken 22 times.
|
22 | msg.push_back("expression `"); |
| 33 |
1/1✓ Branch 1 taken 22 times.
|
22 | msg.push_back((rhs_v)); |
| 34 |
2/2✓ Branch 2 taken 22 times.
✓ Branch 5 taken 22 times.
|
22 | msg.push_back("` "); |
| 35 | } | ||
| 36 |
5/6✓ Branch 1 taken 22 times.
✓ Branch 2 taken 20 times.
✓ Branch 4 taken 22 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 22 times.
✓ Branch 7 taken 20 times.
|
42 | if (!rhs_v.empty() && !rhs_t.empty()) { |
| 37 |
2/2✓ Branch 2 taken 22 times.
✓ Branch 5 taken 22 times.
|
22 | msg.push_back("of "); |
| 38 | } | ||
| 39 |
1/2✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
|
42 | if (!rhs_t.empty()) { |
| 40 |
2/2✓ Branch 2 taken 42 times.
✓ Branch 5 taken 42 times.
|
42 | msg.push_back("type `"); |
| 41 |
1/1✓ Branch 1 taken 42 times.
|
42 | msg.push_back((rhs_t)); |
| 42 |
2/2✓ Branch 2 taken 42 times.
✓ Branch 5 taken 42 times.
|
42 | msg.push_back("`"); |
| 43 | } | ||
| 44 |
3/3✓ Branch 3 taken 42 times.
✓ Branch 6 taken 42 times.
✓ Branch 9 taken 42 times.
|
126 | return std::string(join("", msg)); |
| 45 | 126 | } | |
| 46 | std::string TypeError::message() const { | ||
| 47 | auto _str = [](ExprNode* _expr) { | ||
| 48 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 60 times.
|
84 | if (_expr == nullptr) |
| 49 | 24 | return String(); | |
| 50 | 60 | return str(_expr); | |
| 51 | }; | ||
| 52 |
5/5✓ Branch 1 taken 21 times.
✓ Branch 4 taken 21 times.
✓ Branch 7 taken 21 times.
✓ Branch 10 taken 21 times.
✓ Branch 13 taken 21 times.
|
42 | return message(_str(lhs_v), _str(lhs_t), _str(rhs_v), _str(rhs_t)); |
| 53 | } | ||
| 54 | |||
| 55 | std::string NameError::message() const { | ||
| 56 |
2/2✓ Branch 1 taken 668 times.
✓ Branch 4 taken 668 times.
|
1336 | return fmt::format("NameError: name '{}' is not defined", str(name)); |
| 57 | } | ||
| 58 | |||
| 59 | std::string AttributeError::message() const { return message(str(obj->name), str(attr)); } | ||
| 60 | |||
| 61 | std::string AttributeError::message(String const& name, String const& attr) { | ||
| 62 |
1/1✓ Branch 1 taken 4 times.
|
8 | return fmt::format("AttributeError: '{}' has no attribute '{}'", name, attr); |
| 63 | } | ||
| 64 | |||
| 65 | std::string UnsupportedOperand::message() const { return message(operand, str(lhs_t), str(rhs_t)); } | ||
| 66 | |||
| 67 | std::string | ||
| 68 | UnsupportedOperand::message(String const& operand, String const& lhs_t, String const& rhs_t) { | ||
| 69 | return fmt::format( | ||
| 70 |
1/1✓ Branch 1 taken 99 times.
|
198 | "TypeError: unsupported operand type(s) for {}: '{}' and '{}'", operand, lhs_t, rhs_t); |
| 71 | } | ||
| 72 | |||
| 73 | std::string ModuleNotFoundError::message() const { return message(str(module)); } | ||
| 74 | |||
| 75 | std::string ModuleNotFoundError::message(String const& module) { | ||
| 76 |
1/1✓ Branch 1 taken 20 times.
|
40 | return fmt::format("ModuleNotFoundError: No module named '{}'", module); |
| 77 | } | ||
| 78 | |||
| 79 | std::string ImportError::message() const { return message(str(module), str(name)); } | ||
| 80 | |||
| 81 | std::string ImportError::message(String const& module, String const& name) { | ||
| 82 | ✗ | return fmt::format("ImportError: cannot import name {} from '{}'", name, module); | |
| 83 | } | ||
| 84 | |||
| 85 | std::string RecursiveDefinition::message() const { return message(fun, cls); } | ||
| 86 | |||
| 87 | std::string RecursiveDefinition::message(ExprNode const* fun, ClassDef const* cls) { | ||
| 88 | ✗ | return "RecursiveDefinition: "; | |
| 89 | } | ||
| 90 | |||
| 91 | String get_parent(SemaException const& error) { | ||
| 92 |
2/2✓ Branch 0 taken 117 times.
✓ Branch 1 taken 19 times.
|
136 | if (error.stmt != nullptr) { |
| 93 | 117 | return shortprint(get_parent(error.stmt)); | |
| 94 | } | ||
| 95 |
1/1✓ Branch 2 taken 19 times.
|
19 | return "<module>"; |
| 96 | } | ||
| 97 | |||
| 98 | void SemaErrorPrinter::print(SemaException const& err) { | ||
| 99 |
1/1✓ Branch 1 taken 136 times.
|
136 | String filename = get_filename(); |
| 100 | 136 | Node* node = err.expr; | |
| 101 |
1/1✓ Branch 1 taken 136 times.
|
136 | String parent = get_parent(err); |
| 102 | |||
| 103 | 136 | int line = 0; | |
| 104 | 136 | bool written = false; | |
| 105 | |||
| 106 |
2/2✓ Branch 0 taken 117 times.
✓ Branch 1 taken 19 times.
|
136 | if (err.stmt != nullptr) { |
| 107 | 117 | line = err.stmt->lineno; | |
| 108 | } | ||
| 109 | |||
| 110 |
7/7✓ Branch 1 taken 136 times.
✓ Branch 4 taken 136 times.
✓ Branch 7 taken 136 times.
✓ Branch 10 taken 136 times.
✓ Branch 13 taken 136 times.
✓ Branch 16 taken 136 times.
✓ Branch 19 taken 136 times.
|
136 | firstline() << "File \"" << filename << "\", line " << line << ", in " << parent; |
| 111 | |||
| 112 | { | ||
| 113 |
1/1✓ Branch 1 taken 136 times.
|
136 | auto noline_buf = NoNewLine(out); |
| 114 |
1/1✓ Branch 1 taken 136 times.
|
136 | std::ostream noline(&noline_buf); |
| 115 |
1/1✓ Branch 1 taken 136 times.
|
136 | codeline(); |
| 116 |
2/2✓ Branch 0 taken 117 times.
✓ Branch 1 taken 19 times.
|
136 | if (err.stmt != nullptr) { |
| 117 |
2/2✓ Branch 1 taken 117 times.
✓ Branch 4 taken 117 times.
|
117 | noline << str(err.stmt); |
| 118 | 117 | written = true; | |
| 119 | } else { | ||
| 120 |
2/2✓ Branch 1 taken 19 times.
✓ Branch 4 taken 19 times.
|
19 | noline << str(node); |
| 121 | } | ||
| 122 |
1/1✓ Branch 1 taken 136 times.
|
136 | noline.flush(); |
| 123 | 136 | } | |
| 124 | |||
| 125 |
4/4✓ Branch 0 taken 117 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 111 times.
✓ Branch 3 taken 6 times.
|
136 | if (written && err.expr != nullptr) { |
| 126 |
1/1✓ Branch 1 taken 111 times.
|
111 | underline(*err.expr); |
| 127 | } | ||
| 128 | |||
| 129 |
2/2✓ Branch 1 taken 136 times.
✓ Branch 5 taken 136 times.
|
136 | errorline() << err.what(); |
| 130 |
1/1✓ Branch 1 taken 136 times.
|
136 | end(); |
| 131 | 136 | } | |
| 132 | |||
| 133 | } // namespace lython | ||
| 134 |