forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverilog_linter_test.cc
912 lines (824 loc) · 32.4 KB
/
verilog_linter_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
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
// 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.
// This tests the VerilogLinter class and its associated functions, end-to-end.
//
// Tests for individual lint rules can be found in
// verilog/analysis/checkers/.
#include "verilog/analysis/verilog_linter.h"
#include <algorithm>
#include <array>
#include <fstream>
#include <iterator>
#include <memory>
#include <sstream> // IWYU pragma: keep // for ostringstream
#include <string>
#include <utility>
#include <vector>
#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "common/analysis/violation_handler.h"
#include "common/util/file_util.h"
#include "common/util/logging.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "verilog/analysis/default_rules.h"
#include "verilog/analysis/verilog_analyzer.h"
#include "verilog/analysis/verilog_linter_configuration.h"
namespace verilog {
namespace {
using ::testing::EndsWith;
using ::testing::StartsWith;
using verible::ViolationFixer;
using verible::ViolationPrinter;
using verible::file::GetContents;
using verible::file::testing::ScopedTestFile;
class DefaultLinterConfigTestFixture {
public:
// Test code using the default rule set.
DefaultLinterConfigTestFixture() : config_() {
config_.UseRuleSet(RuleSet::kDefault);
}
protected:
LinterConfiguration config_;
};
class LintOneFileTest : public DefaultLinterConfigTestFixture,
public testing::Test {
public:
LintOneFileTest() = default;
};
// Tests that nonexistent file is handled as a fatal error.
TEST_F(LintOneFileTest, FileNotFound) {
std::ostringstream output;
ViolationPrinter violation_printer(&output);
const int exit_code =
LintOneFile(&output, "FileNotFound.sv", config_, &violation_printer, true,
false, false, false);
EXPECT_EQ(exit_code, 2);
}
// Tests that clean code exits 0 (success).
TEST_F(LintOneFileTest, LintCleanFiles) {
constexpr absl::string_view kTestCases[] = {
"", // empty file
"\n",
"class foo;\n"
"endclass : foo\n",
};
for (const auto test_code : kTestCases) {
const ScopedTestFile temp_file(testing::TempDir(), test_code);
{
std::ostringstream output;
ViolationPrinter violation_printer(&output);
const int exit_code =
LintOneFile(&output, temp_file.filename(), config_,
&violation_printer, true, false, false, false);
EXPECT_EQ(exit_code, 0);
EXPECT_TRUE(output.str().empty()); // silence
}
{ // enable additional error context printing
std::ostringstream output;
ViolationPrinter violation_printer(&output);
const int exit_code =
LintOneFile(&output, temp_file.filename(), config_,
&violation_printer, true, false, false, true);
EXPECT_EQ(exit_code, 0);
EXPECT_TRUE(output.str().empty()); // silence
}
}
}
// Tests that invalid code is handled according to 'parse_fatal' parameter.
TEST_F(LintOneFileTest, SyntaxError) {
constexpr absl::string_view kTestCases[] = {
"class foo;\n", // no endclass
"endclass : foo\n", // no begin class
"module 444bad_name; endmodule\n", // lexical error
};
for (const auto test_code : kTestCases) {
const ScopedTestFile temp_file(testing::TempDir(), test_code);
{ // continue even with syntax error
std::ostringstream output;
ViolationPrinter violation_printer(&output);
const int exit_code =
LintOneFile(&output, temp_file.filename(), config_,
&violation_printer, true, false, false, false);
EXPECT_EQ(exit_code, 0);
EXPECT_FALSE(output.str().empty());
}
{ // continue even with syntax error with additional error context
std::ostringstream output;
ViolationPrinter violation_printer(&output);
const int exit_code =
LintOneFile(&output, temp_file.filename(), config_,
&violation_printer, true, false, false, true);
EXPECT_EQ(exit_code, 0);
EXPECT_FALSE(output.str().empty());
}
{ // abort on syntax error
std::ostringstream output;
ViolationPrinter violation_printer(&output);
const int exit_code =
LintOneFile(&output, temp_file.filename(), config_,
&violation_printer, true, true, false, false);
EXPECT_EQ(exit_code, 1);
EXPECT_FALSE(output.str().empty());
}
{ // ignore syntax error
std::ostringstream output;
ViolationPrinter violation_printer(&output);
const int exit_code =
LintOneFile(&output, temp_file.filename(), config_,
&violation_printer, false, false, false, false);
EXPECT_EQ(exit_code, 0);
EXPECT_TRUE(output.str().empty()); // silence
}
}
}
TEST_F(LintOneFileTest, LintError) {
constexpr absl::string_view kTestCases[] = {
"task automatic foo;\n"
" $psprintf(\"blah\");\n" // forbidden function
"endtask\n",
};
for (const auto test_code : kTestCases) {
const ScopedTestFile temp_file(testing::TempDir(), test_code);
{ // continue even with lint error
std::ostringstream output;
ViolationPrinter violation_printer(&output);
const int exit_code =
LintOneFile(&output, temp_file.filename(), config_,
&violation_printer, true, false, false, false);
EXPECT_EQ(exit_code, 0) << "output:\n" << output.str();
EXPECT_FALSE(output.str().empty());
}
{ // abort on lint error
std::ostringstream output;
ViolationPrinter violation_printer(&output);
const int exit_code =
LintOneFile(&output, temp_file.filename(), config_,
&violation_printer, true, false, true, false);
EXPECT_EQ(exit_code, 1) << "output:\n" << output.str();
EXPECT_FALSE(output.str().empty());
}
}
}
class VerilogLinterTest : public DefaultLinterConfigTestFixture,
public testing::Test {
public:
// Test code using the default rule set.
VerilogLinterTest() = default;
protected:
// Returns diagnostic text from analyzing source code.
std::pair<absl::Status, std::string> LintAnalyzeText(
absl::string_view filename, absl::string_view content) const {
// Run the analyzer to produce a syntax tree from source code.
const auto analyzer = absl::make_unique<VerilogAnalyzer>(content, filename);
const absl::Status status = ABSL_DIE_IF_NULL(analyzer)->Analyze();
std::ostringstream diagnostics;
if (!status.ok()) {
bool with_diagnostic_contex = false;
const std::vector<std::string> syntax_error_messages(
analyzer->LinterTokenErrorMessages(with_diagnostic_contex));
for (const auto& message : syntax_error_messages) {
diagnostics << message << std::endl;
}
with_diagnostic_contex = true;
const std::vector<std::string> syntax_error_messages_with_context(
analyzer->LinterTokenErrorMessages(with_diagnostic_contex));
for (const auto& message : syntax_error_messages_with_context) {
diagnostics << message << std::endl;
}
}
const auto& text_structure = analyzer->Data();
// For testing purposes we want the status returned to reflect
// lint success, so as long as we have a syntax tree (even if there
// are errors), run the lint checks.
const absl::StatusOr<std::vector<verible::LintRuleStatus>> lint_result =
VerilogLintTextStructure(filename, config_, text_structure);
verilog::ViolationPrinter violation_printer(&diagnostics);
const std::set<verible::LintViolationWithStatus> violations =
GetSortedViolations(lint_result.value());
violation_printer.HandleViolations(violations, text_structure.Contents(),
filename);
return {lint_result.status(), diagnostics.str()};
}
};
// This test verifies that VerilogLintTextStructure runs on an empty tree.
TEST_F(VerilogLinterTest, AnonymousEmptyTree) {
const auto diagnostics = LintAnalyzeText("", "");
EXPECT_TRUE(diagnostics.first.ok());
EXPECT_EQ(diagnostics.second, "");
}
// This test verifies that VerilogLintTextStructure runs on complete source,
// that is lint-clean.
TEST_F(VerilogLinterTest, NoLintViolation) {
const auto diagnostics = LintAnalyzeText("good.sv",
"task automatic foo;\n"
" $display(\"blah\");\n"
"endtask\n");
EXPECT_TRUE(diagnostics.first.ok());
EXPECT_EQ(diagnostics.second, "");
}
// This test verifies that VerilogLintTextStructure runs on complete source,
// with one syntax tree lint finding.
TEST_F(VerilogLinterTest, KnownTreeLintViolation) {
const auto diagnostics = LintAnalyzeText("bad.sv",
"task automatic foo;\n"
" $psprintf(\"blah\");\n"
"endtask\n");
EXPECT_TRUE(diagnostics.first.ok());
const auto expected =
"bad.sv:2:3-11: $psprintf is a forbidden system function "
"or task, please use $sformatf instead";
EXPECT_THAT(diagnostics.second, StartsWith(expected));
EXPECT_THAT(diagnostics.second, EndsWith("[invalid-system-task-function]\n"));
}
// This test verifies that VerilogLintTextStructure runs on complete source,
// with one syntax tree lint finding that is waived (next-line).
TEST_F(VerilogLinterTest, KnownTreeLintViolationWaivedNextLine) {
const auto diagnostics =
LintAnalyzeText("bad.sv",
"task automatic foo;\n"
" // verilog_lint: waive invalid-system-task-function\n"
" $psprintf(\"blah\");\n"
"endtask\n");
EXPECT_TRUE(diagnostics.first.ok());
EXPECT_EQ(diagnostics.second, "");
}
// This test verifies that VerilogLintTextStructure runs on complete source,
// with one syntax tree lint finding that is waived (same-line).
TEST_F(VerilogLinterTest, KnownTreeLintViolationWaivedSameLine) {
const auto diagnostics =
LintAnalyzeText("bad.sv",
"task automatic foo;\n"
" $psprintf(\"blah\"); // verilog_lint: waive "
"invalid-system-task-function\n"
"endtask\n");
EXPECT_TRUE(diagnostics.first.ok());
EXPECT_EQ(diagnostics.second, "");
}
// This test verifies that VerilogLintTextStructure runs on complete source,
// with one syntax tree lint finding that is waived (line-range).
TEST_F(VerilogLinterTest, KnownTreeLintViolationWaivedLineRange) {
const auto diagnostics = LintAnalyzeText(
"bad.sv",
"task automatic foo;\n"
" // verilog_lint: waive-start invalid-system-task-function\n"
" $psprintf(\"blah\");\n"
" // verilog_lint: waive-end invalid-system-task-function\n"
"endtask\n");
EXPECT_TRUE(diagnostics.first.ok());
EXPECT_EQ(diagnostics.second, "");
}
// This test verifies that VerilogLintTextStructure runs on complete source,
// with one token stream lint finding.
TEST_F(VerilogLinterTest, KnownTokenStreamLintViolation) {
using analysis::kDefaultRuleSet;
// TODO(fangism): Remove this conditional check or choose a different
// token-stream based lint rule that is enabled by default.
// (Just so happens that we may not want endif-comment enabled by default.)
if (std::find(std::begin(kDefaultRuleSet), std::end(kDefaultRuleSet),
"endif-comment") != std::end(kDefaultRuleSet)) {
const auto diagnostics = LintAnalyzeText("endif.sv",
"`ifdef SIM\n"
"module foo;\n"
"endmodule\n"
"`endif\n");
static const auto expect_message =
"endif.sv:4:1: `endif should be followed on the same line by a "
"comment that matches the opening `ifdef/`ifndef. (SIM) ";
EXPECT_TRUE(diagnostics.first.ok());
EXPECT_THAT(diagnostics.second, StartsWith(expect_message));
EXPECT_THAT(diagnostics.second, EndsWith("[endif-comment]\n"));
}
}
// This test verifies that VerilogLintTextStructure runs on complete source,
// with one line-lint-rule finding.
TEST_F(VerilogLinterTest, KnownLineLintViolation) {
const auto diagnostics = LintAnalyzeText("tab.sv",
"`include \"blah.svh\";\n"
"\n"
"module\ttab;\n" // bad tab here
"endmodule\n");
EXPECT_TRUE(diagnostics.first.ok());
EXPECT_THAT(diagnostics.second,
StartsWith("tab.sv:3:7: Use spaces, not tabs."));
EXPECT_THAT(diagnostics.second, EndsWith("[no-tabs]\n"));
}
// This test verifies that VerilogLintTextStructure runs on complete source,
// with one text structure lint rule finding (line-length).
TEST_F(VerilogLinterTest, KnownTextStructureLintViolation) {
const auto diagnostics = LintAnalyzeText(
"long.sv",
"module long;\n"
"initial xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = "
"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy[777777777777];\n"
"endmodule\n");
EXPECT_TRUE(diagnostics.first.ok());
EXPECT_THAT(diagnostics.second,
StartsWith("long.sv:2:101-114: Line length exceeds "
"max: 100; is: 114"));
EXPECT_THAT(diagnostics.second, EndsWith("[line-length]\n"));
}
// This test verifies that VerilogLintTextStructure runs on complete source,
// with one waived lint rule finding (line-length).
TEST_F(VerilogLinterTest, KnownTextStructureLintViolationWaived) {
const auto diagnostics = LintAnalyzeText(
"long.sv",
"module long;\n"
"initial xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = "
"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy[777777777777]; "
"// verilog_lint: waive line-length\n"
"endmodule\n");
EXPECT_TRUE(diagnostics.first.ok());
EXPECT_EQ(diagnostics.second, "");
}
// This test verifies that VerilogLintTextStructure runs on a module-body
// source, with one lint rule finding (line-length).
TEST_F(VerilogLinterTest, ModuleBodyLineLength) {
const auto diagnostics = LintAnalyzeText(
"module-body.sv",
"// verilog_syntax: parse-as-module-body\n"
"\n"
"initial xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = "
"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy[777777777777];\n");
EXPECT_TRUE(diagnostics.first.ok());
EXPECT_THAT(
diagnostics.second,
StartsWith("module-body.sv:3:101-114: Line length exceeds max: "));
EXPECT_THAT(diagnostics.second, EndsWith("[line-length]\n"));
}
// This test verifies that VerilogLintTextStructure runs on a module-body
// source, with one waived lint rule finding (line-length).
TEST_F(VerilogLinterTest, ModuleBodyLineLengthWaived) {
const auto diagnostics = LintAnalyzeText(
"module-body.sv",
"// verilog_syntax: parse-as-module-body\n"
"\n"
"initial xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = "
"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy[777777777777]; "
"// verilog_lint: waive line-length\n");
EXPECT_TRUE(diagnostics.first.ok());
EXPECT_EQ(diagnostics.second, "");
}
TEST_F(VerilogLinterTest, MultiByteUTF8CharactersAreOnlyCountedOnce) {
// Typical comment that might be found in verilog: some ASCII-art diagram
// except that the 'ˉ'-'overscore' is actually a two-byte UTF8 character.
constexpr char comment_with_utf8[] =
"module utf8_short;\n"
R"(initial a = 42; // __/ˉˉˉˉˉˉˉˉˉ\___/ˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉ\___/ˉˉˉˉˉ)"
"\nendmodule\n";
const auto diagnostics = LintAnalyzeText("utf8_short.sv", comment_with_utf8);
EXPECT_TRUE(diagnostics.first.ok());
EXPECT_EQ(diagnostics.second, "");
}
TEST(VerilogLinterDocumentationTest, AllRulesHelpDescriptions) {
std::ostringstream stream;
verilog::GetLintRuleDescriptionsHelpFlag(&stream, "all");
// Spot-check a few patterns, must mostly make sure generation
// works without any fatal errors.
EXPECT_TRUE(absl::StrContains(stream.str(), "line-length"));
EXPECT_TRUE(absl::StrContains(stream.str(), "posix-eof"));
EXPECT_TRUE(absl::StrContains(stream.str(), "Enabled by default:"));
}
TEST(VerilogLinterDocumentationTest, AllRulesMarkdown) {
std::ostringstream stream;
verilog::GetLintRuleDescriptionsMarkdown(&stream);
// Spot-check a few patterns, must mostly make sure generation
// works without any fatal errors.
EXPECT_TRUE(absl::StrContains(stream.str(), "line-length"));
EXPECT_TRUE(absl::StrContains(stream.str(), "posix-eof"));
EXPECT_TRUE(absl::StrContains(stream.str(), "Enabled by default:"));
}
class ViolationFixerTest : public testing::Test {
public:
ViolationFixerTest() : config_() {
config_.UseRuleSet(RuleSet::kNone);
config_.TurnOn("forbid-consecutive-null-statements");
config_.TurnOn("no-trailing-spaces");
config_.TurnOn("posix-eof");
}
protected:
LinterConfiguration config_;
absl::Status LintAnalyzeFixText(absl::string_view content,
ViolationFixer& violation_fixer,
std::string* fixed_content) const {
const ScopedTestFile temp_file(testing::TempDir(), content);
// Run the analyzer to produce a syntax tree from source code.
const auto analyzer =
absl::make_unique<VerilogAnalyzer>(content, temp_file.filename());
const absl::Status status = ABSL_DIE_IF_NULL(analyzer)->Analyze();
const auto& text_structure = analyzer->Data();
const absl::StatusOr<std::vector<verible::LintRuleStatus>> lint_result =
VerilogLintTextStructure(temp_file.filename(), config_, text_structure);
const std::set<verible::LintViolationWithStatus> violations =
GetSortedViolations(lint_result.value());
violation_fixer.HandleViolations(violations, text_structure.Contents(),
temp_file.filename());
const auto ok = GetContents(temp_file.filename(), fixed_content);
return lint_result.status();
}
void DoFixerTest(
std::initializer_list<ViolationFixer::Answer> choices,
std::initializer_list<absl::string_view> expected_fixed_sources) const {
static constexpr std::array<const absl::string_view, 3> input_sources{
// Input source 0:
// :2:10: no-trailing-spaces
// :3:10: forbid-consecutive-null-statements
// :4:10: forbid-consecutive-null-statements
// :4:11: no-trailing-spaces
// :5:10: forbid-consecutive-null-statements
// :6:10: forbid-consecutive-null-statements
// :7:10: no-trailing-spaces
// :7:14: posix-eof
"module Autofix; \n"
" wire a;;\n"
" wire b;; \n"
" wire c;;\n"
" wire d;;\n"
"endmodule ",
// Input source 1:
// (no issues)
"module AutofixTwo;\n"
"endmodule\n",
// Input source 2:
// :1:21: forbid-consecutive-null-statements
// :2:10: no-trailing-spaces
"module AutofixThree;;\n"
" wire a; \n"
"endmodule\n",
};
EXPECT_EQ(expected_fixed_sources.size(), input_sources.size());
std::initializer_list<ViolationFixer::Answer>::iterator choice_it;
const ViolationFixer::AnswerChooser answer_chooser =
[&choice_it, &choices](const verible::LintViolation&,
absl::string_view) {
EXPECT_NE(choice_it, choices.end())
<< "AnswerChooser called more times than expected.";
return *choice_it++;
};
// In-place fixing
{
choice_it = choices.begin();
// intentionally unopened, diagnostics are discarded
std::ofstream diagnostics;
ViolationFixer violation_fixer(&diagnostics, nullptr, answer_chooser);
std::vector<std::string> fixed_sources(input_sources.size());
for (size_t i = 0; i < input_sources.size(); ++i) {
const absl::string_view input_source = input_sources[i];
std::string& fixed_source = fixed_sources[i];
const absl::Status status =
LintAnalyzeFixText(input_source, violation_fixer, &fixed_source);
EXPECT_TRUE(status.ok());
}
EXPECT_EQ(choice_it, choices.end())
<< "AnswerChooser called fewer times than expected.";
for (size_t i = 0; i < input_sources.size(); ++i) {
const std::string& fixed_source = fixed_sources[i];
const absl::string_view expected_fixed_source =
*(expected_fixed_sources.begin() + i);
EXPECT_EQ(fixed_source, expected_fixed_source);
}
}
// Patch generation
{
choice_it = choices.begin();
// intentionally unopened, diagnostics are discarded
std::ofstream diagnostics;
std::ostringstream patch;
ViolationFixer violation_fixer(&diagnostics, &patch, answer_chooser);
std::vector<std::string> fixed_sources(input_sources.size());
for (size_t i = 0; i < input_sources.size(); ++i) {
const absl::string_view input_source = input_sources[i];
std::string& fixed_source = fixed_sources[i];
const absl::Status status =
LintAnalyzeFixText(input_source, violation_fixer, &fixed_source);
EXPECT_TRUE(status.ok());
}
EXPECT_EQ(choice_it, choices.end())
<< "AnswerChooser called fewer times than expected.";
bool expect_empty_patch = true;
for (size_t i = 0; i < input_sources.size(); ++i) {
const absl::string_view input_source = input_sources[i];
const std::string& fixed_source = fixed_sources[i];
const absl::string_view expected_fixed_source =
*(expected_fixed_sources.begin() + i);
EXPECT_EQ(input_source, fixed_source);
if (input_source != expected_fixed_source) {
expect_empty_patch = false;
}
}
EXPECT_EQ(patch.str().empty(), expect_empty_patch);
}
}
};
TEST_F(ViolationFixerTest, ApplyAll) {
DoFixerTest(
{
{ViolationFixer::AnswerChoice::kApplyAll, 0},
},
{
"module Autofix;\n"
" wire a;\n"
" wire b;\n"
" wire c;\n"
" wire d;\n"
"endmodule\n",
"module AutofixTwo;\n"
"endmodule\n",
"module AutofixThree;\n"
" wire a;\n"
"endmodule\n",
});
}
TEST_F(ViolationFixerTest, RejectAll) {
DoFixerTest(
{
{ViolationFixer::AnswerChoice::kRejectAll, 0},
},
{
"module Autofix; \n"
" wire a;;\n"
" wire b;; \n"
" wire c;;\n"
" wire d;;\n"
"endmodule ",
"module AutofixTwo;\n"
"endmodule\n",
"module AutofixThree;;\n"
" wire a; \n"
"endmodule\n",
});
}
TEST_F(ViolationFixerTest, Reject) {
DoFixerTest(
{
{ViolationFixer::AnswerChoice::kReject, 0},
{ViolationFixer::AnswerChoice::kApplyAll, 0},
},
{
"module Autofix; \n"
" wire a;\n"
" wire b;\n"
" wire c;\n"
" wire d;\n"
"endmodule\n",
"module AutofixTwo;\n"
"endmodule\n",
"module AutofixThree;\n"
" wire a;\n"
"endmodule\n",
});
}
TEST_F(ViolationFixerTest, Apply) {
DoFixerTest(
{
{ViolationFixer::AnswerChoice::kApply, 0},
{ViolationFixer::AnswerChoice::kRejectAll, 0},
},
{
"module Autofix;\n"
" wire a;;\n"
" wire b;; \n"
" wire c;;\n"
" wire d;;\n"
"endmodule ",
"module AutofixTwo;\n"
"endmodule\n",
"module AutofixThree;;\n"
" wire a; \n"
"endmodule\n",
});
}
TEST_F(ViolationFixerTest, ApplyAllForRule) {
DoFixerTest(
{
// Input source 0:
// :2:10: no-trailing-spaces
{ViolationFixer::AnswerChoice::kApplyAllForRule},
// :3:10: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kReject},
// :4:10: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kReject},
// :4:11: no-trailing-spaces
// AUTOMATICALLY APPLIED due to kApplyAllForRule
// :5:10: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kReject},
// :6:10: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kReject},
// :7:10: no-trailing-spaces
// AUTOMATICALLY APPLIED due to kApplyAllForRule
// :7:14: posix-eof
{ViolationFixer::AnswerChoice::kReject},
// Input source 2:
// :1:21: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kReject},
// :2:10: no-trailing-spaces
// AUTOMATICALLY APPLIED due to kApplyAllForRule
},
{
"module Autofix;\n"
" wire a;;\n"
" wire b;;\n"
" wire c;;\n"
" wire d;;\n"
"endmodule",
"module AutofixTwo;\n"
"endmodule\n",
"module AutofixThree;;\n"
" wire a;\n"
"endmodule\n",
});
}
TEST_F(ViolationFixerTest, RejectAllForRule) {
DoFixerTest(
{
// Input source 0:
// :2:10: no-trailing-spaces
{ViolationFixer::AnswerChoice::kRejectAllForRule},
// :3:10: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kApply},
// :4:10: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kApply},
// :4:11: no-trailing-spaces
// AUTOMATICALLY REJECTED due to kApplyAllForRule
// :5:10: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kApply},
// :6:10: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kApply},
// :7:10: no-trailing-spaces
// AUTOMATICALLY REJECTED due to kApplyAllForRule
// :7:14: posix-eof
{ViolationFixer::AnswerChoice::kApply},
// Input source 2:
// :1:21: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kApply},
// :2:10: no-trailing-spaces
// AUTOMATICALLY REJECTED due to kApplyAllForRule
},
{
"module Autofix; \n"
" wire a;\n"
" wire b; \n"
" wire c;\n"
" wire d;\n"
"endmodule \n",
"module AutofixTwo;\n"
"endmodule\n",
"module AutofixThree;\n"
" wire a; \n"
"endmodule\n",
});
}
TEST_F(ViolationFixerTest, RejectAllForRuleApplyAllForRule) {
DoFixerTest(
{
// Input source 0:
// :2:10: no-trailing-spaces
{ViolationFixer::AnswerChoice::kRejectAllForRule},
// :3:10: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kApplyAllForRule},
// :4:10: forbid-consecutive-null-statements
// AUTOMATICALLY APPLIED due to kApplyAllForRule
// :4:11: no-trailing-spaces
// AUTOMATICALLY REJECTED due to kApplyAllForRule
// :5:10: forbid-consecutive-null-statements
// AUTOMATICALLY APPLIED due to kApplyAllForRule
// :6:10: forbid-consecutive-null-statements
// AUTOMATICALLY APPLIED due to kApplyAllForRule
// :7:10: no-trailing-spaces
// AUTOMATICALLY REJECTED due to kApplyAllForRule
// :7:14: posix-eof
{ViolationFixer::AnswerChoice::kReject},
// Input source 2:
// :1:21: forbid-consecutive-null-statements
// AUTOMATICALLY APPLIED due to kApplyAllForRule
// :2:10: no-trailing-spaces
// AUTOMATICALLY REJECTED due to kApplyAllForRule
},
{
"module Autofix; \n"
" wire a;\n"
" wire b; \n"
" wire c;\n"
" wire d;\n"
"endmodule ",
"module AutofixTwo;\n"
"endmodule\n",
"module AutofixThree;\n"
" wire a; \n"
"endmodule\n",
});
}
TEST_F(ViolationFixerTest, PrintFix) {
// Just checks that kPrintFix doesn't affect choices
DoFixerTest(
{
// Input source 0:
// :2:10: no-trailing-spaces
{ViolationFixer::AnswerChoice::kApply},
{ViolationFixer::AnswerChoice::kPrintFix},
{ViolationFixer::AnswerChoice::kPrintFix},
// :3:10: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kReject},
// :4:10: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kReject},
{ViolationFixer::AnswerChoice::kPrintFix},
// :4:11: no-trailing-spaces
{ViolationFixer::AnswerChoice::kApply},
{ViolationFixer::AnswerChoice::kPrintFix},
// :5:10: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kApply},
// :6:10: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kApply},
{ViolationFixer::AnswerChoice::kPrintFix},
{ViolationFixer::AnswerChoice::kPrintFix},
// :7:10: no-trailing-spaces
{ViolationFixer::AnswerChoice::kApply},
// :7:14: posix-eof
{ViolationFixer::AnswerChoice::kReject},
{ViolationFixer::AnswerChoice::kPrintFix},
// Input source 2:
// :1:21: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kApply},
{ViolationFixer::AnswerChoice::kPrintFix},
// :2:10: no-trailing-spaces
{ViolationFixer::AnswerChoice::kReject},
},
{
"module Autofix;\n"
" wire a;;\n"
" wire b;;\n"
" wire c;\n"
" wire d;\n"
"endmodule",
"module AutofixTwo;\n"
"endmodule\n",
"module AutofixThree;\n"
" wire a; \n"
"endmodule\n",
});
}
TEST_F(ViolationFixerTest, PrintAppliedFixes) {
// Just checks that kPrintAppliedFixes doesn't affect choices
DoFixerTest(
{
// Input source 0:
// :2:10: no-trailing-spaces
{ViolationFixer::AnswerChoice::kApply},
{ViolationFixer::AnswerChoice::kPrintAppliedFixes},
{ViolationFixer::AnswerChoice::kPrintAppliedFixes},
// :3:10: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kReject},
// :4:10: forbid-consecutive-null-staments
{ViolationFixer::AnswerChoice::kReject},
{ViolationFixer::AnswerChoice::kPrintAppliedFixes},
// :4:11: no-trailing-spaces
{ViolationFixer::AnswerChoice::kApply},
{ViolationFixer::AnswerChoice::kPrintAppliedFixes},
// :5:10: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kApply},
// :6:10: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kApply},
{ViolationFixer::AnswerChoice::kPrintAppliedFixes},
{ViolationFixer::AnswerChoice::kPrintAppliedFixes},
// :7:10: no-trailing-spaces
{ViolationFixer::AnswerChoice::kApply},
// :7:14: posix-eof
{ViolationFixer::AnswerChoice::kReject},
{ViolationFixer::AnswerChoice::kPrintAppliedFixes},
// Input source 2:
// :1:21: forbid-consecutive-null-statements
{ViolationFixer::AnswerChoice::kApply},
{ViolationFixer::AnswerChoice::kPrintAppliedFixes},
// :2:10: no-trailing-spaces
{ViolationFixer::AnswerChoice::kReject},
},
{
"module Autofix;\n"
" wire a;;\n"
" wire b;;\n"
" wire c;\n"
" wire d;\n"
"endmodule",
"module AutofixTwo;\n"
"endmodule\n",
"module AutofixThree;\n"
" wire a; \n"
"endmodule\n",
});
}
} // namespace
} // namespace verilog