-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathgenerate_label_prefix_rule.cc
100 lines (86 loc) · 3.29 KB
/
generate_label_prefix_rule.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
// 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/checkers/generate_label_prefix_rule.h"
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "common/analysis/lint_rule_status.h"
#include "common/analysis/matcher/bound_symbol_manager.h"
#include "common/analysis/matcher/matcher.h"
#include "common/text/symbol.h"
#include "common/text/syntax_tree_context.h"
#include "common/text/token_info.h"
#include "common/text/tree_utils.h"
#include "verilog/CST/seq_block.h"
#include "verilog/CST/verilog_matchers.h"
#include "verilog/CST/verilog_nonterminals.h"
#include "verilog/analysis/descriptions.h"
#include "verilog/analysis/lint_rule_registry.h"
namespace verilog {
namespace analysis {
using verible::matcher::Matcher;
// Register the lint rule
VERILOG_REGISTER_LINT_RULE(GenerateLabelPrefixRule);
static constexpr absl::string_view kMessage =
"All generate block labels must start with g_ or gen_";
// TODO(fangism): and be lower_snake_case?
// TODO(fangism): generalize to a configurable pattern and
// rename this class/rule to GenerateLabelNamingStyle?
const LintRuleDescriptor &GenerateLabelPrefixRule::GetDescriptor() {
static const LintRuleDescriptor d{
.name = "generate-label-prefix",
.topic = "generate-constructs",
.desc = "Checks that every generate block label starts with g_ or gen_.",
};
return d;
}
// Matches begin statements
static const Matcher &BlockMatcher() {
static const Matcher matcher(NodekGenerateBlock());
return matcher;
}
void GenerateLabelPrefixRule::HandleSymbol(
const verible::Symbol &symbol, const verible::SyntaxTreeContext &context) {
verible::matcher::BoundSymbolManager manager;
if (BlockMatcher().Matches(symbol, &manager)) {
// Exclude case generate statements, as kGenerateBlock is generated for
// each 'case' item too.
if (context.IsInside(NodeEnum::kGenerateCaseItemList)) {
return;
}
for (const auto &child : SymbolCastToNode(symbol).children()) {
const verible::TokenInfo *label = nullptr;
switch (NodeEnum(SymbolCastToNode(*child).Tag().tag)) {
case NodeEnum::kBegin:
label = GetBeginLabelTokenInfo(*child);
break;
case NodeEnum::kEnd:
label = GetEndLabelTokenInfo(*child);
break;
default:
continue;
}
if (label != nullptr) {
if (!(absl::StartsWith(label->text(), "g_") ||
absl::StartsWith(label->text(), "gen_"))) {
violations_.insert(verible::LintViolation(*label, kMessage, context));
}
}
}
}
}
verible::LintRuleStatus GenerateLabelPrefixRule::Report() const {
return verible::LintRuleStatus(violations_, GetDescriptor());
}
} // namespace analysis
} // namespace verilog