Skip to content

Commit

Permalink
Reduce linter warnings.
Browse files Browse the repository at this point in the history
In particular, add [[nodiscard]] attributes where appropriate.
  • Loading branch information
kmhofmann committed Sep 8, 2019
1 parent 594b07e commit 27c9014
Show file tree
Hide file tree
Showing 45 changed files with 286 additions and 291 deletions.
6 changes: 6 additions & 0 deletions .lvimrc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ let g:ale_cpp_clangtidy_checks = [
\ '-android-*',
\ '-*-no-malloc',
\ '-cert*',
\ '-cppcoreguidelines-avoid-magic-numbers',
\ '-cppcoreguidelines-owning-memory',
\ '-cppcoreguidelines-pro-bounds-constant-array-index',
\ '-cppcoreguidelines-pro-bounds-pointer-arithmetic',
\ '-cppcoreguidelines-pro-type-vararg',
\ '-cppcoreguidelines-pro-type-reinterpret-cast',
Expand All @@ -56,9 +58,13 @@ let g:ale_cpp_clangtidy_checks = [
\ '-google-runtime-references',
\ '-hicpp-no-array-decay',
\ '-hicpp-signed-bitwise',
\ '-hicpp-uppercase-literal-suffix',
\ '-hicpp-vararg',
\ '-llvm-header-guard',
\ '-misc-non-private-member-variables-in-classes',
\ '-readability-implicit-bool-conversion',
\ '-readability-magic-numbers',
\ '-readability-named-parameter',
\ '-readability-redundant-declaration',
\ '-readability-uppercase-literal-suffix',
\ ]
2 changes: 1 addition & 1 deletion selene/base/Bitcount.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ constexpr std::size_t bit_count(T x)
x = static_cast<T>(x - ((x >> 1) & static_cast<T>(~T{0}) / 3));
x = static_cast<T>((x & static_cast<T>(~T{0}) / 15 * 3) + ((x >> 2) & static_cast<T>(~T{0}) / 15 * 3));
x = static_cast<T>((x + (x >> 4)) & static_cast<T>(~T{0}) / 255 * 15);
const std::size_t c = static_cast<std::size_t>(static_cast<T>(x * ((T)~T{0} / 255)) >> (sizeof(T) - 1) * CHAR_BIT);
const auto c = static_cast<std::size_t>(static_cast<T>(x * ((T)~T{0} / 255)) >> (sizeof(T) - 1) * CHAR_BIT);
return c;
}

Expand Down
2 changes: 1 addition & 1 deletion selene/base/Kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Kernel
const_iterator end() const noexcept;
const_iterator cend() const noexcept;

constexpr std::size_t size() const noexcept;
[[nodiscard]] constexpr std::size_t size() const noexcept;
constexpr value_type operator[](std::size_t idx) const noexcept;

constexpr void normalize(value_type sum) noexcept;
Expand Down
4 changes: 2 additions & 2 deletions selene/base/MemoryBlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class MemoryBlock
MemoryBlock(MemoryBlock&& other) noexcept = default;
MemoryBlock& operator=(MemoryBlock&& other) noexcept = default;

std::uint8_t* data() const noexcept;
std::size_t size() const noexcept;
[[nodiscard]] std::uint8_t* data() const noexcept;
[[nodiscard]] std::size_t size() const noexcept;

std::uint8_t* transfer_data() noexcept;

Expand Down
2 changes: 1 addition & 1 deletion selene/base/MessageLog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class MessageLog
using Messages = std::vector<Message>; ///< A message collection.
MessageLog() = default;

const Messages& messages() const;
[[nodiscard]] const Messages& messages() const;

// TODO: Replace by std::string_view
void add(const char* text, MessageType type);
Expand Down
14 changes: 7 additions & 7 deletions selene/img/common/BoundingBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ class BoundingBox
constexpr BoundingBox(PixelIndex x0, PixelIndex y0, PixelLength width, PixelLength height) noexcept;
constexpr BoundingBox(PixelIndex x0, PixelIndex y0, PixelIndex x1, PixelIndex y1) noexcept;

