Skip to content

Commit 5b3e9be

Browse files
author
Werner Henze
committed
clang-format improvements
- Add a clang-format linter check to the PR pipeline - Apply clang-format to files where the linter initially failed - Remove `CommentPragmas` from `.clang-format` - Remove all `// clang-format off` and `// NO-FORMAT` as they are not needed - Remove a commented out `GSL_SUPPRESS`
1 parent f3c5967 commit 5b3e9be

18 files changed

Lines changed: 116 additions & 151 deletions

.clang-format

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,3 @@ AlignConsecutiveAssignments: false
3131
AlignTrailingComments: true
3232

3333
SpaceAfterCStyleCast: true
34-
CommentPragmas: '^ NO-FORMAT:'

.github/workflows/clang-format.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Code Formatting
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
clang-format:
14+
name: Run clang-format
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
21+
# Runs clang-format over the repository codebase
22+
- name: Check format
23+
uses: jidicula/clang-format-action@v4.11.0
24+
with:
25+
clang-format-version: '19'
26+
check-path: 'include tests'

include/gsl/algorithm

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ void copy(span<SrcElementType, SrcExtent> src, span<DestElementType, DestExtent>
4848
"Source range is longer than target range");
4949

5050
Expects(dest.size() >= src.size());
51-
// clang-format off
52-
GSL_SUPPRESS(stl.1) // NO-FORMAT: attribute
53-
// clang-format on
51+
GSL_SUPPRESS(stl.1)
5452
std::copy_n(src.data(), src.size(), dest.data());
5553
}
5654

include/gsl/assert

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ namespace details
9393

9494
typedef void(__cdecl* terminate_handler)();
9595

96-
// clang-format off
97-
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute
98-
// clang-format on
96+
GSL_SUPPRESS(f.6)
9997
[[noreturn]] inline void __cdecl default_terminate_handler()
10098
{
10199
__fastfail(RANGE_CHECKS_FAILURE);

include/gsl/byte

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ constexpr IntegerType to_integer(byte b) noexcept
172172

173173

174174
template <typename T>
175-
// NOTE: need suppression since c++14 does not allow "return {t}"
176-
// GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work
177175
constexpr gsl::impl::byte to_byte(T t) noexcept
178176
{
179177
static_assert(std::is_same<T, unsigned char>::value,

include/gsl/narrow

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,19 @@ struct narrowing_error : public std::exception
2828

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

43-
GSL_SUPPRESS(es.103) // NO-FORMAT: attribute // don't overflow
44-
GSL_SUPPRESS(es.104) // NO-FORMAT: attribute // don't underflow
45-
GSL_SUPPRESS(p.2) // NO-FORMAT: attribute // don't rely on undefined behavior
41+
GSL_SUPPRESS(es.103) // don't overflow
42+
GSL_SUPPRESS(es.104) // don't underflow
43+
GSL_SUPPRESS(p.2) // don't rely on undefined behavior
4644
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
4745
// and cannot fit into the destination integral type), the resultant behavior is benign on the platforms
4846
// that we target (i.e., no hardware trap representations are hit).
@@ -64,10 +62,8 @@ GSL_SUPPRESS(p.2) // NO-FORMAT: attribute // don't rely on undefined behavior
6462
}
6563

6664
template <class T, class U, typename std::enable_if<!std::is_arithmetic<T>::value>::type* = nullptr>
67-
// clang-format off
68-
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
69-
// clang-format on
70-
constexpr T narrow(U u)
65+
GSL_SUPPRESS(type.1)
66+
constexpr T narrow(U u)
7167
{
7268
const T t = narrow_cast<T>(u);
7369

include/gsl/span

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@ namespace details
163163
constexpr span_iterator& operator++() noexcept
164164
{
165165
Expects(current_ != end_);
166-
// clang-format off
167-
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
168-
// clang-format on
166+
GSL_SUPPRESS(bounds.1)
169167
++current_;
170168
return *this;
171169
}
@@ -196,9 +194,7 @@ namespace details
196194
if (n != 0) Expects(begin_ && current_ && end_);
197195
if (n > 0) Expects(end_ - current_ >= n);
198196
if (n < 0) Expects(current_ - begin_ >= -n);
199-
// clang-format off
200-
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
201-
// clang-format on
197+
GSL_SUPPRESS(bounds.1)
202198
current_ += n;
203199
return *this;
204200
}
@@ -315,9 +311,7 @@ namespace details
315311
if (n < 0) Expects(current_ - begin_ >= -n);
316312
}
317313

318-
// clang-format off
319-
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
320-
// clang-format on
314+
GSL_SUPPRESS(bounds.1)
321315
constexpr pointer _Unwrapped() const noexcept
322316
{ // after seeking *this to a high water mark, or using one of the
323317
// _Verify_xxx functions above, unwrap this span_iterator to a raw
@@ -332,9 +326,7 @@ namespace details
332326
#else
333327
static constexpr bool _Unwrap_when_unverified = false;
334328
#endif
335-
// clang-format off
336-
GSL_SUPPRESS(con.3) // NO-FORMAT: attribute // TODO: false positive
337-
// clang-format on
329+
GSL_SUPPRESS(con.3) // TODO: false positive
338330
constexpr void _Seek_to(const pointer p) noexcept
339331
{ // adjust the position of *this to previously verified location p
340332
// after _Unwrapped
@@ -349,7 +341,8 @@ namespace details
349341
template <typename Ptr>
350342
friend struct std::pointer_traits;
351343
};
352-
}} // namespace gsl::details
344+
} // namespace details
345+
} // namespace gsl
353346

