Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LanguageServer Improve error messages in symbol table log #2080

Merged
merged 4 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions common/util/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,22 @@ static absl::Status CreateErrorStatusFromSysError(absl::string_view filename,
const char *fallback_msg) {
const char *const system_msg =
sys_error == 0 ? fallback_msg : strerror(sys_error);
const std::string msg = filename.empty()
? std::string{system_msg}
: absl::StrCat(filename, ": ", system_msg);
if (filename.empty()) filename = "<empty-filename>";
std::string msg = absl::StrCat(filename, ": ", system_msg);
switch (sys_error) {
case EPERM:
case EACCES:
return {absl::StatusCode::kPermissionDenied, msg};
case ENOENT:
case ESRCH: // Win32 returns this for fs::status() on non-existing file.
return {absl::StatusCode::kNotFound, msg};
case EEXIST:
return {absl::StatusCode::kAlreadyExists, msg};
case EINVAL:
case EISDIR:
return {absl::StatusCode::kInvalidArgument, msg};
default:
absl::StrAppend(&msg, " (sys_error=", sys_error, ")");
return {absl::StatusCode::kUnknown, msg};
}
}
Expand Down Expand Up @@ -127,15 +128,17 @@ absl::Status UpwardFileSearch(absl::string_view start,
if (one_up == probe_dir) break;
probe_dir = one_up;
}
return absl::NotFoundError("No matching file found.");
return absl::NotFoundError(absl::StrCat("UpwardFileSearch: starting from '",
start, "', no file '", filename,
"' found'"));
}

absl::Status FileExists(const std::string &filename) {
std::error_code err;
fs::file_status stat = fs::status(filename, err);
const fs::file_status stat = fs::status(filename, err);

if (err.value() != 0) {
return absl::NotFoundError(absl::StrCat(filename, ": ", err.message()));
return CreateErrorStatusFromErr(filename, err, "file exists check");
}

if (fs::is_regular_file(stat) || fs::is_fifo(stat)) {
Expand Down
6 changes: 6 additions & 0 deletions common/util/file_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ TEST(FileUtil, ScopedTestFileEmplace) {
}
}

TEST(FileUtil, FileNotExistsTests) {
absl::Status s = file::FileExists("not/an/existing/file");
EXPECT_FALSE(s.ok());
EXPECT_EQ(s.code(), absl::StatusCode::kNotFound);
}

TEST(FileUtil, FileExistsDirectoryErrorMessage) {
absl::Status s;
s = file::FileExists(testing::TempDir());
Expand Down
3 changes: 2 additions & 1 deletion verilog/analysis/symbol_table_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8252,7 +8252,8 @@ TEST(BuildSymbolTableTest, ModuleInstancesFromProjectMissingFile) {
symbol_table.BuildSingleTranslationUnit("file/not/found.txt",
&build_diagnostics);
ASSERT_FALSE(build_diagnostics.empty());
EXPECT_THAT(build_diagnostics.front().code(), absl::StatusCode::kNotFound);
EXPECT_THAT(build_diagnostics.front().code(), absl::StatusCode::kNotFound)
<< build_diagnostics.front();
}

TEST(BuildSymbolTableTest, ModuleInstancesFromProjectFilesGood) {
Expand Down
22 changes: 10 additions & 12 deletions verilog/analysis/verilog_project.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,14 @@ absl::StatusOr<VerilogSourceFile *> VerilogProject::OpenTranslationUnit(

absl::Status VerilogProject::IncludeFileNotFoundError(
absl::string_view referenced_filename) const {
return absl::NotFoundError(absl::StrCat(
"Unable to find '", referenced_filename,
"' among the included paths: ", absl::StrJoin(include_paths_, ", ")));
return absl::NotFoundError(
absl::StrCat("'", referenced_filename, "' not in any of the ",
include_paths_.size(), " include paths"));
}

absl::StatusOr<VerilogSourceFile *> VerilogProject::OpenIncludedFile(
absl::string_view referenced_filename) {
VLOG(1) << __FUNCTION__ << ", referenced: " << referenced_filename;
VLOG(2) << __FUNCTION__ << ", referenced: " << referenced_filename;
// Check for a pre-existing entry to avoid duplicate files.
{
const auto opened_file = FindOpenedFile(referenced_filename);
Expand All @@ -329,18 +329,16 @@ absl::StatusOr<VerilogSourceFile *> VerilogProject::OpenIncludedFile(

// Locate the file among the base paths.
for (const auto &include_path : include_paths_) {
const std::string resolved_filename =
const std::string resolved =
verible::file::JoinPath(include_path, referenced_filename);
if (verible::file::FileExists(resolved_filename).ok()) {
VLOG(2) << "File '" << resolved_filename << "' exists. Resolved from '"
<< referenced_filename << "'";
return OpenFile(referenced_filename, resolved_filename, Corpus());
if (verible::file::FileExists(resolved).ok()) {
VLOG(2) << referenced_filename << " in incdir '" << resolved << "'";
return OpenFile(referenced_filename, resolved, Corpus());
}
VLOG(2) << "Checked for file'" << resolved_filename
<< "', but not found. Resolved from '" << referenced_filename
<< "'";
VLOG(2) << referenced_filename << " not in incdir '" << resolved << "'";
}

VLOG(1) << __FUNCTION__ << "': '" << referenced_filename << "' not found";
// Not found in any path. Cache this status.
const auto inserted = files_.emplace(
referenced_filename,
Expand Down
Loading
Loading