constexpr PixelIndex x0() const noexcept;
constexpr PixelIndex y0() const noexcept;
constexpr PixelLength width() const noexcept;
constexpr PixelLength height() const noexcept;
[[nodiscard]] constexpr PixelIndex x0() const noexcept;
[[nodiscard]] constexpr PixelIndex y0() const noexcept;
[[nodiscard]] constexpr PixelLength width() const noexcept;
[[nodiscard]] constexpr PixelLength height() const noexcept;

constexpr PixelIndex x1() const noexcept;
constexpr PixelIndex y1() const noexcept;
[[nodiscard]] constexpr PixelIndex x1() const noexcept;
[[nodiscard]] constexpr PixelIndex y1() const noexcept;

constexpr bool empty() const noexcept;
[[nodiscard]] constexpr bool empty() const noexcept;
constexpr void sanitize(PixelLength max_img_width, PixelLength max_img_height) noexcept;

private:
Expand Down
16 changes: 6 additions & 10 deletions selene/img/common/DataPtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@ class DataPtr<ImageModifiability::Constant>
using Type = const std::uint8_t*;
using ConstType = const std::uint8_t*;

constexpr DataPtr() noexcept
: data_(nullptr)
{ }
constexpr DataPtr() noexcept = default;

constexpr /*explicit*/ DataPtr(const std::uint8_t* data) noexcept
: data_(data)
{ }

constexpr const std::uint8_t* data() const noexcept { return data_; }
[[nodiscard]] constexpr const std::uint8_t* data() const noexcept { return data_; }

private:
const std::uint8_t* data_;
const std::uint8_t* data_ = nullptr;
};

// ---
Expand All @@ -57,19 +55,17 @@ class DataPtr<ImageModifiability::Mutable>
using Type = std::uint8_t*;
using ConstType = const std::uint8_t*;

constexpr DataPtr() noexcept
: data_(nullptr)
{ }
constexpr DataPtr() noexcept = default;

constexpr /*explicit*/ DataPtr(std::uint8_t* data) noexcept
: data_(data)
{ }

constexpr std::uint8_t* data() noexcept { return data_; }
constexpr std::uint8_t* data() const noexcept { return data_; }
[[nodiscard]] constexpr std::uint8_t* data() const noexcept { return data_; }

private:
std::uint8_t* data_;
std::uint8_t* data_ = nullptr;
};

/// @}
Expand Down
4 changes: 2 additions & 2 deletions selene/img/common/Iterators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class ImageRow
*
* @return Row index.
*/
PixelIndex index() const noexcept
[[nodiscard]] PixelIndex index() const noexcept
{
return row_index_;
}
Expand Down Expand Up @@ -218,7 +218,7 @@ class DynImageRow
*
* @return Row index.
*/
PixelIndex index() const noexcept
[[nodiscard]] PixelIndex index() const noexcept
{
return row_index_;
}
Expand Down
44 changes: 22 additions & 22 deletions selene/img/dynamic/DynImage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,22 @@ class DynImage

template <ImageModifiability modifiability> DynImage& operator=(const DynImageView<modifiability>&);

const UntypedLayout& layout() const noexcept;
const UntypedImageSemantics& semantics() const noexcept;

PixelLength width() const noexcept;
PixelLength height() const noexcept;
std::int16_t nr_channels() const noexcept;
std::int16_t nr_bytes_per_channel() const noexcept;
Stride stride_bytes() const noexcept;
std::ptrdiff_t row_bytes() const noexcept;
std::ptrdiff_t total_bytes() const noexcept;
PixelFormat pixel_format() const noexcept;
SampleFormat sample_format() const noexcept;

bool is_packed() const noexcept;
bool is_empty() const noexcept;
bool is_valid() const noexcept;
[[nodiscard]] const UntypedLayout& layout() const noexcept;
[[nodiscard]] const UntypedImageSemantics& semantics() const noexcept;

