1 // RUN: %check_clang_tidy %s modernize-deprecated-ios-base-aliases %t
2 
3 namespace std {
4 class ios_base {
5 public:
6   typedef int io_state;
7   typedef int open_mode;
8   typedef int seek_dir;
9 
10   typedef int streampos;
11   typedef int streamoff;
12 };
13 
14 template <class CharT>
15 class basic_ios : public ios_base {
16 };
17 } // namespace std
18 
19 // Test function return values (declaration)
20 std::ios_base::io_state f_5();
21 // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: 'std::ios_base::io_state' is deprecated; use 'std::ios_base::iostate' instead [modernize-deprecated-ios-base-aliases]
22 // CHECK-FIXES: std::ios_base::iostate f_5();
23 
24 // Test function parameters.
25 void f_6(std::ios_base::open_mode);
26 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 'std::ios_base::open_mode' is deprecated
27 // CHECK-FIXES: void f_6(std::ios_base::openmode);
28 void f_7(const std::ios_base::seek_dir &);
29 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: 'std::ios_base::seek_dir' is deprecated
30 // CHECK-FIXES: void f_7(const std::ios_base::seekdir &);
31 
32 // Test on record type fields.
33 struct A {
34   std::ios_base::io_state field;
35   // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 'std::ios_base::io_state' is deprecated
36   // CHECK-FIXES: std::ios_base::iostate field;
37 
38   typedef std::ios_base::io_state int_ptr_type;
39   // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: 'std::ios_base::io_state' is deprecated
40   // CHECK-FIXES: typedef std::ios_base::iostate int_ptr_type;
41 };
42 
43 struct B : public std::ios_base {
44   io_state a;
45   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'std::ios_base::io_state' is deprecated
46   // CHECK-FIXES: iostate a;
47 };
48 
49 struct C : public std::basic_ios<char> {
50   io_state a;
51   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'std::ios_base::io_state' is deprecated
52   // CHECK-FIXES: iostate a;
53 };
54 
f_1()55 void f_1() {
56   std::ios_base::io_state a;
57   // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 'std::ios_base::io_state' is deprecated
58   // CHECK-FIXES: std::ios_base::iostate a;
59 
60   // Check that spaces aren't modified unnecessarily.
61   std :: ios_base :: io_state b;
62   // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 'std::ios_base::io_state' is deprecated
63   // CHECK-FIXES: std :: ios_base :: iostate b;
64 
65   // Test construction from a temporary.
66   std::ios_base::io_state c = std::ios_base::io_state{};
67   // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 'std::ios_base::io_state' is deprecated
68   // CHECK-MESSAGES: :[[@LINE-2]]:46: warning: 'std::ios_base::io_state' is deprecated
69   // CHECK-FIXES: std::ios_base::iostate c = std::ios_base::iostate{};
70 
71   typedef std::ios_base::io_state alias1;
72   // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: 'std::ios_base::io_state' is deprecated
73   // CHECK-FIXES: typedef std::ios_base::iostate alias1;
74   alias1 d(a);
75 
76   using alias2 = std::ios_base::io_state;
77   // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: 'std::ios_base::io_state' is deprecated
78   // CHECK-FIXES: using alias2 = std::ios_base::iostate;
79   alias2 e;
80 
81   // Test pointers.
82   std::ios_base::io_state *f;
83   // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 'std::ios_base::io_state' is deprecated
84   // CHECK-FIXES: std::ios_base::iostate *f;
85 
86   // Test 'static' declarations.
87   static std::ios_base::io_state g;
88   // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 'std::ios_base::io_state' is deprecated
89   // CHECK-FIXES: static std::ios_base::iostate g;
90 
91   // Test with cv-qualifiers.
92   const std::ios_base::io_state h(0);
93   // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: 'std::ios_base::io_state' is deprecated
94   // CHECK-FIXES: const std::ios_base::iostate h(0);
95   volatile std::ios_base::io_state i;
96   // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 'std::ios_base::io_state' is deprecated
97   // CHECK-FIXES: volatile std::ios_base::iostate i;
98   const volatile std::ios_base::io_state j(0);
99   // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: 'std::ios_base::io_state' is deprecated
100   // CHECK-FIXES: const volatile std::ios_base::iostate j(0);
101 
102   // Test auto and initializer-list.
103   auto k = std::ios_base::io_state{};
104   // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 'std::ios_base::io_state' is deprecated
105   // CHECK-FIXES: auto k = std::ios_base::iostate{};
106 
107   std::ios_base::io_state l{std::ios_base::io_state()};
108   // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 'std::ios_base::io_state' is deprecated
109   // CHECK-MESSAGES: :[[@LINE-2]]:44: warning: 'std::ios_base::io_state' is deprecated
110   // CHECK-FIXES: std::ios_base::iostate l{std::ios_base::iostate()};
111 
112   // Test temporaries.
113   std::ios_base::io_state();
114   // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 'std::ios_base::io_state' is deprecated
115   // CHECK-FIXES: std::ios_base::iostate();
116 
117   // Test inherited type usage
118   std::basic_ios<char>::io_state m;
119   // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 'std::ios_base::io_state' is deprecated
120   // CHECK-FIXES: std::basic_ios<char>::iostate m;
121 
122   std::ios_base::streampos n;
123   // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 'std::ios_base::streampos' is deprecated [modernize-deprecated-ios-base-aliases]
124 
125   std::ios_base::streamoff o;
126   // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 'std::ios_base::streamoff' is deprecated [modernize-deprecated-ios-base-aliases]
127 }
128 
129 // Test without the nested name specifiers.
f_2()130 void f_2() {
131   using namespace std;
132 
133   ios_base::io_state a;
134   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: 'std::ios_base::io_state' is deprecated
135   // CHECK-FIXES: ios_base::iostate a;
136 }
137 
138 // Test messing-up with macros.
f_4()139 void f_4() {
140 #define MACRO_1 std::ios_base::io_state
141   // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: 'std::ios_base::io_state' is deprecated
142   MACRO_1 a;
143 
144 #define MACRO_2 io_state
145   // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: 'std::ios_base::io_state' is deprecated
146   std::ios_base::MACRO_2 b;
147 
148 #define MACRO_3 std::ios_base
149   MACRO_3::io_state c;
150   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: 'std::ios_base::io_state' is deprecated
151 
152 #define MACRO_4(type) type::io_state
153   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: 'std::ios_base::io_state' is deprecated
154   MACRO_4(std::ios_base) d;
155 
156 #undef MACRO_1
157 #undef MACRO_2
158 #undef MACRO_3
159 #undef MACRO_4
160 }
161 
162 // Test function return values (definition).
f_5()163 std::ios_base::io_state f_5()
164 // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: 'std::ios_base::io_state' is deprecated
165 // CHECK-FIXES: std::ios_base::iostate f_5()
166 {
167   // Test constructor.
168   return std::ios_base::io_state(0);
169   // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 'std::ios_base::io_state' is deprecated
170   // CHECK-FIXES: return std::ios_base::iostate(0);
171 }
172 
173 // Test that other aliases with same name aren't replaced
174 struct my_ios_base {
175   typedef int io_state;
176 };
177 
178 namespace ns_1 {
179 struct my_ios_base2 {
180   typedef int io_state;
181 };
182 } // namespace ns_1
183 
f_8()184 void f_8() {
185   my_ios_base::io_state a;
186 
187   ns_1::my_ios_base2::io_state b;
188 }
189 
190 // Test templates
191 template <typename X>
f_9()192 void f_9() {
193   typename std::basic_ios<X>::io_state p;
194   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: 'std::ios_base::io_state' is deprecated
195   typename std::ios_base::io_state q;
196   // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 'std::ios_base::io_state' is deprecated
197   // CHECK-FIXES: typename std::ios_base::iostate q;
198 }
199 
200 template <typename T>
f_10(T arg)201 void f_10(T arg) {
202   T x(arg);
203 }
204 
205 template <typename T>
f_11()206 void f_11() {
207   typename T::io_state x{};
208   // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: 'std::ios_base::io_state' is deprecated
209 }
210 
211 template <typename T>
212 struct D : std::ios_base {
213   io_state a;
214   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'std::ios_base::io_state' is deprecated
215   // CHECK-FIXES: iostate a;
216 
217   typename std::basic_ios<T>::io_state b;
218   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: 'std::ios_base::io_state' is deprecated
219 };
220 
221 template <typename T>
222 struct E {
223   T t;
224 };
225 
f_12()226 void f_12() {
227   f_9<char>();
228 
229   f_10<std::ios_base::io_state>(0);
230   // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: 'std::ios_base::io_state' is deprecated
231   // CHECK-FIXES: f_10<std::ios_base::iostate>(0);
232 
233   f_11<std::ios_base>();
234   D<char> d;
235 
236   E<std::ios_base::io_state> e;
237   // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 'std::ios_base::io_state' is deprecated
238   // CHECK-FIXES: E<std::ios_base::iostate> e;
239 }
240