forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint_rule_registry.h
158 lines (134 loc) · 5.71 KB
/
lint_rule_registry.h
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
// 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.
// File for registering lint rules so that they can by dynamically turned on
// and off at runtime. The goal is to provide a single place to register
// lint rules.
//
// To register an implemented rule, call
// VERILOG_REGISTER_LINT_RULE in source file.
//
// This will have the following effects:
// 1. Allow that rule to be used in commandline flags that accept vectors
// of LintRuleEnum's
// 2. Allow the rule to be used by any component that dynamically loads
// rules from LintRuleRegistry
//
#ifndef VERIBLE_VERILOG_ANALYSIS_LINT_RULE_REGISTRY_H_
#define VERIBLE_VERILOG_ANALYSIS_LINT_RULE_REGISTRY_H_
#include <functional>
#include <map>
#include <memory>
#include <set>
#include <vector>
#include "common/analysis/line_lint_rule.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/strings/compare.h"
#include "verilog/analysis/descriptions.h"
namespace verilog {
namespace analysis {
template <typename RuleType>
class LintRuleRegisterer;
template <typename RuleType>
using LintRuleGeneratorFun = std::function<std::unique_ptr<RuleType>()>;
using LintDescriptionFun = std::function<LintRuleDescriptor()>;
template <typename RuleType>
struct LintRuleInfo {
LintRuleGeneratorFun<RuleType> lint_rule_generator;
LintDescriptionFun description;
};
struct LintRuleDefaultConfig {
LintRuleDescriptor descriptor;
bool default_enabled = false;
};
using LintRuleDescriptionsMap =
std::map<LintRuleId, LintRuleDefaultConfig, verible::StringViewCompare>;
// Helper macro to register a LintRule with LintRuleRegistry. In order to have
// a global registry, some static initialization is needed. This macros
// centralizes this unsafe code into one place in order to prevent mistakes.
//
// Args:
// class_name: the name of the class to register
//
// Usage:
// (in my_lint_rule.h):
// class MyLintRule : LintRuleType {
// public:
// using rule_type = LintRuleType;
// static absl::string_view Name();
// static std::string GetDescription(DescriptionType);
// ... implement internals ...
// }
//
// (in my_lint_rule.cc):
// VERILOG_REGISTER_LINT_RULE(MyLintRule);
//
// Name() needs to be a function and not a constant to guarantee proper
// string initialization (from first invocation during static global
// initialization, when registration happens), and must be backed by string
// memory with guaranteed lifetime. e.g.
//
// absl::string_view MyLintRule::Name() {
// return "my-lint-rule"; // safely initialized function-local string literal
// }
//
// TODO(hzeller): once the class does not contain a state, extract the name
// and description from the instance to avoid weird static initialization.
#define VERILOG_REGISTER_LINT_RULE(class_name) \
static verilog::analysis::LintRuleRegisterer<class_name::rule_type> \
__##class_name##__registerer(class_name::GetDescriptor, []() { \
return std::unique_ptr<class_name::rule_type>(new class_name()); \
});
// Static objects of type LintRuleRegisterer are used to register concrete
// parsers in LintRuleRegistry. Users are expected to create these objects
// using the VERILOG_REGISTER_LINT_RULE macro.
template <typename RuleType>
class LintRuleRegisterer {
public:
LintRuleRegisterer(const LintDescriptionFun& descriptor,
const LintRuleGeneratorFun<RuleType>& creator);
};
// Returns true if rule_name refers to a known lint rule.
bool IsRegisteredLintRule(const LintRuleId& rule_name);
// Returns sequence of syntax tree rule names.
std::vector<LintRuleId> RegisteredSyntaxTreeRulesNames();
// Returns a syntax tree lint rule object corresponding the rule_name.
std::unique_ptr<verible::SyntaxTreeLintRule> CreateSyntaxTreeLintRule(
const LintRuleId& rule_name);
// Returns sequence of token stream rule names.
std::vector<LintRuleId> RegisteredTokenStreamRulesNames();
// Returns a token stream lint rule object corresponding the rule_name.
std::unique_ptr<verible::TokenStreamLintRule> CreateTokenStreamLintRule(
const LintRuleId& rule_name);
// Returns sequence of line rule names.
std::vector<LintRuleId> RegisteredLineRulesNames();
// Returns a token stream lint rule object corresponding the rule_name.
std::unique_ptr<verible::LineLintRule> CreateLineLintRule(
const LintRuleId& rule_name);
// Returns sequence of text structure rule names.
std::vector<LintRuleId> RegisteredTextStructureRulesNames();
// Returns a token stream lint rule object corresponding the rule_name.
std::unique_ptr<verible::TextStructureLintRule> CreateTextStructureLintRule(
const LintRuleId& rule_name);
// Returns set of all registered lint rule names.
// When storing string_views to the lint rule keys, use the ones returned in
// this set, because their lifetime is guaranteed by the registration process.
std::set<LintRuleId> GetAllRegisteredLintRuleNames();
// Returns a map mapping each rule to a struct of information about the rule to
// print.
LintRuleDescriptionsMap GetAllRuleDescriptions();
} // namespace analysis
} // namespace verilog
#endif // VERIBLE_VERILOG_ANALYSIS_LINT_RULE_REGISTRY_H_