forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tasks_test.cc
419 lines (396 loc) · 14.6 KB
/
tasks_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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// 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/tasks.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/strings/string_view.h"
#include "common/analysis/syntax_tree_search.h"
#include "common/analysis/syntax_tree_search_test_utils.h"
#include "common/text/concrete_syntax_leaf.h"
#include "common/text/concrete_syntax_tree.h"
#include "common/text/symbol.h"
#include "common/text/text_structure.h"
#include "common/text/token_info.h"
#include "common/text/tree_utils.h"
#include "common/util/casts.h"
#include "common/util/logging.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "verilog/CST/identifier.h"
#include "verilog/CST/match_test_utils.h"
#include "verilog/CST/verilog_nonterminals.h"
#include "verilog/analysis/verilog_analyzer.h"
#include "verilog/parser/verilog_token_enum.h"
#undef ASSERT_OK
#define ASSERT_OK(value) ASSERT_TRUE((value).ok())
namespace verilog {
namespace {
using verible::down_cast;
using verible::SyntaxTreeSearchTestCase;
using verible::TextStructureView;
using verible::TreeSearchMatch;
TEST(FindAllTaskDeclarationsTest, Various) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"class foo; endclass"},
{"module m(); endmodule: m"},
{"function f(); endfunction"},
{{kTag, "task foo();\nendtask"}},
{{kTag, "task foo();\nendtask : foo"}},
{{kTag, "task t();\nint x = 1;\nendtask"}},
{// task inside package
"package pkg;\n",
{kTag,
"task t;\n "
"int x = 1;"
"\nendtask"},
"\nendpackage"},
{// multiple tasks
{kTag, "task foo();\nendtask"},
"\n",
{kTag, "task foo2();\nendtask"}},
{// task declaration as class method
"class bar;\n",
{kTag, "task foo();\nendtask"},
" endclass"},
{// task declaration inside module
"module bar;\n",
{kTag, "task foo();\nendtask"},
" endmodule"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
return FindAllTaskDeclarations(*ABSL_DIE_IF_NULL(root));
});
}
}
TEST(FindAllTaskPrototypesTest, Various) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{// task declaration is not considered a prototype
"task foo();\n"
"endtask"},
{// task declaration is not considered a prototype
"class bar;\n"
" task foo();\n"
" endtask\n"
"endclass"},
{// virtual task can be defined and is overridable
"class bar;\n"
" virtual task foo();\n"
" endtask\n"
"endclass"},
{// pure virtual is a prototype
"class bar;\n",
"pure virtual ",
{kTag, "task foo();"},
"\nendclass"},
{// extern is a prototype
"class bar;\n",
"extern ",
{kTag, "task foo();"},
"\nendclass"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
return FindAllTaskPrototypes(*ABSL_DIE_IF_NULL(root));
});
}
// Prototype header span the same range as their enclosing prototypes.
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
std::vector<TreeSearchMatch> headers;
for (const auto& proto :
FindAllTaskPrototypes(*ABSL_DIE_IF_NULL(root))) {
headers.push_back(TreeSearchMatch{
GetTaskPrototypeHeader(*proto.match), /* no context */});
}
return headers;
});
}
}
TEST(TaskPrototypesIdsTest, Various) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{// pure virtual is a prototype
"class bar;\n",
"pure virtual task ",
{kTag, "foo"},
"();\n"
"endclass"},
{// extern is a prototype
"class bar;\n",
"extern task ",
{kTag, "ggghhh"},
"();\n"
"endclass"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
std::vector<TreeSearchMatch> ids;
for (const auto& proto :
FindAllTaskPrototypes(*ABSL_DIE_IF_NULL(root))) {
const auto* header = GetTaskPrototypeHeader(*proto.match);
const auto* id = ABSL_DIE_IF_NULL(GetTaskHeaderId(*header));
EXPECT_TRUE(verible::SymbolCastToNode(*id).MatchesTag(
NodeEnum::kUnqualifiedId));
ids.push_back(TreeSearchMatch{id, /* no context */});
}
return ids;
});
}
}
TEST(FindAllTaskHeadersTest, Various) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"class c; endclass"},
{"package c; endpackage"},
{"module m; endmodule"},
{"function f; endfunction"},
{{kTag, "task foo();"},
"\n"
"endtask"},
{"class bar;\n"
" ",
{kTag, "task foo();"},
"\n"
" endtask\n"
"endclass"},
{"class bar;\n",
{kTag, "virtual task foo();"},
"\n"
" endtask\n"
"endclass"},
{// pure virtual is a prototype
"class bar;\n",
"pure virtual ",
{kTag, "task foo();"},
"\nendclass"},
{// extern is a prototype
"class bar;\n",
"extern ",
{kTag, "task foo();"},
"\nendclass"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
return FindAllTaskHeaders(*ABSL_DIE_IF_NULL(root));
});
}
}
TEST(GetTaskHeaderTest, DeclarationsHeader) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{{kTag, "task foo();"}, " endtask"},
{{kTag, "task my_class::foo();"}, " endtask"},
{"class c; ", {kTag, "task foo();"}, " endtask endclass"},
{"module m; ", {kTag, "task foo();"}, " endtask endmodule"},
{"package p; ", {kTag, "task foo();"}, " endtask endpackage"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
// Root node is a description list, not a task.
const auto& root = text_structure.SyntaxTree();
const auto task_declarations = FindAllTaskDeclarations(*root);
std::vector<TreeSearchMatch> headers;
for (const auto& decl : task_declarations) {
const auto& task_node = verible::SymbolCastToNode(*decl.match);
headers.push_back(
TreeSearchMatch{GetTaskHeader(task_node), /* no context */});
}
return headers;
});
}
}
TEST(GetTaskLifetimeTest, NoLifetimeDeclared) {
VerilogAnalyzer analyzer("task foo(); endtask", "");
ASSERT_OK(analyzer.Analyze());
// Root node is a description list, not a task.
const auto& root = analyzer.Data().SyntaxTree();
const auto task_declarations = FindAllTaskDeclarations(*root);
ASSERT_EQ(task_declarations.size(), 1);
const auto& task_node =
verible::SymbolCastToNode(*task_declarations.front().match);
const auto* lifetime = GetTaskLifetime(task_node);
EXPECT_EQ(lifetime, nullptr);
}
TEST(GetTaskLifetimeTest, StaticLifetimeDeclared) {
VerilogAnalyzer analyzer("task static foo(); endtask", "");
ASSERT_OK(analyzer.Analyze());
// Root node is a description list, not a task.
const auto& root = analyzer.Data().SyntaxTree();
const auto task_declarations = FindAllTaskDeclarations(*root);
ASSERT_EQ(task_declarations.size(), 1);
const auto& task_node =
verible::SymbolCastToNode(*task_declarations.front().match);
const auto* lifetime = GetTaskLifetime(task_node);
const auto& leaf = verible::SymbolCastToLeaf(*ABSL_DIE_IF_NULL(lifetime));
EXPECT_EQ(leaf.get().token_enum(), TK_static);
}
TEST(GetTaskLifetimeTest, AutomaticLifetimeDeclared) {
VerilogAnalyzer analyzer("task automatic foo(); endtask", "");
ASSERT_OK(analyzer.Analyze());
// Root node is a description list, not a task.
const auto& root = analyzer.Data().SyntaxTree();
const auto task_declarations = FindAllTaskDeclarations(*root);
ASSERT_EQ(task_declarations.size(), 1);
const auto& task_node =
verible::SymbolCastToNode(*task_declarations.front().match);
const auto* lifetime = GetTaskLifetime(task_node);
const auto& leaf = verible::SymbolCastToLeaf(*ABSL_DIE_IF_NULL(lifetime));
EXPECT_EQ(leaf.get().token_enum(), TK_automatic);
}
TEST(GetTaskIdTest, UnqualifiedIds) {
constexpr int kTag = 1; // don't care
const SyntaxTreeSearchTestCase kTestCases[] = {
{"task ", {kTag, "foo"}, "(); endtask"},
{"task automatic ", {kTag, "bar"}, "(); endtask"},
{"task static ", {kTag, "baz"}, "(); endtask"},
{"package p; task ", {kTag, "foo"}, "(); endtask endpackage"},
{"class c; task ", {kTag, "zoo"}, "(); endtask endclass"},
{"task ", {kTag, "myclass"}, "::", {kTag, "foo"}, "(); endtask"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
// Root node is a description list, not a task.
const auto& root = text_structure.SyntaxTree();
const auto task_declarations = FindAllTaskDeclarations(*root);
std::vector<TreeSearchMatch> got_ids;
for (const auto& task_decl : task_declarations) {
const auto& task_node =
down_cast<const verible::SyntaxTreeNode&>(*task_decl.match);
const auto* task_id = GetTaskId(task_node);
const auto ids = FindAllUnqualifiedIds(*task_id);
for (const auto& id : ids) {
got_ids.push_back(TreeSearchMatch{
GetIdentifier(*ABSL_DIE_IF_NULL(id.match)),
/* no context */});
}
}
return got_ids;
});
}
}
TEST(GetTaskIdTest, QualifiedIds) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{"task foo(); endtask"},
{"class c; task foo(); endtask endclass"},
{"task ", {kTag, "myclass::foo"}, "(); endtask"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
// Root node is a description list, not a task.
const auto& root = text_structure.SyntaxTree();
const auto task_declarations = FindAllTaskDeclarations(*root);
std::vector<TreeSearchMatch> ids;
for (const auto& decl : task_declarations) {
const auto& task_node = verible::SymbolCastToNode(*decl.match);
const auto* task_id = GetTaskId(task_node);
for (const auto& id : FindAllQualifiedIds(*task_id)) {
ids.push_back(id);
}
}
return ids;
});
}
}
TEST(GetTaskHeaderTest, GetTaskName) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"module m(); endmodule: m"},
{"task ", {kTag, "foo"}, "(int a, bit b);\nendtask"},
{"task ",
{kTag, "foo"},
"();\n endtask\n task ",
{kTag, "bar"},
"()\n; endtask"},
{"module my_module;\ntask ",
{kTag, "inner_task"},
"(int my_args);\n$display(my_arg2);\nendtask\nendmodule"},
{"class task_class;\ntask ",
{kTag, "my_task"},
"(input int a, b, output int c);\nc = a + b;\nendtask\nendclass"},
{"package my_pkg;\ntask automatic ",
{kTag, "my_task"},
"(input int a, b, output int c);\nc = a + b;\nendtask\nendpackage"},
{"class m;\n virtual task ",
{kTag, "my_task"},
"();\n endtask\n endclass"},
{"class m;\n static task ",
{kTag, "my_task"},
"();\n endtask\n endclass"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto decls = FindAllTaskDeclarations(*ABSL_DIE_IF_NULL(root));
std::vector<TreeSearchMatch> types;
for (const auto& decl : decls) {
const auto* type = GetTaskName(*decl.match);
types.push_back(TreeSearchMatch{type, {/* ignored context */}});
}
return types;
});
}
} // namespace
TEST(GetTaskHeaderTest, GetTaskBody) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"module m(); endmodule: m"},
{"task t();\n ", {kTag, "int x = 1;"}, "\nendtask"},
{"task t;\n ", {kTag, "int x = 1;"}, "\nendtask"},
{"package pkg;\ntask t;\n ",
{kTag, "int x = 1;"},
"\nendtask\nendpackage"},
{"package pkg;\ntask t();\n ",
{kTag, "int x = 1;"},
"\nendtask\nendpackage"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto decls = FindAllTaskDeclarations(*ABSL_DIE_IF_NULL(root));
std::vector<TreeSearchMatch> bodies;
for (const auto& decl : decls) {
const auto& body = GetTaskStatementList(*decl.match);
bodies.push_back(TreeSearchMatch{body, {/* ignored context */}});
}
return bodies;
});
}
}
} // namespace
} // namespace verilog