|
| 1 | +/** |
| 2 | + * |
| 3 | + * @file NoTypeNamedRule.cpp |
| 4 | + * @author Gaspard Kirira |
| 5 | + * |
| 6 | + * Copyright 2025, Gaspard Kirira. All rights reserved. |
| 7 | + * https://github.com/vixcpp/vix |
| 8 | + * Use of this source code is governed by a MIT license |
| 9 | + * that can be found in the License file. |
| 10 | + * |
| 11 | + * Vix.cpp |
| 12 | + * |
| 13 | + */ |
| 14 | +#include <vix/cli/errors/template/ITemplateErrorRule.hpp> |
| 15 | +#include <vix/cli/errors/CodeFrame.hpp> |
| 16 | + |
| 17 | +#include <filesystem> |
| 18 | +#include <iostream> |
| 19 | +#include <memory> |
| 20 | +#include <string> |
| 21 | + |
| 22 | +#include <vix/cli/Style.hpp> |
| 23 | + |
| 24 | +using namespace vix::cli::style; |
| 25 | + |
| 26 | +namespace vix::cli::errors::template_rules |
| 27 | +{ |
| 28 | + namespace |
| 29 | + { |
| 30 | + bool icontains(const std::string &text, const std::string &needle) |
| 31 | + { |
| 32 | + if (needle.empty()) |
| 33 | + return true; |
| 34 | + |
| 35 | + auto lower = [](unsigned char c) -> char |
| 36 | + { |
| 37 | + if (c >= 'A' && c <= 'Z') |
| 38 | + return static_cast<char>(c + ('a' - 'A')); |
| 39 | + return static_cast<char>(c); |
| 40 | + }; |
| 41 | + |
| 42 | + if (text.size() < needle.size()) |
| 43 | + return false; |
| 44 | + |
| 45 | + for (std::size_t i = 0; i + needle.size() <= text.size(); ++i) |
| 46 | + { |
| 47 | + bool ok = true; |
| 48 | + |
| 49 | + for (std::size_t j = 0; j < needle.size(); ++j) |
| 50 | + { |
| 51 | + if (lower(static_cast<unsigned char>(text[i + j])) != |
| 52 | + lower(static_cast<unsigned char>(needle[j]))) |
| 53 | + { |
| 54 | + ok = false; |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (ok) |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + return false; |
| 64 | + } |
| 65 | + } // namespace |
| 66 | + |
| 67 | + class NoTypeNamedRule final : public ITemplateErrorRule |
| 68 | + { |
| 69 | + public: |
| 70 | + bool match(const vix::cli::errors::CompilerError &err) const override |
| 71 | + { |
| 72 | + const std::string &m = err.message; |
| 73 | + |
| 74 | + return (icontains(m, "no type named") && icontains(m, "in")) || |
| 75 | + (icontains(m, "no type named") && icontains(m, "value_type")) || |
| 76 | + (icontains(m, "has no member named") && icontains(m, "type")); |
| 77 | + } |
| 78 | + |
| 79 | + bool handle( |
| 80 | + const vix::cli::errors::CompilerError &err, |
| 81 | + const vix::cli::errors::ErrorContext &ctx) const override |
| 82 | + { |
| 83 | + std::filesystem::path filePath(err.file); |
| 84 | + const std::string fileName = filePath.filename().string(); |
| 85 | + |
| 86 | + std::cerr << RED |
| 87 | + << "error: type alias or nested type not found" |
| 88 | + << RESET << "\n"; |
| 89 | + |
| 90 | + printCodeFrame(err, ctx); |
| 91 | + |
| 92 | + std::cerr << YELLOW |
| 93 | + << "hint: the template argument does not provide the nested type you are trying to use" |
| 94 | + << RESET << "\n"; |
| 95 | + |
| 96 | + std::cerr << YELLOW |
| 97 | + << "hint: check names like T::value_type, T::iterator, or U::type and make sure the chosen type actually defines them" |
| 98 | + << RESET << "\n"; |
| 99 | + |
| 100 | + std::cerr << GREEN |
| 101 | + << "at: " << fileName << ":" << err.line << ":" << err.column |
| 102 | + << RESET << "\n"; |
| 103 | + |
| 104 | + return true; |
| 105 | + } |
| 106 | + }; |
| 107 | + |
| 108 | + std::unique_ptr<ITemplateErrorRule> makeNoTypeNamedRule() |
| 109 | + { |
| 110 | + return std::make_unique<NoTypeNamedRule>(); |
| 111 | + } |
| 112 | +} // namespace vix::cli::errors::template_rules |
0 commit comments