forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions_test.cc
832 lines (790 loc) · 29.2 KB
/
functions_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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
// 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/functions.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/strings/string_view.h"
#include "common/analysis/matcher/matcher_builders.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/token_info_test_util.h"
#include "common/text/tree_utils.h"
#include "common/util/casts.h"
#include "common/util/logging.h"
#include "common/util/range.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "verilog/CST/identifier.h"
#include "verilog/CST/match_test_utils.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::CheckSymbolAsLeaf;
using verible::SymbolCastToNode;
using verible::SyntaxTreeSearchTestCase;
using verible::TextStructureView;
using verible::TreeSearchMatch;
TEST(FindAllFunctionDeclarationsTest, Various) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"class foo; endclass"},
{"task foo; endtask"},
{"module foo; endmodule"},
{{kTag, "function foo(); endfunction"}},
{// multiple function declarations
{kTag, "function foo(); endfunction"},
"\n",
{kTag, "function foo2(); endfunction"},
"\n"},
{// package function method declaration
"package bar;\n",
{kTag, "function foo(); endfunction"},
"\nendpackage\n"},
{// class function method declaration
"class bar;\n",
{kTag, "function foo(); endfunction"},
"\nendclass\n"},
{// function declaration inside module
"module bar;\n",
{kTag, "function foo(); endfunction"},
"\nendmodule\n"},
{// forward declaration is not a full function declaration
"class bar;\n",
"extern function foo();\n"
"endclass\n"},
{// pure virtual is not a full function declaration
"class bar;\n",
"pure virtual function foo();\n"
"endclass\n"},
{// virtual is a full function declaration
"class bar;\n",
{kTag,
"virtual function foo();\n"
"endfunction"},
"\n"
"endclass\n"},
{// function declaration inside cross_body_item
"module cover_that;\n"
"covergroup settings;\n"
" _name : cross dbi, mask {\n"
" ",
{kTag,
"function int foo(int bar);\n" // function declaration
" return bar;\n"
" endfunction"},
"\n"
" }\n"
"endgroup\n"
"endmodule"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
return FindAllFunctionDeclarations(*ABSL_DIE_IF_NULL(root));
});
}
}
TEST(FindAllFunctionPrototypesTest, Various) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"class foo; endclass"},
{"task foo; endtask"},
{"module foo; endmodule"},
{"function foo(); endfunction"},
{// package function method declaration
"package bar;\n",
"function foo(); endfunction\n"
"endpackage\n"},
{// class function method declaration (not a prototype)
"class bar;\n",
"function foo(); endfunction\n"
"endclass\n"},
{// function declaration inside module
"module bar;\n"
"function foo(); endfunction\n"
"endmodule\n"},
{// forward declaration is a prototype
"class bar;\n",
"extern ",
{kTag, "function foo();"},
"\n"
"endclass\n"},
{// pure virtual is a prototype
"class bar;\n",
"pure virtual ",
{kTag, "function foo();"},
"\n"
"endclass\n"},
{// virtual declaration is not a prototype
"class bar;\n",
"virtual function foo();\n"
"endfunction\n"
"endclass\n"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
return FindAllFunctionPrototypes(*ABSL_DIE_IF_NULL(root));
});
}
// Protype headers map to the same range as the enclosing prototype.
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto protos(FindAllFunctionPrototypes(*ABSL_DIE_IF_NULL(root)));
std::vector<TreeSearchMatch> headers;
headers.reserve(protos.size());
for (const auto& proto : protos) {
headers.push_back(TreeSearchMatch{
GetFunctionPrototypeHeader(*proto.match), /* no context */});
}
return headers;
});
}
}
TEST(FunctionPrototypesReturnTypesTest, Various) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{// forward declaration is a prototype, implicit return type
"class bar;\n"
"extern function foo();\n"
"endclass\n"},
{// forward declaration is a prototype, explicit return type
"class bar;\n"
"extern function ",
{kTag, "bar#(int)"},
" foo();\n"
"endclass\n"},
{// pure virtual is a prototype, implicit return type
"class bar;\n"
"pure virtual function foo();\n"
"endclass\n"},
{// pure virtual is a prototype, explicit return type
"class bar;\n"
"pure virtual function ",
{kTag, "p_pkg::baz_t"},
" foo();\n"
"endclass\n"},
};
// Protype headers map to the same range as the enclosing prototype.
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto protos(FindAllFunctionPrototypes(*ABSL_DIE_IF_NULL(root)));
std::vector<TreeSearchMatch> returns;
for (const auto& proto : protos) {
const auto* return_type = GetFunctionHeaderReturnType(
*GetFunctionPrototypeHeader(*proto.match));
if (return_type == nullptr) continue;
if (verible::StringSpanOfSymbol(*return_type).empty()) continue;
returns.push_back(TreeSearchMatch{return_type, /* no context */});
}
return returns;
});
}
}
TEST(FunctionPrototypesIdsTest, Various) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{// forward declaration is a prototype, implicit return type
"class bar;\n"
"extern function ",
{kTag, "moo"},
"();\n"
"endclass\n"},
{// forward declaration is a prototype, explicit return type
"class bar;\n"
"extern function bar#(int) ",
{kTag, "gooo"},
"();\n"
"endclass\n"},
{// pure virtual is a prototype, implicit return type
"class bar;\n"
"pure virtual function ",
{kTag, "weeee"},
"();\n"
"endclass\n"},
{// pure virtual is a prototype, explicit return type
"class bar;\n",
"pure virtual function p_pkg::baz_t ",
{kTag, "h456"},
"();\n"
"endclass\n"},
};
// Protype headers map to the same range as the enclosing prototype.
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto protos(FindAllFunctionPrototypes(*ABSL_DIE_IF_NULL(root)));
std::vector<TreeSearchMatch> ids;
for (const auto& proto : protos) {
const auto* id =
GetFunctionHeaderId(*GetFunctionPrototypeHeader(*proto.match));
if (id == nullptr) continue;
if (verible::StringSpanOfSymbol(*id).empty()) continue;
ids.push_back(TreeSearchMatch{id, /* no context */});
}
return ids;
});
}
}
TEST(FindAllFunctionHeadersTest, Various) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"class foo; endclass"},
{"task foo; endtask"},
{"module foo; endmodule"},
{{kTag, "function foo();"}, " endfunction"},
{// multiple function declarations
{kTag, "function foo();"},
" endfunction\n",
{kTag, "function foo2();"},
" endfunction\n"},
{// package function method declaration
"package bar;\n",
{kTag, "function foo();"},
" endfunction"
"\nendpackage\n"},
{// class function method declaration
"class bar;\n",
{kTag, "function foo();"},
" endfunction"
"\nendclass\n"},
{// function declaration inside module
"module bar;\n",
{kTag, "function foo();"},
" endfunction"
"\nendmodule\n"},
{// forward declaration
"class bar;\n",
"extern ",
{kTag, "function foo();"},
"\n"
"endclass\n"},
{// pure virtual
"class bar;\n",
"pure virtual ",
{kTag, "function foo();"},
"\n"
"endclass\n"},
{// virtual
"class bar;\n",
{kTag, "virtual function foo();"},
"\n"
"endfunction\n"
"endclass\n"},
{// function declaration inside cross_body_item
"module cover_that;\n"
"covergroup settings;\n"
" _name : cross dbi, mask {\n"
" ",
{kTag, "function int foo(int bar);"},
"\n"
" return bar;\n"
" endfunction\n"
" }\n"
"endgroup\n"
"endmodule"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
return FindAllFunctionHeaders(*ABSL_DIE_IF_NULL(root));
});
}
}
TEST(GetFunctionHeaderTest, DeclarationHeader) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{{kTag, "function foo();"}, " endfunction"},
{"class c; ", {kTag, "function foo();"}, " endfunction endclass"},
{"module m; ", {kTag, "function foo();"}, " endfunction endmodule"},
{"package p; ", {kTag, "function foo();"}, " endfunction endpackage"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
// Root node is a description list, not a function.
const auto function_declarations = FindAllFunctionDeclarations(*root);
std::vector<TreeSearchMatch> headers;
for (const auto& decl : function_declarations) {
const auto& function_node = SymbolCastToNode(*decl.match);
headers.push_back(TreeSearchMatch{GetFunctionHeader(function_node),
/* no context */});
}
return headers;
});
}
}
TEST(GetFunctionLifetimeTest, NoLifetimeDeclared) {
VerilogAnalyzer analyzer("function foo(); endfunction", "");
ASSERT_OK(analyzer.Analyze());
// Root node is a description list, not a function.
const auto& root = analyzer.Data().SyntaxTree();
const auto function_declarations = FindAllFunctionDeclarations(*root);
ASSERT_EQ(function_declarations.size(), 1);
const auto& function_node =
SymbolCastToNode(*function_declarations.front().match);
const auto* lifetime = GetFunctionLifetime(function_node);
EXPECT_EQ(lifetime, nullptr);
}
TEST(GetFunctionLifetimeTest, StaticLifetimeDeclared) {
VerilogAnalyzer analyzer("function static foo(); endfunction", "");
ASSERT_OK(analyzer.Analyze());
// Root node is a description list, not a function.
const auto& root = analyzer.Data().SyntaxTree();
const auto function_declarations = FindAllFunctionDeclarations(*root);
ASSERT_EQ(function_declarations.size(), 1);
const auto& function_node =
SymbolCastToNode(*function_declarations.front().match);
const auto* lifetime = GetFunctionLifetime(function_node);
CheckSymbolAsLeaf(*ABSL_DIE_IF_NULL(lifetime), TK_static);
// TODO(b/151371397): verify substring range
}
TEST(GetFunctionLifetimeTest, AutomaticLifetimeDeclared) {
VerilogAnalyzer analyzer("function automatic foo(); endfunction", "");
ASSERT_OK(analyzer.Analyze());
// Root node is a description list, not a function.e
const auto& root = analyzer.Data().SyntaxTree();
const auto function_declarations = FindAllFunctionDeclarations(*root);
ASSERT_EQ(function_declarations.size(), 1);
const auto& function_node =
SymbolCastToNode(*function_declarations.front().match);
const auto* lifetime = GetFunctionLifetime(function_node);
CheckSymbolAsLeaf(*ABSL_DIE_IF_NULL(lifetime), TK_automatic);
// TODO(b/151371397): verify substring range
}
TEST(GetFunctionIdTest, UnqualifiedIds) {
constexpr int kTag = 1; // don't care
const SyntaxTreeSearchTestCase kTestCases[] = {
{"function ", {kTag, "foo"}, "(); endfunction"},
{"function automatic ", {kTag, "bar"}, "(); endfunction"},
{"function static ", {kTag, "baz"}, "(); endfunction"},
{"package p; function ", {kTag, "foo"}, "(); endfunction endpackage"},
{"class c; function ", {kTag, "zoo"}, "(); endfunction endclass"},
{"function ", {kTag, "myclass"}, "::", {kTag, "foo"}, "(); endfunction"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto function_declarations = FindAllFunctionDeclarations(*root);
std::vector<TreeSearchMatch> got_ids;
for (const auto& decl : function_declarations) {
const auto& function_node = SymbolCastToNode(*decl.match);
const auto* function_id = GetFunctionId(function_node);
for (const auto& id : FindAllUnqualifiedIds(*function_id)) {
const verible::SyntaxTreeLeaf* base = GetIdentifier(*id.match);
got_ids.push_back(TreeSearchMatch{base, /* empty context */});
}
}
return got_ids;
});
}
}
TEST(GetFunctionIdTest, QualifiedIds) {
constexpr int kTag = 1; // don't care
const SyntaxTreeSearchTestCase kTestCases[] = {
{"function foo(); endfunction"},
{"function ", {kTag, "myclass::foo"}, "(); endfunction"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto function_declarations = FindAllFunctionDeclarations(*root);
std::vector<TreeSearchMatch> got_ids;
for (const auto& decl : function_declarations) {
const auto& function_node = SymbolCastToNode(*decl.match);
const auto* function_id = GetFunctionId(function_node);
for (const auto& id : FindAllQualifiedIds(*function_id)) {
got_ids.push_back(id);
}
}
return got_ids;
});
}
}
struct SubtreeTestData {
NodeEnum expected_construct;
verible::TokenInfoTestData token_data;
};
TEST(GetFunctionReturnTypeTest, VariousReturnTypes) {
constexpr int kTag = 1; // don't care
const SyntaxTreeSearchTestCase kTestCases[] = {
// implicit/missing return types
{"function f;endfunction\n"},
{"package p;\nfunction f;\nendfunction\nendpackage\n"},
{"class c;\nfunction f;\nendfunction\nendclass\n"},
{"module m;\nfunction f;\nendfunction\nendmodule\n"},
// explicit return types
{"function ", {kTag, "void"}, " f;endfunction\n"},
{"package p;\nfunction ",
{kTag, "int"},
" f;\nendfunction\nendpackage\n"},
{"class c;\nfunction ",
{kTag, "foo_pkg::bar_t"},
" f;\nendfunction\nendclass\n"},
{"module m;\nfunction ",
{kTag, "foo#(bar)"},
" f;\nendfunction\nendmodule\n"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto decls = FindAllFunctionDeclarations(*root);
std::vector<TreeSearchMatch> returns;
for (const auto& decl : decls) {
const auto& statement = *decl.match;
const auto* return_type = GetFunctionReturnType(statement);
// Expect a type node, even when type is implicit or empty.
if (return_type == nullptr) continue;
const auto return_type_span =
verible::StringSpanOfSymbol(*return_type);
if (!return_type_span.empty()) {
returns.push_back(TreeSearchMatch{return_type, /* no context */});
}
}
return returns;
});
}
}
TEST(GetFunctionFormalPortsGroupTest, MixedFormalPorts) {
constexpr int kTag = 1; // don't care
const SyntaxTreeSearchTestCase kTestCases[] = {
// no ports
{"function f;endfunction\n"},
{"package p;\nfunction f;\nendfunction\nendpackage\n"},
{"class c;\nfunction f;\nendfunction\nendclass\n"},
{"module m;\nfunction f;\nendfunction\nendmodule\n"},
// with ports
{"function f", {kTag, "()"}, ";endfunction\n"},
{"package p;\nfunction f",
{kTag, "(string s)"},
";\nendfunction\nendpackage\n"},
{"class c;\nfunction f",
{kTag, "(int i, string s)"},
";\nendfunction\nendclass\n"},
{"module m;\nfunction f",
{kTag, "(input logic foo, bar)"},
";\nendfunction\nendmodule\n"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto decls = FindAllFunctionDeclarations(*root);
std::vector<TreeSearchMatch> ports;
for (const auto& decl : decls) {
const auto& statement = *decl.match;
const auto* port_formals = GetFunctionFormalPortsGroup(statement);
if (port_formals == nullptr) continue;
const auto port_formals_span =
verible::StringSpanOfSymbol(*port_formals);
if (port_formals_span.empty()) continue;
ports.push_back(TreeSearchMatch{port_formals, /* no context */});
}
return ports;
});
}
}
TEST(GetFunctionHeaderTest, GetFunctionName) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"module m(); endmodule: m"},
{"function ",
{kTag, "foo"},
"();\n endfunction\n function ",
{kTag, "bar"},
"()\n; endfunction"},
{"function int ",
{kTag, "foo"},
"();\n endfunction\n function ",
{kTag, "bar"},
"()\n; endfunction"},
{"function int ",
{kTag, "foo"},
"(int a, bit x);\nreturn a;\n endfunction\n function ",
{kTag, "bar"},
"()\n; endfunction"},
{"module my_module;\nfunction int ",
{kTag, "inner_function"},
"(int my_args);\nreturn my_args;\nendfunction\nendmodule"},
{"class function_class;\nfunction int ",
{kTag, "my_function"},
"(input int a, b, output int c);\nc = a + b;\nreturn "
"c;\nendfunction\nendclass"},
{"package my_pkg;\nfunction automatic int ",
{kTag, "my_function"},
"(input int a, b, output int c);\nc = a + b;\nreturn "
"c;\nendfunction\nendpackage"},
{"class m;\n virtual function int ",
{kTag, "my_fun"},
"();\n return 10;\n endfunction\n endclass"},
{"class m;\n static function int ",
{kTag, "my_fun"},
"();\n return 10;\n endfunction\n endclass"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto decls =
FindAllFunctionDeclarations(*ABSL_DIE_IF_NULL(root));
std::vector<TreeSearchMatch> types;
for (const auto& decl : decls) {
const auto* type = GetFunctionName(*decl.match);
types.push_back(TreeSearchMatch{type, {/* ignored context */}});
}
return types;
});
}
}
TEST(GetFunctionHeaderTest, GetFunctionClassCallName) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"module m(); endmodule: m"},
{"module m();\n initial $display(my_class.",
{kTag, "function_name"},
"());\nendmodule"},
{"module m();\n initial "
"$display(pkg::my_class.",
{kTag, "function_name"},
"());\nendmodule"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto calls =
FindAllFunctionOrTaskCallsExtension(*ABSL_DIE_IF_NULL(root));
std::vector<TreeSearchMatch> names;
for (const auto& Call : calls) {
const auto* name =
GetFunctionCallNameFromCallExtension(*Call.match);
names.emplace_back(TreeSearchMatch{name, {/* ignored context */}});
}
return names;
});
}
}
TEST(GetFunctionBlockStatement, GetFunctionBody) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"module m(); endmodule: m"},
{"function int foo(int a, bit x);\n",
{kTag, "return a;"},
" endfunction\n"},
{"module my_module;\nfunction int inner_function(int my_args);\n",
{kTag, "return my_args;"},
"\nendfunction\nendmodule"},
{"class function_class;\nfunction int my_function(input int a, b, output "
"int c);\n",
{kTag, "c = a + b;\nreturn c;"},
"\nendfunction\nendclass"},
{"package my_pkg;\nfunction automatic int my_function(input int a, b, "
"output int c);\n",
{kTag, "c = a + b;\nreturn c;"},
"\nendfunction\nendpackage"},
{"class m;\n virtual function int my_fun();\n ",
{kTag, "return 10;"},
"\n endfunction\n endclass"},
{"class m;\n static function int my_fun();\n ",
{kTag, "return 10;"},
"\n endfunction\n endclass"},
{"function int f;\n", {kTag, "return 1;"}, "\nendfunction"},
{"class s;\nfunction int f;",
{kTag, "return 1;"},
"\nendfunction\nendclass"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto decls =
FindAllFunctionDeclarations(*ABSL_DIE_IF_NULL(root));
std::vector<TreeSearchMatch> functions_body;
for (const auto& decl : decls) {
const auto& body = GetFunctionBlockStatementList(*decl.match);
functions_body.push_back(
TreeSearchMatch{body, {/* ignored context */}});
}
return functions_body;
});
}
}
TEST(FunctionCallTest, GetFunctionCallName) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"module m;\nendmodule"},
{"module m;\ninitial begin\n", {kTag, "f1"}, "();\nend\nendmodule"},
{"module m;\ninitial begin\n", {kTag, "pkg::f1"}, "();\nend\nendmodule"},
{"module m;\ninitial begin\n",
{kTag, "class_name#(1)::f1"},
"(a, b, c);\nend\nendmodule"},
{"r=this();"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto& calls =
FindAllFunctionOrTaskCalls(*ABSL_DIE_IF_NULL(root));
std::vector<TreeSearchMatch> identifiers;
for (const auto& call : calls) {
const auto* identifier =
GetIdentifiersFromFunctionCall(*call.match);
if (identifier == nullptr) {
continue;
}
identifiers.emplace_back(
TreeSearchMatch{identifier, {/* ignored context */}});
}
return identifiers;
});
}
} // namespace
TEST(FunctionCallTest, GetFunctionCallArguments) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{"module m;\ninitial foo", {kTag, "()"}, ";\nendmodule"},
{"module m;\ninitial foo", {kTag, "(a, b, c)"}, ";\nendmodule"},
{"class c;\nfunction foo();\nendfunction\nfunction "
"bar();\nfoo",
{kTag, "()"},
";\nendfunction\ntask tsk(x, y);\nendtask\ntask "
"task_2();\ntsk",
{kTag, "(a, b)"},
";\nendtask\nendclass"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto& instances =
FindAllFunctionOrTaskCalls(*ABSL_DIE_IF_NULL(root));
std::vector<TreeSearchMatch> paren_groups;
for (const auto& decl : instances) {
const auto* paren_group = GetParenGroupFromCall(*decl.match);
paren_groups.emplace_back(
TreeSearchMatch{paren_group, {/* ignored context */}});
}
return paren_groups;
});
}
}
TEST(FunctionCallTest, GetFunctionCallExtensionArguments) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"module m(); endmodule: m"},
{"module m();\n initial $display(my_class.function_name",
{kTag, "(x, y)"},
");\nendmodule"},
{"module m();\n initial "
"$display(pkg::my_class.function_name",
{kTag, "()"},
");\nendmodule"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto& instances =
FindAllFunctionOrTaskCallsExtension(*ABSL_DIE_IF_NULL(root));
std::vector<TreeSearchMatch> paren_groups;
for (const auto& decl : instances) {
const auto* paren_group =
GetParenGroupFromCallExtension(*decl.match);
paren_groups.emplace_back(
TreeSearchMatch{paren_group, {/* ignored context */}});
}
return paren_groups;
});
}
}
TEST(FunctionCallTest, GetConstructorNewKeyword) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"class c; endclass: c"},
{"class c;\n"
"function int foo();\n"
"endfunction\n"
"endclass\n"},
{"class c;\n"
"extern function int foo();\n"
"endclass\n"},
{"class c;\n"
"extern function ",
{kTag, "new"},
";\n"
"endclass\n"},
{"class c;\n"
"extern function ",
{kTag, "new"},
"();\n"
"endclass\n"},
{"class c;\n"
"extern function ",
{kTag, "new"},
"(string name);\n"
"endclass\n"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto& ctors =
FindAllConstructorPrototypes(*ABSL_DIE_IF_NULL(root));
std::vector<TreeSearchMatch> new_tokens;
for (const auto& decl : ctors) {
const auto* paren_group =
GetConstructorPrototypeNewKeyword(*decl.match);
new_tokens.emplace_back(
TreeSearchMatch{paren_group, {/* ignored context */}});
}
return new_tokens;
});
}
}
} // namespace
} // namespace verilog