354347
namespace std
355348
{
@@ -364,7 +357,10 @@ struct pointer_traits<::gsl::details::span_iterator<Type>>
364357
};
365358
} // namespace std
366359

367-
namespace gsl { namespace details {
360+
namespace gsl
361+
{
362+
namespace details
363+
{
368364
template <std::size_t Ext>
369365
class extent_type
370366
{
@@ -589,10 +585,8 @@ public:
589585
}
590586

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

603597
template <std::size_t Offset, std::size_t Count = dynamic_extent>
604-
// clang-format off
605-
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
606-
// clang-format on
607-
constexpr auto subspan() const noexcept ->
598+
GSL_SUPPRESS(bounds.1)
599+
constexpr auto subspan() const noexcept ->
608600
typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type
609601
{
610602
static_assert(Extent == dynamic_extent || (Extent >= Offset && (Count == dynamic_extent ||
@@ -642,9 +634,7 @@ public:
642634
constexpr bool empty() const noexcept { return size() == 0; }
643635

644636
// [span.elem], span element access
645-
// clang-format off
646-
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
647-
// clang-format on
637+
GSL_SUPPRESS(bounds.1)
648638
constexpr reference operator[](size_type idx) const noexcept
649639
{
650640
Expects(idx < size());
@@ -669,18 +659,14 @@ public:
669659
constexpr iterator begin() const noexcept
670660
{
671661
const auto data = storage_.data();
672-
// clang-format off
673-
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
674-
// clang-format on
662+
GSL_SUPPRESS(bounds.1)
675663
return {data, data + size(), data};
676664
}
677665

678666
constexpr iterator end() const noexcept
679667
{
680668
const auto data = storage_.data();
681-
// clang-format off
682-
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
683-
// clang-format on
669+
GSL_SUPPRESS(bounds.1)
684670
const auto endData = data + storage_.size();
685671
return {data, endData, endData};
686672
}
@@ -693,9 +679,7 @@ public:
693679
constexpr pointer _Unchecked_begin() const noexcept { return data(); }
694680
constexpr pointer _Unchecked_end() const noexcept
695681
{
696-
// clang-format off
697-
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
698-
// clang-format on
682+
GSL_SUPPRESS(bounds.1)
699683
return data() + size();
700684
}
701685
#endif // _MSC_VER
@@ -752,9 +736,7 @@ private:
752736
return tmp.subspan(offset, count);
753737
}
754738

755-
// clang-format off
756-
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
757-
// clang-format on
739+
GSL_SUPPRESS(bounds.1)
758740
constexpr span<element_type, dynamic_extent>
759741
make_subspan(size_type offset, size_type count, subspan_selector<dynamic_extent>) const noexcept
760742
{
@@ -792,7 +774,9 @@ span(const Container&) -> span<Element>;
792774
#if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
793775
#if defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
794776
#pragma clang diagnostic push
795-
#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.
777+
#pragma clang diagnostic ignored \
778+
"-Wdeprecated" // Bug in clang-cl.exe which raises a C++17 -Wdeprecated warning about this
779+
// static constexpr workaround in C++14 mode.
796780
#endif // defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
797781
template <class ElementType, std::size_t Extent>
798782
constexpr const typename span<ElementType, Extent>::size_type span<ElementType, Extent>::extent;
@@ -827,11 +811,10 @@ template <class ElementType, std::size_t Extent>
827811
span<const gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>
828812
as_bytes(span<ElementType, Extent> s) noexcept
829813
{
830-
using type = span<const gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>;
814+
using type =
815+
span<const gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>;
831816

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

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

845-
// clang-format off
846-
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
847-
// clang-format on
828+
GSL_SUPPRESS(type.1)
848829
return type{reinterpret_cast<gsl::impl::byte*>(s.data()), s.size_bytes()};
849830
}
850831

include/gsl/util

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ GSL_NODISCARD auto finally(F&& f) noexcept
127127

128128
// narrow_cast(): a searchable way to do narrowing casts of values
129129
template <class T, class U>
130-
// clang-format off
131-
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
132-
// clang-format on
130+
GSL_SUPPRESS(type.1)
133131
constexpr T narrow_cast(U&& u) noexcept
134132
{
135133
return static_cast<T>(std::forward<U>(u));
@@ -139,10 +137,8 @@ GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
139137
// at() - Bounds-checked way of accessing builtin arrays, std::array, std::vector
140138
//
141139
template <class T, std::size_t N>
142-
// clang-format off
143-
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
144-
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
145-
// clang-format on
140+
GSL_SUPPRESS(bounds.4)
141+
GSL_SUPPRESS(bounds.2)
146142
constexpr T& at(T (&arr)[N], const index i)
147143
{
148144
static_assert(N <= static_cast<std::size_t>((std::numeric_limits<std::ptrdiff_t>::max)()), "We only support arrays up to PTRDIFF_MAX bytes.");
@@ -151,10 +147,8 @@ GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
151147
}
152148

153149
template <class Cont>
154-
// clang-format off
155-
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
156-
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
157-
// clang-format on
150+
GSL_SUPPRESS(bounds.4)
151+
GSL_SUPPRESS(bounds.2)
158152
constexpr auto at(Cont& cont, const index i) -> decltype(cont[cont.size()])
159153
{
160154
Expects(i >= 0 && i < narrow_cast<index>(cont.size()));
@@ -163,9 +157,7 @@ GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
163157
}
164158

165159
template <class T>
166-
// clang-format off
167-
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
168-
// clang-format on
160+
GSL_SUPPRESS(bounds.1)
169161
constexpr T at(const std::initializer_list<T> cont, const index i)
170162
{
171163
Expects(i >= 0 && i < narrow_cast<index>(cont.size()));

tests/byte_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ static_assert(!RShiftAssignCompilesFor<float>, "!RShiftAssignCompilesFor<float>"
171171
template <typename U, typename = void>
172172
static constexpr bool ToIntegerCompilesFor = false;
173173
template <typename U>
174-
static constexpr bool
175-
ToIntegerCompilesFor<U, void_t<decltype(gsl::to_integer<U>(gsl::byte{}))>> = true;
174+
static constexpr bool ToIntegerCompilesFor<U, void_t<decltype(gsl::to_integer<U>(gsl::byte{}))>> =
175+
true;
176176
static_assert(!ToIntegerCompilesFor<float>, "!ToIntegerCompilesFor<float>");
177177

178178
} // namespace

tests/constexpr_notnull_tests.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ constexpr bool comparison_test(const int* ptr1, const int* ptr2)
2828
const not_null<const int*> p1(ptr1);
2929
const not_null<const int*> p1_same(ptr1);
3030
const not_null<const int*> p2(ptr2);
31-
31+
3232
// Testing operator==
3333
const bool eq_result = (p1 == p1_same); // Should be true
3434
const bool neq_result = (p1 != p2); // Should be true
35-
35+
3636
// Testing operator<= and operator>=
3737
const bool le_result = (p1 <= p1_same); // Should be true
3838
const bool ge_result = (p1 >= p1_same); // Should be true
39-
39+
4040
// The exact comparison results will depend on pointer ordering,
4141
// but we can verify that the basic equality checks work as expected
4242
return eq_result && neq_result && le_result && ge_result;
@@ -47,20 +47,22 @@ constexpr bool workaround_test(const int* ptr1, const int* ptr2)
4747
const not_null<const int*> p1(ptr1);
4848
const not_null<const int*> p1_same(ptr1);
4949
const not_null<const int*> p2(ptr2);
50-
50+
5151
// Using .get() to compare
5252
const bool eq_result = (p1.get() == p1_same.get()); // Should be true
5353
const bool neq_result = (p1.get() != p2.get()); // Should be true
54-
54+
5555
return eq_result && neq_result;
5656
}
5757
} // namespace
5858

5959
constexpr int test_value1 = 1;
6060
constexpr int test_value2 = 2;
6161

62-
static_assert(comparison_test(&test_value1, &test_value2), "not_null comparison operators should be constexpr");
63-
static_assert(workaround_test(&test_value1, &test_value2), "not_null .get() comparison workaround should work");
62+
static_assert(comparison_test(&test_value1, &test_value2),
63+
"not_null comparison operators should be constexpr");
64+
static_assert(workaround_test(&test_value1, &test_value2),
65+
"not_null .get() comparison workaround should work");
6466

6567
TEST(notnull_constexpr_tests, TestNotNullConstexprComparison)
6668
{
@@ -71,4 +73,3 @@ TEST(notnull_constexpr_tests, TestNotNullConstexprComparison)
7173
EXPECT_TRUE(comparison_test(&value1, &value2));
7274
EXPECT_TRUE(workaround_test(&value1, &value2));
7375
}
74-

0 commit comments

Comments
 (0)