Skip to content

Commit b28ed80

Browse files
authored
Merge pull request #2180 from sconwayaus/filelist_remove_empty_file_bugfix
Fixes language server issue when empty file is removed from verible.filelist
2 parents 5ef1624 + cb15f2d commit b28ed80

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

verilog/analysis/verilog_project.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ void VerilogProject::ContentToFileIndex::Unregister(
133133
CHECK(file);
134134
const absl::string_view content = file->GetContent();
135135
auto full_content_found = string_view_map_.find(content);
136-
CHECK(full_content_found != string_view_map_.end());
137-
string_view_map_.erase(full_content_found);
136+
if (full_content_found != string_view_map_.end()) {
137+
string_view_map_.erase(full_content_found);
138+
}
138139
buffer_to_analyzer_map_.erase(content.begin());
139140
}
140141

verilog/analysis/verilog_project_test.cc

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,66 @@ TEST(VerilogProjectTest, UpdateFileContents) {
336336
EXPECT_EQ(project.LookupFileOrigin(search_substring), from_file);
337337
}
338338

339+
TEST(VerilogProjectTest, UpdateFileContentsEmptyFile) {
340+
// Users can add empty files to the filelist and subsequently
341+
// remove them.
342+
const auto tempdir = ::testing::TempDir();
343+
const std::string project_root_dir = JoinPath(tempdir, "srcs");
344+
EXPECT_TRUE(CreateDir(project_root_dir).ok());
345+
346+
// root is directory with sources.
347+
VerilogProject project(project_root_dir, {});
348+
349+
// Prepare file to be auto-loaded later
350+
const ScopedTestFile tf(project_root_dir, "");
351+
const absl::string_view reference_name = Basename(tf.filename());
352+
353+
// Push a local analyzed name under the name of the file.
354+
constexpr absl::string_view external_content("localparam int p = 1;\n");
355+
std::unique_ptr<VerilogAnalyzer> analyzed_structure =
356+
std::make_unique<VerilogAnalyzer>(external_content, "internal");
357+
project.UpdateFileContents(tf.filename(), analyzed_structure.get());
358+
359+
// Look up the file and see that content is the external content
360+
VerilogSourceFile *from_file;
361+
absl::string_view search_substring;
362+
from_file = *project.OpenTranslationUnit(reference_name);
363+
EXPECT_EQ(from_file->GetContent(), external_content);
364+
365+
// ... and we find our file given the substring.
366+
search_substring = from_file->GetContent().substr(5);
367+
EXPECT_EQ(project.LookupFileOrigin(search_substring), from_file);
368+
369+
// Prepare an empty file
370+
constexpr absl::string_view empty_file_content("");
371+
const ScopedTestFile empty_file(project_root_dir, empty_file_content);
372+
const absl::string_view empty_file_reference =
373+
Basename(empty_file.filename());
374+
375+
// Push the empty file into the project
376+
std::unique_ptr<VerilogAnalyzer> analyzed_empty_structure =
377+
std::make_unique<VerilogAnalyzer>(empty_file_content, "internal");
378+
project.UpdateFileContents(empty_file.filename(),
379+
analyzed_empty_structure.get());
380+
381+
// Check the content from the two files are present
382+
from_file = *project.OpenTranslationUnit(reference_name);
383+
EXPECT_EQ(from_file->GetContent(), external_content);
384+
385+
from_file = *project.OpenTranslationUnit(empty_file_reference);
386+
EXPECT_EQ(from_file->GetContent(), empty_file_content);
387+
388+
// Remove the empty file
389+
project.UpdateFileContents(empty_file.filename(), nullptr);
390+
391+
// Make sure the remaining file is still present
392+
from_file = *project.OpenTranslationUnit(reference_name);
393+
EXPECT_EQ(from_file->GetContent(), external_content);
394+
395+
// Make sure the empty file was removed
396+
EXPECT_EQ(project.LookupFileOrigin(empty_file.filename()), nullptr);
397+
}
398+
339399
TEST(VerilogProjectTest, LookupFileOriginTest) {
340400
const auto tempdir = ::testing::TempDir();
341401
const std::string sources_dir = JoinPath(tempdir, "srcs");

0 commit comments

Comments
 (0)