forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seq_block_test.cc
202 lines (159 loc) · 5.94 KB
/
seq_block_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
// 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/CST/seq_block.h"
#include <vector>
#include "absl/strings/str_cat.h"
#include "common/analysis/syntax_tree_search.h"
#include "common/text/syntax_tree_context.h"
#include "common/text/text_structure.h"
#include "common/util/logging.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "verilog/CST/verilog_matchers.h"
#include "verilog/CST/verilog_nonterminals.h"
#include "verilog/analysis/verilog_analyzer.h"
#undef ASSERT_OK
#define ASSERT_OK(value) ASSERT_TRUE((value).ok())
namespace verilog {
namespace {
static std::vector<verible::TreeSearchMatch> FindAllBeginStatements(
const verible::Symbol& root) {
return SearchSyntaxTree(root, NodekBegin());
}
TEST(GetMatchingEndTest, Simple) {
VerilogAnalyzer analyzer(R"(
module foo;
initial begin
end
endmodule)",
"");
ASSERT_OK(analyzer.Analyze());
const auto& root = analyzer.Data().SyntaxTree();
const auto begin_statements = FindAllBeginStatements(*root);
ASSERT_EQ(begin_statements.size(), 1);
const auto matchingEnd =
GetMatchingEnd(*begin_statements[0].match, begin_statements[0].context);
EXPECT_EQ(NodeEnum(matchingEnd->Tag().tag), NodeEnum::kEnd);
}
TEST(GetBeginLabelTokenInfoTest, Single) {
VerilogAnalyzer analyzer(R"(
module foo;
initial begin : begin_label
end
endmodule)",
"");
ASSERT_OK(analyzer.Analyze());
const auto& root = analyzer.Data().SyntaxTree();
const auto begin_statements = FindAllBeginStatements(*root);
ASSERT_EQ(begin_statements.size(), 1);
const auto* beginToken = GetBeginLabelTokenInfo(*begin_statements[0].match);
EXPECT_EQ(ABSL_DIE_IF_NULL(beginToken)->text(), "begin_label");
}
TEST(GetBeginLabelTokenInfoTest, SingleNoLabel) {
VerilogAnalyzer analyzer(R"(
module foo;
initial begin
end : begin_has_no_label
endmodule)",
"");
ASSERT_OK(analyzer.Analyze());
const auto& root = analyzer.Data().SyntaxTree();
const auto begin_statements = FindAllBeginStatements(*root);
ASSERT_EQ(begin_statements.size(), 1);
const auto* beginToken = GetBeginLabelTokenInfo(*begin_statements[0].match);
EXPECT_EQ(beginToken, nullptr);
}
TEST(GetBeginLabelTokenInfoTest, GenerateBlockPrefixLabel) {
VerilogAnalyzer analyzer(R"(
module foo;
if (1) prefix_label : begin
end
endmodule)",
"");
ASSERT_OK(analyzer.Analyze());
const auto& root = analyzer.Data().SyntaxTree();
const auto begin_statements = FindAllBeginStatements(*root);
ASSERT_EQ(begin_statements.size(), 1);
const auto* beginToken = GetBeginLabelTokenInfo(*begin_statements[0].match);
EXPECT_EQ(ABSL_DIE_IF_NULL(beginToken)->text(), "prefix_label");
}
TEST(GetEndLabelTokenInfoTest, SingleNoLabel) {
VerilogAnalyzer analyzer(R"(
module foo;
initial begin : end_has_no_label
end
endmodule)",
"");
ASSERT_OK(analyzer.Analyze());
const auto& root = analyzer.Data().SyntaxTree();
const auto begin_statements = FindAllBeginStatements(*root);
ASSERT_EQ(begin_statements.size(), 1);
const auto* matchingEnd =
GetMatchingEnd(*begin_statements[0].match, begin_statements[0].context);
ASSERT_EQ(NodeEnum(matchingEnd->Tag().tag), NodeEnum::kEnd);
const auto* endToken = GetEndLabelTokenInfo(*matchingEnd);
EXPECT_EQ(endToken, nullptr);
}
TEST(GetEndLabelTokenInfoTest, Single) {
VerilogAnalyzer analyzer(R"(
module foo;
initial begin
end : end_label
endmodule)",
"");
ASSERT_OK(analyzer.Analyze());
const auto& root = analyzer.Data().SyntaxTree();
const auto begin_statements = FindAllBeginStatements(*root);
ASSERT_EQ(begin_statements.size(), 1);
const auto* matchingEnd =
GetMatchingEnd(*begin_statements[0].match, begin_statements[0].context);
ASSERT_EQ(NodeEnum(matchingEnd->Tag().tag), NodeEnum::kEnd);
const auto* endToken = GetEndLabelTokenInfo(*matchingEnd);
EXPECT_EQ(ABSL_DIE_IF_NULL(endToken)->text(), "end_label");
}
TEST(GetMatchingEndTest, Complex) {
VerilogAnalyzer analyzer(R"(
module foo;
initial begin : outer_begin_label
for(int i = 0; i < 5; ++i)
begin : inner_begin_label
end : inner_end_label
end : outer_end_label
endmodule)",
"");
ASSERT_OK(analyzer.Analyze());
const auto& root = analyzer.Data().SyntaxTree();
const auto begin_statements = FindAllBeginStatements(*root);
ASSERT_EQ(begin_statements.size(), 2);
const auto* matchingOuterEnd =
GetMatchingEnd(*begin_statements[0].match, begin_statements[0].context);
ASSERT_EQ(NodeEnum(matchingOuterEnd->Tag().tag), NodeEnum::kEnd);
const auto* matchingInnerEnd =
GetMatchingEnd(*begin_statements[1].match, begin_statements[1].context);
ASSERT_EQ(NodeEnum(matchingInnerEnd->Tag().tag), NodeEnum::kEnd);
const auto& outerBeginToken =
*ABSL_DIE_IF_NULL(GetBeginLabelTokenInfo(*begin_statements[0].match));
const auto& innerBeginToken =
*ABSL_DIE_IF_NULL(GetBeginLabelTokenInfo(*begin_statements[1].match));
const auto& outerEndToken =
*ABSL_DIE_IF_NULL(GetEndLabelTokenInfo(*matchingOuterEnd));
const auto& innerEndToken =
*ABSL_DIE_IF_NULL(GetEndLabelTokenInfo(*matchingInnerEnd));
EXPECT_EQ(outerBeginToken.text(), "outer_begin_label");
EXPECT_EQ(outerEndToken.text(), "outer_end_label");
EXPECT_EQ(innerBeginToken.text(), "inner_begin_label");
EXPECT_EQ(innerEndToken.text(), "inner_end_label");
}
} // namespace
} // namespace verilog