1// RUN: cp %S/Inputs/concat-nested-namespaces/modernize-concat-nested-namespaces.h %T/modernize-concat-nested-namespaces.h
2// RUN: %check_clang_tidy -std=c++17 -check-suffix=NORMAL %s modernize-concat-nested-namespaces %t -- -header-filter=".*" -- -I %T
3// RUN: FileCheck -input-file=%T/modernize-concat-nested-namespaces.h %S/Inputs/concat-nested-namespaces/modernize-concat-nested-namespaces.h -check-prefix=CHECK-FIXES
4// Restore header file and re-run with c++20:
5// RUN: cp %S/Inputs/concat-nested-namespaces/modernize-concat-nested-namespaces.h %T/modernize-concat-nested-namespaces.h
6// RUN: %check_clang_tidy -std=c++20 -check-suffixes=NORMAL,CPP20 %s modernize-concat-nested-namespaces %t -- -header-filter=".*" -- -I %T
7// RUN: FileCheck -input-file=%T/modernize-concat-nested-namespaces.h %S/Inputs/concat-nested-namespaces/modernize-concat-nested-namespaces.h -check-prefix=CHECK-FIXES
8
9#include "modernize-concat-nested-namespaces.h"
10// CHECK-MESSAGES-NORMAL-DAG: modernize-concat-nested-namespaces.h:1:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
11
12namespace n1 {}
13
14namespace n2 {
15namespace n3 {
16void t();
17}
18namespace n4 {
19void t();
20}
21} // namespace n2
22
23namespace n6 {
24namespace [[deprecated]] attr_ns {
25void t();
26} // namespace attr_ns
27} // namespace n6
28
29namespace n7 {
30void t();
31
32namespace n8 {
33void t();
34}
35} // namespace n7
36
37namespace n9 {
38namespace n10 {
39// CHECK-MESSAGES-NORMAL-DAG: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
40// CHECK-FIXES-NORMAL: namespace n9::n10
41void t();
42} // namespace n10
43} // namespace n9
44// CHECK-FIXES-NORMAL: }
45
46namespace n11 {
47namespace n12 {
48// CHECK-MESSAGES-NORMAL-DAG: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
49// CHECK-FIXES-NORMAL: namespace n11::n12
50namespace n13 {
51void t();
52}
53namespace n14 {
54void t();
55}
56} // namespace n12
57} // namespace n11
58// CHECK-FIXES-NORMAL: }
59
60namespace n15 {
61namespace n16 {
62void t();
63}
64
65inline namespace n17 {
66void t();
67}
68
69namespace n18 {
70namespace n19 {
71namespace n20 {
72// CHECK-MESSAGES-NORMAL-DAG: :[[@LINE-3]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
73// CHECK-FIXES-NORMAL: namespace n18::n19::n20
74void t();
75} // namespace n20
76} // namespace n19
77} // namespace n18
78// CHECK-FIXES-NORMAL: }
79
80namespace n21 {
81void t();
82}
83} // namespace n15
84
85namespace n22 {
86namespace {
87void t();
88}
89} // namespace n22
90
91namespace n23 {
92namespace {
93namespace n24 {
94namespace n25 {
95// CHECK-MESSAGES-NORMAL-DAG: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
96// CHECK-FIXES-NORMAL: namespace n24::n25
97void t();
98} // namespace n25
99} // namespace n24
100// CHECK-FIXES-NORMAL: }
101} // namespace
102} // namespace n23
103
104namespace n26::n27 {
105namespace n28 {
106namespace n29::n30 {
107// CHECK-MESSAGES-NORMAL-DAG: :[[@LINE-3]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
108// CHECK-FIXES-NORMAL: namespace n26::n27::n28::n29::n30 {
109void t() {}
110} // namespace n29::n30
111} // namespace n28
112} // namespace n26::n27
113// CHECK-FIXES-NORMAL: }
114
115namespace n31 {
116namespace n32 {}
117// CHECK-MESSAGES-NORMAL-DAG: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
118} // namespace n31
119
120namespace n33 {
121namespace n34 {
122namespace n35 {}
123// CHECK-MESSAGES-NORMAL-DAG: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
124} // namespace n34
125namespace n36 {
126void t();
127}
128} // namespace n33
129
130namespace n37::n38 {
131void t();
132}
133
134#define IEXIST
135namespace n39 {
136namespace n40 {
137// CHECK-MESSAGES-NORMAL-DAG: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
138// CHECK-FIXES-NORMAL: namespace n39::n40
139#ifdef IEXIST
140void t() {}
141#endif
142} // namespace n40
143} // namespace n39
144// CHECK-FIXES-NORMAL: } // namespace n39::n40
145
146namespace n41 {
147namespace n42 {
148// CHECK-MESSAGES-NORMAL-DAG: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
149// CHECK-FIXES-NORMAL: namespace n41::n42
150#ifdef IDONTEXIST
151void t() {}
152#endif
153} // namespace n42
154} // namespace n41
155// CHECK-FIXES-NORMAL: } // namespace n41::n42
156
157
158// CHECK-MESSAGES-NORMAL-DAG: :[[@LINE+1]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
159namespace n43 {
160#define N43_INNER
161namespace n44 {
162void foo() {}
163} // namespace n44
164#undef N43_INNER
165} // namespace n43
166// CHECK-FIXES-NORMAL: #define N43_INNER
167// CHECK-FIXES-NORMAL: namespace n43::n44 {
168// CHECK-FIXES-NORMAL: } // namespace n43::n44
169// CHECK-FIXES-NORMAL: #undef N43_INNER
170
171// CHECK-MESSAGES-NORMAL-DAG: :[[@LINE+1]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
172namespace n45{
173#define N45_INNER
174namespace n46
175{
176#pragma clang diagnostic push
177namespace n47 {
178void foo() {}
179} // namespace n47
180#pragma clang diagnostic pop
181} // namespace n46
182#undef N45_INNER
183} // namespace n45
184// CHECK-FIXES-NORMAL: #define N45_INNER
185// CHECK-FIXES-NORMAL: #pragma clang diagnostic push
186// CHECK-FIXES-NORMAL: namespace n45::n46::n47 {
187// CHECK-FIXES-NORMAL: } // namespace n45::n46::n47
188// CHECK-FIXES-NORMAL: #pragma clang diagnostic pop
189// CHECK-FIXES-NORMAL: #undef N45_INNER
190
191inline namespace n48 {
192// CHECK-MESSAGES-NORMAL-DAG: :[[@LINE+1]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
193namespace n49 {
194namespace n50 {
195// CHECK-FIXES-NORMAL: namespace n49::n50 {
196void foo() {}
197}
198}
199}
200
201// CHECK-MESSAGES-CPP20-DAG: :[[@LINE+1]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
202namespace n51 {
203inline namespace n52 {
204namespace n53 {
205// CHECK-FIXES-CPP20: namespace n51::inline n52::n53 {
206void foo() {}
207}
208}
209}
210
211#if __cplusplus >= 202002L
212// CHECK-MESSAGES-CPP20-DAG: :[[@LINE+1]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
213namespace n54 {
214namespace n55::inline n56::n57 {
215namespace n58 {
216// CHECK-FIXES-CPP20: namespace n54::n55::inline n56::n57::n58 {
217void foo() {}
218}
219}
220}
221#endif
222
223// CHECK-MESSAGES-NORMAL-DAG: :[[@LINE+1]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
224namespace avoid_add_close_comment {
225namespace inner {
226void foo() {}
227}
228}
229// CHECK-FIXES-NORMAL: namespace avoid_add_close_comment::inner {
230// CHECK-FIXES-NORMAL-NOT: } // namespace avoid_add_close_comment::inner
231
232// CHECK-MESSAGES-NORMAL-DAG: :[[@LINE+1]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
233namespace avoid_change_close_comment {
234namespace inner {
235void foo() {}
236} // namespace inner and other comments
237} // namespace avoid_change_close_comment and other comments
238// CHECK-FIXES-NORMAL: namespace avoid_change_close_comment::inner {
239// CHECK-FIXES-NORMAL-NOT: } // namespace avoid_add_close_comment::inner
240
241namespace /*::*/ comment_colon_1 {
242void foo() {}
243} // namespace comment_colon_1
244// CHECK-FIXES-NORMAL: namespace /*::*/ comment_colon_1 {
245
246// CHECK-MESSAGES-NORMAL-DAG: :[[@LINE+1]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
247namespace /*::*/ comment_colon_2 {
248namespace comment_colon_2 {
249void foo() {}
250} // namespace comment_colon_2
251} // namespace comment_colon_2
252
253int main() {
254 n26::n27::n28::n29::n30::t();
255#ifdef IEXIST
256 n39::n40::t();
257#endif
258
259#ifdef IDONTEXIST
260 n41::n42::t();
261#endif
262
263 return 0;
264}
265

source code of clang-tools-extra/test/clang-tidy/checkers/modernize/concat-nested-namespaces.cpp