| 1 | // RUN: %check_clang_tidy %s bugprone-unintended-char-ostream-output %t |
| 2 | |
| 3 | namespace std { |
| 4 | |
| 5 | template <class _CharT, class _Traits = void> class basic_ostream { |
| 6 | public: |
| 7 | basic_ostream &operator<<(int); |
| 8 | basic_ostream &operator<<(unsigned int); |
| 9 | }; |
| 10 | |
| 11 | template <class CharT, class Traits> |
| 12 | basic_ostream<CharT, Traits> &operator<<(basic_ostream<CharT, Traits> &, CharT); |
| 13 | template <class CharT, class Traits> |
| 14 | basic_ostream<CharT, Traits> &operator<<(basic_ostream<CharT, Traits> &, char); |
| 15 | template <class _Traits> |
| 16 | basic_ostream<char, _Traits> &operator<<(basic_ostream<char, _Traits> &, char); |
| 17 | template <class _Traits> |
| 18 | basic_ostream<char, _Traits> &operator<<(basic_ostream<char, _Traits> &, |
| 19 | signed char); |
| 20 | template <class _Traits> |
| 21 | basic_ostream<char, _Traits> &operator<<(basic_ostream<char, _Traits> &, |
| 22 | unsigned char); |
| 23 | |
| 24 | using ostream = basic_ostream<char>; |
| 25 | |
| 26 | } // namespace std |
| 27 | |
| 28 | class A : public std::ostream {}; |
| 29 | |
| 30 | using uint8_t = unsigned char; |
| 31 | using int8_t = signed char; |
| 32 | |
| 33 | void origin_ostream(std::ostream &os) { |
| 34 | uint8_t unsigned_value = 9; |
| 35 | os << unsigned_value; |
| 36 | // CHECK-MESSAGES: [[@LINE-1]]:6: warning: 'uint8_t' (aka 'unsigned char') passed to 'operator<<' outputs as character instead of integer |
| 37 | // CHECK-FIXES: os << static_cast<unsigned int>(unsigned_value); |
| 38 | |
| 39 | int8_t signed_value = 9; |
| 40 | os << signed_value; |
| 41 | // CHECK-MESSAGES: [[@LINE-1]]:6: warning: 'int8_t' (aka 'signed char') passed to 'operator<<' outputs as character instead of integer |
| 42 | // CHECK-FIXES: os << static_cast<int>(signed_value); |
| 43 | |
| 44 | char char_value = 9; |
| 45 | os << char_value; |
| 46 | unsigned char unsigned_char_value = 9; |
| 47 | os << unsigned_char_value; |
| 48 | signed char signed_char_value = 9; |
| 49 | os << signed_char_value; |
| 50 | } |
| 51 | |
| 52 | void explicit_cast_to_char_type(std::ostream &os) { |
| 53 | enum V : uint8_t {}; |
| 54 | V e{}; |
| 55 | os << static_cast<unsigned char>(e); |
| 56 | os << (unsigned char)(e); |
| 57 | os << (static_cast<unsigned char>(e)); |
| 58 | } |
| 59 | |
| 60 | void based_on_ostream(A &os) { |
| 61 | uint8_t unsigned_value = 9; |
| 62 | os << unsigned_value; |
| 63 | // CHECK-MESSAGES: [[@LINE-1]]:6: warning: 'uint8_t' (aka 'unsigned char') passed to 'operator<<' outputs as character instead of integer |
| 64 | // CHECK-FIXES: os << static_cast<unsigned int>(unsigned_value); |
| 65 | |
| 66 | int8_t signed_value = 9; |
| 67 | os << signed_value; |
| 68 | // CHECK-MESSAGES: [[@LINE-1]]:6: warning: 'int8_t' (aka 'signed char') passed to 'operator<<' outputs as character instead of integer |
| 69 | // CHECK-FIXES: os << static_cast<int>(signed_value); |
| 70 | |
| 71 | char char_value = 9; |
| 72 | os << char_value; |
| 73 | } |
| 74 | |
| 75 | void other_ostream_template_parameters(std::basic_ostream<uint8_t> &os) { |
| 76 | uint8_t unsigned_value = 9; |
| 77 | os << unsigned_value; |
| 78 | |
| 79 | int8_t signed_value = 9; |
| 80 | os << signed_value; |
| 81 | |
| 82 | char char_value = 9; |
| 83 | os << char_value; |
| 84 | } |
| 85 | |
| 86 | template <class T> class B : public std::ostream {}; |
| 87 | void template_based_on_ostream(B<int> &os) { |
| 88 | uint8_t unsigned_value = 9; |
| 89 | os << unsigned_value; |
| 90 | // CHECK-MESSAGES: [[@LINE-1]]:6: warning: 'uint8_t' (aka 'unsigned char') passed to 'operator<<' outputs as character instead of integer |
| 91 | // CHECK-FIXES: os << static_cast<unsigned int>(unsigned_value); |
| 92 | } |
| 93 | |
| 94 | template<class T> void template_fn_1(T &os) { |
| 95 | uint8_t unsigned_value = 9; |
| 96 | os << unsigned_value; |
| 97 | // CHECK-MESSAGES: [[@LINE-1]]:6: warning: 'uint8_t' (aka 'unsigned char') passed to 'operator<<' outputs as character instead of integer |
| 98 | // CHECK-FIXES: os << static_cast<unsigned int>(unsigned_value); |
| 99 | } |
| 100 | template<class T> void template_fn_2(std::ostream &os) { |
| 101 | T unsigned_value = 9; |
| 102 | os << unsigned_value; |
| 103 | // It should be detected as well. But we cannot get the sugared type name for SubstTemplateTypeParmType. |
| 104 | } |
| 105 | template<class T> void template_fn_3(std::ostream &os) { |
| 106 | T unsigned_value = 9; |
| 107 | os << unsigned_value; |
| 108 | } |
| 109 | void call_template_fn() { |
| 110 | A a{}; |
| 111 | template_fn_1(os&: a); |
| 112 | template_fn_2<uint8_t>(os&: a); |
| 113 | template_fn_3<char>(os&: a); |
| 114 | } |
| 115 | |
| 116 | using C8 = char; |
| 117 | void alias_char(std::ostream &os) { |
| 118 | C8 v = 9; |
| 119 | os << v; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | #define MACRO_VARIANT_NAME a |
| 124 | void macro_variant_name(std::ostream &os) { |
| 125 | uint8_t MACRO_VARIANT_NAME = 9; |
| 126 | os << MACRO_VARIANT_NAME; |
| 127 | // CHECK-MESSAGES: [[@LINE-1]]:6: warning: 'uint8_t' (aka 'unsigned char') passed to 'operator<<' outputs as character instead of integer |
| 128 | // CHECK-FIXES: os << static_cast<unsigned int>(MACRO_VARIANT_NAME); |
| 129 | } |
| 130 | |