forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependencies_test.cc
366 lines (309 loc) · 12.8 KB
/
dependencies_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
// 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/analysis/dependencies.h"
#include <ostream>
#include <string>
#include <vector>
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "common/util/file_util.h"
#include "common/util/logging.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "verilog/analysis/symbol_table.h"
#include "verilog/analysis/verilog_project.h"
namespace verilog {
namespace {
using testing::ElementsAre;
using verible::file::Basename;
using verible::file::CreateDir;
using verible::file::JoinPath;
using verible::file::testing::ScopedTestFile;
TEST(FileDependenciesTest, EmptyData) {
const auto tempdir = ::testing::TempDir();
const std::string sources_dir = JoinPath(tempdir, __FUNCTION__);
ASSERT_TRUE(CreateDir(sources_dir).ok());
VerilogProject project(sources_dir, {/* no include paths */});
// no files
SymbolTable symbol_table(&project);
{ // pre-build should be empty
FileDependencies file_deps(symbol_table);
EXPECT_TRUE(file_deps.Empty()) << file_deps;
}
std::vector<absl::Status> build_diagnostics;
symbol_table.Build(&build_diagnostics);
EXPECT_TRUE(build_diagnostics.empty());
{ // post-build should be empty
FileDependencies file_deps(symbol_table);
EXPECT_TRUE(file_deps.Empty()) << file_deps;
}
}
TEST(FileDependenciesTest, OneFileNoDeps) {
const auto tempdir = ::testing::TempDir();
const std::string sources_dir = JoinPath(tempdir, __FUNCTION__);
ASSERT_TRUE(CreateDir(sources_dir).ok());
// None of these test cases will yield any inter-file deps.
constexpr absl::string_view kTestCases[] = {
"",
// one module
"module mmm;\n"
"endmodule\n",
// two modules, with dependency
"module mmm;\n"
"endmodule\n"
"module ppp;\n"
" mmm mmm_inst();\n"
"endmodule\n",
// parameters with dependency
"localparam int foo = 0;\n"
"localparam int bar = foo + 1;\n",
};
for (const auto &code : kTestCases) {
VLOG(1) << "code: " << code;
VerilogProject project(sources_dir, {/* no include paths */});
ScopedTestFile tf(sources_dir, code);
{
const auto status_or_file =
project.OpenTranslationUnit(Basename(tf.filename()));
ASSERT_TRUE(status_or_file.ok()) << status_or_file.status().message();
}
SymbolTable symbol_table(&project);
std::vector<absl::Status> build_diagnostics;
symbol_table.Build(&build_diagnostics);
EXPECT_TRUE(build_diagnostics.empty());
// post-build should be empty
FileDependencies file_deps(symbol_table);
EXPECT_TRUE(file_deps.Empty()) << file_deps;
}
}
TEST(FileDependenciesTest, TwoFilesNoDeps) {
const auto tempdir = ::testing::TempDir();
const std::string sources_dir = JoinPath(tempdir, __FUNCTION__);
ASSERT_TRUE(CreateDir(sources_dir).ok());
VerilogProject project(sources_dir, {/* no include paths */});
ScopedTestFile tf1(sources_dir, "localparam int foo = 0;");
const auto status_or_file1 =
project.OpenTranslationUnit(Basename(tf1.filename()));
ScopedTestFile tf2(sources_dir, "localparam int bar = 2;");
const auto status_or_file2 =
project.OpenTranslationUnit(Basename(tf2.filename()));
SymbolTable symbol_table(&project);
std::vector<absl::Status> build_diagnostics;
symbol_table.Build(&build_diagnostics);
EXPECT_TRUE(build_diagnostics.empty());
// post-build should be empty
FileDependencies file_deps(symbol_table);
EXPECT_TRUE(file_deps.Empty()) << file_deps;
}
struct SymbolTableDebug {
const SymbolTable &symbol_table;
};
static std::ostream &operator<<(std::ostream &stream,
const SymbolTableDebug &p) {
stream << "Definitions:" << std::endl;
p.symbol_table.PrintSymbolDefinitions(stream) << std::endl;
stream << "References:" << std::endl;
return p.symbol_table.PrintSymbolReferences(stream) << std::endl;
}
TEST(FileDependenciesTest, TwoFilesWithParamDepAtRootScope) {
const auto tempdir = ::testing::TempDir();
const std::string sources_dir = JoinPath(tempdir, __FUNCTION__);
ASSERT_TRUE(CreateDir(sources_dir).ok());
VerilogProject project(sources_dir, {/* no include paths */});
ScopedTestFile tf1(sources_dir, "localparam int zzz = 0;\n");
const auto status_or_file1 =
project.OpenTranslationUnit(Basename(tf1.filename()));
const VerilogSourceFile *file1 = *status_or_file1;
ScopedTestFile tf2(sources_dir, "localparam int yyy = zzz * 2;\n");
const auto status_or_file2 =
project.OpenTranslationUnit(Basename(tf2.filename()));
const VerilogSourceFile *file2 = *status_or_file2;
SymbolTable symbol_table(&project);
std::vector<absl::Status> build_diagnostics;
symbol_table.Build(&build_diagnostics);
EXPECT_TRUE(build_diagnostics.empty());
FileDependencies file_deps(symbol_table);
// "zzz" is defined in the same $root scope, but different files,
// so we expect a dependency edge.
EXPECT_FALSE(file_deps.Empty()) << file_deps;
EXPECT_EQ(file_deps.file_deps.size(), 1) << SymbolTableDebug{symbol_table};
const auto found_ref = file_deps.file_deps.find(file2);
ASSERT_NE(found_ref, file_deps.file_deps.end())
<< file2->GetTextStructure()->Contents();
const auto found_def = found_ref->second.find(file1);
ASSERT_NE(found_def, found_ref->second.end())
<< file1->GetTextStructure()->Contents();
EXPECT_THAT(found_def->second, ElementsAre("zzz"));
}
TEST(FileDependenciesTest, TwoFilesWithParamDep) {
const auto tempdir = ::testing::TempDir();
const std::string sources_dir = JoinPath(tempdir, __FUNCTION__);
ASSERT_TRUE(CreateDir(sources_dir).ok());
VerilogProject project(sources_dir, {/* no include paths */});
ScopedTestFile tf1(sources_dir,
"localparam int foo = 0;\n"
"package p_pkg;\n"
" localparam int goo = 1;\n"
"endpackage\n");
const auto status_or_file1 =
project.OpenTranslationUnit(Basename(tf1.filename()));
const VerilogSourceFile *file1 = *status_or_file1;
ScopedTestFile tf2(sources_dir,
"localparam int bar = foo - 2;\n"
"localparam int baz = p_pkg::goo;\n");
const auto status_or_file2 =
project.OpenTranslationUnit(Basename(tf2.filename()));
const VerilogSourceFile *file2 = *status_or_file2;
SymbolTable symbol_table(&project);
std::vector<absl::Status> build_diagnostics;
symbol_table.Build(&build_diagnostics);
EXPECT_TRUE(build_diagnostics.empty());
FileDependencies file_deps(symbol_table);
EXPECT_FALSE(file_deps.Empty()) << file_deps;
EXPECT_EQ(file_deps.file_deps.size(), 1) << SymbolTableDebug{symbol_table};
const auto found_ref = file_deps.file_deps.find(file2);
ASSERT_NE(found_ref, file_deps.file_deps.end())
<< file2->GetTextStructure()->Contents();
const auto found_def = found_ref->second.find(file1);
ASSERT_NE(found_def, found_ref->second.end())
<< file1->GetTextStructure()->Contents();
EXPECT_THAT(found_def->second, ElementsAre("foo", "p_pkg"));
}
TEST(FileDependenciesTest, TwoFilesWithCyclicDep) {
const auto tempdir = ::testing::TempDir();
const std::string sources_dir = JoinPath(tempdir, __FUNCTION__);
ASSERT_TRUE(CreateDir(sources_dir).ok());
VerilogProject project(sources_dir, {/* no include paths */});
ScopedTestFile tf1(sources_dir,
"localparam int foo = 0;\n"
"package p_pkg;\n"
" localparam int goo = bar;\n"
"endpackage\n");
const auto status_or_file1 =
project.OpenTranslationUnit(Basename(tf1.filename()));
const VerilogSourceFile *file1 = *status_or_file1;
ScopedTestFile tf2(sources_dir,
"localparam int bar = foo - 2;\n"
"localparam int baz = p_pkg::goo;\n");
const auto status_or_file2 =
project.OpenTranslationUnit(Basename(tf2.filename()));
const VerilogSourceFile *file2 = *status_or_file2;
SymbolTable symbol_table(&project);
std::vector<absl::Status> build_diagnostics;
symbol_table.Build(&build_diagnostics);
EXPECT_TRUE(build_diagnostics.empty());
FileDependencies file_deps(symbol_table);
EXPECT_FALSE(file_deps.Empty()) << file_deps;
{ // dependencies in one direction
const auto found_ref = file_deps.file_deps.find(file2);
ASSERT_NE(found_ref, file_deps.file_deps.end())
<< file2->GetTextStructure()->Contents();
const auto found_def = found_ref->second.find(file1);
ASSERT_NE(found_def, found_ref->second.end())
<< file1->GetTextStructure()->Contents();
EXPECT_THAT(found_def->second, ElementsAre("foo", "p_pkg"));
}
{ // dependencies in reverse direction
const auto found_ref = file_deps.file_deps.find(file1);
ASSERT_NE(found_ref, file_deps.file_deps.end())
<< file1->GetTextStructure()->Contents();
const auto found_def = found_ref->second.find(file2);
ASSERT_NE(found_def, found_ref->second.end())
<< file2->GetTextStructure()->Contents();
EXPECT_THAT(found_def->second, ElementsAre("bar"));
}
}
TEST(FileDependenciesTest, ModuleDiamondDependencies) {
const auto tempdir = ::testing::TempDir();
const std::string sources_dir = JoinPath(tempdir, __FUNCTION__);
ASSERT_TRUE(CreateDir(sources_dir).ok());
VerilogProject project(sources_dir, {/* no include paths */});
// 4 files: with a diamond dependency relationship
ScopedTestFile mmm(sources_dir,
"module mmm;\n"
" ppp ppp_i();\n"
" qqq qqq_i();\n"
"endmodule\n");
const auto mmm_status_or_file =
project.OpenTranslationUnit(Basename(mmm.filename()));
const VerilogSourceFile *mmm_file = *mmm_status_or_file;
ScopedTestFile ppp(sources_dir,
"module ppp;\n"
" rrr rrr_i();\n"
"endmodule\n");
const auto ppp_status_or_file =
project.OpenTranslationUnit(Basename(ppp.filename()));
const VerilogSourceFile *ppp_file = *ppp_status_or_file;
ScopedTestFile qqq(sources_dir,
"module qqq;\n"
" rrr rrr_i();\n"
"endmodule\n");
const auto qqq_status_or_file =
project.OpenTranslationUnit(Basename(qqq.filename()));
const VerilogSourceFile *qqq_file = *qqq_status_or_file;
ScopedTestFile rrr(sources_dir,
"module rrr;\n"
" wire w;\n"
"endmodule\n");
const auto rrr_status_or_file =
project.OpenTranslationUnit(Basename(rrr.filename()));
const VerilogSourceFile *rrr_file = *rrr_status_or_file;
SymbolTable symbol_table(&project);
std::vector<absl::Status> build_diagnostics;
symbol_table.Build(&build_diagnostics);
EXPECT_TRUE(build_diagnostics.empty());
FileDependencies file_deps(symbol_table);
EXPECT_FALSE(file_deps.Empty()) << file_deps;
// Verify 4 edges in graph.
{
const auto found_ref = file_deps.file_deps.find(mmm_file);
ASSERT_NE(found_ref, file_deps.file_deps.end())
<< mmm_file->GetTextStructure()->Contents();
{
// mmm -> ppp
const auto found_def = found_ref->second.find(ppp_file);
ASSERT_NE(found_def, found_ref->second.end())
<< ppp_file->GetTextStructure()->Contents();
EXPECT_THAT(found_def->second, ElementsAre("ppp"));
}
{
// mmm -> qqq
const auto found_def = found_ref->second.find(qqq_file);
ASSERT_NE(found_def, found_ref->second.end())
<< qqq_file->GetTextStructure()->Contents();
EXPECT_THAT(found_def->second, ElementsAre("qqq"));
}
}
{ // ppp -> rrr
const auto found_ref = file_deps.file_deps.find(ppp_file);
ASSERT_NE(found_ref, file_deps.file_deps.end())
<< ppp_file->GetTextStructure()->Contents();
const auto found_def = found_ref->second.find(rrr_file);
ASSERT_NE(found_def, found_ref->second.end())
<< rrr_file->GetTextStructure()->Contents();
EXPECT_THAT(found_def->second, ElementsAre("rrr"));
}
{
// qqq -> rrr
const auto found_ref = file_deps.file_deps.find(qqq_file);
ASSERT_NE(found_ref, file_deps.file_deps.end())
<< qqq_file->GetTextStructure()->Contents();
const auto found_def = found_ref->second.find(rrr_file);
ASSERT_NE(found_def, found_ref->second.end())
<< rrr_file->GetTextStructure()->Contents();
EXPECT_THAT(found_def->second, ElementsAre("rrr"));
}
}
} // namespace
} // namespace verilog