Skip to content

Commit 809cc39

Browse files
authored
Addressing some static analysis warnings. (#179)
Avoid native integer types in favor of explicitly sized types. Avoid bitwise operations on signed types. Eliminate a TODO: replaced with a comment stating that the code should be removed when possible.
1 parent 5835ebf commit 809cc39

File tree

9 files changed

+56
-30
lines changed

9 files changed

+56
-30
lines changed

examples/bytes_to_utf8.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
#include <algorithm>
2+
#include <array>
3+
#include <cstddef>
4+
#include <cstdint>
15
#include <iostream>
6+
#include <iterator>
7+
#include <ranges>
28

39
#include "icubaby/icubaby.hpp"
410

include/icubaby/icubaby.hpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ template <> class transcoder<char32_t, char8> {
815815
/// \param dest An output iterator to which the output sequence is written.
816816
/// \returns Iterator one past the last element assigned.
817817
template <ICUBABY_CONCEPT_OUTPUT_ITERATOR (output_type) OutputIterator>
818-
static constexpr OutputIterator write_continuation (unsigned const number, input_type const code_unit,
818+
static constexpr OutputIterator write_continuation (std::uint_least8_t const number, input_type const code_unit,
819819
OutputIterator dest) {
820820
if (number == 0U) {
821821
return dest;
@@ -827,6 +827,8 @@ template <> class transcoder<char32_t, char8> {
827827

828828
/// Writes a two CU value to the output.
829829
///
830+
/// Code points in the range [U+80, U+800) are represented as two UTF-8 code units.
831+
///
830832
/// \tparam OutputIterator An output iterator type to which values of type output_type can be written.
831833
/// \param code_unit The code unit to be written.
832834
/// \param dest An output iterator to which the output sequence is written.
@@ -835,10 +837,12 @@ template <> class transcoder<char32_t, char8> {
835837
static OutputIterator write2 (input_type code_unit, OutputIterator dest) {
836838
assert (code_unit >= 0x80U && code_unit <= 0x7FFU && "Code point is out-of-range for 2 byte UTF-8");
837839
*(dest++) = static_cast<output_type> ((code_unit >> details::utf8_shift) | byte_1_of_2);
838-
return transcoder::write_continuation (1U, code_unit, dest);
840+
return transcoder::write_continuation (std::uint_least8_t{1}, code_unit, dest);
839841
}
840842
/// Writes a three CU value to the output.
841843
///
844+
/// Code points in the range [U+800, U+10000) are represented as three UTF-8 code units.
845+
///
842846
/// \tparam OutputIterator An output iterator type to which values of type output_type can be written.
843847
/// \param code_unit The code unit to be written.
844848
/// \param dest An output iterator to which the output sequence is written.
@@ -847,10 +851,12 @@ template <> class transcoder<char32_t, char8> {
847851
static OutputIterator write3 (input_type code_unit, OutputIterator dest) {
848852
assert (code_unit >= 0x800U && code_unit <= 0xFFFFU && "Code point is out-of-range for 3 byte UTF-8");
849853
*(dest++) = static_cast<output_type> ((code_unit >> (details::utf8_shift * 2U)) | byte_1_of_3);
850-
return transcoder::write_continuation (2U, code_unit, dest);
854+
return transcoder::write_continuation (std::uint_least8_t{2}, code_unit, dest);
851855
}
852856
/// Writes a four CU value to the output.
853857
///
858+
/// Code points in the range [U+10000, U+10FFFF] are represented as four UTF-8 code units.
859+
///
854860
/// \tparam OutputIterator An output iterator type to which values of type output_type can be written.
855861
/// \param code_unit The code unit to be written.
856862
/// \param dest An output iterator to which the output sequence is written.
@@ -859,7 +865,7 @@ template <> class transcoder<char32_t, char8> {
859865
static OutputIterator write4 (input_type code_unit, OutputIterator dest) {
860866
assert (code_unit >= 0x10000U && code_unit <= 0x10FFFFU && "Code point is out-of-range for 4 byte UTF-8");
861867
*(dest++) = static_cast<output_type> ((code_unit >> (details::utf8_shift * 3U)) | byte_1_of_4);
862-
return transcoder::write_continuation (3U, code_unit, dest);
868+
return transcoder::write_continuation (std::uint_least8_t{3}, code_unit, dest);
863869
}
864870
/// Writes U+FFFD REPLACEMENT CHAR to the output and records the input as not well formed.
865871
///
@@ -1212,7 +1218,7 @@ template <> class transcoder<char16_t, char32_t> {
12121218
};
12131219

12141220
/// \brief An enumeration representing the encoding detected by transcoder<std::byte, X>.
1215-
enum class encoding {
1221+
enum class encoding : std::uint_least8_t {
12161222
unknown, ///< No encoding has yet been determined.
12171223
utf8, ///< The detected encoding is UTF-8.
12181224
utf16be, ///< The detected encoding is big-endian UTF-16.
@@ -1616,9 +1622,9 @@ template <ICUBABY_CONCEPT_UNICODE_CHAR_TYPE ToEncoding> class transcoder<std::by
16161622
}
16171623

16181624
/// \brief Returns a byte from the byte order marker table which corresponds to a specific state as denoted by
1619-
/// \p state_byte and byte count \p byte_number.the
1625+
/// \p state_byte and byte count \p byte_number.
16201626
///
1621-
/// \param state A valid state machine state.
1627+
/// \param state_byte A valid state machine state.
16221628
/// \param byte_number The index of the byte within the byte order marker.
16231629
/// \returns A byte from the byte order marker table.
16241630
[[nodiscard]] static constexpr std::byte bom_value (std::byte const state_byte,
@@ -1637,9 +1643,8 @@ template <ICUBABY_CONCEPT_UNICODE_CHAR_TYPE ToEncoding> class transcoder<std::by
16371643
return enc[byte_number];
16381644
}
16391645
/// \brief Returns a byte from the byte order marker table which corresponds to a specific state as denoted by
1640-
/// the current state and byte count \p byte_number.the
1646+
/// the current state and byte count
16411647
///
1642-
/// \param byte_number The index of the byte within the byte order marker.
16431648
/// \returns A byte from the byte order marker table.
16441649
[[nodiscard]] constexpr std::byte bom_value () const noexcept {
16451650
return transcoder::bom_value (static_cast<std::byte> (state_), transcoder::get_byte_no (state_));
@@ -1793,16 +1798,17 @@ template <ICUBABY_CONCEPT_UNICODE_CHAR_TYPE ToEncoding> class transcoder<std::by
17931798
/// \param value An input byte
17941799
/// \returns A native-endian 16 bit value.
17951800
[[nodiscard]] constexpr char16_t char16_from_big_endian_buffer (input_type const value) const noexcept {
1796-
return static_cast<char16_t> ((static_cast<std::uint_least16_t> (buffer_[0]) << 8U) |
1797-
static_cast<std::uint_least16_t> (value));
1801+
return static_cast<char16_t> (
1802+
static_cast<std::uint_least16_t> (static_cast<std::uint_least16_t> (buffer_[0]) << 8U) |
1803+
static_cast<std::uint_least16_t> (value));
17981804
}
17991805
/// \brief Produces a native-endian 16-bit value from little endian encoded input by combining the first entry in the
18001806
/// buffer_ array with \p value.
18011807
///
18021808
/// \param value An input byte
18031809
/// \returns A native-endian 16 bit value.
18041810
[[nodiscard]] constexpr char16_t char16_from_little_endian_buffer (input_type const value) const noexcept {
1805-
return static_cast<char16_t> ((static_cast<std::uint_least16_t> (value) << 8U) |
1811+
return static_cast<char16_t> (static_cast<std::uint_least16_t> (static_cast<std::uint_least16_t> (value) << 8U) |
18061812
static_cast<std::uint_least16_t> (buffer_[0]));
18071813
}
18081814
/// \brief Produces a native-endian 32-bit value from big endian encoded input by combining the entries in the
@@ -1811,10 +1817,11 @@ template <ICUBABY_CONCEPT_UNICODE_CHAR_TYPE ToEncoding> class transcoder<std::by
18111817
/// \param value An input byte
18121818
/// \returns A native-endian 32 bit value.
18131819
[[nodiscard]] constexpr char32_t char32_from_big_endian_buffer (input_type const value) const noexcept {
1814-
return static_cast<char32_t> ((static_cast<std::uint_least32_t> (buffer_[0]) << 24U) |
1815-
(static_cast<std::uint_least32_t> (buffer_[1]) << 16U) |
1816-
(static_cast<std::uint_least32_t> (buffer_[2]) << 8U) |
1817-
static_cast<std::uint_least32_t> (value));
1820+
return static_cast<char32_t> (
1821+
static_cast<std::uint_least32_t> (static_cast<std::uint_least32_t> (buffer_[0]) << 24U) |
1822+
static_cast<std::uint_least32_t> (static_cast<std::uint_least32_t> (buffer_[1]) << 16U) |
1823+
static_cast<std::uint_least32_t> (static_cast<std::uint_least32_t> (buffer_[2]) << 8U) |
1824+
static_cast<std::uint_least32_t> (value));
18181825
}
18191826
/// \brief Produces a native-endian 32-bit value from little endian encoded input by combining the entries in the
18201827
/// buffer_ array with \p value.

tests/demo8/demo8.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class ios_flags_saver {
7373
ios_flags_saver (ios_flags_saver const&) = delete;
7474
ios_flags_saver (ios_flags_saver&&) noexcept = delete;
7575

76-
~ios_flags_saver () { stream_->flags (flags_); }
76+
~ios_flags_saver () { (void)stream_->flags (flags_); }
7777

7878
ios_flags_saver& operator= (ios_flags_saver const&) = delete;
7979
ios_flags_saver& operator= (ios_flags_saver&&) noexcept = delete;

tests/iconv/iconv.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ namespace {
4242
#if defined(__cpp_lib_endian) && __cpp_lib_endian >= 201907L
4343
using std::endian;
4444
#elif defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__) && defined(__BYTE_ORDER__)
45+
// NOLINTNEXTLINE(performance-enum-size)
4546
enum class endian { little = __ORDER_LITTLE_ENDIAN__, big = __ORDER_BIG_ENDIAN__, native = __BYTE_ORDER__ };
4647
#elif defined(_WIN32) || defined(__i386__) || defined(__x86_64__)
47-
enum class endian { little = 0, big = 1, native = little };
48+
enum class endian : std::uint_least8_t { little = 0, big = 1, native = little };
4849
#else
4950
#error "Can't determine endianness of the target system"
5051
#endif
@@ -82,6 +83,7 @@ constexpr ResultType pointer_cast (ArgType *const ptr) noexcept {
8283
#else
8384
ResultType result = nullptr;
8485
static_assert (sizeof (ptr) == sizeof (result));
86+
// NOLINTNEXTLINE(bugprone-multi-level-implicit-pointer-conversion)
8587
(void)std::memcpy (&result, &ptr, sizeof (result));
8688
return result;
8789
#endif

tests/performance/performance.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ template <typename FromEncoding, typename ToEncoding> ICUBABY_NOINLINE void go (
138138
auto const elapsed = std::chrono::steady_clock::now () - start_time;
139139
std::cout << static_cast<double> (std::chrono::duration_cast<std::chrono::milliseconds> (elapsed).count ()) /
140140
static_cast<double> (iterations)
141-
<< " ms" << std::endl;
141+
<< " ms\n"
142+
<< std::flush;
142143
}
143144

144145
std::uint_least16_t iteration_count (std::string_view const str) {
@@ -167,7 +168,7 @@ int main (int const argc, char const *argv[]) {
167168
}
168169
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
169170
auto const iterations = iteration_count (argc > 1 ? argv[1] : "16");
170-
std::cout << "Time to transcode all code points (" << iterations << " iterations):" << std::endl;
171+
std::cout << "Time to transcode all code points (" << iterations << " iterations):\n" << std::flush;
171172
go<char8, char8> (iterations);
172173
go<char8, char16_t> (iterations);
173174
go<char8, char32_t> (iterations);

tests/ranges/ranges.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class ios_flags_saver {
9292
ios_flags_saver (ios_flags_saver const&) = delete;
9393
ios_flags_saver (ios_flags_saver&&) noexcept = delete;
9494

95-
~ios_flags_saver () { stream_->flags (flags_); }
95+
~ios_flags_saver () { (void)stream_->flags (flags_); }
9696

9797
ios_flags_saver& operator= (ios_flags_saver const&) = delete;
9898
ios_flags_saver& operator= (ios_flags_saver&&) noexcept = delete;

unittests/encoded_char.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23-
#ifndef ICUBABY_UNITTESTS_ENCODED_CHAR_HPP
24-
#define ICUBABY_UNITTESTS_ENCODED_CHAR_HPP
23+
#ifndef ICUBABY_ENCODED_CHAR_HPP
24+
#define ICUBABY_ENCODED_CHAR_HPP (1)
2525

26+
#include <algorithm>
2627
#include <array>
2728

2829
#include "icubaby/icubaby.hpp"
@@ -534,4 +535,4 @@ template <code_point C, typename To, typename OutputIterator> OutputIterator app
534535
#endif // ICUBABY_HAVE_RANGES
535536
}
536537

537-
#endif // ICUBABY_UNITTESTS_ENCODED_CHAR_HPP
538+
#endif // ICUBABY_ENCODED_CHAR_HPP

unittests/test_byte.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
#include <ostream>
3232
#include <vector>
3333

34+
#if ICUBABY_HAVE_RANGES
35+
#include <ranges>
36+
#endif // ICUBABY_HAVE_RANGES
37+
3438
// google mock/test/fuzz
3539
#include <gmock/gmock.h>
3640
#include <gtest/gtest.h>
@@ -422,7 +426,9 @@ TEST (ByteTranscoder, Utf16BEAndIterMove) {
422426
}
423427
#endif // ICUBABY_HAVE_RANGES
424428

425-
static void ByteTranscoderNeverCrashes (std::vector<std::byte> const& input) {
429+
namespace {
430+
431+
void ByteTranscoderNeverCrashes (std::vector<std::byte> const& input) {
426432
icubaby::transcoder<std::byte, char32_t> transcoder;
427433
std::vector<char32_t> output;
428434
auto output_iterator = icubaby::iterator{&transcoder, std::back_inserter (output)};
@@ -433,6 +439,9 @@ static void ByteTranscoderNeverCrashes (std::vector<std::byte> const& input) {
433439
#endif // ICUBABY_HAVE_RANGES
434440
(void)transcoder.end_cp (output_iterator);
435441
}
442+
443+
} // end anonymous namespace
444+
436445
#if ICUBABY_FUZZTEST
437446
// NOLINTNEXTLINE
438447
FUZZ_TEST (ByteTranscoder, ByteTranscoderNeverCrashes);

unittests/typed_test.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23-
#ifndef ICUBABY_UNITTESTS_TYPED_TEST_HPP
24-
#define ICUBABY_UNITTESTS_TYPED_TEST_HPP
23+
#ifndef ICUBABY_TYPED_TEST_HPP
24+
#define ICUBABY_TYPED_TEST_HPP (1)
2525

2626
#include <gtest/gtest.h>
2727

@@ -30,9 +30,9 @@
3030

3131
#include "icubaby/icubaby.hpp"
3232

33-
// TODO(paul): Remove this code!
3433
// A specialization of the gtest GetTypeName<char8_t>() function. This is required for compiling with (at least)
35-
// Xcode 14.1/15.2 where we have a link error due to missing typeinfo for char8_t.
34+
// Xcode 14.1/15.2 where we have a link error due to missing typeinfo for char8_t. This code should be removed once it
35+
// is no longer needed for any of our targets.
3636
#if defined(__cpp_char8_t) && defined(__cpp_lib_char8_t)
3737
namespace testing::internal {
3838

@@ -76,4 +76,4 @@ class OutputTypeNames {
7676

7777
using OutputTypes = testing::Types<icubaby::char8, char16_t, char32_t>;
7878

79-
#endif // ICUBABY_UNITTESTS_TYPED_TEST_HPP
79+
#endif // ICUBABY_TYPED_TEST_HPP

0 commit comments

Comments
 (0)