[[nodiscard]] PixelLength width() const noexcept;
[[nodiscard]] PixelLength height() const noexcept;
[[nodiscard]] std::int16_t nr_channels() const noexcept;
[[nodiscard]] std::int16_t nr_bytes_per_channel() const noexcept;
[[nodiscard]] Stride stride_bytes() const noexcept;
[[nodiscard]] std::ptrdiff_t row_bytes() const noexcept;
[[nodiscard]] std::ptrdiff_t total_bytes() const noexcept;
[[nodiscard]] PixelFormat pixel_format() const noexcept;
[[nodiscard]] SampleFormat sample_format() const noexcept;

[[nodiscard]] bool is_packed() const noexcept;
[[nodiscard]] bool is_empty() const noexcept;
[[nodiscard]] bool is_valid() const noexcept;

template <typename PixelType> auto begin() noexcept -> iterator<PixelType>;
template <typename PixelType> auto begin() const noexcept -> const_iterator<PixelType>;
Expand All @@ -127,13 +127,13 @@ class DynImage
template <typename PixelType> auto cend() const noexcept -> const_iterator<PixelType>;

DataPtrType byte_ptr() noexcept;
ConstDataPtrType byte_ptr() const noexcept;
[[nodiscard]] ConstDataPtrType byte_ptr() const noexcept;

DataPtrType byte_ptr(PixelIndex y) noexcept;
ConstDataPtrType byte_ptr(PixelIndex y) const noexcept;
[[nodiscard]] ConstDataPtrType byte_ptr(PixelIndex y) const noexcept;

DataPtrType byte_ptr(PixelIndex x, PixelIndex y) noexcept;
ConstDataPtrType byte_ptr(PixelIndex x, PixelIndex y) const noexcept;
[[nodiscard]] ConstDataPtrType byte_ptr(PixelIndex x, PixelIndex y) const noexcept;

template <typename PixelType> PixelType* data() noexcept;
template <typename PixelType> const PixelType* data() const noexcept;
Expand All @@ -151,8 +151,8 @@ class DynImage
template <typename PixelType> const PixelType& pixel(PixelIndex x, PixelIndex y) const noexcept;

DynImageView<ImageModifiability::Mutable>& view() noexcept;
DynImageView<ImageModifiability::Constant> view() const noexcept;
DynImageView<ImageModifiability::Constant> constant_view() const noexcept;
[[nodiscard]] DynImageView<ImageModifiability::Constant> view() const noexcept;
[[nodiscard]] DynImageView<ImageModifiability::Constant> constant_view() const noexcept;

void clear();

Expand All @@ -165,7 +165,7 @@ class DynImage
impl::CompressedPair<DynImageView<ImageModifiability::Mutable>, Allocator> view_and_alloc_;

DynImageView<ImageModifiability::Mutable>& mem_view() { return view_and_alloc_.first(); }
const DynImageView<ImageModifiability::Mutable>& mem_view() const { return view_and_alloc_.first(); }
[[nodiscard]] const DynImageView<ImageModifiability::Mutable>& mem_view() const { return view_and_alloc_.first(); }

Allocator& mem_alloc() { return view_and_alloc_.second(); }
const Allocator& mem_alloc() const { return view_and_alloc_.second(); }
Expand Down
36 changes: 18 additions & 18 deletions selene/img/dynamic/DynImageView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,22 @@ class DynImageView
UntypedLayout layout,
UntypedImageSemantics semantics = UntypedImageSemantics{});

const UntypedLayout& layout() const noexcept;
const UntypedImageSemantics& semantics() const noexcept;

PixelLength width() const noexcept;
PixelLength height() const noexcept;
std::int16_t nr_channels() const noexcept;
std::int16_t nr_bytes_per_channel() const noexcept;
Stride stride_bytes() const noexcept;
std::ptrdiff_t row_bytes() const noexcept;
std::ptrdiff_t total_bytes() const noexcept;
PixelFormat pixel_format() const noexcept;
SampleFormat sample_format() const noexcept;

