util/compiler-warning.spice
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | // Std imports | ||
| 4 | import "std/text/stringstream"; | ||
| 5 | |||
| 6 | // Own imports | ||
| 7 | import "@/reader/code-loc"; | ||
| 8 | |||
| 9 | public type CompilerWarningType enum { | ||
| 10 | UNUSED_FUNCTION, | ||
| 11 | UNUSED_PROCEDURE, | ||
| 12 | UNUSED_METHOD, | ||
| 13 | UNUSED_STRUCT, | ||
| 14 | UNUSED_INTERFACE, | ||
| 15 | UNUSED_UNION, | ||
| 16 | UNUSED_IMPORT, | ||
| 17 | UNUSED_FIELD, | ||
| 18 | UNUSED_ENUM_ITEM, | ||
| 19 | UNUSED_ALIAS, | ||
| 20 | UNUSED_VARIABLE, | ||
| 21 | UNUSED_RETURN_VALUE, | ||
| 22 | UNREACHABLE_CODE, | ||
| 23 | SHADOWED_VARIABLE, | ||
| 24 | IDENTITY_CAST, | ||
| 25 | SINGLE_GENERIC_TYPE_CONDITION, | ||
| 26 | INEFFECTIVE_GENERIC_TYPE_CONDITION, | ||
| 27 | BOOL_ASSIGN_AS_CONDITION, | ||
| 28 | NON_TRIVIAL_TYPE_PASSED_BY_VALUE, | ||
| 29 | ASYNC_LAMBDA_CAPTURE_RULE_VIOLATION, | ||
| 30 | UNINSTALL_FAILED | ||
| 31 | } | ||
| 32 | |||
| 33 | /** | ||
| 34 | * Compiler warning template engine | ||
| 35 | */ | ||
| 36 | 16223 | public type CompilerWarning struct { | |
| 37 | public String warningMessage | ||
| 38 | } | ||
| 39 | |||
| 40 | /** | ||
| 41 | * Constructor: Used in case that the exact code position where the warning occurred is known | ||
| 42 | * | ||
| 43 | * @param codeLoc Code location, where the warning occurred | ||
| 44 | * @param warningType Type of the warning | ||
| 45 | * @param message Warning message suffix | ||
| 46 | */ | ||
| 47 | 5485 | public p CompilerWarning.ctor(const CodeLoc& codeLoc, const CompilerWarningType warningType, const string message) { | |
| 48 | 5485 | StringStream msg; | |
| 49 | 5485 | msg << "[Warning] " << codeLoc.toPrettyString() << ": " << this.getMessagePrefix(warningType) << ": " << message; | |
| 50 | 5485 | this.warningMessage = msg.str(); | |
| 51 | 5485 | } | |
| 52 | |||
| 53 | /** | ||
| 54 | * Constructor: Used in case the exact code position where the warning occurred is not known | ||
| 55 | * | ||
| 56 | * @param warningType Type of the warning | ||
| 57 | * @param message Warning message suffix | ||
| 58 | */ | ||
| 59 | ✗ | public p CompilerWarning.ctor(const CompilerWarningType warningType, const string message) { | |
| 60 | ✗ | StringStream msg; | |
| 61 | ✗ | msg << "[Warning] " << this.getMessagePrefix(warningType) << ": " << message; | |
| 62 | ✗ | this.warningMessage = msg.str(); | |
| 63 | ✗ | } | |
| 64 | |||
| 65 | /** | ||
| 66 | * Print the compiler warning to the standard error output | ||
| 67 | */ | ||
| 68 | 242 | public p CompilerWarning.print() { | |
| 69 | 242 | printf("\033[33m%s\033[0m\n", this.warningMessage); | |
| 70 | 242 | } | |
| 71 | |||
| 72 | /** | ||
| 73 | * Get the prefix of the warning message for a particular error | ||
| 74 | * | ||
| 75 | * @param type Type of the warning | ||
| 76 | * @return Prefix string for the warning type | ||
| 77 | */ | ||
| 78 | 5485 | f<string> CompilerWarning.getMessagePrefix(const CompilerWarningType warningType) { | |
| 79 | 5485 | switch warningType { | |
| 80 | 3 | case CompilerWarningType::UNUSED_FUNCTION: { return "Unused function"; } | |
| 81 | 4 | case CompilerWarningType::UNUSED_PROCEDURE: { return "Unused procedure"; } | |
| 82 | 8 | case CompilerWarningType::UNUSED_METHOD: { return "Unused method"; } | |
| 83 | 1 | case CompilerWarningType::UNUSED_STRUCT: { return "Unused struct"; } | |
| 84 | 1 | case CompilerWarningType::UNUSED_INTERFACE: { return "Unused interface"; } | |
| 85 | 1 | case CompilerWarningType::UNUSED_UNION: { return "Unused union"; } | |
| 86 | 5 | case CompilerWarningType::UNUSED_IMPORT: { return "Unused import"; } | |
| 87 | 59 | case CompilerWarningType::UNUSED_FIELD: { return "Unused field"; } | |
| 88 | 3 | case CompilerWarningType::UNUSED_ENUM_ITEM: { return "Unused enum item"; } | |
| 89 | 1 | case CompilerWarningType::UNUSED_ALIAS: { return "Unused type alias"; } | |
| 90 | 83 | case CompilerWarningType::UNUSED_VARIABLE: { return "Unused variable"; } | |
| 91 | 5175 | case CompilerWarningType::UNUSED_RETURN_VALUE: { return "Unused return value"; } | |
| 92 | 3 | case CompilerWarningType::UNREACHABLE_CODE: { return "Unreachable code detected"; } | |
| 93 | 6 | case CompilerWarningType::SHADOWED_VARIABLE: { return "Shadowed variable"; } | |
| 94 | 19 | case CompilerWarningType::IDENTITY_CAST: { return "Identity cast"; } | |
| 95 | 1 | case CompilerWarningType::SINGLE_GENERIC_TYPE_CONDITION: { return "Only one type condition"; } | |
| 96 | 2 | case CompilerWarningType::INEFFECTIVE_GENERIC_TYPE_CONDITION: { return "Ineffective type condition"; } | |
| 97 | 1 | case CompilerWarningType::BOOL_ASSIGN_AS_CONDITION: { return "Bool assignment as condition"; } | |
| 98 | 101 | case CompilerWarningType::NON_TRIVIAL_TYPE_PASSED_BY_VALUE: { return "Non-trivial type passed by value"; } | |
| 99 | 8 | case CompilerWarningType::ASYNC_LAMBDA_CAPTURE_RULE_VIOLATION: { return "Lambda violates async lambda capture rules"; } | |
| 100 | ✗ | case CompilerWarningType::UNINSTALL_FAILED: { return "Uninstall failed"; } | |
| 101 | − | default: { panic(Error("Unknown warning")); } // LCOV_EXCL_LINE | |
| 102 | } | ||
| 103 | ✗ | } | |
| 104 |