forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro_test.cc
316 lines (292 loc) · 11.4 KB
/
macro_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
// 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/macro.h"
#include "common/analysis/syntax_tree_search.h"
#include "common/analysis/syntax_tree_search_test_utils.h"
#include "common/text/text_structure.h"
#include "common/text/token_info_test_util.h"
#include "common/util/range.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "verilog/CST/match_test_utils.h"
#include "verilog/CST/verilog_nonterminals.h"
#include "verilog/analysis/verilog_analyzer.h"
#undef EXPECT_OK
#define EXPECT_OK(value) EXPECT_TRUE((value).ok())
#undef ASSERT_OK
#define ASSERT_OK(value) ASSERT_TRUE((value).ok())
namespace verilog {
namespace {
using ::testing::ElementsAreArray;
using verible::SyntaxTreeSearchTestCase;
using verible::TextStructureView;
using verible::TreeSearchMatch;
struct FindAllTestCase {
absl::string_view code;
int expected_matches;
};
TEST(FindAllMacroCallsTest, Various) {
const FindAllTestCase kTestCases[] = {
{"", 0},
{"module m; endmodule\n", 0},
{"`FOO;\n", 0},
{"`FOO()\n", 1},
{"// `FOO()\n", 0},
{"/* `FOO() */\n", 0},
{"`FOO()\n`BAR()\n", 2},
{"`FOO();\n", 1},
{"`FOO();\n`BAR();\n", 2},
{"`FOO(`BAR());\n", 2}, // nested
{"`FOO(bar);\n", 1},
{"`FOO(bar, 77);\n", 1},
{"function f;\nf = foo(`FOO);\nendfunction\n", 0},
{"function f;\nf = foo(`FOO());\nendfunction\n", 1},
{"function f;\nf = `BAR(`FOO);\nendfunction\n", 1},
{"function f;\nf = `BAR(`FOO());\nendfunction\n", 2},
{"function f;\nf = `BAR() * `FOO();\nendfunction\n", 2},
};
for (const auto& test : kTestCases) {
VerilogAnalyzer analyzer(test.code, "");
EXPECT_OK(analyzer.Analyze());
const auto& root = analyzer.Data().SyntaxTree();
const auto macro_calls = FindAllMacroCalls(*ABSL_DIE_IF_NULL(root));
EXPECT_EQ(macro_calls.size(), test.expected_matches) << "code:\n"
<< test.code;
// TODO(b/151371397): check exact substrings
}
}
struct MatchIdTestCase {
absl::string_view code;
std::vector<absl::string_view> expected_names;
};
TEST(GetMacroCallIdsTest, Various) {
const MatchIdTestCase kTestCases[] = {
{"`FOO1()\n", {"`FOO1"}},
{"`FOO2()\n`BAR2()\n", {"`FOO2", "`BAR2"}},
{"`FOO3();\n", {"`FOO3"}},
{"`FOO4();\n`BAR4();\n", {"`FOO4", "`BAR4"}},
{"`FOO5(`BAR5());\n", {"`FOO5", "`BAR5"}}, // nested
{"`FOO6(bar);\n", {"`FOO6"}},
{"function f;\nf = foo(`FOO7());\nendfunction\n", {"`FOO7"}},
{"function f;\nf = `BAR8(`FOO);\nendfunction\n", {"`BAR8"}},
{"function f;\nf = `BAR9(`FOO9());\nendfunction\n", {"`BAR9", "`FOO9"}},
{"function f;\nf = `BAR10() * `FOO10();\nendfunction\n",
{"`BAR10", "`FOO10"}},
};
for (const auto& test : kTestCases) {
VerilogAnalyzer analyzer(test.code, "");
EXPECT_OK(analyzer.Analyze());
const auto& root = analyzer.Data().SyntaxTree();
const auto macro_calls = FindAllMacroCalls(*ABSL_DIE_IF_NULL(root));
std::vector<absl::string_view> found_names;
for (const auto& match : macro_calls) {
found_names.push_back(GetMacroCallId(*match.match)->text());
}
EXPECT_THAT(found_names, ElementsAreArray(test.expected_names))
<< "code:\n"
<< test.code;
// TODO(b/151371397): check exact substrings
}
}
struct CallArgsTestCase {
absl::string_view code;
bool expect_empty;
};
TEST(MacroCallArgsTest, Emptiness) {
const CallArgsTestCase kTestCases[] = {
// checks the number of call args of the first found macro call
{"`FOO()\n", true},
{"`FOO();\n", true},
{"`FOO(`BAR());\n", false}, // nested
{"`FOO(bar);\n", false},
{"`FOO(bar, 77);\n", false},
{"function f;\nf = foo(`FOO());\nendfunction\n", true},
{"function f;\nf = `BAR(`FOO);\nendfunction\n", false},
{"function f;\nf = `BAR(`FOO());\nendfunction\n", false},
{"function f;\nf = `BAR() * `FOO();\nendfunction\n", true},
};
for (const auto& test : kTestCases) {
VerilogAnalyzer analyzer(test.code, "");
EXPECT_OK(analyzer.Analyze());
const auto& root = analyzer.Data().SyntaxTree();
const auto macro_calls = FindAllMacroCalls(*ABSL_DIE_IF_NULL(root));
ASSERT_FALSE(macro_calls.empty());
const auto* args = GetMacroCallArgs(*macro_calls.front().match);
EXPECT_EQ(MacroCallArgsIsEmpty(*args), test.expect_empty) << "code:\n"
<< test.code;
// TODO(b/151371397): check exact substrings
}
}
TEST(GetFunctionFormalPortsGroupTest, WithFormalPorts) {
constexpr int kTag = 1; // value not important
const verible::TokenInfoTestData kTestCases[] = {
{{kTag, "`FOO"}, "\n"},
{"package p;\n", {kTag, "`FOO"}, "\nendpackage\n"},
{"class c;\n", {kTag, "`FOO"}, "\nendclass\n"},
{"function f;\n", {kTag, "`FOO"}, "\nendfunction\n"},
{"task t;\n", {kTag, "`FOO"}, "\nendtask\n"},
{"module m;\n", {kTag, "`FOO"}, "\nendmodule\n"},
};
for (const auto& test : kTestCases) {
const absl::string_view code(test.code);
VerilogAnalyzer analyzer(code, "test-file");
const absl::string_view code_copy(analyzer.Data().Contents());
ASSERT_OK(analyzer.Analyze()) << "failed on:\n" << code;
const auto& root = analyzer.Data().SyntaxTree();
const auto macro_items = FindAllMacroGenericItems(*root);
ASSERT_EQ(macro_items.size(), 1);
const auto& macro_item = *macro_items.front().match;
const auto& id = GetMacroGenericItemId(macro_item);
const absl::string_view id_text = id->text();
// TODO(b/151371397): Refactor this test code along with
// common/analysis/linter_test_util.h to be able to compare set-symmetric
// differences in 'findings'.
// Find the tokens, rebased into the other buffer.
const auto expected_excerpts = test.FindImportantTokens(code_copy);
ASSERT_EQ(expected_excerpts.size(), 1);
// Compare the string_views and their exact spans.
const auto expected_span = expected_excerpts.front().text();
ASSERT_TRUE(verible::IsSubRange(id_text, code_copy));
ASSERT_TRUE(verible::IsSubRange(expected_span, code_copy));
EXPECT_EQ(id_text, expected_span);
EXPECT_TRUE(verible::BoundsEqual(id_text, expected_span));
}
}
TEST(FindAllMacroDefinitions, MacroName) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"`define ", {kTag, "PRINT_STRING"}, "(str1) $display(\"%s\\n\", str1)"},
{
"`define ",
{kTag, "PRINT_STRING"},
"(str1) $display(\"%s\\n\", str1)\n",
"`define ",
{kTag, "PRINT_3_STRING"},
R"((str1, str2, str3) \
`PRINT_STRING(str1); \
`PRINT_STRING(str2); \
`PRINT_STRING(str3);)",
"\n`define ",
{kTag, "TEN"},
" 10",
},
{"module m();\n `define ", {kTag, "my_macro"}, " 10\n endmodule"},
{"class m;\n `define ", {kTag, "my_macro"}, " 10\n endclass"},
{"function m();\n `define ", {kTag, "my_macro"}, " 10\n endfunction"},
{"function int m();\n `define ",
{kTag, "my_macro"},
" 10\n return 1;\n endfunction"},
{"task m();\n `define ", {kTag, "my_macro"}, " 10\n endtask"},
{"package m;\n `define ", {kTag, "my_macro"}, " 10\n endpackage"},
{"class m;\n `define ",
{kTag, "my_macro"},
" 10\n endclass\n module m(); int x = `TEN;\n endmodule"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto decls = FindAllMacroDefinitions(*ABSL_DIE_IF_NULL(root));
std::vector<TreeSearchMatch> names;
for (const auto& decl : decls) {
const auto* type = GetMacroName(*decl.match);
names.push_back(TreeSearchMatch{type, {/* ignored context */}});
}
return names;
});
}
}
TEST(FindAllMacroDefinitions, MacroArgsName) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"`define PRINT_STRING(", {kTag, "str1"}, ") $display(\"%s\\n\", str1)"},
{
"`define PRINT_STRING(",
{kTag, "str1"},
") $display(\"%s\\n\", str1)\n",
"`define PRINT_3_STRING(",
{kTag, "str1"},
", ",
{kTag, "str2"},
", ",
{kTag, "str3"},
")",
R"( \
`PRINT_STRING(str1); \
`PRINT_STRING(str2); \
`PRINT_STRING(str3);)",
"\n`define TEN 10",
},
{"module m();\n `define my_macro(", {kTag, "i"}, ") i\n endmodule"},
{"class m;\n `define my_macro(", {kTag, "i"}, ") i\n endclass"},
{"function m();\n `define my_macro(", {kTag, "i"}, ") i\n endfunction"},
{"function int m();\n `define my_macro(",
{kTag, "i"},
") i\n return 1;\n endfunction"},
{"task m();\n `define my_macro(", {kTag, "i"}, ") i\n endtask"},
{"package m;\n `define my_macro(", {kTag, "i"}, ") i\n endpackage"},
{"class m;\n `define my_macro(",
{kTag, "i"},
") i\n endclass\n module m(); int x = `TEN(1);\n endmodule"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto decls = FindAllMacroDefinitions(*ABSL_DIE_IF_NULL(root));
std::vector<TreeSearchMatch> names;
for (const auto& decl : decls) {
const auto& args = FindAllMacroDefinitionsArgs(*decl.match);
for (const auto& arg : args) {
const auto* name = GetMacroArgName(*arg.match);
names.push_back(TreeSearchMatch{name, {/* ignored context */}});
}
}
return names;
});
}
}
TEST(FindAllPreprocessorInclude, IncludedFileName) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"interface m;\nendinterface"},
{"module m;\nendmodule"},
{"`include ", {kTag, "\"file.sv\""}},
{"`include `d"},
};
for (const auto& test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView& text_structure) {
const auto& root = text_structure.SyntaxTree();
const auto& includes =
FindAllPreprocessorInclude(*ABSL_DIE_IF_NULL(root));
std::vector<TreeSearchMatch> names;
for (const auto& include : includes) {
const auto* filename =
GetFileFromPreprocessorInclude(*include.match);
if (filename == nullptr) {
continue;
}
names.emplace_back(
TreeSearchMatch{filename, {/* ignored context */}});
}
return names;
});
}
}
} // namespace
} // namespace verilog