bool is_packed() const noexcept;
bool is_empty() const noexcept;
bool is_valid() const noexcept;
[[nodiscard]] const UntypedLayout& layout() const noexcept;
[[nodiscard]] const UntypedImageSemantics& semantics() const noexcept;

[[nodiscard]] PixelLength width() const noexcept;
[[nodiscard]] PixelLength height() const noexcept;
[[nodiscard]] std::int16_t nr_channels() const noexcept;
[[nodiscard]] std::int16_t nr_bytes_per_channel() const noexcept;
[[nodiscard]] Stride stride_bytes() const noexcept;
[[nodiscard]] std::ptrdiff_t row_bytes() const noexcept;
[[nodiscard]] std::ptrdiff_t total_bytes() const noexcept;
[[nodiscard]] PixelFormat pixel_format() const noexcept;
[[nodiscard]] SampleFormat sample_format() const noexcept;

[[nodiscard]] bool is_packed() const noexcept;
[[nodiscard]] bool is_empty() const noexcept;
[[nodiscard]] bool is_valid() const noexcept;

template <typename PixelType> iterator<PixelType> begin() noexcept;
template <typename PixelType> const_iterator<PixelType> begin() const noexcept;
Expand Down Expand Up @@ -162,8 +162,8 @@ class DynImageView
UntypedLayout layout_;
UntypedImageSemantics semantics_;

std::ptrdiff_t compute_data_offset(PixelIndex y) const noexcept;
std::ptrdiff_t compute_data_offset(PixelIndex x, PixelIndex y) const noexcept;
[[nodiscard]] std::ptrdiff_t compute_data_offset(PixelIndex y) const noexcept;
[[nodiscard]] std::ptrdiff_t compute_data_offset(PixelIndex x, PixelIndex y) const noexcept;

