Skip to content

Commit

Permalink
Adds a clang-tidy, and its cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
zwoop authored and SolidWallOfCode committed Nov 6, 2023
1 parent 85e7761 commit a7eeeb8
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 42 deletions.
4 changes: 2 additions & 2 deletions code/include/swoc/MemArena.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class MemArena
MemArena(self_type const &that) = delete;

/// Allow moving the arena.
MemArena(self_type &&that);
MemArena(self_type &&that) noexcept;

/// Destructor.
~MemArena();
Expand All @@ -210,7 +210,7 @@ class MemArena
self_type & operator=(self_type const & that) = delete;

/// Move assignment.
self_type & operator=(self_type &&that);
self_type & operator=(self_type &&that) noexcept;

/** Make a self-contained instance.
*
Expand Down
2 changes: 1 addition & 1 deletion code/include/swoc/bwf_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ struct Spec {
static const struct Property {
Property(); ///< Default constructor, creates initialized flag set.
/// Flag storage, indexed by character value.
uint8_t _data[0x100];
uint8_t _data[0x100]{};
/// Flag mask values.
static constexpr uint8_t ALIGN_MASK = 0x0F; ///< Alignment type.
static constexpr uint8_t TYPE_CHAR = 0x10; ///< A valid type character.
Expand Down
3 changes: 2 additions & 1 deletion code/src/ArenaWriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ ArenaWriter::realloc(size_t n) {
memcpy(_buffer, text.data(), text.size());
}

}} // namespace swoc::SWOC_VERSION_NS
} // namespace SWOC_VERSION_NS
} // namespace swoc
5 changes: 3 additions & 2 deletions code/src/Errata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Errata::note_s(std::optional<Severity> severity, std::string_view text) {
Errata &
Errata::note_localized(std::string_view const &text, std::optional<Severity> severity) {
auto d = this->data();
Annotation *n = d->_arena.make<Annotation>(text, severity);
auto *n = d->_arena.make<Annotation>(text, severity);
d->_notes.append(n);
return *this;
}
Expand Down Expand Up @@ -185,4 +185,5 @@ operator<<(std::ostream &os, Errata const &err) {
return err.write(os);
}

}} // namespace swoc::SWOC_VERSION_NS
} // namespace SWOC_VERSION_NS
} // namespace swoc
24 changes: 15 additions & 9 deletions code/src/MemArena.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ MemArena::MemArena(MemSpan<void> static_block) {
// integral values in @a that.

MemArena::MemArena(swoc::MemArena::self_type &&that)
: _active_allocated(that._active_allocated),
noexcept : _active_allocated(that._active_allocated),
_active_reserved(that._active_reserved),
_frozen_allocated(that._frozen_allocated),
_frozen_reserved(that._frozen_reserved),
Expand All @@ -57,7 +57,7 @@ MemArena::construct_self_contained(size_t n) {
}

MemArena &
MemArena::operator=(swoc::MemArena::self_type &&that) {
MemArena::operator=(swoc::MemArena::self_type &&that) noexcept {
this->clear();
std::swap(_active_allocated, that._active_allocated);
std::swap(_active_reserved, that._active_reserved);
Expand Down Expand Up @@ -155,10 +155,11 @@ MemArena::require(size_t n, size_t align) {

// Search back through the list until a full block is hit, which is a miss.
while (spot != _active.end() && !spot->satisfies(n, align)) {
if (spot->is_full())
if (spot->is_full()) {
spot = _active.end();
else
} else {
++spot;
}
}
if (spot == _active.end()) { // no block has enough free space
block = this->make_block(n); // assuming a new block is sufficiently aligned.
Expand All @@ -178,8 +179,9 @@ MemArena::destroy_active() {
auto sb = _static_block; // C++20 nonsense - capture of @a this is incompatible with C++17.
_active
.apply([=](Block *b) {
if (b != sb)
if (b != sb) {
delete b;
}
})
.clear();
}
Expand All @@ -189,8 +191,9 @@ MemArena::destroy_frozen() {
auto sb = _static_block; // C++20 nonsense - capture of @a this is incompatible with C++17.
_frozen
.apply([=](Block *b) {
if (b != sb)
if (b != sb) {
delete b;
}
})
.clear();
}
Expand Down Expand Up @@ -228,14 +231,16 @@ MemArena::~MemArena() {
while (bf) {
Block *b = bf;
bf = bf->_link._next;
if (b != sb)
if (b != sb) {
delete b;
}
}
while (ba) {
Block *b = ba;
ba = ba->_link._next;
if (b != sb)
if (b != sb) {
delete b;
}
}
}

Expand All @@ -254,4 +259,5 @@ MemArena::do_is_equal(std::pmr::memory_resource const &that) const noexcept {
}
#endif

}} // namespace swoc::SWOC_VERSION_NS
} // namespace SWOC_VERSION_NS
} // namespace swoc
9 changes: 6 additions & 3 deletions code/src/RBTree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ RBNode *
RBNode::rebalance_after_remove(Color c, //!< The color of the removed node
Direction d //!< Direction of removed node from its parent
) {
self_type *root;
self_type *root = nullptr;

if (Color::BLACK == c) { // only rebalance if too much black
self_type *n = this;
Expand Down Expand Up @@ -341,10 +341,13 @@ RBNode::validate() {
auto
RBNode::left_most_descendant() const -> self_type * {
const self_type *n = this;
while (n->_left)
while (n->_left) {
n = n->_left;
}

return const_cast<self_type *>(n);
}

}}} // namespace swoc::SWOC_VERSION_NS::detail
} // namespace detail
} // namespace SWOC_VERSION_NS
} // namespace swoc
5 changes: 3 additions & 2 deletions code/src/TextView.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ svtou(TextView src, TextView *out, int base) {
}
if (src.ltrim_if(&isspace).size()) {
auto origin = src.data();
int8_t v;
int8_t v = 0;
// If base is 0, it wasn't specified - check for standard base prefixes
if (0 == base) {
base = 10;
Expand Down Expand Up @@ -220,7 +220,8 @@ svtod(swoc::TextView text, swoc::TextView *parsed) {
// Do the template instantiations.
template std::ostream &TextView::stream_write(std::ostream &, const TextView &) const;

}} // namespace swoc::SWOC_VERSION_NS
} // namespace SWOC_VERSION_NS
} // namespace swoc

namespace std {
ostream &
Expand Down
21 changes: 12 additions & 9 deletions code/src/bw_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <chrono>
#include <cmath>
#include <ctime>
#include <cmath>
#include <sys/param.h>
#include <unistd.h>

Expand Down Expand Up @@ -190,7 +191,7 @@ Spec::parse(TextView fmt) {
/// @return @c true if a specifier was parsed, @c false if not.
bool
Format::TextViewExtractor::parse(TextView &fmt, std::string_view &literal, std::string_view &specifier) {
TextView::size_type off;
TextView::size_type off = 0;

// Check for brace delimiters.
off = fmt.find_if([](char c) { return '{' == c || '}' == c; });
Expand Down Expand Up @@ -524,13 +525,13 @@ Format_Float(BufferWriter &w, Spec const &spec, double f, bool negative_p) {
return w;
}

uint64_t whole_part = static_cast<uint64_t>(f);
auto whole_part = static_cast<uint64_t>(f);
if (whole_part == f || spec._prec == 0) { // integral
return Format_Integer(w, spec, whole_part, negative_p);
}

static constexpr char dec = '.';
double frac;
double frac = NAN;
size_t l = 0;
size_t r = 0;
char whole[std::numeric_limits<double>::digits10 + 1];
Expand All @@ -549,7 +550,7 @@ Format_Float(BufferWriter &w, Spec const &spec, double f, bool negative_p) {

// Shift the floating point based on the precision. Used to convert
// trailing fraction into an integer value.
uint64_t shift;
uint64_t shift = 0;
if (precision < POWERS_OF_TEN.size()) {
shift = POWERS_OF_TEN[precision];
} else { // not precomputed.
Expand All @@ -559,7 +560,7 @@ Format_Float(BufferWriter &w, Spec const &spec, double f, bool negative_p) {
}
}

uint64_t frac_part = static_cast<uint64_t>(frac * shift + 0.5 /* rounding */);
auto frac_part = static_cast<uint64_t>(frac * shift + 0.5 /* rounding */);

