Skip to content
Open
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
1 change: 0 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ AlignConsecutiveAssignments: false
AlignTrailingComments: true

SpaceAfterCStyleCast: true
CommentPragmas: '^ NO-FORMAT:'
26 changes: 26 additions & 0 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Code Formatting

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

permissions:
contents: read

jobs:
clang-format:
name: Run clang-format
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

# Runs clang-format over the repository codebase
- name: Check format
uses: jidicula/clang-format-action@v4.11.0
with:
clang-format-version: '20'
check-path: 'include tests'
Comment thread
beinhaerter marked this conversation as resolved.
Outdated
4 changes: 1 addition & 3 deletions include/gsl/algorithm
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ void copy(span<SrcElementType, SrcExtent> src, span<DestElementType, DestExtent>
"Source range is longer than target range");

Expects(dest.size() >= src.size());
// clang-format off
GSL_SUPPRESS(stl.1) // NO-FORMAT: attribute
// clang-format on
GSL_SUPPRESS(stl.1)
std::copy_n(src.data(), src.size(), dest.data());
}

Expand Down
4 changes: 1 addition & 3 deletions include/gsl/assert
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ namespace details

typedef void(__cdecl* terminate_handler)();

// clang-format off
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute
// clang-format on
GSL_SUPPRESS(f.6)
[[noreturn]] inline void __cdecl default_terminate_handler()
{
__fastfail(RANGE_CHECKS_FAILURE);
Expand Down
2 changes: 0 additions & 2 deletions include/gsl/byte
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ constexpr IntegerType to_integer(byte b) noexcept


template <typename T>
// NOTE: need suppression since c++14 does not allow "return {t}"
// GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work
constexpr gsl::impl::byte to_byte(T t) noexcept
{
static_assert(std::is_same<T, unsigned char>::value,
Expand Down
24 changes: 10 additions & 14 deletions include/gsl/narrow
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,19 @@ struct narrowing_error : public std::exception

// narrow() : a checked version of narrow_cast() that throws if the cast changed the value
template <class T, class U, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
// clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
GSL_SUPPRESS(es.46) // NO-FORMAT: attribute // The warning suggests that a floating->unsigned conversion can occur
// in the static_cast below, and that gsl::narrow should be used instead.
// Suppress this warning, since gsl::narrow is defined in terms of
// static_cast
// clang-format on
GSL_SUPPRESS(type.1)
GSL_SUPPRESS(es.46) // The warning suggests that a floating->unsigned conversion can occur
// in the static_cast below, and that gsl::narrow should be used instead.
// Suppress this warning, since gsl::narrow is defined in terms of
// static_cast
constexpr T narrow(U u)
{
constexpr const bool is_different_signedness =
(std::is_signed<T>::value != std::is_signed<U>::value);

GSL_SUPPRESS(es.103) // NO-FORMAT: attribute // don't overflow
GSL_SUPPRESS(es.104) // NO-FORMAT: attribute // don't underflow
GSL_SUPPRESS(p.2) // NO-FORMAT: attribute // don't rely on undefined behavior
GSL_SUPPRESS(es.103) // don't overflow
GSL_SUPPRESS(es.104) // don't underflow
GSL_SUPPRESS(p.2) // don't rely on undefined behavior
const T t = narrow_cast<T>(u); // While this is technically undefined behavior in some cases (i.e., if the source value is of floating-point type
// and cannot fit into the destination integral type), the resultant behavior is benign on the platforms
// that we target (i.e., no hardware trap representations are hit).
Expand All @@ -64,10 +62,8 @@ GSL_SUPPRESS(p.2) // NO-FORMAT: attribute // don't rely on undefined behavior
}

template <class T, class U, typename std::enable_if<!std::is_arithmetic<T>::value>::type* = nullptr>
// clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
// clang-format on
constexpr T narrow(U u)
GSL_SUPPRESS(type.1)
constexpr T narrow(U u)
{
const T t = narrow_cast<T>(u);

Expand Down
71 changes: 26 additions & 45 deletions include/gsl/span
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ namespace details
constexpr span_iterator& operator++() noexcept
{
Expects(current_ != end_);
// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
// clang-format on
GSL_SUPPRESS(bounds.1)
++current_;
return *this;
}
Expand Down Expand Up @@ -196,9 +194,7 @@ namespace details
if (n != 0) Expects(begin_ && current_ && end_);
if (n > 0) Expects(end_ - current_ >= n);
if (n < 0) Expects(current_ - begin_ >= -n);
// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
// clang-format on
GSL_SUPPRESS(bounds.1)
current_ += n;
return *this;
}
Expand Down Expand Up @@ -315,9 +311,7 @@ namespace details
if (n < 0) Expects(current_ - begin_ >= -n);
}

// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
// clang-format on
GSL_SUPPRESS(bounds.1)
constexpr pointer _Unwrapped() const noexcept
{ // after seeking *this to a high water mark, or using one of the
// _Verify_xxx functions above, unwrap this span_iterator to a raw
Expand All @@ -332,9 +326,7 @@ namespace details
#else
static constexpr bool _Unwrap_when_unverified = false;
#endif
// clang-format off
GSL_SUPPRESS(con.3) // NO-FORMAT: attribute // TODO: false positive
// clang-format on
GSL_SUPPRESS(con.3) // TODO: false positive
constexpr void _Seek_to(const pointer p) noexcept
{ // adjust the position of *this to previously verified location p
// after _Unwrapped
Expand All @@ -349,7 +341,8 @@ namespace details
template <typename Ptr>
friend struct std::pointer_traits;
};
}} // namespace gsl::details
} // namespace details
} // namespace gsl

