forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverilog_matchers_test.cc
706 lines (667 loc) · 30.1 KB
/
verilog_matchers_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
// 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/verilog_matchers.h"
#include "common/analysis/matcher/core_matchers.h"
#include "common/analysis/matcher/matcher.h"
#include "common/analysis/matcher/matcher_builders.h"
#include "common/analysis/matcher/matcher_test_utils.h"
#include "gtest/gtest.h"
#include "verilog/CST/verilog_treebuilder_utils.h"
#include "verilog/analysis/verilog_analyzer.h"
namespace verilog {
namespace {
using verible::matcher::RawMatcherTestCase;
// Tests for SystemTFIdentifierLeaf matching
TEST(VerilogMatchers, SystemTFIdentifierLeafTests) {
const RawMatcherTestCase tests[] = {
{SystemTFIdentifierLeaf(), EmbedInClassMethod("$psprintf(\"foo\");"), 1},
{SystemTFIdentifierLeaf(), EmbedInClassMethod("psprintf(\"foo\");"), 0},
{SystemTFIdentifierLeaf(), EmbedInClass(""), 0},
{SystemTFIdentifierLeaf(), "", 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for MacroCallIdLeaf matching
TEST(VerilogMatchers, MacroCallIdLeafTests) {
const RawMatcherTestCase tests[] = {
{MacroCallIdLeaf(), "", 0},
{MacroCallIdLeaf(), EmbedInClass(""), 0},
{MacroCallIdLeaf(), EmbedInClassMethod("`uvm_foo"), 0}, // not a call
{MacroCallIdLeaf(), "`uvm_foo(\"foo\");", 1},
{MacroCallIdLeaf(), "`uvm_foo(\"foo\")\n", 1},
{MacroCallIdLeaf(), EmbedInClassMethod("`uvm_foo(\"foo\");"), 1},
{MacroCallIdLeaf(), EmbedInClassMethod("`uvm_foo(\"foo\")\n"), 1},
{MacroCallIdLeaf(), EmbedInClassMethod("uvm_foo(\"foo\");"), 0},
{MacroCallIdLeaf(), EmbedInClassMethod("$uvm_foo(\"foo\");"), 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for SymbolIdentifierLeaf matching
TEST(VerilogMatchers, SymbolIdentifierLeaf) {
const RawMatcherTestCase tests[] = {
{SymbolIdentifierLeaf(), "", 0},
{SymbolIdentifierLeaf(), EmbedInClass(""), 1}, // +1 by the class name
{SymbolIdentifierLeaf(), EmbedInClassMethod("reg foo;"),
3}, // count +2 by class & method names
{SymbolIdentifierLeaf(), EmbedInClassMethod("uvm_foo(\"foo\");"), 3},
{SymbolIdentifierLeaf(), "parameter foo = 32'hDEADBEEF;", 1},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for NodekVoidcast matching
TEST(VerilogMatchers, VoidCastNodeTests) {
const RawMatcherTestCase tests[] = {
{NodekVoidcast(), EmbedInClassMethod("void'(bad());"), 1},
{NodekVoidcast(), EmbedInClassMethod("rar(bad());"), 0},
{NodekVoidcast(), EmbedInClass(""), 0},
{NodekVoidcast(), "", 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for NodekExpression matching
TEST(VerilogMatchers, ExpressionNodeTests) {
const RawMatcherTestCase tests[] = {
{NodekExpression(), EmbedInClassMethod("x = 1;"), 1},
{NodekExpression(), EmbedInClassMethod("foo();"), 0},
{NodekExpression(), EmbedInClass(""), 0},
{NodekExpression(), "", 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for ExpressionHasFunctionCall matching
TEST(VerilogMatchers, ExpressionHasFunctionCallTests) {
const RawMatcherTestCase tests[] = {
{ExpressionHasFunctionCall(), EmbedInClassMethod("foo();"), 1},
{ExpressionHasFunctionCall(), EmbedInClassMethod("x = foo();"), 1},
{ExpressionHasFunctionCall(), EmbedInClassMethod("foo(bar);"), 1},
{ExpressionHasFunctionCall(), EmbedInClassMethod("foo(bar, baz);"), 1},
{ExpressionHasFunctionCall(), EmbedInClassMethod("x = foo;"), 0},
{ExpressionHasFunctionCall(), EmbedInClass(""), 0},
{ExpressionHasFunctionCall(), "", 0},
// Qualified Id function call:
{ExpressionHasFunctionCall(), EmbedInClassMethod("bar::foo();"), 1},
{ExpressionHasFunctionCall(), EmbedInClassMethod("x = bar::foo();"), 1},
// This is a method call, different from a function call:
{ExpressionHasFunctionCall(), EmbedInClassMethod("bar.foo();"), 0},
{ExpressionHasFunctionCall(), EmbedInClassMethod("x = bar.foo();"), 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for ExpressionHasRandomizeCallExtension matching
TEST(VerilogMatchers, ExpressionHasRandomizeCallExtensionTests) {
const RawMatcherTestCase tests[] = {
{ExpressionHasRandomizeCallExtension(),
EmbedInClassMethod("foo().randomize();"), 1},
{ExpressionHasRandomizeCallExtension(), EmbedInClassMethod("foo;"), 0},
{ExpressionHasRandomizeCallExtension(),
EmbedInClassMethod("randomize();"), 0},
{ExpressionHasRandomizeCallExtension(), EmbedInClass(""), 0},
{ExpressionHasRandomizeCallExtension(), "", 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for ExpressionHasRandomizeFunction matching
TEST(VerilogMatchers, ExpressionHasRandomizeFunctionTests) {
const RawMatcherTestCase tests[] = {
{ExpressionHasRandomizeFunction(), EmbedInClassMethod("randomize();"), 1},
{ExpressionHasRandomizeFunction(), EmbedInClassMethod("foo.randomize();"),
0},
{ExpressionHasRandomizeFunction(), EmbedInClass(""), 0},
{ExpressionHasRandomizeFunction(), "", 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for FunctionCallHasId matching
TEST(VerilogMatchers, FunctionCallHasIdTests) {
const RawMatcherTestCase tests[] = {
{FunctionCallHasId(), EmbedInClassMethod("foo();"), 1},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for NumberHasConstantWidth matching
TEST(VerilogMatchers, NumberHasConstantWidthTests) {
const RawMatcherTestCase tests[] = {
{NumberHasConstantWidth(), "", 0},
{NumberHasConstantWidth(), "localparam x = 1'bx;", 1},
{NumberHasConstantWidth(), "localparam x = 2'b 01;", 1},
{NumberHasConstantWidth(), "localparam x = 8'b0000_1111;", 1},
{NumberHasConstantWidth(), "localparam x = `WIDTH'bx;", 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for NumberHasBasedLiteral matching
TEST(VerilogMatchers, NumberHasBasedLiteralTests) {
const RawMatcherTestCase tests[] = {
{NumberHasBasedLiteral(), "", 0},
{NumberHasBasedLiteral(), "localparam x = 1'0;", 0},
{NumberHasBasedLiteral(), "localparam x = 1'b0;", 1},
{NumberHasBasedLiteral(), "localparam x = 1'B1;", 1},
{NumberHasBasedLiteral(), "localparam x = 4'b 10_10;", 1},
{NumberHasBasedLiteral(), "localparam x = 8'b0000_1111;", 1},
{NumberHasBasedLiteral(), "localparam x = `WIDTH'bx;", 1},
{NumberHasBasedLiteral(), "localparam x = 2'sb0;", 1},
{NumberHasBasedLiteral(), "localparam x = 2'SB1;", 1},
{NumberHasBasedLiteral(), "localparam x = 32'd 2000;", 1},
{NumberHasBasedLiteral(), "localparam x = 32'D 4095;", 1},
{NumberHasBasedLiteral(), "localparam x = 32'o 66666;", 1},
{NumberHasBasedLiteral(), "localparam x = 32'O 777_777;", 1},
{NumberHasBasedLiteral(), "localparam x = 32'h 0000_4321;", 1},
{NumberHasBasedLiteral(), "localparam x = 64'H aaaa_bbbb_cccc_dddd;", 1},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for NumberIsBinary matching
TEST(VerilogMatchers, NumberIsBinaryTests) {
const RawMatcherTestCase tests[] = {
{NumberIsBinary(), "", 0},
{NumberIsBinary(), "localparam x = 1'0;", 0},
{NumberIsBinary(), "localparam x = 1'b0;", 1},
{NumberIsBinary(), "localparam x = 1'B1;", 1},
{NumberIsBinary(), "localparam x = 4'b 10_10;", 1},
{NumberIsBinary(), "localparam x = 8'b0000_1111;", 1},
{NumberIsBinary(), "localparam x = `WIDTH'bx;", 1},
{NumberIsBinary(), "localparam x = `WIDTH'b`DIGITS;", 1},
{NumberIsBinary(), "localparam x = 2'sb0;", 1},
{NumberIsBinary(), "localparam x = 2'SB1;", 1},
{NumberIsBinary(), "localparam x = 32'd 2000;", 0},
{NumberIsBinary(), "localparam x = 32'D 4095;", 0},
{NumberIsBinary(), "localparam x = 32'o 66666;", 0},
{NumberIsBinary(), "localparam x = 32'O 777_777;", 0},
{NumberIsBinary(), "localparam x = 32'h 0000_4321;", 0},
{NumberIsBinary(), "localparam x = 64'H aaaa_bbbb_cccc_dddd;", 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for NumberHasBInaryDigits matching
TEST(VerilogMatchers, NumberHasBinaryDigitsTests) {
const RawMatcherTestCase tests[] = {
{NumberHasBinaryDigits(), "", 0},
{NumberHasBinaryDigits(), "localparam x = 1'0;", 0},
{NumberHasBinaryDigits(), "localparam x = 1'b0;", 1},
{NumberHasBinaryDigits(), "localparam x = 1'B1;", 1},
{NumberHasBinaryDigits(), "localparam x = 4'b 10_10;", 1},
{NumberHasBinaryDigits(), "localparam x = 8'b0000_1111;", 1},
{NumberHasBinaryDigits(), "localparam x = `WIDTH'bx;", 1},
{NumberHasBinaryDigits(), "localparam x = `WIDTH'b`DIGITS;", 0},
{NumberHasBinaryDigits(), "localparam x = 2'sb0;", 1},
{NumberHasBinaryDigits(), "localparam x = 2'SB1;", 1},
{NumberHasBinaryDigits(), "localparam x = 32'd 2000;", 0},
{NumberHasBinaryDigits(), "localparam x = 32'D 4095;", 0},
{NumberHasBinaryDigits(), "localparam x = 32'o 66666;", 0},
{NumberHasBinaryDigits(), "localparam x = 32'O 777_777;", 0},
{NumberHasBinaryDigits(), "localparam x = 32'h 0000_4321;", 0},
{NumberHasBinaryDigits(), "localparam x = 64'H aaaa_bbbb_cccc_dddd;", 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for NodekActualParameterList matching
TEST(VerilogMatchers, ActualParameterListNodeTests) {
const RawMatcherTestCase tests[] = {
{NodekActualParameterList(), EmbedInModule("foo #(1, 2) bar;"), 1},
{NodekActualParameterList(),
EmbedInModule("foo #(.foo(1), .bar(5)) bar;"), 1},
{NodekActualParameterList(), EmbedInModule("foo bar;"), 0},
{NodekActualParameterList(), EmbedInModule(""), 0},
{NodekActualParameterList(), "", 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for ActualParameterListHasPositionalParameterList matching
TEST(VerilogMatchers, ActualParameterListHasPositionalParameterListTests) {
const RawMatcherTestCase tests[] = {
{ActualParameterListHasPositionalParameterList(),
EmbedInModule("foo #(1, 2) bar;"), 1},
{ActualParameterListHasPositionalParameterList(),
EmbedInModule("foo #(.foo(1), .bar(5)) bar;"), 0},
{ActualParameterListHasPositionalParameterList(), EmbedInModule(""), 0},
{ActualParameterListHasPositionalParameterList(), "", 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for NodekGateInstance matching
TEST(VerilogMatchers, GateInstanceNodeTests) {
const RawMatcherTestCase tests[] = {
{NodekGateInstance(), EmbedInModule("foo bar(1, 2);"), 1},
{NodekGateInstance(), EmbedInModule("foo bar;"), 0},
{NodekGateInstance(), EmbedInModule("and a0(a, b, x1);"), 0},
{NodekGateInstance(), EmbedInModule(""), 0},
{NodekGateInstance(), "", 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for GateInstanceHasPortList matching
TEST(VerilogMatchers, GateInstanceHasPortListTests) {
const RawMatcherTestCase tests[] = {
{GateInstanceHasPortList(), EmbedInModule("foo bar(1, 2);"), 1},
{GateInstanceHasPortList(), EmbedInModule("foo bar;"), 0},
{GateInstanceHasPortList(), EmbedInModule(""), 0},
{GateInstanceHasPortList(), "", 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for NodekGenerateBlock matching
TEST(VerilogMatchers, GenerateBlockNodeTests) {
const RawMatcherTestCase tests[] = {
{NodekGenerateBlock(),
EmbedInModule("generate\n"
"if (TypeIsPosedge) begin : gen_posedge\n"
" always @(posedge clk) foo <= bar;\n"
"end\n"
"endgenerate"),
1},
{NodekGenerateBlock(),
EmbedInModule("generate\n"
"endgenerate"),
0},
{NodekGenerateBlock(), EmbedInModule(""), 0},
{NodekGenerateBlock(), "", 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for GenerateRegionNode matching
TEST(VerilogMatchers, GenerateRegionNodeTests) {
const RawMatcherTestCase tests[] = {
{NodekGenerateRegion(),
EmbedInModule("generate\n"
"if (TypeIsPosedge) begin : foobar\n"
" foo bar;\n"
"end\n"
"endgenerate"),
1},
{NodekGenerateRegion(),
EmbedInModule("generate\n"
"endgenerate"),
1},
{NodekGenerateRegion(), EmbedInModule("wire rats_nest;"), 0},
{NodekGenerateRegion(), EmbedInModule(""), 0},
{NodekGenerateRegion(), "", 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for HasBeginLabel matching
TEST(VerilogMatchers, HasBeginLabelTests) {
const RawMatcherTestCase tests[] = {
{HasBeginLabel(),
EmbedInModule("generate\n"
"if (TypeIsPosedge) begin : gen_posedge\n"
" always @(posedge clk) foo <= bar;\n"
"end\n"
"endgenerate"),
1},
{HasBeginLabel(),
EmbedInModule("generate\n"
"if (TypeIsPosedge) begin\n"
" always @(posedge clk) foo <= bar;\n"
"end\n"
"endgenerate"),
0},
{HasBeginLabel(), EmbedInModule(""), 0},
{HasBeginLabel(), "", 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for HasGenerateBlock matching
TEST(VerilogMatchers, HasGenerateBlockTests) {
const RawMatcherTestCase tests[] = {
{HasGenerateBlock(), "", 0},
{HasGenerateBlock(), EmbedInModule(""), 0},
{HasGenerateBlock(),
EmbedInModule("generate\n"
"begin\n"
" always @(posedge clk) foo <= bar;\n"
"end\n"
"endgenerate"),
1},
{HasGenerateBlock(),
EmbedInModule("generate\n"
"begin\n"
" genvar j;\n"
"end\n"
"endgenerate"),
1},
{HasGenerateBlock(),
EmbedInModule("generate\n"
"if (TypeIsPosedge) begin : gen_posedge\n"
" always @(posedge clk) foo <= bar;\n"
"end\n"
"endgenerate"),
0},
{HasGenerateBlock(),
EmbedInModule("generate\n"
"if (TypeIsPosedge) begin\n"
" always @(posedge clk) foo <= bar;\n"
"end\n"
"endgenerate"),
0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for integration between NodekGenerateBlock and HasBeginLabel
TEST(VerilogMatchers, GenerateBlockHasBeginLabelTests) {
const RawMatcherTestCase tests[] = {
{NodekGenerateBlock(verible::matcher::Unless(HasBeginLabel())),
EmbedInModule("generate\n"
"if (TypeIsPosedge) begin\n"
" always @(posedge clk) foo <= bar;\n"
"end\n"
"endgenerate"),
1},
{NodekGenerateBlock(verible::matcher::Unless(HasBeginLabel())),
EmbedInModule("generate\n"
"if (TypeIsPosedge) begin : gen_posedge\n"
" always @(posedge clk) foo <= bar;\n"
"end\n"
"endgenerate"),
0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for NodekAlwaysStatement matching
TEST(VerilogMatchers, AlwaysStatementNodeTests) {
const RawMatcherTestCase tests[] = {
{NodekAlwaysStatement(), EmbedInModule(""), 0},
{NodekAlwaysStatement(), EmbedInModule("initial begin a <= 0; end"), 0},
{NodekAlwaysStatement(), EmbedInModule("always_ff begin a <= b; end"), 1},
{NodekAlwaysStatement(), EmbedInModule("always_comb begin a = b; end"),
1},
{NodekAlwaysStatement(), EmbedInModule("always @* begin a = b; end"), 1},
{NodekAlwaysStatement(), EmbedInModule("always @(*) begin a = b; end"),
1},
{NodekAlwaysStatement(),
EmbedInModule("always @(posedge foo) begin a <= b; end"), 1},
{NodekAlwaysStatement(),
EmbedInModule("always_ff begin a <= b; end\n"
"always_comb begin a = b; end"),
2},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for AlwaysKeyword matching
TEST(VerilogMatchers, AlwaysKeywordTests) {
const RawMatcherTestCase tests[] = {
{AlwaysKeyword(), EmbedInModule(""), 0},
{AlwaysKeyword(), EmbedInModule("initial begin a <= 0; end"), 0},
{AlwaysKeyword(), EmbedInModule("always_ff begin a <= b; end"), 0},
{AlwaysKeyword(), EmbedInModule("always_comb begin a = b; end"), 0},
{AlwaysKeyword(), EmbedInModule("always @* begin a = b; end"), 1},
{AlwaysKeyword(), EmbedInModule("always @(*) begin a = b; end"), 1},
{AlwaysKeyword(),
EmbedInModule("always @(posedge foo) begin a <= b; end"), 1},
{AlwaysKeyword(),
EmbedInModule("always_ff begin a <= b; end\n"
"always_comb begin a = b; end"),
0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for AlwaysCombKeyword matching
TEST(VerilogMatchers, AlwaysCombKeywordTests) {
const RawMatcherTestCase tests[] = {
{AlwaysCombKeyword(), EmbedInModule(""), 0},
{AlwaysCombKeyword(), EmbedInModule("initial begin a <= 0; end"), 0},
{AlwaysCombKeyword(), EmbedInModule("always_ff begin a <= b; end"), 0},
{AlwaysCombKeyword(), EmbedInModule("always_comb begin a = b; end"), 1},
{AlwaysCombKeyword(), EmbedInModule("always @* begin a = b; end"), 0},
{AlwaysCombKeyword(), EmbedInModule("always @(*) begin a = b; end"), 0},
{AlwaysCombKeyword(),
EmbedInModule("always @(posedge foo) begin a <= b; end"), 0},
{AlwaysCombKeyword(),
EmbedInModule("always_ff begin a <= b; end\n"
"always_comb begin a = b; end"),
1},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for AlwaysFFKeyword matching
TEST(VerilogMatchers, AlwaysFFKeywordTests) {
const RawMatcherTestCase tests[] = {
{AlwaysFFKeyword(), EmbedInModule(""), 0},
{AlwaysFFKeyword(), EmbedInModule("initial begin a <= 0; end"), 0},
{AlwaysFFKeyword(), EmbedInModule("always_ff begin a <= b; end"), 1},
{AlwaysFFKeyword(), EmbedInModule("always_comb begin a = b; end"), 0},
{AlwaysFFKeyword(), EmbedInModule("always @* begin a = b; end"), 0},
{AlwaysFFKeyword(), EmbedInModule("always @(*) begin a = b; end"), 0},
{AlwaysFFKeyword(),
EmbedInModule("always @(posedge foo) begin a <= b; end"), 0},
{AlwaysFFKeyword(),
EmbedInModule("always_ff begin a <= b; end\n"
"always_comb begin a = b; end"),
1},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for AlwaysStatementHasEventControlStar matching
TEST(VerilogMatchers, AlwaysStatementHasEventControlStarTests) {
const RawMatcherTestCase tests[] = {
{AlwaysStatementHasEventControlStar(), EmbedInModule(""), 0},
{AlwaysStatementHasEventControlStar(),
EmbedInModule("initial begin a <= 0; end"), 0},
{AlwaysStatementHasEventControlStar(),
EmbedInModule("always_ff begin a <= b; end"), 0},
{AlwaysStatementHasEventControlStar(),
EmbedInModule("always_comb begin a = b; end"), 0},
{AlwaysStatementHasEventControlStar(),
EmbedInModule("always @* begin a = b; end"), 1},
{AlwaysStatementHasEventControlStar(),
EmbedInModule("always @(*) begin a = b; end"), 1},
{AlwaysStatementHasEventControlStar(),
EmbedInModule("always @(posedge foo) begin a <= b; end"), 0},
{AlwaysStatementHasEventControlStar(),
EmbedInModule("always_ff begin a <= b; end\n"
"always_comb begin a = b; end"),
0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests that matcher finds left-hand-sides of assignments.
TEST(VerilogMatchers, PathkLPValueTests) {
const RawMatcherTestCase tests[] = {
{PathkLPValue(), EmbedInClassMethod(""), 0},
{PathkLPValue(), EmbedInClassMethod("foo();"), 0},
{PathkLPValue(), EmbedInClassMethod("x = 1;"), 1},
{PathkLPValue(), EmbedInClassMethod("x = a + b;"), 1},
{PathkLPValue(), EmbedInModule("initial begin\nfoo();\nend\n"), 0},
{PathkLPValue(), EmbedInModule("initial begin\nx = 1;\nend\n"), 1},
{PathkLPValue(), EmbedInClassMethod("x[1] = 1;"), 1},
{PathkLPValue(), EmbedInClassMethod("x.y = 2;"), 1},
{PathkLPValue(), EmbedInClassMethod("x[0].y = 3;"), 1},
{PathkLPValue(), EmbedInClassMethod("x.y[2] = 4;"), 1},
{PathkLPValue(), EmbedInClassMethod("x[0].y[0] = 5;"), 1},
{PathkLPValue(), EmbedInClassMethod("if (0) x = 1;"), 1},
{PathkLPValue(), EmbedInClassMethod("forever x = y;"), 1},
{PathkLPValue(), EmbedInClassMethod("x = 1;\ny = two();"), 2},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests that matcher finds assignments values that are function calls.
TEST(VerilogMatchers, RValueIsFunctionCallTest) {
const RawMatcherTestCase tests[] = {
{RValueIsFunctionCall(), EmbedInModule(""), 0},
{RValueIsFunctionCall(), EmbedInClassMethod(""), 0},
{RValueIsFunctionCall(), EmbedInClassMethod("x = 1;"), 0},
{RValueIsFunctionCall(), EmbedInClassMethod("x = y / z;"), 0},
{RValueIsFunctionCall(), EmbedInClassMethod("x = y / z();"), 0},
{RValueIsFunctionCall(), EmbedInClassMethod("x = foo();"), 1},
{RValueIsFunctionCall(), EmbedInClassMethod("x = bar::foo();"), 1},
{RValueIsFunctionCall(), EmbedInClassMethod("x = bar::foo(a);"), 1},
{RValueIsFunctionCall(), EmbedInClassMethod("x.y = bar::foo(a);"), 1},
{RValueIsFunctionCall(), EmbedInClassMethod("x = bar.foo();"), 0},
{RValueIsFunctionCall(), EmbedInClassMethod("z = pkg::bar::foo();"), 1},
{RValueIsFunctionCall(), EmbedInClassMethod("x = bar::foo() -12;"), 0},
{RValueIsFunctionCall(), EmbedInClassMethod("x = a + bar::foo();"), 0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests that qualified function calls are found.
TEST(VerilogMatchers, FunctionCallIsQualifiedTest) {
const RawMatcherTestCase tests[] = {
{FunctionCallIsQualified(), EmbedInModule(""), 0},
{FunctionCallIsQualified(), EmbedInClassMethod(""), 0},
{FunctionCallIsQualified(), EmbedInClassMethod("x = 1;"), 0},
{FunctionCallIsQualified(), EmbedInClassMethod("x = y / z;"), 0},
{FunctionCallIsQualified(), EmbedInClassMethod("x = y / z();"), 0},
{FunctionCallIsQualified(), EmbedInClassMethod("x = foo();"), 0},
{FunctionCallIsQualified(), EmbedInClassMethod("x = bar::foo();"), 1},
{FunctionCallIsQualified(), EmbedInClassMethod("x = bar::foo(a);"), 1},
{FunctionCallIsQualified(), EmbedInClassMethod("x.y = bar::foo(a);"), 1},
{FunctionCallIsQualified(), EmbedInClassMethod("x = bar.foo();"), 0},
{FunctionCallIsQualified(), EmbedInClassMethod("z = pkg::bar::foo();"),
1},
{FunctionCallIsQualified(), EmbedInClassMethod("x = bar::foo() -12;"), 1},
{FunctionCallIsQualified(), EmbedInClassMethod("x = a + bar::foo();"), 1},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests that assignments to qualified function calls are found.
TEST(VerilogMatchers, RValueFunctionCallIsQualifiedTest) {
const RawMatcherTestCase tests[] = {
{RValueIsFunctionCall(FunctionCallIsQualified()), EmbedInModule(""), 0},
{RValueIsFunctionCall(FunctionCallIsQualified()), EmbedInClassMethod(""),
0},
{RValueIsFunctionCall(FunctionCallIsQualified()),
EmbedInClassMethod("x = 1;"), 0},
{RValueIsFunctionCall(FunctionCallIsQualified()),
EmbedInClassMethod("x = y / z;"), 0},
{RValueIsFunctionCall(FunctionCallIsQualified()),
EmbedInClassMethod("x = y / z();"), 0},
{RValueIsFunctionCall(FunctionCallIsQualified()),
EmbedInClassMethod("x = foo();"), 0},
{RValueIsFunctionCall(FunctionCallIsQualified()),
EmbedInClassMethod("x = bar::foo();"), 1},
{RValueIsFunctionCall(FunctionCallIsQualified()),
EmbedInClassMethod("bar::foo();"), 0}, // no assignment
// TODO(fangism): The following test case wrongly matches, but only
// because the matcher matches two different function calls: qqq is the
// outermost match, but the recursive inner match finds a qualified
// function call deeper in the syntax tree. Solution is to use a direct
// inner matcher that does not search recursively.
// {RValueIsFunctionCall(FunctionCallIsQualified()),
// EmbedInClassMethod("qqq(bar::foo(), 1, 2);"), 0},
{RValueIsFunctionCall(FunctionCallIsQualified()),
EmbedInClassMethod("y = z();\nbar::foo();"),
0}, // no matching assignment
{RValueIsFunctionCall(FunctionCallIsQualified()),
EmbedInClassMethod("x = bar::foo(a);"), 1},
{RValueIsFunctionCall(FunctionCallIsQualified()),
EmbedInClassMethod("x.y = bar::foo(a);"), 1},
{RValueIsFunctionCall(FunctionCallIsQualified()),
EmbedInClassMethod("x = bar.foo();"), 0},
{RValueIsFunctionCall(FunctionCallIsQualified()),
EmbedInClassMethod("z = pkg::bar::foo();"), 1},
{RValueIsFunctionCall(FunctionCallIsQualified()),
EmbedInClassMethod("z = pkg::bar::foo(\"a\", b, cc);"), 1},
{RValueIsFunctionCall(FunctionCallIsQualified()),
EmbedInClassMethod("x = bar::foo() -12;"), 0}, // outermost rvalue is -
{RValueIsFunctionCall(FunctionCallIsQualified()),
EmbedInClassMethod("x = a + bar::foo();"), 0}, // outermost rvalue is +
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests that function call arguments are matched.
TEST(VerilogMatchers, FunctionCallArgumentsTest) {
const RawMatcherTestCase tests[] = {
{FunctionCallArguments(), EmbedInModule(""), 0},
{FunctionCallArguments(), EmbedInClassMethod(""), 0},
{FunctionCallArguments(), EmbedInClassMethod("a = 1 + 2;"), 0},
{FunctionCallArguments(), EmbedInClassMethod("foobar();"), 0},
{FunctionCallArguments(), EmbedInClassMethod("foobar(1);"), 1},
{FunctionCallArguments(), EmbedInClassMethod("foobar(a, b, c);"), 1},
{FunctionCallArguments(), EmbedInClassMethod("`foobar();"),
0}, // macro call arguments are different
{FunctionCallArguments(), EmbedInClassMethod("`foobar(1);"), 0},
{FunctionCallArguments(), EmbedInClassMethod("`foobar(a, b, c);"), 0},
{FunctionCallArguments(), EmbedInClassMethod("foo(bar());"), 1},
{FunctionCallArguments(), EmbedInClassMethod("x = foobar();"), 0},
{FunctionCallArguments(), EmbedInClassMethod("f.g.h = foobar(g);"), 1},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests that ranged dimensions are matched.
TEST(VerilogMatchers, DeclarationDimensionsHasRanges) {
const RawMatcherTestCase tests[] = {
{DeclarationDimensionsHasRanges(), "", 0},
{DeclarationDimensionsHasRanges(), EmbedInModule(""), 0},
{DeclarationDimensionsHasRanges(), "wire w;", 0},
{DeclarationDimensionsHasRanges(), "wire [1:0] w;", 1},
{DeclarationDimensionsHasRanges(), "wire w [1:0];", 1},
{DeclarationDimensionsHasRanges(), "wire [1:2] w [1:0];", 2},
{DeclarationDimensionsHasRanges(), EmbedInModule("wire w;"), 0},
{DeclarationDimensionsHasRanges(), EmbedInModule("wire [1:0] w;"), 1},
{DeclarationDimensionsHasRanges(), EmbedInModule("wire w [1:0];"), 1},
{DeclarationDimensionsHasRanges(), EmbedInModule("wire [1:2] w [1:0];"),
2},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
// Tests for HasDefaultCase matching.
TEST(VerilogMatchers, HasDefaultCaseTests) {
const RawMatcherTestCase tests[] = {
{HasDefaultCase(), "", 0},
{HasDefaultCase(),
R"(
function automatic int foo (input in);
case (in)
default: return 0;
endcase
endfunction
)",
1},
{HasDefaultCase(),
R"(
function automatic int foo (input in);
case (in)
1: return 0;
endcase
endfunction
)",
0},
};
for (const auto& test : tests)
verible::matcher::RunRawMatcherTestCase<VerilogAnalyzer>(test);
}
} // namespace
} // namespace verilog