Skip to content

Commit

Permalink
Fix clang-tidy warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
rsmmr committed Jan 11, 2023
1 parent 3240fa8 commit 0df4162
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 50 deletions.
8 changes: 5 additions & 3 deletions hilti/runtime/include/types/interval.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Interval {
*
* @param nsecs interval in nanoseconds.
*/
explicit Interval(hilti::rt::integer::safe<int64_t> nsecs, NanosecondTag /*unused*/) : _nsecs(nsecs) {}
explicit Interval(const hilti::rt::integer::safe<int64_t>& nsecs, NanosecondTag /*unused*/) : _nsecs(nsecs) {}

/**
* Constructs an interval from a double value.
Expand Down Expand Up @@ -73,9 +73,11 @@ class Interval {
Interval operator+(const Interval& other) const { return Interval(_nsecs + other._nsecs, NanosecondTag()); }
Interval operator-(const Interval& other) const { return Interval(_nsecs - other._nsecs, NanosecondTag()); }

Interval operator*(hilti::rt::integer::safe<std::int64_t> i) const { return Interval(_nsecs * i, NanosecondTag()); }
Interval operator*(const hilti::rt::integer::safe<std::int64_t>& i) const {
return Interval(_nsecs * i, NanosecondTag());
}

Interval operator*(hilti::rt::integer::safe<std::uint64_t> i) const {
Interval operator*(const hilti::rt::integer::safe<std::uint64_t>& i) const {
return Interval(_nsecs * i.Ref(), NanosecondTag());
}

Expand Down
77 changes: 39 additions & 38 deletions hilti/runtime/include/types/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,18 @@ class Chunk {
using Array = std::pair<Size, std::array<Byte, SmallBufferSize>>;
using Vector = std::vector<Byte>;

Chunk(Offset o, std::array<Byte, SmallBufferSize> d, Size n) : _offset(o), _data(std::make_pair(n, d)) {}
Chunk(Offset o, Vector&& d) : _offset(o), _data(std::move(d)) {}
Chunk(Offset o, const View& d);
Chunk(Offset o, const std::string& s);
Chunk(const Offset& o, std::array<Byte, SmallBufferSize> d, const Size& n)
: _offset(o), _data(std::make_pair(n, d)) {}
Chunk(const Offset& o, Vector&& d) : _offset(o), _data(std::move(d)) {}
Chunk(const Offset& o, const View& d);
Chunk(const Offset& o, const std::string& s);

template<int N>
Chunk(Offset o, std::array<Byte, N> d) : Chunk(_fromArray(o, std::move(d))) {}
Chunk(Offset o, const char* d, Size n) : Chunk(_fromArray(o, d, n)) {}
Chunk(const Offset& o, const char* d, const Size& n) : Chunk(_fromArray(o, d, n)) {}

// Constructs a gap chunk which signifies empty data.
Chunk(Offset o, size_t len) : _offset(o), _data(Gap{len}) {}
Chunk(const Offset& o, size_t len) : _offset(o), _data(Gap{len}) {}

Chunk(const Chunk& other) : _offset(other._offset), _data(other._data) {}
Chunk(Chunk&& other) noexcept
Expand All @@ -121,7 +122,7 @@ class Chunk {
Offset offset() const { return _offset; }
Offset endOffset() const { return _offset + size(); }
bool isGap() const { return std::holds_alternative<Gap>(_data); }
bool inRange(Offset offset) const { return offset >= _offset && offset < endOffset(); }
bool inRange(const Offset& offset) const { return offset >= _offset && offset < endOffset(); }

const Byte* data() const {
if ( auto a = std::get_if<Array>(&_data) )
Expand All @@ -135,7 +136,7 @@ class Chunk {
hilti::rt::cannot_be_reached();
}

const Byte* data(Offset offset) const {
const Byte* data(const Offset& offset) const {
assert(inRange(offset));
return data() + (offset - _offset).Ref();
}
Expand Down Expand Up @@ -187,7 +188,7 @@ class Chunk {
// only from chain so that it can track any changes.
friend class Chain;

void trim(Offset o);
void trim(const Offset& o);

// Update offset for current chunk and all others linked from it.
void setOffset(Offset o) {
Expand Down Expand Up @@ -230,7 +231,7 @@ class Chunk {
void clearNext() { _next = nullptr; }

private:
inline Chunk _fromArray(Offset o, const char* d, Size n) {
inline Chunk _fromArray(const Offset& o, const char* d, const Size& n) {
auto ud = reinterpret_cast<const Byte*>(d);

if ( n <= Chunk::SmallBufferSize ) {
Expand Down Expand Up @@ -276,7 +277,7 @@ class Chain : public intrusive_ptr::ManagedObject {
Size size() const { return (endOffset() - offset()).Ref(); }
bool isFrozen() const { return _state == State::Frozen; }
bool isValid() const { return _state != State::Invalid; }
bool inRange(Offset o) const { return o >= offset() && o < endOffset(); }
bool inRange(const Offset& o) const { return o >= offset() && o < endOffset(); }

Offset offset() const { return _head_offset; }
Offset endOffset() const { return _tail ? _tail->endOffset() : _head_offset; }
Expand All @@ -286,16 +287,16 @@ class Chain : public intrusive_ptr::ManagedObject {
// allowing that to be found more quickly. If given, *hint_prev* must be
// pointing to a current chunk of the chain, but it's ok if it's
// non-helpful for finding the target (i.e., pointing to a later chunk).
const Chunk* findChunk(Offset offset, const Chunk* hint_prev = nullptr) const;
Chunk* findChunk(Offset offset, Chunk* hint_prev = nullptr);
const Chunk* findChunk(const Offset& offset, const Chunk* hint_prev = nullptr) const;
Chunk* findChunk(const Offset& offset, Chunk* hint_prev = nullptr);

// Returns a pointer to the byte at a given offset. Returns null if
// offset is out of range. See find() for semantics of *hint_prev*.
const Byte* data(Offset offset, Chunk* hint_prev = nullptr) const;
const Byte* data(const Offset& offset, Chunk* hint_prev = nullptr) const;

SafeConstIterator begin() const;
SafeConstIterator end() const;
SafeConstIterator at(Offset offset) const;
SafeConstIterator at(const Offset& offset) const;
UnsafeConstIterator unsafeBegin() const;
UnsafeConstIterator unsafeEnd() const;

Expand All @@ -306,7 +307,7 @@ class Chain : public intrusive_ptr::ManagedObject {
void append(std::unique_ptr<Chunk> chunk);
void append(Chain&& other);

void trim(Offset offset);
void trim(const Offset& offset);
void trim(const SafeConstIterator& i);
void trim(const UnsafeConstIterator& i);

Expand Down Expand Up @@ -424,7 +425,7 @@ class SafeConstIterator {
}

/** Advances the iterator by a given number of stream. */
auto& operator+=(integer::safe<uint64_t> i) {
auto& operator+=(const integer::safe<uint64_t>& i) {
_increment(i);
return *this;
}
Expand All @@ -443,7 +444,7 @@ class SafeConstIterator {
}

/** Moves back the iterator by a given number of stream. */
auto& operator-=(integer::safe<uint64_t> i) {
auto& operator-=(const integer::safe<uint64_t>& i) {
_decrement(i);
return *this;
}
Expand All @@ -452,14 +453,14 @@ class SafeConstIterator {
auto operator*() const { return _dereference(); }

/** Return a new iterator advanced by a given number of bytes. */
auto operator+(integer::safe<uint64_t> i) const {
auto operator+(const integer::safe<uint64_t>& i) const {
auto x = *this;
x._increment(i);
return x;
}

/** Returns a new iterator moved back by a given number of bytes. */
auto operator-(integer::safe<uint64_t> i) const {
auto operator-(const integer::safe<uint64_t>& i) const {
auto x = *this;
x._decrement(i);
return x;
Expand Down Expand Up @@ -577,7 +578,7 @@ class SafeConstIterator {
const Chain* chain() const { return _chain.get(); }

private:
SafeConstIterator(ConstChainPtr chain, Offset offset, const Chunk* chunk)
SafeConstIterator(ConstChainPtr chain, const Offset& offset, const Chunk* chunk)
: _chain(std::move(chain)), _offset(offset), _chunk(chunk) {
assert(! isUnset());
}
Expand All @@ -602,7 +603,7 @@ class SafeConstIterator {
throw InvalidIterator("incompatible iterators");
}

void _increment(integer::safe<uint64_t> n) {
void _increment(const integer::safe<uint64_t>& n) {
if ( ! _chain )
throw InvalidIterator("unbound stream iterator");

Expand All @@ -618,7 +619,7 @@ class SafeConstIterator {
// chunk will be null if we're pointing beyond the end.
}

void _decrement(integer::safe<uint64_t> n) {
void _decrement(const integer::safe<uint64_t>& n) {
if ( ! _chain )
throw InvalidIterator("unbound stream iterator");

Expand Down Expand Up @@ -747,7 +748,7 @@ class UnsafeConstIterator {
}

/** Moves back the iterator by a given number of stream. */
auto& operator-=(integer::safe<uint64_t> i) {
auto& operator-=(const integer::safe<uint64_t>& i) {
_decrement(i);
return *this;
}
Expand All @@ -756,14 +757,14 @@ class UnsafeConstIterator {
auto operator*() const { return _dereference(); }

/** Return a new iterator advanced by a given number of bytes. */
auto operator+(integer::safe<uint64_t> i) const {
auto operator+(const integer::safe<uint64_t>& i) const {
auto x = *this;
x._increment(i);
return x;
}

/** Return a new iterator moved back by a given number of bytes. */
auto operator-(integer::safe<uint64_t> i) const {
auto operator-(const integer::safe<uint64_t>& i) const {
auto x = *this;
x._decrement(i);
return x;
Expand Down Expand Up @@ -857,17 +858,17 @@ class UnsafeConstIterator {
const Chain* chain() const { return _chain; }

private:
UnsafeConstIterator(const ConstChainPtr& chain, Offset offset, const Chunk* chunk)
UnsafeConstIterator(const ConstChainPtr& chain, const Offset& offset, const Chunk* chunk)
: _chain(chain.get()), _offset(offset), _chunk(chunk) {
assert(! isUnset());
}

UnsafeConstIterator(const Chain* chain, Offset offset, const Chunk* chunk)
UnsafeConstIterator(const Chain* chain, const Offset& offset, const Chunk* chunk)
: _chain(chain), _offset(offset), _chunk(chunk) {
assert(! isUnset());
}

void _increment(integer::safe<uint64_t> n) {
void _increment(const integer::safe<uint64_t>& n) {
_offset += n;

if ( _chunk && _offset < _chunk->endOffset() )
Expand All @@ -876,7 +877,7 @@ class UnsafeConstIterator {
_chunk = _chain->findChunk(_offset, _chunk);
}

void _decrement(integer::safe<uint64_t> n) {
void _decrement(const integer::safe<uint64_t>& n) {
_offset -= n;

if ( _chunk && _offset > _chunk->offset() )
Expand Down Expand Up @@ -955,7 +956,7 @@ inline SafeConstIterator Chain::end() const {
return {ConstChainPtr(intrusive_ptr::NewRef(), this), endOffset(), _tail};
}

inline SafeConstIterator Chain::at(Offset offset) const {
inline SafeConstIterator Chain::at(const Offset& offset) const {
return {ConstChainPtr(intrusive_ptr::NewRef(), this), offset, findChunk(offset)};
}

Expand Down Expand Up @@ -1205,7 +1206,7 @@ class View {
*
* @param i the number of stream to advance.
*/
View advance(integer::safe<uint64_t> i) const { return View(begin() + i, _end); }
View advance(const integer::safe<uint64_t>& i) const { return View(begin() + i, _end); }

/**
* Advances the view to the next, none gap offset. This always advances at least by one byte.
Expand Down Expand Up @@ -1241,18 +1242,18 @@ class View {
* @param from offset of start of subrange, relative to beginning of view
* @param to offset of one beyond end of subrange, relative to beginning of view
*/
View sub(Offset from, Offset to) const { return View(begin() + from, begin() + to); }
View sub(const Offset& from, const Offset& to) const { return View(begin() + from, begin() + to); }

/**
* Extracts subrange of stream from the beginning of the view, returned as
* a new view.
*
* @param to of one beyond end of subrange, relative to beginning of view
*/
View sub(Offset to) const { return View(begin(), begin() + to); }
View sub(const Offset& to) const { return View(begin(), begin() + to); }

/** Returns an iterator representing an offset inside the view's data */
SafeConstIterator at(Offset offset) const { return begin() + (offset - begin().offset()); }
SafeConstIterator at(const Offset& offset) const { return begin() + (offset - begin().offset()); }

/**
* Returns a new view moves the beginning to a subsequent iterator while
Expand All @@ -1276,7 +1277,7 @@ class View {
* at a specified offset from that beginning. The returned view will not
* be able to expand any further.
*/
View limit(Offset incr) const { return View(begin(), begin() + incr); }
View limit(const Offset& incr) const { return View(begin(), begin() + incr); }

/**
* Extracts a fixed number of stream from the view.
Expand Down Expand Up @@ -1442,7 +1443,7 @@ class Stream {
* Creates an instance from an existing memory block. The data
* will be copied if set, otherwise a gap will be recorded.
*/
Stream(const char* d, Size n);
Stream(const char* d, const Size& n);
/**
* Creates an instance from an existing stream view.
* @param d `View` to create the stream from
Expand Down Expand Up @@ -1566,7 +1567,7 @@ class Stream {
* Returns an iterator representing a specific offset.
* @param offset offset to use for the created iterator
*/
SafeConstIterator at(Offset offset) const { return _chain->at(offset); }
SafeConstIterator at(const Offset& offset) const { return _chain->at(offset); }

/**
* Returns a view representing the entire instance.
Expand Down
2 changes: 1 addition & 1 deletion hilti/runtime/include/types/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Time {
*
* @param nsecs interval in nanoseconds.
*/
explicit Time(hilti::rt::integer::safe<uint64_t> nsecs, NanosecondTag /*unused*/) : _nsecs(nsecs) {}
explicit Time(const hilti::rt::integer::safe<uint64_t>& nsecs, NanosecondTag /*unused*/) : _nsecs(nsecs) {}

/**
* Constructs a time from a double value.
Expand Down
16 changes: 8 additions & 8 deletions hilti/runtime/src/types/stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using namespace hilti::rt;
using namespace hilti::rt::stream;
using namespace hilti::rt::stream::detail;

Chunk::Chunk(Offset offset, const View& d) : _offset(offset) {
Chunk::Chunk(const Offset& offset, const View& d) : _offset(offset) {
if ( d.size() <= SmallBufferSize ) {
std::array<Byte, SmallBufferSize> a{};
d.copyRaw(a.data());
Expand All @@ -23,7 +23,7 @@ Chunk::Chunk(Offset offset, const View& d) : _offset(offset) {
}
}

Chunk::Chunk(Offset offset, const std::string& s) : _offset(offset) {
Chunk::Chunk(const Offset& offset, const std::string& s) : _offset(offset) {
if ( s.size() <= SmallBufferSize ) {
std::array<Byte, SmallBufferSize> a{};
memcpy(a.data(), s.data(), s.size());
Expand All @@ -37,7 +37,7 @@ Chunk::Chunk(Offset offset, const std::string& s) : _offset(offset) {
}
}

void Chunk::trim(Offset o) {
void Chunk::trim(const Offset& o) {
assert(o >= _offset && o < _offset + size());
if ( auto a = std::get_if<Array>(&_data) ) {
auto begin = a->second.data() + (o - _offset).Ref();
Expand All @@ -54,7 +54,7 @@ void Chunk::trim(Offset o) {
_offset = o;
}

const Chunk* Chain::findChunk(Offset offset, const Chunk* hint_prev) const {
const Chunk* Chain::findChunk(const Offset& offset, const Chunk* hint_prev) const {
_ensureValid();

const Chunk* c = _head.get();
Expand All @@ -71,7 +71,7 @@ const Chunk* Chain::findChunk(Offset offset, const Chunk* hint_prev) const {
return c;
}

Chunk* Chain::findChunk(Offset offset, Chunk* hint_prev) {
Chunk* Chain::findChunk(const Offset& offset, Chunk* hint_prev) {
_ensureValid();

Chunk* c = _head.get();
Expand All @@ -88,7 +88,7 @@ Chunk* Chain::findChunk(Offset offset, Chunk* hint_prev) {
return c;
}

const Byte* Chain::data(Offset offset, Chunk* hint_prev) const {
const Byte* Chain::data(const Offset& offset, Chunk* hint_prev) const {
auto c = findChunk(offset, hint_prev);
if ( ! c )
throw InvalidIterator("stream iterator outside of valid range");
Expand Down Expand Up @@ -126,7 +126,7 @@ void Chain::append(Chain&& other) {
other.reset();
}

void Chain::trim(Offset offset) {
void Chain::trim(const Offset& offset) {
_ensureValid();

// We search the first chunk that's containing the desired position,
Expand Down Expand Up @@ -425,7 +425,7 @@ std::optional<View::Block> View::nextBlock(std::optional<Block> current) const {

Stream::Stream(const Bytes& d) : Stream(Chunk(0, d.str())) {}

Stream::Stream(const char* d, Size n) : Stream() { append(d, n); }
Stream::Stream(const char* d, const Size& n) : Stream() { append(d, n); }

void Stream::append(Bytes&& data) {
if ( data.isEmpty() )
Expand Down

0 comments on commit 0df4162

Please sign in to comment.