forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint_rule_registry_test.cc
285 lines (248 loc) · 8.87 KB
/
lint_rule_registry_test.cc
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "verilog/analysis/lint_rule_registry.h"
#include <map>
#include <memory>
#include <utility>
#include "absl/status/status.h"
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "common/analysis/line_lint_rule.h"
#include "common/analysis/lint_rule_status.h"
#include "common/analysis/syntax_tree_lint_rule.h"
#include "common/analysis/text_structure_lint_rule.h"
#include "common/analysis/token_stream_lint_rule.h"
#include "common/text/concrete_syntax_leaf.h"
#include "common/text/concrete_syntax_tree.h"
#include "common/text/syntax_tree_context.h"
#include "common/text/text_structure.h"
#include "common/text/token_info.h"
#include "gtest/gtest.h"
#include "verilog/analysis/descriptions.h"
namespace verilog {
namespace analysis {
namespace {
using verible::LineLintRule;
using verible::SyntaxTreeLintRule;
using verible::TextStructureLintRule;
using verible::TokenStreamLintRule;
// Fake SyntaxTreeLintRule that does nothing.
class TreeRuleBase : public SyntaxTreeLintRule {
public:
using rule_type = SyntaxTreeLintRule;
void HandleLeaf(const verible::SyntaxTreeLeaf& leaf,
const verible::SyntaxTreeContext& context) final {}
void HandleNode(const verible::SyntaxTreeNode& node,
const verible::SyntaxTreeContext& context) final {}
verible::LintRuleStatus Report() const final {
return verible::LintRuleStatus();
}
};
class TreeRule1 : public TreeRuleBase {
public:
using rule_type = SyntaxTreeLintRule;
static const LintRuleDescriptor& GetDescriptor() {
static const LintRuleDescriptor d{
.name = "test-rule-1",
.desc = "TreeRule1",
};
return d;
}
};
class TreeRule2 : public TreeRuleBase {
public:
using rule_type = SyntaxTreeLintRule;
static const LintRuleDescriptor& GetDescriptor() {
static const LintRuleDescriptor d{
.name = "test-rule-2",
.desc = "TreeRule2",
};
return d;
}
};
VERILOG_REGISTER_LINT_RULE(TreeRule1);
VERILOG_REGISTER_LINT_RULE(TreeRule2);
// Verifies that a known syntax tree rule is registered.
TEST(LintRuleRegistryTest, ContainsTreeRuleTrue) {
EXPECT_TRUE(IsRegisteredLintRule("test-rule-2"));
}
// Verifies that an unknown syntax tree rule is not found.
TEST(LintRuleRegistryTest, ContainsTreeRuleFalse) {
EXPECT_FALSE(IsRegisteredLintRule("invalid-id"));
}
// Verifies that a nonexistent syntax tree rule yields a nullptr.
TEST(LintRuleRegistryTest, CreateTreeLintRuleInvalid) {
EXPECT_EQ(CreateSyntaxTreeLintRule("invalid-id"), nullptr);
}
// Verifies that a registered syntax tree rule is properly created.
TEST(LintRuleRegistryTest, CreateTreeLintRuleValid) {
auto any_rule = CreateSyntaxTreeLintRule("test-rule-1");
EXPECT_NE(any_rule, nullptr);
#if defined(__GXX_RTTI)
auto rule_1 = dynamic_cast<TreeRule1*>(any_rule.get());
EXPECT_NE(rule_1, nullptr);
#endif
}
// Verifies that GetAllRuleDescriptionsHelpFlag correctly gets the descriptions
// for a SyntaxTreeLintRule.
TEST(GetAllRuleDescriptions, SyntaxRuleValid) {
const auto rule_map = GetAllRuleDescriptions();
EXPECT_EQ(rule_map.size(), 5);
const auto it = rule_map.find("test-rule-1");
ASSERT_NE(it, rule_map.end());
EXPECT_EQ(it->second.descriptor.desc, "TreeRule1");
}
class TokenRuleBase : public TokenStreamLintRule {
public:
using rule_type = TokenStreamLintRule;
void HandleToken(const verible::TokenInfo&) final {}
verible::LintRuleStatus Report() const final {
return verible::LintRuleStatus();
}
};
class TokenRule1 : public TokenRuleBase {
public:
using rule_type = TokenStreamLintRule;
static const LintRuleDescriptor& GetDescriptor() {
static const LintRuleDescriptor d{
.name = "token-rule-1",
.desc = "TokenRule1",
};
return d;
}
};
VERILOG_REGISTER_LINT_RULE(TokenRule1);
// Verifies that a known token stream rule is registered.
TEST(LintRuleRegistryTest, ContainsTokenRuleTrue) {
EXPECT_TRUE(IsRegisteredLintRule("token-rule-1"));
}
// Verifies that a nonexistent token stream rule yields a nullptr.
TEST(LintRuleRegistryTest, CreateTokenLintRuleInvalid) {
EXPECT_EQ(CreateTokenStreamLintRule("invalid-id"), nullptr);
}
// Verifies that a known token stream rule is properly created.
TEST(LintRuleRegistryTest, CreateTokenLintRuleValid) {
auto any_rule = CreateTokenStreamLintRule("token-rule-1");
EXPECT_NE(any_rule, nullptr);
#if defined(__GXX_RTTI)
auto rule_1 = dynamic_cast<TokenRule1*>(any_rule.get());
EXPECT_NE(rule_1, nullptr);
#endif
}
// Verifies that GetAllRuleDescriptionsHelpFlag correctly gets the descriptions
// for a TokenStreamLintRule.
TEST(GetAllRuleDescriptions, TokenRuleValid) {
const auto rule_map = GetAllRuleDescriptions();
EXPECT_EQ(rule_map.size(), 5);
const auto it = rule_map.find("token-rule-1");
ASSERT_NE(it, rule_map.end());
EXPECT_EQ(it->second.descriptor.desc, "TokenRule1");
}
class LineRule1 : public LineLintRule {
public:
using rule_type = LineLintRule;
static const LintRuleDescriptor& GetDescriptor() {
static const LintRuleDescriptor d{
.name = "line-rule-1",
.desc = "LineRule1",
};
return d;
}
void HandleLine(absl::string_view) final {}
verible::LintRuleStatus Report() const final {
return verible::LintRuleStatus();
}
};
VERILOG_REGISTER_LINT_RULE(LineRule1);
// Verifies that a known line-based rule is registered.
TEST(LintRuleRegistryTest, ContainsLineRuleTrue) {
EXPECT_TRUE(IsRegisteredLintRule("line-rule-1"));
}
// Verifies that a nonexistent line-based rule yields a nullptr.
TEST(LintRuleRegistryTest, CreateLineLintRuleInvalid) {
EXPECT_EQ(CreateLineLintRule("invalid-id"), nullptr);
}
// Verifies that a known line-based rule is properly created.
TEST(LintRuleRegistryTest, CreateLineLintRuleValid) {
auto any_rule = CreateLineLintRule("line-rule-1");
EXPECT_NE(any_rule, nullptr);
#if defined(__GXX_RTTI)
auto rule_1 = dynamic_cast<LineRule1*>(any_rule.get());
EXPECT_NE(rule_1, nullptr);
#endif
}
// Verifies that GetAllRuleDescriptionsHelpFlag correctly gets the descriptions
// for a LineLintRule.
TEST(GetAllRuleDescriptions, LineRuleValid) {
const auto rule_map = GetAllRuleDescriptions();
EXPECT_EQ(rule_map.size(), 5);
const auto it = rule_map.find("line-rule-1");
ASSERT_NE(it, rule_map.end());
EXPECT_EQ(it->second.descriptor.desc, "LineRule1");
}
class TextRule1 : public TextStructureLintRule {
public:
using rule_type = TextStructureLintRule;
static const LintRuleDescriptor& GetDescriptor() {
static const LintRuleDescriptor d{
.name = "text-rule-1",
.desc = "TextRule1",
};
return d;
}
void Lint(const verible::TextStructureView&, absl::string_view) final {}
verible::LintRuleStatus Report() const final {
return verible::LintRuleStatus();
}
};
VERILOG_REGISTER_LINT_RULE(TextRule1);
// Verifies that a known text-structure-based rule is registered.
TEST(LintRuleRegistryTest, ContainsTextRuleTrue) {
EXPECT_TRUE(IsRegisteredLintRule("text-rule-1"));
}
// Verifies that a nonexistent text-structure-based rule yields a nullptr.
TEST(LintRuleRegistryTest, CreateTextLintRuleInvalid) {
EXPECT_EQ(CreateTextStructureLintRule("invalid-id"), nullptr);
}
// Verifies that a known text-structure-based rule is properly created.
TEST(LintRuleRegistryTest, CreateTextLintRuleValid) {
auto any_rule = CreateTextStructureLintRule("text-rule-1");
EXPECT_NE(any_rule, nullptr);
#if defined(__GXX_RTTI)
auto rule_1 = dynamic_cast<TextRule1*>(any_rule.get());
EXPECT_NE(rule_1, nullptr);
#endif
}
TEST(LintRuleRegistryTest, ConfigureFactoryCreatedRule) {
auto any_rule = CreateTextStructureLintRule("text-rule-1");
EXPECT_NE(any_rule, nullptr);
// Test configuration of freshly instantiated rule.
absl::Status status = any_rule->Configure("");
EXPECT_TRUE(status.ok()) << status.message();
status = any_rule->Configure("bogus");
EXPECT_FALSE(status.ok());
EXPECT_TRUE(absl::StrContains(status.message(), "not support configuration"));
}
// Verifies that GetAllRuleDescriptionsHelpFlag correctly gets the descriptions
// for a TextStructureLintRule.
TEST(GetAllRuleDescriptions, TextRuleValid) {
const auto rule_map = GetAllRuleDescriptions();
EXPECT_EQ(rule_map.size(), 5);
const auto it = rule_map.find("text-rule-1");
ASSERT_NE(it, rule_map.end());
EXPECT_EQ(it->second.descriptor.desc, "TextRule1");
}
} // namespace
} // namespace analysis
} // namespace verilog