-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUseOfUninitializedRule.cpp
More file actions
100 lines (84 loc) · 2.42 KB
/
Copy pathUseOfUninitializedRule.cpp
File metadata and controls
100 lines (84 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
*
* @file UseOfUninitializedRule.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
*/
#include <vix/cli/errors/IErrorRule.hpp>
#include <vix/cli/errors/CodeFrame.hpp>
#include <algorithm>
#include <cctype>
#include <iostream>
#include <memory>
#include <string>
#include <vix/cli/Style.hpp>
using namespace vix::cli::style;
namespace vix::cli::errors
{
namespace
{
std::string to_lower_ascii(std::string text)
{
std::transform(
text.begin(),
text.end(),
text.begin(),
[](unsigned char c)
{
return static_cast<char>(std::tolower(c));
});
return text;
}
} // namespace
class UseOfUninitializedRule final : public IErrorRule
{
public:
bool match(const CompilerError &err) const override
{
const std::string message = to_lower_ascii(err.message);
const bool mentionsUninitialized =
message.find("uninitialized") != std::string::npos;
if (!mentionsUninitialized)
return false;
const bool strongPhrase =
message.find("may be used") != std::string::npos ||
message.find("is used") != std::string::npos ||
message.find("use of uninitialized") != std::string::npos ||
message.find("uninitialized use") != std::string::npos ||
message.find("used uninitialized") != std::string::npos ||
message.find("maybe-uninitialized") != std::string::npos;
return strongPhrase;
}
bool handle(
const CompilerError &err,
const ErrorContext &ctx) const override
{
std::cerr << RED
<< "error: uninitialized value"
<< RESET << "\n";
printCodeFrame(err, ctx);
std::cerr << YELLOW
<< "hint: "
<< RESET
<< "initialize the variable before reading or passing it"
<< "\n";
std::cerr << GREEN
<< "at: "
<< RESET
<< err.file << ":" << err.line << ":" << err.column
<< "\n";
return true;
}
};
std::unique_ptr<IErrorRule> makeUseOfUninitializedRule()
{
return std::make_unique<UseOfUninitializedRule>();
}
} // namespace vix::cli::errors