GCC Code Coverage Report


Directory: ../
File: src/exception/SemanticError.cpp
Date: 2024-11-22 23:10:59
Exec Total Coverage
Lines: 150 216 69.4%
Functions: 3 3 100.0%
Branches: 151 331 45.6%

Line Branch Exec Source
1 // Copyright (c) 2021-2024 ChilliBits. All rights reserved.
2
3 #include "SemanticError.h"
4
5 #include <ast/ASTNodes.h>
6 #include <util/CodeLoc.h>
7
8 namespace spice::compiler {
9
10 187 SemanticError::SemanticError(const ASTNode *node, const SemanticErrorType &type, const std::string &msg, bool printErrorMessage) {
11
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 187 times.
187 assert(node != nullptr);
12
3/6
✓ Branch 1 taken 187 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 187 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 187 times.
✗ Branch 8 not taken.
187 errorMessage = "[Error|Semantic] " + node->codeLoc.toPrettyString() + ":\n";
13
4/8
✓ Branch 1 taken 187 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 187 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 187 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 187 times.
✗ Branch 11 not taken.
187 errorMessage += getMessagePrefix(type) + ": " + msg;
14
2/2
✓ Branch 0 taken 185 times.
✓ Branch 1 taken 2 times.
187 if (printErrorMessage) {
15
1/2
✓ Branch 1 taken 185 times.
✗ Branch 2 not taken.
185 const std::string nodeErrorMessage = node->getErrorMessage();
16
2/4
✓ Branch 1 taken 185 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 185 times.
✗ Branch 5 not taken.
185 errorMessage += "\n\n" + nodeErrorMessage;
17 185 }
18 187 }
19
20 /**
21 * Get the message for this particular error instance
22 *
23 * @return Error message in form of a char array
24 */
25 187 const char *SemanticError::what() const noexcept { return errorMessage.c_str(); }
26
27 /**
28 * Get the prefix of the error message for a particular error
29 *
30 * @param errorType Type of the error
31 * @return Prefix string for the error type
32 */
33 187 std::string SemanticError::getMessagePrefix(SemanticErrorType errorType) {
34
69/103
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 7 times.
✓ Branch 10 taken 3 times.
✓ Branch 11 taken 1 times.
✓ Branch 12 taken 2 times.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 2 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 21 taken 6 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✓ Branch 25 taken 1 times.
✓ Branch 26 taken 1 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 1 times.
✓ Branch 29 taken 2 times.
✓ Branch 30 taken 6 times.
✓ Branch 31 taken 1 times.
✓ Branch 32 taken 2 times.
✓ Branch 33 taken 1 times.
✓ Branch 34 taken 1 times.
✓ Branch 35 taken 27 times.
✓ Branch 36 taken 2 times.
✓ Branch 37 taken 5 times.
✓ Branch 38 taken 2 times.
✓ Branch 39 taken 5 times.
✓ Branch 40 taken 1 times.
✓ Branch 41 taken 2 times.
✓ Branch 42 taken 2 times.
✓ Branch 43 taken 3 times.
✓ Branch 44 taken 2 times.
✓ Branch 45 taken 2 times.
✓ Branch 46 taken 2 times.
✓ Branch 47 taken 2 times.
✓ Branch 48 taken 6 times.
✓ Branch 49 taken 2 times.
✓ Branch 50 taken 2 times.
✓ Branch 51 taken 2 times.
✓ Branch 52 taken 1 times.
✓ Branch 53 taken 1 times.
✓ Branch 54 taken 2 times.
✗ Branch 55 not taken.
✓ Branch 56 taken 4 times.
✓ Branch 57 taken 1 times.
✓ Branch 58 taken 1 times.
✓ Branch 59 taken 1 times.
✓ Branch 60 taken 1 times.
✓ Branch 61 taken 1 times.
✓ Branch 62 taken 2 times.
✓ Branch 63 taken 1 times.
✓ Branch 64 taken 1 times.
✓ Branch 65 taken 1 times.
✓ Branch 66 taken 1 times.
✗ Branch 67 not taken.
✗ Branch 68 not taken.
✗ Branch 69 not taken.
✗ Branch 70 not taken.
✓ Branch 71 taken 2 times.
✓ Branch 72 taken 2 times.
✓ Branch 73 taken 1 times.
✓ Branch 74 taken 2 times.
✗ Branch 75 not taken.
✗ Branch 76 not taken.
✗ Branch 77 not taken.
✓ Branch 78 taken 1 times.
✓ Branch 79 taken 1 times.
✓ Branch 80 taken 2 times.
✓ Branch 81 taken 8 times.
✓ Branch 82 taken 2 times.
✓ Branch 83 taken 4 times.
✗ Branch 84 not taken.
✓ Branch 85 taken 3 times.
✗ Branch 86 not taken.
✗ Branch 87 not taken.
✗ Branch 88 not taken.
✗ Branch 89 not taken.
✗ Branch 90 not taken.
✗ Branch 91 not taken.
✗ Branch 92 not taken.
✗ Branch 93 not taken.
✓ Branch 94 taken 1 times.
✓ Branch 95 taken 1 times.
✓ Branch 96 taken 1 times.
✓ Branch 97 taken 1 times.
✗ Branch 98 not taken.
✓ Branch 99 taken 1 times.
✓ Branch 100 taken 1 times.
✗ Branch 101 not taken.
✗ Branch 102 not taken.
187 switch (errorType) {
35 15 case REFERENCED_UNDEFINED_FUNCTION:
36
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
30 return "Referenced undefined function";
37 6 case REFERENCED_UNDEFINED_VARIABLE:
38
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
12 return "Referenced undefined variable";
39 3 case REFERENCED_UNDEFINED_STRUCT:
40
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 return "Referenced undefined struct";
41 case REFERENCED_UNDEFINED_INTERFACE:
42 return "Referenced undefined interface";
43 case REFERENCED_UNDEFINED_FIELD:
44 return "Referenced undefined struct field";
45 2 case USED_BEFORE_DECLARED:
46
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Used before declared";
47 1 case FUNCTION_AMBIGUITY:
48
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Function ambiguity";
49 case STRUCT_AMBIGUITY:
50 return "Struct ambiguity";
51 case INTERFACE_AMBIGUITY:
52 return "Interface ambiguity";
53 7 case DUPLICATE_SYMBOL:
54
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
14 return "Duplicate symbol";
55 3 case VARIABLE_DECLARED_TWICE:
56
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 return "Multiple declarations of the same variable";
57 1 case GLOBAL_DECLARED_TWICE:
58
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Multiple declarations of the same global variable";
59 2 case FUNCTION_DECLARED_TWICE:
60
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Multiple declarations of a function";
61 1 case PROCEDURE_DECLARED_TWICE:
62
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Multiple declarations of a procedure";
63 case GENERIC_TYPE_DECLARED_TWICE:
64 return "Multiple declarations of a generic type with the same name";
65 case STRUCT_WITH_ILLEGAL_NAME:
66 return "Struct name illegal";
67 case STRUCT_INFINITE_SIZE:
68 return "Struct with infinite size";
69 case STRUCT_DECLARED_TWICE:
70 return "Multiple declarations of a struct with the same name";
71 2 case MISSING_NO_ARGS_CTOR:
72
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "No matching ctor found";
73 case COPY_CTOR_REQUIRED:
74 return "Missing mandatory copy ctor";
75 case INTERFACE_DECLARED_TWICE:
76 return "Multiple declarations of an interface with the same name";
77 6 case INTERFACE_METHOD_NOT_IMPLEMENTED:
78
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
12 return "Interface method not implemented";
79 case ENUM_DECLARED_TWICE:
80 return "Multiple declarations of an enum with the same name";
81 case INVALID_SYMBOL_ACCESS:
82 return "Invalid symbol access";
83 case DUPLICATE_ENUM_ITEM_NAME:
84 return "Duplicate enum item name";
85 1 case DUPLICATE_ENUM_ITEM_VALUE:
86
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Duplicate enum item value";
87 1 case GLOBAL_OF_TYPE_DYN:
88
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Global of type dyn";
89 case GLOBAL_OF_INVALID_TYPE:
90 return "Global of invalid type";
91 1 case GLOBAL_CONST_WITHOUT_VALUE:
92
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Global const without value";
93 2 case MISSING_RETURN_STMT:
94
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Missing return statement";
95 6 case INVALID_PARAM_ORDER:
96
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
12 return "Invalid argument order";
97 1 case LAMBDA_WITH_OPTIONAL_PARAMS:
98
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Lambda with optional parameters";
99 2 case REFERENCED_OVERLOADED_FCT:
100
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Referenced overloaded function";
101 1 case DTOR_MUST_BE_PROCEDURE:
102
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Destructor must be a procedure";
103 1 case DTOR_WITH_PARAMS:
104
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Destructors must not have parameters";
105 27 case OPERATOR_WRONG_DATA_TYPE:
106
1/2
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
54 return "Wrong data type for operator";
107 2 case INVALID_ITERATOR:
108
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Invalid iterator";
109 5 case UNEXPECTED_DYN_TYPE:
110
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
10 return "Unexpected dyn type";
111 2 case REASSIGN_CONST_VARIABLE:
112
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Cannot re-assign constant variable";
113 5 case CONDITION_MUST_BE_BOOL:
114
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
10 return "Condition must be bool";
115 1 case SWITCH_EXPR_MUST_BE_PRIMITIVE:
116
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Switch expression of wrong type";
117 2 case SWITCH_CASE_TYPE_MISMATCH:
118
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Case constant type does not match switch expression type";
119 2 case MISSING_MAIN_FUNCTION:
120
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Spice programs must contain a main function";
121 3 case FCT_PARAM_IS_TYPE_DYN:
122
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 return "Parameter type dyn not valid in function/procedure definition without default value";
123 2 case INVALID_BREAK_NUMBER:
124
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Invalid number of break calls";
125 2 case INVALID_CONTINUE_NUMBER:
126
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Invalid number of continue calls";
127 2 case FALLTHROUGH_NOT_ALLOWED:
128
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Fallthrough statement not allowed here";
129 2 case CASE_CONSTANT_NOT_ENUM:
130
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Case constant not of enum type";
131 6 case PRINTF_TYPE_ERROR:
132
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
12 return "Types of printf call not matching";
133 2 case PRINTF_ARG_COUNT_ERROR:
134
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Printf arg number not matching template string";
135 2 case DUPLICATE_IMPORT_NAME:
136
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Duplicate import name";
137 2 case IMPORTED_FILE_NOT_EXISTING:
138
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Imported source file not existing";
139 1 case CIRCULAR_DEPENDENCY:
140
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Circular import detected";
141 1 case ACCESS_TO_NON_EXISTING_MEMBER:
142
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Access to non-existing member";
143 2 case INVALID_MEMBER_ACCESS:
144
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Member access is only allowed on structs and Strings";
145 case SCOPE_ACCESS_ONLY_IMPORTS:
146 return "Scope access is only allowed on imports";
147 4 case UNKNOWN_DATATYPE:
148
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
8 return "Unknown datatype";
149 1 case UNKNOWN_ATTR:
150
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Unknown attribute";
151 1 case INVALID_ATTR_TARGET:
152
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Invalid attribute target";
153 1 case MISSING_ATTR_VALUE:
154
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Missing attribute value";
155 1 case NUMBER_OF_FIELDS_NOT_MATCHING:
156
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Number of struct fields not matching declaration";
157 1 case FIELD_TYPE_NOT_MATCHING:
158
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "The type of a field value does not match the declaration";
159 2 case ARRAY_SIZE_INVALID:
160
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Array size invalid";
161 1 case FOREACH_IDX_NOT_LONG:
162
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Foreach index not of type long";
163 1 case ARRAY_INDEX_NOT_INT_OR_LONG:
164
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Array index not of type int or long";
165 1 case ARRAY_ITEM_TYPE_NOT_MATCHING:
166
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Array item type not matching";
167 1 case EXPECTED_ARRAY_TYPE:
168
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Expected array type";
169 case EXPECTED_ERROR_TYPE:
170 return "Expected error type";
171 case INVALID_SYSCALL_NUMBER_TYPE:
172 return "Invalid syscall number type";
173 case SYSCALL_NUMBER_OUT_OF_RANGE:
174 return "Syscall number out of range";
175 case TOO_MANY_SYSCALL_ARGS:
176 return "Too many syscall args";
177 2 case RETURN_WITHOUT_VALUE_RESULT:
178
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Return without initialization of result variable";
179 2 case RETURN_WITH_VALUE_IN_PROCEDURE:
180
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Return with value in procedure";
181 1 case INVALID_STRUCT_INSTANTIATION:
182
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Invalid struct instantiation";
183 2 case DYN_POINTERS_NOT_ALLOWED:
184
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Dyn pointers not allowed";
185 case REF_POINTERS_ARE_NOT_ALLOWED:
186 return "Ref pointers not allowed";
187 case DYN_REFERENCES_NOT_ALLOWED:
188 return "Dyn references not allowed";
189 case MULTI_REF_NOT_ALLOWED:
190 return "Multi references not allowed";
191 1 case DYN_ARRAYS_NOT_ALLOWED:
192
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Dyn arrays not allowed";
193 1 case REFERENCE_WITHOUT_INITIALIZER:
194
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Reference without initializer";
195 2 case TEMP_TO_NON_CONST_REF:
196
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Temporary bound to non-const reference";
197 8 case GENERIC_TYPE_NOT_IN_TEMPLATE:
198
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
16 return "Generic type not contained in template";
199 2 case SPECIFIER_AT_ILLEGAL_CONTEXT:
200
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return "Specifier at illegal context";
201 4 case INSUFFICIENT_VISIBILITY:
202
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
8 return "Insufficient symbol visibility";
203 case JOIN_ARG_MUST_BE_TID:
204 return "Argument of join builtin must be a tid";
205 3 case EXPECTED_GENERIC_TYPE:
206
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 return "Expected a generic type";
207 case EXPECTED_NON_GENERIC_TYPE:
208 return "Expected a non-generic type";
209 case EXPECTED_STRUCT_TYPE:
210 return "Expected a struct type";
211 case EXPECTED_INTERFACE_TYPE:
212 return "Expected an interface type";
213 case ALIAS_WITH_TEMPLATE_LIST:
214 return "Alias with template list";
215 case INTERFACE_WITH_TEMPLATE_LIST:
216 return "Interface reference with template list";
217 case INVALID_TEMPLATE_TYPES:
218 return "Invalid template types";
219 case EXPECTED_VALUE:
220 return "Expected value";
221 case EXPECTED_TYPE:
222 return "Expected type";
223 1 case UNSAFE_OPERATION_IN_SAFE_CONTEXT:
224
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Unsafe operation in safe context";
225 1 case ASSERTION_CONDITION_BOOL:
226
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Assertion condition must be bool";
227 1 case ARRAY_INDEX_OUT_OF_BOUNDS:
228
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Array index out of bounds";
229 1 case EXPECTED_CONST_VARIABLE:
230
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Expected const variable";
231 case DIVISION_BY_ZERO:
232 return "Division by zero";
233 1 case TEST_FUNCTION_WITH_PARAMS:
234
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Test function with parameters";
235 1 case TEST_FUNCTION_WRONG_RETURN_TYPE:
236
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return "Test function with wrong return type";
237 case COMING_SOON_SA:
238 return "Coming soon";
239 }
240 return "Unknown error"; // GCOV_EXCL_LINE
241 }
242
243 } // namespace spice::compiler
244