namespace std
{
Expand All @@ -364,7 +357,10 @@ struct pointer_traits<::gsl::details::span_iterator<Type>>
};
} // namespace std

namespace gsl { namespace details {
namespace gsl
{
namespace details
{
template <std::size_t Ext>
class extent_type
{
Expand Down Expand Up @@ -589,10 +585,8 @@ public:
}

template <std::size_t Count>
// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
// clang-format on
constexpr span<element_type, Count> last() const noexcept
GSL_SUPPRESS(bounds.1)
constexpr span<element_type, Count> last() const noexcept
{
static_assert(Extent == dynamic_extent || Count <= Extent,
"last() cannot extract more elements from a span than it contains.");
Expand All @@ -601,10 +595,8 @@ public:
}

template <std::size_t Offset, std::size_t Count = dynamic_extent>
// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
// clang-format on
constexpr auto subspan() const noexcept ->
GSL_SUPPRESS(bounds.1)
constexpr auto subspan() const noexcept ->
typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type
{
static_assert(Extent == dynamic_extent || (Extent >= Offset && (Count == dynamic_extent ||
Expand Down Expand Up @@ -642,9 +634,7 @@ public:
constexpr bool empty() const noexcept { return size() == 0; }

// [span.elem], span element access
// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
// clang-format on
GSL_SUPPRESS(bounds.1)
constexpr reference operator[](size_type idx) const noexcept
{
Expects(idx < size());
Expand All @@ -669,18 +659,14 @@ public:
constexpr iterator begin() const noexcept
{
const auto data = storage_.data();
// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
// clang-format on
GSL_SUPPRESS(bounds.1)
return {data, data + size(), data};
}

constexpr iterator end() const noexcept
{
const auto data = storage_.data();
// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
// clang-format on
GSL_SUPPRESS(bounds.1)
const auto endData = data + storage_.size();
return {data, endData, endData};
}
Expand All @@ -693,9 +679,7 @@ public:
constexpr pointer _Unchecked_begin() const noexcept { return data(); }
constexpr pointer _Unchecked_end() const noexcept
{
// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
// clang-format on
GSL_SUPPRESS(bounds.1)
return data() + size();
}
#endif // _MSC_VER
Expand Down Expand Up @@ -752,9 +736,7 @@ private:
return tmp.subspan(offset, count);
}

// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
// clang-format on
GSL_SUPPRESS(bounds.1)
constexpr span<element_type, dynamic_extent>
make_subspan(size_type offset, size_type count, subspan_selector<dynamic_extent>) const noexcept
{
Expand Down Expand Up @@ -792,7 +774,9 @@ span(const Container&) -> span<Element>;
#if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
#if defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated" // Bug in clang-cl.exe which raises a C++17 -Wdeprecated warning about this static constexpr workaround in C++14 mode.
#pragma clang diagnostic ignored \
"-Wdeprecated" // Bug in clang-cl.exe which raises a C++17 -Wdeprecated warning about this
// static constexpr workaround in C++14 mode.
#endif // defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
template <class ElementType, std::size_t Extent>
constexpr const typename span<ElementType, Extent>::size_type span<ElementType, Extent>::extent;
Expand Down Expand Up @@ -827,11 +811,10 @@ template <class ElementType, std::size_t Extent>
span<const gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>
as_bytes(span<ElementType, Extent> s) noexcept
{
using type = span<const gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>;
using type =
span<const gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>;

// clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
// clang-format on
GSL_SUPPRESS(type.1)
return type{reinterpret_cast<const gsl::impl::byte*>(s.data()), s.size_bytes()};
}

Expand All @@ -842,9 +825,7 @@ as_writable_bytes(span<ElementType, Extent> s) noexcept
{
using type = span<gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>;

// clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
// clang-format on
GSL_SUPPRESS(type.1)
return type{reinterpret_cast<gsl::impl::byte*>(s.data()), s.size_bytes()};
}

Expand Down
20 changes: 6 additions & 14 deletions include/gsl/util
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ GSL_NODISCARD auto finally(F&& f) noexcept

// narrow_cast(): a searchable way to do narrowing casts of values
template <class T, class U>
// clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
// clang-format on
GSL_SUPPRESS(type.1)
constexpr T narrow_cast(U&& u) noexcept
{
return static_cast<T>(std::forward<U>(u));
Expand All @@ -139,10 +137,8 @@ GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
// at() - Bounds-checked way of accessing builtin arrays, std::array, std::vector
//
template <class T, std::size_t N>
// clang-format off
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
// clang-format on
GSL_SUPPRESS(bounds.4)
GSL_SUPPRESS(bounds.2)
constexpr T& at(T (&arr)[N], const index i)
{
static_assert(N <= static_cast<std::size_t>((std::numeric_limits<std::ptrdiff_t>::max)()), "We only support arrays up to PTRDIFF_MAX bytes.");
Expand All @@ -151,10 +147,8 @@ GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
}

template <class Cont>
// clang-format off
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
// clang-format on
GSL_SUPPRESS(bounds.4)
GSL_SUPPRESS(bounds.2)
constexpr auto at(Cont& cont, const index i) -> decltype(cont[cont.size()])
{
Expects(i >= 0 && i < narrow_cast<index>(cont.size()));
Expand All @@ -163,9 +157,7 @@ GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
}

template <class T>
// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
// clang-format on
GSL_SUPPRESS(bounds.1)
constexpr T at(const std::initializer_list<T> cont, const index i)
{
Expects(i >= 0 && i < narrow_cast<index>(cont.size()));
Expand Down
4 changes: 2 additions & 2 deletions tests/at_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
TEST(at_tests, static_array)
{
int a[4] = {1, 2, 3, 4};
const int(&c_a)[4] = a;
const int (&c_a)[4] = a;

for (int i = 0; i < 4; ++i)
{
Expand Down Expand Up @@ -152,7 +152,7 @@ TEST(at_tests, std_span)
static constexpr bool test_constexpr()
{
int a1[4] = {1, 2, 3, 4};
const int(&c_a1)[4] = a1;
const int (&c_a1)[4] = a1;
std::array<int, 4> a2 = {1, 2, 3, 4};
const std::array<int, 4>& c_a2 = a2;

Expand Down
4 changes: 2 additions & 2 deletions tests/byte_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ static_assert(!RShiftAssignCompilesFor<float>, "!RShiftAssignCompilesFor<float>"
template <typename U, typename = void>
static constexpr bool ToIntegerCompilesFor = false;
template <typename U>
static constexpr bool
ToIntegerCompilesFor<U, void_t<decltype(gsl::to_integer<U>(gsl::byte{}))>> = true;
static constexpr bool ToIntegerCompilesFor<U, void_t<decltype(gsl::to_integer<U>(gsl::byte{}))>> =
true;
static_assert(!ToIntegerCompilesFor<float>, "!ToIntegerCompilesFor<float>");

} // namespace
Loading
Loading