Skip to content

Commit 0f9fc49

Browse files
committed
Removed all exceptions, using absl:Status instead now
1 parent 53fe62d commit 0f9fc49

File tree

11 files changed

+169
-144
lines changed

11 files changed

+169
-144
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ target_link_libraries(simview PRIVATE
5151
absl::str_format
5252
absl::time
5353
absl::flat_hash_map
54+
absl::status
55+
absl::statusor
5456
fst
5557
slang::slang
5658
${NCURSES_LIBRARY_NAME}

src/fst_wave_data.cc

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "fst_wave_data.h"
2+
#include "absl/status/status.h"
23
#include "external/fst/fstapi.h"
34
#include <stack>
4-
#include <stdexcept>
55

66
namespace sv {
77
namespace {
@@ -24,15 +24,19 @@ std::string ParseSignalLsb(const std::string &s, int *lsb) {
2424

2525
} // namespace
2626

27-
FstWaveData::FstWaveData(const std::string &file_name, bool keep_glitches)
28-
: WaveData(file_name, keep_glitches) {
29-
reader_ = fstReaderOpen(file_name.c_str());
30-
if (reader_ == nullptr) {
31-
throw std::runtime_error("Unable to read wave file.");
32-
}
33-
ReadScopes();
27+
absl::StatusOr<std::unique_ptr<FstWaveData>> FstWaveData::Create(const std::string &file_name,
28+
bool keep_glitches) {
29+
void *reader = fstReaderOpen(file_name.c_str());
30+
if (reader == nullptr) return absl::InternalError("Error opening wave file.");
31+
std::unique_ptr<FstWaveData> waves(new FstWaveData(file_name, keep_glitches));
32+
waves->reader_ = reader;
33+
waves->ReadScopes();
34+
return waves;
3435
}
3536

37+
FstWaveData::FstWaveData(const std::string &file_name, bool keep_glitches)
38+
: WaveData(file_name, keep_glitches) {}
39+
3640
FstWaveData::~FstWaveData() { fstReaderClose(reader_); }
3741

3842
int FstWaveData::Log10TimeUnits() const { return fstReaderGetTimescale(reader_); }
@@ -180,16 +184,14 @@ void FstWaveData::LoadSignalSamples(const std::vector<const Signal *> &signals,
180184
}
181185
}
182186

183-
void FstWaveData::Reload() {
184-
// First, re-create the reader.
187+
absl::Status FstWaveData::Reload() {
185188
fstReaderClose(reader_);
186-
reader_ = fstReaderOpen(file_name_.c_str());
187-
if (reader_ == nullptr) {
188-
throw std::runtime_error("Unable to read wave file.");
189-
}
190189
waves_.clear();
191190
roots_.clear();
191+
reader_ = fstReaderOpen(file_name_.c_str());
192+
if (reader_ == nullptr) return absl::InternalError("Unable to re-read wave file.");
192193
ReadScopes();
194+
return absl::OkStatus();
193195
}
194196

195197
} // namespace sv

src/fst_wave_data.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ namespace sv {
77
// FST implementation of WaveData, based on the FST library from GTKWave.
88
class FstWaveData : public WaveData {
99
public:
10-
FstWaveData(const std::string &file_name, bool keep_glitches);
10+
static absl::StatusOr<std::unique_ptr<FstWaveData>> Create(const std::string &file_name,
11+
bool keep_glitches);
1112
~FstWaveData() override;
1213
int Log10TimeUnits() const final;
1314
std::pair<uint64_t, uint64_t> TimeRange() const final;
1415
void LoadSignalSamples(const std::vector<const Signal *> &signals, uint64_t start_time,
1516
uint64_t end_time) const final;
16-
void Reload() final;
17+
absl::Status Reload() final;
1718

1819
private:
20+
FstWaveData(const std::string &file_name, bool keep_glitches);
1921
void ReadScopes();
2022
// The FST library is written in C and uses a lot of untyped handles.
2123
void *reader_ = nullptr;

src/ui.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,13 @@ bool UI::Reload() {
154154
if (layout_.has_design) p->PrepareForDesignReload();
155155
if (layout_.has_waves) p->PrepareForWaveDataReload();
156156
}
157-
if (layout_.has_waves) Workspace::Get().Waves()->Reload();
157+
if (layout_.has_waves) {
158+
const absl::Status status = Workspace::Get().Waves()->Reload();
159+
if (!status.ok()) {
160+
final_message_ = "Error re-reading waves. File may have been deleted or corrupted.";
161+
return false;
162+
}
163+
}
158164
if (layout_.has_design) {
159165
// TODO: This can potentially take a long time. Find some way to show a busy message.
160166
const bool success = Workspace::Get().ReParse();

src/vcd_tokenizer.cc

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
#include "vcd_tokenizer.h"
2+
#include "absl/status/status.h"
23
#include <filesystem>
34
#include <fstream>
45
#include <memory>
56

67
namespace sv {
78
namespace {
8-
inline bool is_whitespace(char ch) {
9-
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';
10-
}
9+
inline bool is_whitespace(char ch) { return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'; }
1110
} // namespace
1211

13-
VcdTokenizer::VcdTokenizer(const std::string &file_name) {
14-
in_ = std::make_unique<std::ifstream>(file_name);
15-
if (!in_->is_open()) {
16-
throw std::runtime_error("Unable to read VCD file.");
17-
}
18-
file_size_ = std::filesystem::file_size(file_name);
12+
absl::StatusOr<std::unique_ptr<VcdTokenizer>> VcdTokenizer::Create(const std::string &file_name) {
13+
auto in = std::make_unique<std::ifstream>(file_name);
14+
if (!in->is_open()) return absl::InternalError("Unable to read VCD file.");
15+
std::unique_ptr<VcdTokenizer> tk(new VcdTokenizer());
16+
tk->in_ = std::move(in);
17+
tk->file_size_ = std::filesystem::file_size(file_name);
18+
return tk;
1919
}
2020

21-
int VcdTokenizer::PosPercentage() const {
22-
return 100 * chars_read_ / file_size_;
23-
}
21+
int VcdTokenizer::PosPercentage() const { return 100 * chars_read_ / file_size_; }
2422

2523
bool VcdTokenizer::Eof() const { return in_->eof(); }
2624

src/vcd_tokenizer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include "absl/status/statusor.h"
34
#include <fstream>
45
#include <memory>
56
#include <string>
@@ -9,7 +10,7 @@ namespace sv {
910
// Reads token from a VCD file.
1011
class VcdTokenizer {
1112
public:
12-
explicit VcdTokenizer(const std::string &file_name);
13+
static absl::StatusOr<std::unique_ptr<VcdTokenizer>> Create(const std::string &file_name);
1314
bool Eof() const;
1415
std::streampos Position() const;
1516
void SetPosition(const std::streampos &pos);
@@ -20,6 +21,7 @@ class VcdTokenizer {
2021
uint64_t file_size_;
2122
uint64_t chars_read_ = 0;
2223
std::unique_ptr<std::ifstream> in_;
24+
VcdTokenizer() {}
2325
};
2426

2527
} // namespace sv

0 commit comments

Comments
 (0)