| 1 | // RUN: %check_clang_tidy %s performance-avoid-endl %t |
| 2 | |
| 3 | namespace std { |
| 4 | template <typename CharT> |
| 5 | class basic_ostream { |
| 6 | public: |
| 7 | template <typename T> |
| 8 | basic_ostream& operator<<(T); |
| 9 | basic_ostream& operator<<(basic_ostream<CharT>& (*)(basic_ostream<CharT>&)); |
| 10 | }; |
| 11 | |
| 12 | template <typename CharT> |
| 13 | class basic_iostream : public basic_ostream<CharT> {}; |
| 14 | |
| 15 | using ostream = basic_ostream<char>; |
| 16 | using wostream = basic_ostream<wchar_t>; |
| 17 | |
| 18 | using iostream = basic_iostream<char>; |
| 19 | using wiostream = basic_iostream<wchar_t>; |
| 20 | |
| 21 | ostream cout; |
| 22 | wostream wcout; |
| 23 | |
| 24 | ostream cerr; |
| 25 | wostream wcerr; |
| 26 | |
| 27 | ostream clog; |
| 28 | wostream wclog; |
| 29 | |
| 30 | template<typename CharT> |
| 31 | basic_ostream<CharT>& endl(basic_ostream<CharT>&); |
| 32 | } // namespace std |
| 33 | |
| 34 | void good() { |
| 35 | std::cout << "Hello" << '\n'; |
| 36 | std::cout << "World\n" ; |
| 37 | |
| 38 | std::wcout << "Hello" << '\n'; |
| 39 | std::wcout << "World\n" ; |
| 40 | |
| 41 | std::cerr << "Hello" << '\n'; |
| 42 | std::cerr << "World\n" ; |
| 43 | |
| 44 | std::wcerr << "Hello" << '\n'; |
| 45 | std::wcerr << "World\n" ; |
| 46 | |
| 47 | std::clog << "Hello" << '\n'; |
| 48 | std::clog << "World\n" ; |
| 49 | |
| 50 | std::wclog << "Hello" << '\n'; |
| 51 | std::wclog << "World\n" ; |
| 52 | } |
| 53 | |
| 54 | void bad() { |
| 55 | std::cout << "World" << std::endl; |
| 56 | // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 57 | // CHECK-FIXES: std::cout << "World" << '\n'; |
| 58 | std::wcout << "World" << std::endl; |
| 59 | // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 60 | // CHECK-FIXES: std::wcout << "World" << '\n'; |
| 61 | std::cerr << "World" << std::endl; |
| 62 | // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 63 | // CHECK-FIXES: std::cerr << "World" << '\n'; |
| 64 | std::wcerr << "World" << std::endl; |
| 65 | // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 66 | // CHECK-FIXES: std::wcerr << "World" << '\n'; |
| 67 | std::clog << "World" << std::endl; |
| 68 | // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 69 | // CHECK-FIXES: std::clog << "World" << '\n'; |
| 70 | std::wclog << "World" << std::endl; |
| 71 | // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 72 | // CHECK-FIXES: std::wclog << "World" << '\n'; |
| 73 | } |
| 74 | |
| 75 | void bad_single_argument() { |
| 76 | std::cout << std::endl; |
| 77 | // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 78 | // CHECK-FIXES: std::cout << '\n'; |
| 79 | std::wcout << std::endl; |
| 80 | // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 81 | // CHECK-FIXES: std::wcout << '\n'; |
| 82 | std::cerr << std::endl; |
| 83 | // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 84 | // CHECK-FIXES: std::cerr << '\n'; |
| 85 | std::wcerr << std::endl; |
| 86 | // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 87 | // CHECK-FIXES: std::wcerr << '\n'; |
| 88 | std::clog << std::endl; |
| 89 | // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 90 | // CHECK-FIXES: std::clog << '\n'; |
| 91 | std::wclog << std::endl; |
| 92 | // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 93 | // CHECK-FIXES: std::wclog << '\n'; |
| 94 | } |
| 95 | |
| 96 | void bad_multiple() { |
| 97 | std::cout << "Hello" << std::endl << "World" << std::endl; |
| 98 | // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 99 | // CHECK-MESSAGES: :[[@LINE-2]]:51: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 100 | // CHECK-FIXES: std::cout << "Hello" << '\n' << "World" << '\n'; |
| 101 | std::wcout << "Hello" << std::endl << "World" << std::endl; |
| 102 | // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 103 | // CHECK-MESSAGES: :[[@LINE-2]]:52: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 104 | // CHECK-FIXES: std::wcout << "Hello" << '\n' << "World" << '\n'; |
| 105 | std::cerr << "Hello" << std::endl << "World" << std::endl; |
| 106 | // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 107 | // CHECK-MESSAGES: :[[@LINE-2]]:51: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 108 | // CHECK-FIXES: std::cerr << "Hello" << '\n' << "World" << '\n'; |
| 109 | std::wcerr << "Hello" << std::endl << "World" << std::endl; |
| 110 | // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 111 | // CHECK-MESSAGES: :[[@LINE-2]]:52: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 112 | // CHECK-FIXES: std::wcerr << "Hello" << '\n' << "World" << '\n'; |
| 113 | std::clog << "Hello" << std::endl << "World" << std::endl; |
| 114 | // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 115 | // CHECK-MESSAGES: :[[@LINE-2]]:51: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 116 | // CHECK-FIXES: std::clog << "Hello" << '\n' << "World" << '\n'; |
| 117 | std::wclog << "Hello" << std::endl << "World" << std::endl; |
| 118 | // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 119 | // CHECK-MESSAGES: :[[@LINE-2]]:52: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 120 | // CHECK-FIXES: std::wclog << "Hello" << '\n' << "World" << '\n'; |
| 121 | } |
| 122 | |
| 123 | void bad_function_call() { |
| 124 | std::endl(std::cout); |
| 125 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 126 | // CHECK-FIXES: std::cout << '\n'; |
| 127 | std::endl(std::cout << "Hi" ); |
| 128 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 129 | // CHECK-FIXES: std::cout << "Hi" << '\n'; |
| 130 | std::endl(std::wcout); |
| 131 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 132 | // CHECK-FIXES: std::wcout << '\n'; |
| 133 | std::endl(std::wcout << "Hi" ); |
| 134 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 135 | // CHECK-FIXES: std::wcout << "Hi" << '\n'; |
| 136 | std::endl(std::cerr); |
| 137 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 138 | // CHECK-FIXES: std::cerr << '\n'; |
| 139 | std::endl(std::cerr << "Hi" ); |
| 140 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 141 | // CHECK-FIXES: std::cerr << "Hi" << '\n'; |
| 142 | std::endl(std::wcerr); |
| 143 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 144 | // CHECK-FIXES: std::wcerr << '\n'; |
| 145 | std::endl(std::wcerr << "Hi" ); |
| 146 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 147 | // CHECK-FIXES: std::wcerr << "Hi" << '\n'; |
| 148 | std::endl(std::clog); |
| 149 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 150 | // CHECK-FIXES: std::clog << '\n'; |
| 151 | std::endl(std::clog << "Hi" ); |
| 152 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 153 | // CHECK-FIXES: std::clog << "Hi" << '\n'; |
| 154 | std::endl(std::wclog); |
| 155 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 156 | // CHECK-FIXES: std::wclog << '\n'; |
| 157 | std::endl(std::wclog << "Hi" ); |
| 158 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 159 | // CHECK-FIXES: std::wclog << "Hi" << '\n'; |
| 160 | } |
| 161 | |
| 162 | void bad_user_stream() { |
| 163 | std::iostream my_iostream; |
| 164 | std::wiostream my_wiostream; |
| 165 | std::ostream my_ostream; |
| 166 | std::wostream my_wostream; |
| 167 | |
| 168 | my_iostream << "Hi" << std::endl; |
| 169 | // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 170 | // CHECK-FIXES: my_iostream << "Hi" << '\n'; |
| 171 | my_wiostream << "Hi" << std::endl; |
| 172 | // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 173 | // CHECK-FIXES: my_wiostream << "Hi" << '\n'; |
| 174 | my_ostream << "Hi" << std::endl; |
| 175 | // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 176 | // CHECK-FIXES: my_ostream << "Hi" << '\n'; |
| 177 | my_wostream << "Hi" << std::endl; |
| 178 | // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 179 | // CHECK-FIXES: my_wostream << "Hi" << '\n'; |
| 180 | |
| 181 | std::endl(my_iostream); |
| 182 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 183 | // CHECK-FIXES: my_iostream << '\n'; |
| 184 | std::endl(my_wiostream); |
| 185 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 186 | // CHECK-FIXES: my_wiostream << '\n'; |
| 187 | std::endl(my_ostream); |
| 188 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 189 | // CHECK-FIXES: my_ostream << '\n'; |
| 190 | std::endl(my_wostream); |
| 191 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 192 | // CHECK-FIXES: my_wostream << '\n'; |
| 193 | } |
| 194 | |
| 195 | using namespace std; |
| 196 | void bad_using_namespace_std() { |
| 197 | cout << "Hello" << endl; |
| 198 | // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: do not use 'endl' with streams; use '\n' instead [performance-avoid-endl] |
| 199 | // CHECK-FIXES: cout << "Hello" << '\n'; |
| 200 | endl(cout); |
| 201 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'endl' with streams; use '\n' instead [performance-avoid-endl] |
| 202 | // CHECK-FIXES: cout << '\n'; |
| 203 | } |
| 204 | |
| 205 | namespace my_prefix = std; |
| 206 | void bad_using_user_namespace() { |
| 207 | my_prefix::cout << "Hello" << my_prefix::endl; |
| 208 | // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: do not use 'my_prefix::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 209 | // CHECK-FIXES: my_prefix::cout << "Hello" << '\n'; |
| 210 | my_prefix::endl(my_prefix::cout); |
| 211 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'my_prefix::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 212 | // CHECK-FIXES: my_prefix::cout << '\n'; |
| 213 | } |
| 214 | |
| 215 | struct CustomLogger { |
| 216 | template <typename T> |
| 217 | std::ostream& operator<<(T); |
| 218 | std::ostream& operator<<(std::ostream& (*)(std::ostream&)); |
| 219 | }; |
| 220 | |
| 221 | void bad_custom_stream() { |
| 222 | CustomLogger logger; |
| 223 | |
| 224 | logger << std::endl; |
| 225 | // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 226 | // CHECK-FIXES: logger << '\n'; |
| 227 | } |
| 228 | |
| 229 | namespace gh107859 { |
| 230 | |
| 231 | #define ENDL std::endl; |
| 232 | |
| 233 | void bad_macro() { |
| 234 | std::cout << ENDL; |
| 235 | // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl] |
| 236 | } |
| 237 | |
| 238 | } // namespace gh107859 |
| 239 | |