l = bwf::To_Radix<10>(whole_part, whole, sizeof(whole), bwf::LOWER_DIGITS);
r = bwf::To_Radix<10>(frac_part, fraction, sizeof(fraction), bwf::LOWER_DIGITS);
Expand Down Expand Up @@ -637,7 +638,7 @@ Format::is_literal() const {
return true;
}

NameBinding::~NameBinding() {}
NameBinding::~NameBinding() = default;

} // namespace bwf

Expand Down Expand Up @@ -716,8 +717,9 @@ bwformat(BufferWriter &w, bwf::Spec const &spec, MemSpan<void const> const &span
TextView view{span.rebind<char const>()};
bool space_p = false;
while (view) {
if (space_p)
if (space_p) {
w.write(' ');
}
space_p = true;
if (spec._radix_lead_p) {
w.write('0').write(digits[33]);
Expand Down Expand Up @@ -916,7 +918,7 @@ bwformat(BufferWriter &w, bwf::Spec const &spec, bwf::Date const &date) {
if (spec.has_numeric_type()) {
bwformat(w, spec, date._epoch);
} else {
struct tm t;
struct tm t{};
auto r = w.remaining();
size_t n{0};
// Verify @a fmt is null terminated, even outside the bounds of the view.
Expand Down Expand Up @@ -995,7 +997,8 @@ bwformat(BufferWriter &w, bwf::Spec const& spec, bwf::UnHex const& obj) {
return w;
}

}} // namespace swoc::SWOC_VERSION_NS
} // namespace SWOC_VERSION_NS
} // namespace swoc

namespace std {
ostream &
Expand Down
12 changes: 8 additions & 4 deletions code/src/bw_ip_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,12 @@ bwformat(BufferWriter &w, Spec const &spec, sockaddr const *addr) {
w.print("*Invalid IP family [{}]*", addr->sa_family);
break;
}
if (bracket_p)
if (bracket_p) {
w.write(']');
if (port_p)
}
if (port_p) {
w.write(':');
}
}
if (port_p) {
if (local_numeric_fill_p) {
Expand All @@ -158,8 +160,9 @@ bwformat(BufferWriter &w, Spec const &spec, sockaddr const *addr) {
}
if (family_p) {
local_spec._min = 0;
if (addr_p || port_p)
if (addr_p || port_p) {
w.write(' ');
}
if (spec.has_numeric_type()) {
bwformat(w, local_spec, static_cast<uintmax_t>(addr->sa_family));
} else {
Expand Down Expand Up @@ -382,4 +385,5 @@ bwformat(BufferWriter &w, Spec const &spec, IPMask const &mask) {
return bwformat(w, spec, mask.width());
}

}} // namespace swoc::SWOC_VERSION_NS
} // namespace SWOC_VERSION_NS
} // namespace swoc
13 changes: 7 additions & 6 deletions code/src/swoc_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,12 @@ uintmax_t
remove_all(const path &p, std::error_code &ec)
{
// coverity TOCTOU - issue is doing stat before doing operation. Stupid complaint, ignore.
DIR *dir;
struct dirent *entry;
DIR *dir = nullptr;
struct dirent *entry = nullptr;
std::error_code err;
uintmax_t zret = 0;

struct ::stat s;
struct ::stat s{};
if (p.empty()) {
ec = std::error_code(EINVAL, std::system_category());
return zret;
Expand Down Expand Up @@ -414,7 +414,7 @@ remove_all(const path &p, std::error_code &ec)

bool remove(path const& p, std::error_code &ec) {
// coverity TOCTOU - issue is doing stat before doing operation. Stupid complaint, ignore.
struct ::stat fs;
struct ::stat fs{};
if (p.empty()) {
ec = std::error_code(EINVAL, std::system_category());
} else if (::stat(p.c_str(), &fs) < 0) {
Expand Down Expand Up @@ -442,7 +442,7 @@ load(const path &p, std::error_code &ec) {
if (unique_fd fd(::open(p.c_str(), O_RDONLY)) ; fd < 0) {
ec = std::error_code(errno, std::system_category());
} else {
struct stat info;
struct stat info{};
if (0 != ::fstat(fd, &info)) {
ec = std::error_code(errno, std::system_category());
} else {
Expand All @@ -464,4 +464,5 @@ bwformat(BufferWriter &w, bwf::Spec const &spec, file::path const &p) {
return bwformat(w, spec, p.string());
}

}} // namespace swoc::SWOC_VERSION_NS
} // namespace SWOC_VERSION_NS
} // namespace swoc
7 changes: 4 additions & 3 deletions code/src/swoc_ip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,8 @@ IPSrv::load(swoc::TextView text) {
}


IPSrv::IPSrv(IPAddr addr, in_port_t port) {
_family = addr.family();
IPSrv::IPSrv(IPAddr addr, in_port_t port) : _family(addr.family()) {

if (addr.is_ip4()) {
_srv._ip4.assign(addr.ip4(), port);
} else if (addr.is_ip6()) {
Expand Down Expand Up @@ -1254,4 +1254,5 @@ IPRangeView::operator==(IPRangeView::self_type const &that) const {
return false;
}

}} // namespace swoc::SWOC_VERSION_NS
} // namespace SWOC_VERSION_NS
} // namespace swoc
Loading

0 comments on commit a7eeeb8

Please sign in to comment.