src/util/CompilerWarning.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "CompilerWarning.h" | ||
| 4 | |||
| 5 | #include <util/CodeLoc.h> | ||
| 6 | |||
| 7 | namespace spice::compiler { | ||
| 8 | |||
| 9 | /** | ||
| 10 | * Constructor: Used in case that the exact code position where the warning occurred is known | ||
| 11 | * | ||
| 12 | * @param codeLoc Code location, where the warning occurred | ||
| 13 | * @param type Type of the warning | ||
| 14 | * @param message Warning message suffix | ||
| 15 | */ | ||
| 16 | 9524 | CompilerWarning::CompilerWarning(const CodeLoc &codeLoc, const CompilerWarningType type, const std::string &message) { | |
| 17 |
7/14✓ Branch 3 → 4 taken 9524 times.
✗ Branch 3 → 34 not taken.
✓ Branch 4 → 5 taken 9524 times.
✗ Branch 4 → 29 not taken.
✓ Branch 5 → 6 taken 9524 times.
✗ Branch 5 → 27 not taken.
✓ Branch 6 → 7 taken 9524 times.
✗ Branch 6 → 25 not taken.
✓ Branch 7 → 8 taken 9524 times.
✗ Branch 7 → 23 not taken.
✓ Branch 8 → 9 taken 9524 times.
✗ Branch 8 → 21 not taken.
✓ Branch 9 → 10 taken 9524 times.
✗ Branch 9 → 19 not taken.
|
9524 | warningMessage = "[Warning] " + codeLoc.toPrettyString() + ": " + getMessagePrefix(type) + ": " + message; |
| 18 | 9524 | } | |
| 19 | |||
| 20 | /** | ||
| 21 | * Constructor: Used in case the exact code position where the warning occurred is not known | ||
| 22 | * | ||
| 23 | * @param type Type of the warning | ||
| 24 | * @param message Warning message suffix | ||
| 25 | */ | ||
| 26 | ✗ | CompilerWarning::CompilerWarning(CompilerWarningType type, const std::string &message) { | |
| 27 | ✗ | warningMessage = "[Warning] " + getMessagePrefix(type) + ": " + message; | |
| 28 | ✗ | } | |
| 29 | |||
| 30 | /** | ||
| 31 | * Print the compiler warning to the standard error output | ||
| 32 | */ | ||
| 33 | 503 | void CompilerWarning::print() const { std::cout << "\033[33m" << warningMessage << "\033[0m\n"; } | |
| 34 | |||
| 35 | /** | ||
| 36 | * Get the prefix of the warning message for a particular error | ||
| 37 | * | ||
| 38 | * @param warningType Type of the warning | ||
| 39 | * @return Prefix string for the warning type | ||
| 40 | */ | ||
| 41 | 9524 | std::string CompilerWarning::getMessagePrefix(CompilerWarningType warningType) { | |
| 42 |
20/23✓ Branch 2 → 3 taken 10 times.
✓ Branch 2 → 9 taken 6 times.
✓ Branch 2 → 15 taken 20 times.
✓ Branch 2 → 21 taken 2 times.
✓ Branch 2 → 27 taken 2 times.
✓ Branch 2 → 33 taken 2 times.
✓ Branch 2 → 39 taken 8 times.
✓ Branch 2 → 45 taken 90 times.
✓ Branch 2 → 51 taken 8 times.
✓ Branch 2 → 57 taken 2 times.
✓ Branch 2 → 63 taken 223 times.
✓ Branch 2 → 69 taken 7110 times.
✓ Branch 2 → 75 taken 6 times.
✓ Branch 2 → 81 taken 10 times.
✓ Branch 2 → 87 taken 1899 times.
✓ Branch 2 → 93 taken 2 times.
✓ Branch 2 → 99 taken 4 times.
✓ Branch 2 → 105 taken 2 times.
✓ Branch 2 → 111 taken 106 times.
✓ Branch 2 → 117 taken 12 times.
✗ Branch 2 → 123 not taken.
✗ Branch 2 → 129 not taken.
✗ Branch 2 → 135 not taken.
|
9524 | switch (warningType) { |
| 43 | 10 | case UNUSED_FUNCTION: | |
| 44 |
1/2✓ Branch 5 → 6 taken 10 times.
✗ Branch 5 → 137 not taken.
|
20 | return "Unused function"; |
| 45 | 6 | case UNUSED_PROCEDURE: | |
| 46 |
1/2✓ Branch 11 → 12 taken 6 times.
✗ Branch 11 → 140 not taken.
|
12 | return "Unused procedure"; |
| 47 | 20 | case UNUSED_METHOD: | |
| 48 |
1/2✓ Branch 17 → 18 taken 20 times.
✗ Branch 17 → 143 not taken.
|
40 | return "Unused method"; |
| 49 | 2 | case UNUSED_STRUCT: | |
| 50 |
1/2✓ Branch 23 → 24 taken 2 times.
✗ Branch 23 → 146 not taken.
|
4 | return "Unused struct"; |
| 51 | 2 | case UNUSED_INTERFACE: | |
| 52 |
1/2✓ Branch 29 → 30 taken 2 times.
✗ Branch 29 → 149 not taken.
|
4 | return "Unused interface"; |
| 53 | 2 | case UNUSED_UNION: | |
| 54 |
1/2✓ Branch 35 → 36 taken 2 times.
✗ Branch 35 → 152 not taken.
|
4 | return "Unused union"; |
| 55 | 8 | case UNUSED_IMPORT: | |
| 56 |
1/2✓ Branch 41 → 42 taken 8 times.
✗ Branch 41 → 155 not taken.
|
16 | return "Unused import"; |
| 57 | 90 | case UNUSED_FIELD: | |
| 58 |
1/2✓ Branch 47 → 48 taken 90 times.
✗ Branch 47 → 158 not taken.
|
180 | return "Unused field"; |
| 59 | 8 | case UNUSED_ENUM_ITEM: | |
| 60 |
1/2✓ Branch 53 → 54 taken 8 times.
✗ Branch 53 → 161 not taken.
|
16 | return "Unused enum item"; |
| 61 | 2 | case UNUSED_ALIAS: | |
| 62 |
1/2✓ Branch 59 → 60 taken 2 times.
✗ Branch 59 → 164 not taken.
|
4 | return "Unused type alias"; |
| 63 | 223 | case UNUSED_VARIABLE: | |
| 64 |
1/2✓ Branch 65 → 66 taken 223 times.
✗ Branch 65 → 167 not taken.
|
446 | return "Unused variable"; |
| 65 | 7110 | case UNUSED_RETURN_VALUE: | |
| 66 |
1/2✓ Branch 71 → 72 taken 7110 times.
✗ Branch 71 → 170 not taken.
|
14220 | return "Unused return value"; |
| 67 | 6 | case UNREACHABLE_CODE: | |
| 68 |
1/2✓ Branch 77 → 78 taken 6 times.
✗ Branch 77 → 173 not taken.
|
12 | return "Unreachable code detected"; |
| 69 | 10 | case SHADOWED_VARIABLE: | |
| 70 |
1/2✓ Branch 83 → 84 taken 10 times.
✗ Branch 83 → 176 not taken.
|
20 | return "Shadowed variable"; |
| 71 | 1899 | case IDENTITY_CAST: | |
| 72 |
1/2✓ Branch 89 → 90 taken 1899 times.
✗ Branch 89 → 179 not taken.
|
3798 | return "Identity cast"; |
| 73 | 2 | case SINGLE_GENERIC_TYPE_CONDITION: | |
| 74 |
1/2✓ Branch 95 → 96 taken 2 times.
✗ Branch 95 → 182 not taken.
|
4 | return "Only one type condition"; |
| 75 | 4 | case INEFFECTIVE_GENERIC_TYPE_CONDITION: | |
| 76 |
1/2✓ Branch 101 → 102 taken 4 times.
✗ Branch 101 → 185 not taken.
|
8 | return "Ineffective type condition"; |
| 77 | 2 | case BOOL_ASSIGN_AS_CONDITION: | |
| 78 |
1/2✓ Branch 107 → 108 taken 2 times.
✗ Branch 107 → 188 not taken.
|
4 | return "Bool assignment as condition"; |
| 79 | 106 | case NON_TRIVIAL_TYPE_PASSED_BY_VALUE: | |
| 80 |
1/2✓ Branch 113 → 114 taken 106 times.
✗ Branch 113 → 191 not taken.
|
212 | return "Non-trivial type passed by value"; |
| 81 | 12 | case ASYNC_LAMBDA_CAPTURE_RULE_VIOLATION: | |
| 82 |
1/2✓ Branch 119 → 120 taken 12 times.
✗ Branch 119 → 194 not taken.
|
24 | return "Lambda violates async lambda capture rules"; |
| 83 | ✗ | case UNINSTALL_FAILED: | |
| 84 | ✗ | return "Uninstall failed"; | |
| 85 | ✗ | case VERIFIER_DISABLED: | |
| 86 | ✗ | return "Verifier disabled"; | |
| 87 | } | ||
| 88 | − | assert_fail("Unknown warning"); // GCOV_EXCL_LINE | |
| 89 | return "Unknown warning"; // GCOV_EXCL_LINE | ||
| 90 | } | ||
| 91 | |||
| 92 | } // namespace spice::compiler | ||
| 93 |