friend void swap<modifiability_>(DynImageView<modifiability_>&, DynImageView<modifiability_>&) noexcept;
};
Expand Down
35 changes: 13 additions & 22 deletions selene/img/dynamic/UntypedLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ namespace sln {
class UntypedLayout
{
public:
constexpr UntypedLayout() noexcept
: width{PixelLength{0}}
, height{PixelLength{0}}
, nr_channels{0}
, nr_bytes_per_channel{0}
, stride_bytes{Stride{0}}
{ }
constexpr UntypedLayout() noexcept = default;

constexpr UntypedLayout(PixelLength width_,
PixelLength height_,
Expand All @@ -52,16 +46,16 @@ class UntypedLayout
, stride_bytes(stride_bytes_)
{ }

PixelLength width; ///< The image width in pixels.
PixelLength height; ///< The image height in pixels.
std::int16_t nr_channels; ///< The number of image channels
std::int16_t nr_bytes_per_channel; ///< The number of bytes used for a channel value.
Stride stride_bytes; ///< The image row stride in bytes. The layout may include additional padding bytes.
PixelLength width = PixelLength{0}; ///< The image width in pixels.
PixelLength height = PixelLength{0}; ///< The image height in pixels.
std::int16_t nr_channels = 0; ///< The number of image channels
std::int16_t nr_bytes_per_channel = 0; ///< The number of bytes used for a channel value.
Stride stride_bytes = Stride{0}; ///< The image row stride in bytes. The layout may include additional padding bytes.

constexpr std::ptrdiff_t nr_bytes_per_pixel() const noexcept;
constexpr std::ptrdiff_t row_bytes() const noexcept;
constexpr std::ptrdiff_t total_bytes() const noexcept;
constexpr bool is_packed() const noexcept;
[[nodiscard]] constexpr std::ptrdiff_t nr_bytes_per_pixel() const noexcept;
[[nodiscard]] constexpr std::ptrdiff_t row_bytes() const noexcept;
[[nodiscard]] constexpr std::ptrdiff_t total_bytes() const noexcept;
[[nodiscard]] constexpr bool is_packed() const noexcept;
};

constexpr bool operator==(const UntypedLayout& l, const UntypedLayout& r);
Expand All @@ -73,19 +67,16 @@ constexpr bool operator!=(const UntypedLayout& l, const UntypedLayout& r);
class UntypedImageSemantics
{
public:
constexpr UntypedImageSemantics() noexcept
: pixel_format(PixelFormat::Unknown)
, sample_format(SampleFormat::Unknown)
{ }
constexpr UntypedImageSemantics() noexcept = default;

constexpr UntypedImageSemantics(PixelFormat pixel_format_,
SampleFormat sample_format_)
: pixel_format(pixel_format_)
, sample_format(sample_format_)
{ }

PixelFormat pixel_format;
SampleFormat sample_format;
PixelFormat pixel_format = PixelFormat::Unknown;
SampleFormat sample_format = SampleFormat::Unknown;
};

constexpr bool operator==(const UntypedImageSemantics& l, const UntypedImageSemantics& r);
Expand Down
2 changes: 1 addition & 1 deletion selene/img/interop/DynImageToImage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

/// @file

#include <selene/img/dynamic/DynImageView.hpp>
#include <selene/img/dynamic/DynImage.hpp>
#include <selene/img/dynamic/DynImageView.hpp>

#include <selene/img/typed/Image.hpp>
#include <selene/img/typed/ImageView.hpp>
Expand Down
2 changes: 1 addition & 1 deletion selene/img/interop/ImageToDynImage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

/// @file

#include <selene/img/dynamic/DynImageView.hpp>
#include <selene/img/dynamic/DynImage.hpp>
#include <selene/img/dynamic/DynImageView.hpp>

#include <selene/img/typed/Image.hpp>
#include <selene/img/typed/ImageView.hpp>
Expand Down
16 changes: 8 additions & 8 deletions selene/img/typed/ImageBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class ImageExpr

decltype(auto) layout() const noexcept { return this->derived().layout(); }

PixelLength width() const noexcept { return derived().width(); }
PixelLength height() const noexcept { return derived().height(); }
Stride stride_bytes() const noexcept { return this->derived().stride_bytes(); }
[[nodiscard]] PixelLength width() const noexcept { return derived().width(); }
[[nodiscard]] PixelLength height() const noexcept { return derived().height(); }
[[nodiscard]] Stride stride_bytes() const noexcept { return this->derived().stride_bytes(); }

decltype(auto) operator()(PixelIndex x, PixelIndex y) noexcept { return derived().operator()(x, y); }
decltype(auto) operator()(PixelIndex x, PixelIndex y) const noexcept { return derived().operator()(x, y); }
Expand All @@ -55,12 +55,12 @@ class ImageBase : public ImageExpr<Derived>
public:
using PixelType = typename ImageExpr<Derived>::PixelType;

std::ptrdiff_t row_bytes() const noexcept { return this->derived().row_bytes(); }
std::ptrdiff_t total_bytes() const noexcept { return this->derived().total_bytes(); }
bool is_packed() const noexcept { return this->derived().is_packed(); }
[[nodiscard]] std::ptrdiff_t row_bytes() const noexcept { return this->derived().row_bytes(); }
[[nodiscard]] std::ptrdiff_t total_bytes() const noexcept { return this->derived().total_bytes(); }
[[nodiscard]] bool is_packed() const noexcept { return this->derived().is_packed(); }

bool is_empty() const noexcept { return this->derived().is_empty(); }
bool is_valid() const noexcept { return this->derived().is_valid(); }
[[nodiscard]] bool is_empty() const noexcept { return this->derived().is_empty(); }
[[nodiscard]] bool is_valid() const noexcept { return this->derived().is_valid(); }

decltype(auto) begin() noexcept { return this->derived().begin(); }
decltype(auto) begin() const noexcept { return this->derived().begin(); }
Expand Down
2 changes: 1 addition & 1 deletion selene/img/typed/ImageTypeAliases.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

/// @file

#include <selene/img/typed/Image.hpp>
#include <selene/img/pixel/Pixel.hpp>
#include <selene/img/typed/Image.hpp>

namespace sln {

Expand Down
Loading

0 comments on commit 27c9014

Please sign in to comment.