xref: /llvm-project/clang/unittests/Format/QualifierFixerTest.cpp (revision 3bd8b02aa0c5fcbb005f6d72f58f4a05421e7823)
1 //===- unittest/Format/QualifierFixerTest.cpp - Formatting unit tests -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "../lib/Format/QualifierAlignmentFixer.h"
10 #include "FormatTestBase.h"
11 #include "TestLexer.h"
12 
13 #define DEBUG_TYPE "format-qualifier-fixer-test"
14 
15 namespace clang {
16 namespace format {
17 namespace test {
18 namespace {
19 
20 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
21   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
22   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
23   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
24 
25 #define FAIL_PARSE(TEXT, FIELD, VALUE)                                         \
26   EXPECT_NE(0, parseConfiguration(TEXT, &Style).value());                      \
27   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
28 
29 class QualifierFixerTest : public FormatTestBase {
30 protected:
31   TokenList annotate(StringRef Code,
32                      const FormatStyle &Style = getLLVMStyle()) {
33     return TestLexer(Allocator, Buffers, Style).annotate(Code);
34   }
35   llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
36   std::vector<std::unique_ptr<llvm::MemoryBuffer>> Buffers;
37 };
38 
39 TEST_F(QualifierFixerTest, RotateTokens) {
40   // TODO add test
41   EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("const"),
42             tok::kw_const);
43   EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("volatile"),
44             tok::kw_volatile);
45   EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("inline"),
46             tok::kw_inline);
47   EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("static"),
48             tok::kw_static);
49   EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("restrict"),
50             tok::kw_restrict);
51   EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("friend"),
52             tok::kw_friend);
53 }
54 
55 TEST_F(QualifierFixerTest, FailQualifierInvalidConfiguration) {
56   FormatStyle Style = {};
57   Style.Language = FormatStyle::LK_Cpp;
58   FAIL_PARSE("QualifierAlignment: Custom\n"
59              "QualifierOrder: [const, volatile, apples, type]",
60              QualifierOrder,
61              std::vector<std::string>({"const", "volatile", "apples", "type"}));
62 }
63 
64 TEST_F(QualifierFixerTest, FailQualifierDuplicateConfiguration) {
65   FormatStyle Style = {};
66   Style.Language = FormatStyle::LK_Cpp;
67   FAIL_PARSE("QualifierAlignment: Custom\n"
68              "QualifierOrder: [const, volatile, const, type]",
69              QualifierOrder,
70              std::vector<std::string>({"const", "volatile", "const", "type"}));
71 }
72 
73 TEST_F(QualifierFixerTest, FailQualifierMissingType) {
74   FormatStyle Style = {};
75   Style.Language = FormatStyle::LK_Cpp;
76   FAIL_PARSE("QualifierAlignment: Custom\n"
77              "QualifierOrder: [const, volatile ]",
78              QualifierOrder,
79              std::vector<std::string>({
80                  "const",
81                  "volatile",
82              }));
83 }
84 
85 TEST_F(QualifierFixerTest, FailQualifierEmptyOrder) {
86   FormatStyle Style = {};
87   Style.Language = FormatStyle::LK_Cpp;
88   FAIL_PARSE("QualifierAlignment: Custom\nQualifierOrder: []", QualifierOrder,
89              std::vector<std::string>({}));
90 }
91 
92 TEST_F(QualifierFixerTest, FailQualifierMissingOrder) {
93   FormatStyle Style = {};
94   Style.Language = FormatStyle::LK_Cpp;
95   FAIL_PARSE("QualifierAlignment: Custom", QualifierOrder,
96              std::vector<std::string>());
97 }
98 
99 TEST_F(QualifierFixerTest, QualifierLeft) {
100   FormatStyle Style = {};
101   Style.Language = FormatStyle::LK_Cpp;
102   CHECK_PARSE("QualifierAlignment: Left", QualifierOrder,
103               std::vector<std::string>({"const", "volatile", "type"}));
104 }
105 
106 TEST_F(QualifierFixerTest, QualifierRight) {
107   FormatStyle Style = {};
108   Style.Language = FormatStyle::LK_Cpp;
109   CHECK_PARSE("QualifierAlignment: Right", QualifierOrder,
110               std::vector<std::string>({"type", "const", "volatile"}));
111 }
112 
113 TEST_F(QualifierFixerTest, QualifiersCustomOrder) {
114   FormatStyle Style = getLLVMStyle();
115   Style.QualifierAlignment = FormatStyle::QAS_Left;
116   Style.QualifierOrder = {"friend", "inline",   "constexpr", "static",
117                           "const",  "volatile", "type"};
118 
119   verifyFormat("const volatile int a;", Style);
120   verifyFormat("const volatile int a;", "volatile const int a;", Style);
121   verifyFormat("const volatile int a;", "int const volatile a;", Style);
122   verifyFormat("const volatile int a;", "int volatile const a;", Style);
123   verifyFormat("const volatile int a;", "const int volatile a;", Style);
124 
125   verifyFormat("static const volatile int a;", "const static int volatile a;",
126                Style);
127   verifyFormat("inline static const volatile int a;",
128                "const static inline int volatile a;", Style);
129 
130   verifyFormat("constexpr static int a;", "static constexpr int a;", Style);
131   verifyFormat("constexpr static int A;", "static constexpr int A;", Style);
132   verifyFormat("constexpr static int Bar;", "static constexpr int Bar;", Style);
133   verifyFormat("constexpr static LPINT Bar;", "static constexpr LPINT Bar;",
134                Style);
135   verifyFormat("const const int a;", "const int const a;", Style);
136 
137   verifyFormat(
138       "friend constexpr auto operator<=>(const foo &, const foo &) = default;",
139       "constexpr friend auto operator<=>(const foo &, const foo &) = default;",
140       Style);
141   verifyFormat(
142       "friend constexpr bool operator==(const foo &, const foo &) = default;",
143       "constexpr bool friend operator==(const foo &, const foo &) = default;",
144       Style);
145 }
146 
147 TEST_F(QualifierFixerTest, LeftRightQualifier) {
148   FormatStyle Style = getLLVMStyle();
149 
150   // keep the const style unaltered
151   verifyFormat("const int a;", Style);
152   verifyFormat("const int *a;", Style);
153   verifyFormat("const int &a;", Style);
154   verifyFormat("const int &&a;", Style);
155   verifyFormat("int const b;", Style);
156   verifyFormat("int const *b;", Style);
157   verifyFormat("int const &b;", Style);
158   verifyFormat("int const &&b;", Style);
159   verifyFormat("int const *const b;", Style);
160   verifyFormat("int *const c;", Style);
161 
162   verifyFormat("const Foo a;", Style);
163   verifyFormat("const Foo *a;", Style);
164   verifyFormat("const Foo &a;", Style);
165   verifyFormat("const Foo &&a;", Style);
166   verifyFormat("Foo const b;", Style);
167   verifyFormat("Foo const *b;", Style);
168   verifyFormat("Foo const &b;", Style);
169   verifyFormat("Foo const &&b;", Style);
170   verifyFormat("Foo const *const b;", Style);
171 
172   verifyFormat("LLVM_NODISCARD const int &Foo();", Style);
173   verifyFormat("LLVM_NODISCARD int const &Foo();", Style);
174 
175   verifyFormat("volatile const int *restrict;", Style);
176   verifyFormat("const volatile int *restrict;", Style);
177   verifyFormat("const int volatile *restrict;", Style);
178 }
179 
180 TEST_F(QualifierFixerTest, RightQualifier) {
181   FormatStyle Style = getLLVMStyle();
182   Style.QualifierAlignment = FormatStyle::QAS_Right;
183   Style.QualifierOrder = {"type", "const", "volatile"};
184 
185   verifyFormat("int const a;", Style);
186   verifyFormat("int const *a;", Style);
187   verifyFormat("int const &a;", Style);
188   verifyFormat("int const &&a;", Style);
189   verifyFormat("int const b;", Style);
190   verifyFormat("int const *b;", Style);
191   verifyFormat("int const &b;", Style);
192   verifyFormat("int const &&b;", Style);
193   verifyFormat("int const *const b;", Style);
194   verifyFormat("int *const c;", Style);
195 
196   verifyFormat("Foo const a;", Style);
197   verifyFormat("Foo const *a;", Style);
198   verifyFormat("Foo const &a;", Style);
199   verifyFormat("Foo const &&a;", Style);
200   verifyFormat("Foo const b;", Style);
201   verifyFormat("Foo const *b;", Style);
202   verifyFormat("Foo const &b;", Style);
203   verifyFormat("Foo const &&b;", Style);
204   verifyFormat("Foo const *const b;", Style);
205   verifyFormat("Foo *const b;", Style);
206   verifyFormat("Foo const *const b;", Style);
207   verifyFormat("auto const v = get_value();", Style);
208   verifyFormat("long long const &a;", Style);
209   verifyFormat("unsigned char const *a;", Style);
210   verifyFormat("int main(int const argc, char const *const *const argv)",
211                Style);
212 
213   verifyFormat("LLVM_NODISCARD int const &Foo();", Style);
214   verifyFormat("SourceRange getSourceRange() const override LLVM_READONLY",
215                Style);
216   verifyFormat("void foo() const override;", Style);
217   verifyFormat("void foo() const override LLVM_READONLY;", Style);
218   verifyFormat("void foo() const final;", Style);
219   verifyFormat("void foo() const final LLVM_READONLY;", Style);
220   verifyFormat("void foo() const LLVM_READONLY;", Style);
221   verifyFormat("void foo() const volatile override;", Style);
222   verifyFormat("void foo() const volatile override LLVM_READONLY;", Style);
223   verifyFormat("void foo() const volatile final;", Style);
224   verifyFormat("void foo() const volatile final LLVM_READONLY;", Style);
225   verifyFormat("void foo() const volatile LLVM_READONLY;", Style);
226 
227   verifyFormat(
228       "template <typename Func> explicit Action(Action<Func> const &action);",
229       Style);
230   verifyFormat(
231       "template <typename Func> explicit Action(Action<Func> const &action);",
232       "template <typename Func> explicit Action(const Action<Func>& action);",
233       Style);
234   verifyFormat(
235       "template <typename Func> explicit Action(Action<Func> const &action);",
236       "template <typename Func>\nexplicit Action(const Action<Func>& action);",
237       Style);
238 
239   verifyFormat("int const a;", "const int a;", Style);
240   verifyFormat("int const *a;", "const int *a;", Style);
241   verifyFormat("int const &a;", "const int &a;", Style);
242   verifyFormat("foo(int const &a)", "foo(const int &a)", Style);
243   verifyFormat("unsigned char *a;", Style);
244   verifyFormat("unsigned char const *a;", "const unsigned char *a;", Style);
245   verifyFormat("vector<int, int const, int &, int const &> args1",
246                "vector<int, const int, int &, const int &> args1", Style);
247   verifyFormat("unsigned int const &get_nu() const",
248                "const unsigned int &get_nu() const", Style);
249   verifyFormat("Foo<int> const &a", "const Foo<int> &a", Style);
250   verifyFormat("Foo<int>::iterator const &a", "const Foo<int>::iterator &a",
251                Style);
252   verifyFormat("::Foo<int>::iterator const &a", "const ::Foo<int>::iterator &a",
253                Style);
254 
255   verifyFormat("Foo(int a, "
256                "unsigned b, // c-style args\n"
257                "    Bar const &c);",
258                "Foo(int a, "
259                "unsigned b, // c-style args\n"
260                "    const Bar &c);",
261                Style);
262 
263   verifyFormat("int const volatile;", "volatile const int;", Style);
264   verifyFormat("int const volatile;", "const volatile int;", Style);
265   verifyFormat("int const volatile;", "const int volatile;", Style);
266 
267   verifyFormat("int const volatile *restrict;", "volatile const int *restrict;",
268                Style);
269   verifyFormat("int const volatile *restrict;", "const volatile int *restrict;",
270                Style);
271   verifyFormat("int const volatile *restrict;", "const int volatile *restrict;",
272                Style);
273 
274   verifyFormat("long long int const volatile;", "const long long int volatile;",
275                Style);
276   verifyFormat("long long int const volatile;", "long const long int volatile;",
277                Style);
278   verifyFormat("long long int const volatile;", "long long volatile int const;",
279                Style);
280   verifyFormat("long long int const volatile;", "long volatile long const int;",
281                Style);
282   verifyFormat("long long int const volatile;", "const long long volatile int;",
283                Style);
284 
285   verifyFormat("static int const bat;", "static const int bat;", Style);
286   verifyFormat("static int const bat;", Style);
287 
288   // static is not configured, unchanged on the left of the right hand
289   // qualifiers.
290   verifyFormat("int static const volatile;", "volatile const int static;",
291                Style);
292   verifyFormat("int static const volatile;", "const volatile int static;",
293                Style);
294   verifyFormat("int static const volatile;", "const int volatile static;",
295                Style);
296   verifyFormat("Foo static const volatile;", "volatile const Foo static;",
297                Style);
298   verifyFormat("Foo static const volatile;", "const volatile Foo static;",
299                Style);
300   verifyFormat("Foo static const volatile;", "const Foo volatile static;",
301                Style);
302 
303   verifyFormat("Foo inline static const;", "const Foo inline static;", Style);
304   verifyFormat("Foo inline static const;", "Foo const inline static;", Style);
305   verifyFormat("Foo inline static const;", "Foo inline const static;", Style);
306   verifyFormat("Foo inline static const;", Style);
307 
308   verifyFormat("Foo<T volatile>::Bar<Type const, 5> const volatile A::*;",
309                "volatile const Foo<volatile T>::Bar<const Type, 5> A::*;",
310                Style);
311 
312   verifyFormat("int const Foo<int>::bat = 0;", "const int Foo<int>::bat = 0;",
313                Style);
314   verifyFormat("int const Foo<int>::bat = 0;", Style);
315   verifyFormat("void fn(Foo<T> const &i);", "void fn(const Foo<T> &i);", Style);
316   verifyFormat("int const Foo<int>::fn() {", Style);
317   verifyFormat("Foo<Foo<int>> const *p;", "const Foo<Foo<int>> *p;", Style);
318   verifyFormat(
319       "Foo<Foo<int>> const *p = const_cast<Foo<Foo<int>> const *>(&ffi);",
320       "const Foo<Foo<int>> *p = const_cast<const Foo<Foo<int>> *>(&ffi);",
321       Style);
322 
323   verifyFormat("void fn(Foo<T> const &i);", "void fn(const Foo<T> &i);", Style);
324   verifyFormat("void fns(ns::S const &s);", "void fns(const ns::S &s);", Style);
325   verifyFormat("void fns(::ns::S const &s);", "void fns(const ::ns::S &s);",
326                Style);
327   verifyFormat("void fn(ns::Foo<T> const &i);", "void fn(const ns::Foo<T> &i);",
328                Style);
329   verifyFormat("void fns(ns::ns2::S const &s);",
330                "void fns(const ns::ns2::S &s);", Style);
331   verifyFormat("void fn(ns::Foo<Bar<T>> const &i);",
332                "void fn(const ns::Foo<Bar<T>> &i);", Style);
333   verifyFormat("void fn(ns::ns2::Foo<Bar<T>> const &i);",
334                "void fn(const ns::ns2::Foo<Bar<T>> &i);", Style);
335   verifyFormat("void fn(ns::ns2::Foo<Bar<T, U>> const &i);",
336                "void fn(const ns::ns2::Foo<Bar<T, U>> &i);", Style);
337 
338   verifyFormat("LocalScope const *Scope = nullptr;",
339                "const LocalScope* Scope = nullptr;", Style);
340   verifyFormat("struct DOTGraphTraits<Stmt const *>",
341                "struct DOTGraphTraits<const Stmt *>", Style);
342 
343   verifyFormat(
344       "bool tools::addXRayRuntime(ToolChain const &TC, ArgList const &Args) {",
345       "bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args) {",
346       Style);
347   verifyFormat("Foo<Foo<int> const> P;", "Foo<const Foo<int>> P;", Style);
348   verifyFormat("Foo<Foo<int> const> P;\n#if 0\n#else\n#endif",
349                "Foo<const Foo<int>> P;\n#if 0\n#else\n#endif", Style);
350 
351   verifyFormat("auto const i = 0;", "const auto i = 0;", Style);
352   verifyFormat("auto const &ir = i;", "const auto &ir = i;", Style);
353   verifyFormat("auto const *ip = &i;", "const auto *ip = &i;", Style);
354 
355   verifyFormat("void f(Concept auto const &x);",
356                "void f(const Concept auto &x);", Style);
357   verifyFormat("void f(std::integral auto const &x);",
358                "void f(const std::integral auto &x);", Style);
359 
360   verifyFormat("auto lambda = [] { int const i = 0; };",
361                "auto lambda = [] { const int i = 0; };", Style);
362 
363   verifyFormat("Foo<Foo<int> const> P;\n#if 0\n#else\n#endif",
364                "Foo<const Foo<int>> P;\n#if 0\n#else\n#endif", Style);
365 
366   verifyFormat("Bar<Bar<int const> const> P;\n#if 0\n#else\n#endif",
367                "Bar<Bar<const int> const> P;\n#if 0\n#else\n#endif", Style);
368 
369   verifyFormat("Baz<Baz<int const> const> P;\n#if 0\n#else\n#endif",
370                "Baz<const Baz<const int>> P;\n#if 0\n#else\n#endif", Style);
371 
372   // verifyFormat("#if 0\nBoo<Boo<int const> const> P;\n#else\n#endif",
373   //             "#if 0\nBoo<const Boo<const int>> P;\n#else\n#endif", Style);
374 
375   verifyFormat("int const P;\n#if 0\n#else\n#endif",
376                "const int P;\n#if 0\n#else\n#endif", Style);
377 
378   verifyFormat("unsigned long const a;", "const unsigned long a;", Style);
379   verifyFormat("unsigned long long const a;", "const unsigned long long a;",
380                Style);
381 
382   // Multiple template parameters.
383   verifyFormat("Bar<std::Foo const, 32>", "Bar<const std::Foo, 32>", Style);
384   // Variable declaration based on template type.
385   verifyFormat("Bar<std::Foo const> bar", "Bar<const std::Foo> bar", Style);
386 
387   // Using typename for a nested dependent type name.
388   verifyFormat("typename Foo::iterator const;", "const typename Foo::iterator;",
389                Style);
390 
391   // Don't move past C-style struct/class.
392   verifyFormat("void foo(const struct A a);", Style);
393   verifyFormat("void foo(const class A a);", Style);
394 
395   // Don't move past struct/class combined declaration and variable
396   // definition.
397   verifyFormat("const struct {\n} var;", Style);
398   verifyFormat("struct {\n} const var;", Style);
399   verifyFormat("const class {\n} var;", Style);
400   verifyFormat("class {\n} const var;", Style);
401 
402   // Leave left qualifers unchanged for combined declaration and variable
403   // definition.
404   verifyFormat("volatile const class {\n} var;", Style);
405   verifyFormat("const volatile class {\n} var;", Style);
406   // Also do no sorting with respect to not-configured tokens.
407   verifyFormat("const static volatile class {\n} var;", Style);
408   // Sort right qualifiers for combined declaration and variable definition.
409   verifyFormat("class {\n} const volatile var;", Style);
410   verifyFormat("class {\n} const volatile var;",
411                "class {\n} volatile const var;", Style);
412   // Static keyword is not configured, should end up on the left of the right
413   // side.
414   verifyFormat("class {\n} static const volatile var;", Style);
415   verifyFormat("class {\n} static const volatile var;",
416                "class {\n} volatile static const var;", Style);
417 
418   // ::template for dependent names
419   verifyFormat("::template Foo<T> const volatile var;",
420                "const volatile ::template Foo<T> var;", Style);
421   verifyFormat("typename ::template Foo<T> const volatile var;",
422                "const volatile typename ::template Foo<T> var;", Style);
423   verifyFormat("typename Bar::template Foo<T>::T const;",
424                "const typename Bar::template Foo<T>::T;", Style);
425   verifyFormat("typename Bar::template Foo<T>::T const volatile;",
426                "const volatile typename Bar::template Foo<T>::T;", Style);
427 
428   // typename ::
429   verifyFormat("typename ::Bar<int> const;", "const typename ::Bar<int>;",
430                Style);
431   // typename ::template
432   verifyFormat("typename ::template Bar<int> const;",
433                "const typename ::template Bar<int>;", Style);
434 
435   verifyFormat("foo<Bar<Baz> const>();", "foo<const Bar<Baz>>();", Style);
436   verifyFormat("foo<Bar<Baz> const>();", "foo<const Bar<Baz> >();", Style);
437   verifyFormat("Bar<32, Foo<25> const>;", "Bar<32, const Foo<25>>;", Style);
438   verifyFormat("A<B<C<D> const> const>;", "A<const B<const C<D>>>;", Style);
439   verifyFormat("A<B<C<D const> const> const>;", "A<const B<const C<const D>>>;",
440                Style);
441 
442   // Don't move past decltype, typeof, or _Atomic.
443   verifyFormat("const decltype(foo)", Style);
444   verifyFormat("const typeof(foo)", Style);
445   verifyFormat("const _Atomic(foo)", Style);
446 
447   // Comments
448   const int ColumnLimit = Style.ColumnLimit;
449   Style.ColumnLimit = 200;
450   verifyFormat("/*c*/ Foo const *foo;", "const /*c*/ Foo *foo;", Style);
451   verifyFormat("Foo const /*c*/ *foo;", "const Foo /*c*/ *foo;", Style);
452   verifyFormat("Foo const * /*c*/ foo;", "const Foo * /*c*/ foo;", Style);
453 
454   verifyFormat("/*comment*/ std::vector<int> const v;",
455                "const /*comment*/ std::vector<int> v;", Style);
456   verifyFormat("std /*comment*/ ::vector<int> const v;",
457                "const std /*comment*/ ::vector<int> v;", Style);
458   verifyFormat("std::/*comment*/ vector<int> const v;",
459                "const std::/*comment*/ vector<int> v;", Style);
460   verifyFormat("std::vector /*comment*/<int> const v;",
461                "const std::vector /*comment*/ <int> v;", Style);
462   verifyFormat("std::vector</*comment*/ int> const v;",
463                "const std::vector</*comment*/ int> v;", Style);
464   verifyFormat("std::vector<int /*comment*/> const v;",
465                "const std::vector<int /*comment*/> v;", Style);
466   verifyFormat("std::vector<int> const /*comment*/ v;",
467                "const std::vector<int> /*comment*/ v;", Style);
468 
469   verifyFormat("std::vector</*comment*/ int const> v;",
470                "std::vector</*comment*/ const int> v;", Style);
471   verifyFormat("std::vector</*comment*/ int const> v;",
472                "std::vector<const /*comment*/ int> v;", Style);
473   verifyFormat("std::vector<int const /*comment*/> v;",
474                "std::vector<const int /*comment*/> v;", Style);
475   verifyFormat("std::vector</*comment*/ Foo const> v;",
476                "std::vector</*comment*/ const Foo> v;", Style);
477   verifyFormat("std::vector</*comment*/ Foo const> v;",
478                "std::vector<const /*comment*/ Foo> v;", Style);
479   verifyFormat("std::vector<Foo const /*comment*/> v;",
480                "std::vector<const Foo /*comment*/> v;", Style);
481 
482   verifyFormat("typename C<T>::template B<T> const;",
483                "const typename C<T>::template B<T>;", Style);
484   verifyFormat("/*c*/ typename C<T>::template B<T> const;",
485                "const /*c*/ typename C<T>::template B<T>;", Style);
486   verifyFormat("typename /*c*/ C<T>::template B<T> const;",
487                "const typename /*c*/ C<T>::template B<T>;", Style);
488   verifyFormat("typename C /*c*/<T>::template B<T> const;",
489                "const typename C /*c*/<T>::template B<T>;", Style);
490   verifyFormat("typename C<T> /*c*/ ::template B<T> const;",
491                "const typename C<T> /*c*/ ::template B<T>;", Style);
492   verifyFormat("typename C<T>::/*c*/ template B<T> const;",
493                "const typename C<T>::/*c*/ template B<T>;", Style);
494   verifyFormat("typename C<T>::template /*c*/ B<T> const;",
495                "const typename C<T>::template /*c*/B<T>;", Style);
496   verifyFormat("typename C<T>::template B<T> const /*c*/;",
497                "const typename C<T>::template B<T>/*c*/;", Style);
498 
499   verifyFormat("/*c*/ /*c*/ typename /*c*/ C /*c*/<T> /*c*/ ::/*c*/ template "
500                "/*c*/ B /*c*/<T> const /*c*/ v;",
501                "/*c*/ const /*c*/ typename /*c*/ C /*c*/<T> /*c*/ "
502                "::/*c*/template /*c*/ B /*c*/<T> /*c*/ v;",
503                Style);
504 
505   verifyFormat("/*c*/ unsigned /*c*/ long const /*c*/ a;",
506                "const /*c*/ unsigned /*c*/ long /*c*/ a;", Style);
507   verifyFormat("unsigned /*c*/ long /*c*/ long const a;",
508                "const unsigned /*c*/ long /*c*/ long a;", Style);
509 
510   // Not changed
511   verifyFormat("foo() /*c*/ const", Style);
512   verifyFormat("const /*c*/ struct a;", Style);
513   verifyFormat("const /*c*/ class a;", Style);
514   verifyFormat("const /*c*/ decltype(v) a;", Style);
515   verifyFormat("const /*c*/ typeof(v) a;", Style);
516   verifyFormat("const /*c*/ _Atomic(v) a;", Style);
517   verifyFormat("const decltype /*c*/ (v) a;", Style);
518   verifyFormat("const /*c*/ class {\n} volatile /*c*/ foo = {};", Style);
519 
520   Style.ColumnLimit = ColumnLimit;
521 
522   // Don't adjust macros
523   verifyFormat("const INTPTR a;", Style);
524 
525   // Pointers to members
526   verifyFormat("int S::*a;", Style);
527   verifyFormat("int const S::*a;", "const int S::*a;", Style);
528   verifyFormat("int const S::*const a;", "const int S::* const a;", Style);
529   verifyFormat("int A::*const A::*p1;", Style);
530   verifyFormat("float (C::*p)(int);", Style);
531   verifyFormat("float (C::*const p)(int);", Style);
532   verifyFormat("float (C::*p)(int) const;", Style);
533   verifyFormat("float const (C::*p)(int);", "const float (C::*p)(int);", Style);
534 
535   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
536   Style.BraceWrapping.AfterFunction = true;
537   verifyFormat("auto foo() -> T const { return bar; }",
538                "auto foo() -> const T { return bar; }", Style);
539 }
540 
541 TEST_F(QualifierFixerTest, LeftQualifier) {
542   FormatStyle Style = getLLVMStyle();
543   Style.QualifierAlignment = FormatStyle::QAS_Left;
544   Style.QualifierOrder = {"inline", "static", "const", "volatile", "type"};
545 
546   verifyFormat("const int a;", Style);
547   verifyFormat("const int *a;", Style);
548   verifyFormat("const int &a;", Style);
549   verifyFormat("const int &&a;", Style);
550   verifyFormat("const int b;", Style);
551   verifyFormat("const int *b;", Style);
552   verifyFormat("const int &b;", Style);
553   verifyFormat("const int &&b;", Style);
554   verifyFormat("const int *const b;", Style);
555   verifyFormat("int *const c;", Style);
556 
557   verifyFormat("const Foo a;", Style);
558   verifyFormat("const Foo *a;", Style);
559   verifyFormat("const Foo &a;", Style);
560   verifyFormat("const Foo &&a;", Style);
561   verifyFormat("const Foo b;", Style);
562   verifyFormat("const Foo *b;", Style);
563   verifyFormat("const Foo &b;", Style);
564   verifyFormat("const Foo &&b;", Style);
565   verifyFormat("const Foo *const b;", Style);
566   verifyFormat("Foo *const b;", Style);
567   verifyFormat("const Foo *const b;", Style);
568 
569   verifyFormat("LLVM_NODISCARD const int &Foo();", Style);
570 
571   verifyFormat("const char a[];", Style);
572   verifyFormat("const auto v = get_value();", Style);
573   verifyFormat("const long long &a;", Style);
574   verifyFormat("const unsigned char *a;", Style);
575   verifyFormat("const unsigned char *a;", "unsigned char const *a;", Style);
576   verifyFormat("const Foo<int> &a", "Foo<int> const &a", Style);
577   verifyFormat("const Foo<int>::iterator &a", "Foo<int>::iterator const &a",
578                Style);
579   verifyFormat("const ::Foo<int>::iterator &a", "::Foo<int>::iterator const &a",
580                Style);
581 
582   verifyFormat("const int a;", "int const a;", Style);
583   verifyFormat("const int *a;", "int const *a;", Style);
584   verifyFormat("const int &a;", "int const &a;", Style);
585   verifyFormat("foo(const int &a)", "foo(int const &a)", Style);
586   verifyFormat("unsigned char *a;", Style);
587   verifyFormat("const unsigned int &get_nu() const",
588                "unsigned int const &get_nu() const", Style);
589 
590   verifyFormat("const volatile int;", "volatile const int;", Style);
591   verifyFormat("const volatile int;", Style);
592   verifyFormat("const volatile int;", "const int volatile;", Style);
593 
594   verifyFormat("const volatile int *restrict;", "volatile const int *restrict;",
595                Style);
596   verifyFormat("const volatile int *restrict;", Style);
597   verifyFormat("const volatile int *restrict;", "const int volatile *restrict;",
598                Style);
599 
600   verifyFormat("const volatile long long int;", "volatile long long int const;",
601                Style);
602   verifyFormat("const volatile long long int;", "volatile long long const int;",
603                Style);
604   verifyFormat("const volatile long long int;", "long long volatile int const;",
605                Style);
606   verifyFormat("const volatile long long int;", "long volatile long int const;",
607                Style);
608   verifyFormat("const volatile long long int;", "const long long volatile int;",
609                Style);
610 
611   verifyFormat("SourceRange getSourceRange() const override LLVM_READONLY;",
612                Style);
613 
614   verifyFormat("void foo() const override;", Style);
615   verifyFormat("void foo() const override LLVM_READONLY;", Style);
616   verifyFormat("void foo() const final;", Style);
617   verifyFormat("void foo() const final LLVM_READONLY;", Style);
618   verifyFormat("void foo() const LLVM_READONLY;", Style);
619 
620   verifyFormat(
621       "template <typename Func> explicit Action(const Action<Func> &action);",
622       Style);
623   verifyFormat(
624       "template <typename Func> explicit Action(const Action<Func> &action);",
625       "template <typename Func> explicit Action(Action<Func> const &action);",
626       Style);
627 
628   verifyFormat("static const int bat;", Style);
629   verifyFormat("static const int bat;", "static int const bat;", Style);
630 
631   verifyFormat("static const int Foo<int>::bat = 0;", Style);
632   verifyFormat("static const int Foo<int>::bat = 0;",
633                "static int const Foo<int>::bat = 0;", Style);
634 
635   verifyFormat("void fn(const Foo<T> &i);");
636 
637   verifyFormat("const int Foo<int>::bat = 0;", Style);
638   verifyFormat("const int Foo<int>::bat = 0;", "int const Foo<int>::bat = 0;",
639                Style);
640   verifyFormat("void fn(const Foo<T> &i);", "void fn( Foo<T> const &i);",
641                Style);
642   verifyFormat("const int Foo<int>::fn() {", "int const Foo<int>::fn() {",
643                Style);
644   verifyFormat("const Foo<Foo<int>> *p;", "Foo<Foo<int>> const *p;", Style);
645   verifyFormat(
646       "const Foo<Foo<int>> *p = const_cast<const Foo<Foo<int>> *>(&ffi);",
647       "const Foo<Foo<int>> *p = const_cast<Foo<Foo<int>> const *>(&ffi);",
648       Style);
649 
650   verifyFormat("void fn(const Foo<T> &i);", "void fn(Foo<T> const &i);", Style);
651   verifyFormat("void fns(const ns::S &s);", "void fns(ns::S const &s);", Style);
652   verifyFormat("void fns(const ::ns::S &s);", "void fns(::ns::S const &s);",
653                Style);
654   verifyFormat("void fn(const ns::Foo<T> &i);", "void fn(ns::Foo<T> const &i);",
655                Style);
656   verifyFormat("void fns(const ns::ns2::S &s);",
657                "void fns(ns::ns2::S const &s);", Style);
658   verifyFormat("void fn(const ns::Foo<Bar<T>> &i);",
659                "void fn(ns::Foo<Bar<T>> const &i);", Style);
660   verifyFormat("void fn(const ns::ns2::Foo<Bar<T>> &i);",
661                "void fn(ns::ns2::Foo<Bar<T>> const &i);", Style);
662   verifyFormat("void fn(const ns::ns2::Foo<Bar<T, U>> &i);",
663                "void fn(ns::ns2::Foo<Bar<T, U>> const &i);", Style);
664 
665   verifyFormat("const auto i = 0;", "auto const i = 0;", Style);
666   verifyFormat("const auto &ir = i;", "auto const &ir = i;", Style);
667   verifyFormat("const auto *ip = &i;", "auto const *ip = &i;", Style);
668 
669   verifyFormat("void f(const Concept auto &x);",
670                "void f(Concept auto const &x);", Style);
671   verifyFormat("void f(const std::integral auto &x);",
672                "void f(std::integral auto const &x);", Style);
673 
674   verifyFormat("auto lambda = [] { const int i = 0; };",
675                "auto lambda = [] { int const i = 0; };", Style);
676 
677   verifyFormat("Foo<const Foo<int>> P;\n#if 0\n#else\n#endif",
678                "Foo<Foo<int> const> P;\n#if 0\n#else\n#endif", Style);
679 
680   verifyFormat("Foo<Foo<const int>> P;\n#if 0\n#else\n#endif",
681                "Foo<Foo<int const>> P;\n#if 0\n#else\n#endif", Style);
682 
683   verifyFormat("const int P;\n#if 0\n#else\n#endif",
684                "int const P;\n#if 0\n#else\n#endif", Style);
685 
686   verifyFormat("const unsigned long a;", "unsigned long const a;", Style);
687   verifyFormat("const unsigned long long a;", "unsigned long long const a;",
688                Style);
689 
690   verifyFormat("const long long unsigned a;", "long const long unsigned a;",
691                Style);
692 
693   verifyFormat("const std::Foo", Style);
694   verifyFormat("const std::Foo<>", Style);
695   verifyFormat("const std::Foo < int", "const std::Foo<int", Style);
696   verifyFormat("const std::Foo<int>", Style);
697 
698   // Multiple template parameters.
699   verifyFormat("Bar<const std::Foo, 32>;", "Bar<std::Foo const, 32>;", Style);
700 
701   // Variable declaration based on template type.
702   verifyFormat("Bar<const std::Foo> bar;", "Bar<std::Foo const> bar;", Style);
703 
704   // Using typename for a dependent name.
705   verifyFormat("const typename Foo::iterator;", "typename Foo::iterator const;",
706                Style);
707 
708   // Don't move past C-style struct/class.
709   verifyFormat("void foo(struct A const a);", Style);
710   verifyFormat("void foo(class A const a);", Style);
711 
712   // Don't move past struct/class combined declaration and variable
713   // definition.
714   verifyFormat("const struct {\n} var;", Style);
715   verifyFormat("struct {\n} const var;", Style);
716   verifyFormat("const class {\n} var;", Style);
717   verifyFormat("class {\n} const var;", Style);
718 
719   // Sort left qualifiers for struct/class combined declaration and variable
720   // definition.
721   verifyFormat("const volatile class {\n} var;", Style);
722   verifyFormat("const volatile class {\n} var;",
723                "volatile const class {\n} var;", Style);
724   // Leave right qualifers unchanged for struct/class combined declaration and
725   // variable definition.
726   verifyFormat("class {\n} const volatile var;", Style);
727   verifyFormat("class {\n} volatile const var;", Style);
728 
729   verifyFormat("foo<const Bar<Baz<T>>>();", "foo<Bar<Baz<T>> const>();", Style);
730   verifyFormat("foo<const Bar<Baz<T>>>();", "foo<Bar<Baz<T> > const>();",
731                Style);
732   verifyFormat("Bar<32, const Foo<25>>;", "Bar<32, Foo<25> const>;", Style);
733   verifyFormat("A<const B<const C<D>>>;", "A<B<C<D> const> const>;", Style);
734   verifyFormat("A<const B<const C<const D>>>;", "A<B<C<D const> const> const>;",
735                Style);
736 
737   // Don't move past decltype, typeof, or _Atomic.
738   verifyFormat("decltype(foo) const", Style);
739   verifyFormat("typeof(foo) const", Style);
740   verifyFormat("_Atomic(foo) const", Style);
741 
742   // ::template for dependent names
743   verifyFormat("const volatile ::template Foo<T> var;",
744                "::template Foo<T> const volatile var;", Style);
745   verifyFormat("const volatile typename ::template Foo<T> var;",
746                "typename ::template Foo<T> const volatile var;", Style);
747   verifyFormat("const typename Bar::template Foo<T>::T;",
748                "typename Bar::template Foo<T>::T const;", Style);
749   verifyFormat("const volatile typename Bar::template Foo<T>::T;",
750                "typename Bar::template Foo<T>::T const volatile;", Style);
751 
752   // typename ::
753   verifyFormat("const typename ::Bar<int>;", "typename ::Bar<int> const;",
754                Style);
755   // typename ::template
756   verifyFormat("const typename ::template Bar<int>;",
757                "typename ::template Bar<int> const;", Style);
758 
759   // Comments
760   const int ColumnLimit = Style.ColumnLimit;
761   Style.ColumnLimit = 200;
762   verifyFormat("/*c*/ const Foo *foo;", "/*c*/ Foo const *foo;", Style);
763   verifyFormat("const Foo /*c*/ *foo;", "Foo const /*c*/ *foo;", Style);
764   verifyFormat("const Foo * /*c*/ foo;", "Foo const * /*c*/ foo;", Style);
765 
766   verifyFormat("/*comment*/ const std::vector<int> v;",
767                "/*comment*/ std::vector<int> const v;", Style);
768   verifyFormat("const std /*comment*/ ::vector<int> v;",
769                "std /*comment*/ ::vector<int> const v;", Style);
770   verifyFormat("const std::/*comment*/ vector<int> v;",
771                "std::/*comment*/ vector<int> const v;", Style);
772   verifyFormat("const std::vector /*comment*/<int> v;",
773                "std::vector /*comment*/<int> const v;", Style);
774   verifyFormat("const std::vector</*comment*/ int> v;",
775                "std::vector</*comment*/ int> const v;", Style);
776   verifyFormat("const std::vector<int /*comment*/> v;",
777                "std::vector<int /*comment*/> const v;", Style);
778   verifyFormat("const std::vector<int> /*comment*/ v;",
779                "std::vector<int> /*comment*/ const v;", Style);
780 
781   verifyFormat("std::vector</*comment*/ const int> v;",
782                "std::vector</*comment*/ int const> v;", Style);
783   verifyFormat("std::vector<const int /*comment*/> v;",
784                "std::vector<int /*comment*/ const> v;", Style);
785   verifyFormat("std::vector<const int /*comment*/> v;",
786                "std::vector<int const /*comment*/> v;", Style);
787   verifyFormat("std::vector</*comment*/ const Foo> v;",
788                "std::vector</*comment*/ Foo const> v;", Style);
789   verifyFormat("std::vector<const Foo /*comment*/> v;",
790                "std::vector<Foo /*comment*/ const> v;", Style);
791   verifyFormat("std::vector<const Foo /*comment*/> v;",
792                "std::vector<Foo const /*comment*/> v;", Style);
793 
794   verifyFormat("const typename C<T>::template B<T>;",
795                "typename C<T>::template B<T> const;", Style);
796   verifyFormat("/*c*/ const typename C<T>::template B<T>;",
797                "/*c*/ typename C<T>::template B<T> const;", Style);
798   verifyFormat("const typename /*c*/ C<T>::template B<T>;",
799                "typename /*c*/ C<T>::template B<T> const;", Style);
800   verifyFormat("const typename C /*c*/<T>::template B<T>;",
801                "typename C /*c*/<T>::template B<T> const;", Style);
802   verifyFormat("const typename C<T> /*c*/ ::template B<T>;",
803                "typename C<T> /*c*/ ::template B<T> const;", Style);
804   verifyFormat("const typename C<T>::/*c*/ template B<T>;",
805                "typename C<T>::/*c*/ template B<T> const;", Style);
806   verifyFormat("const typename C<T>::template /*c*/ B<T>;",
807                "typename C<T>::template /*c*/ B<T> const;", Style);
808   verifyFormat("const typename C<T>::template B<T> /*c*/;",
809                "typename C<T>::template B<T> /*c*/ const;", Style);
810 
811   verifyFormat("/*c*/ const typename /*c*/ C /*c*/<T> /*c*/ ::/*c*/ template "
812                "/*c*/ B /*c*/<T> /*c*/ v;",
813                "/*c*/ typename /*c*/ C /*c*/<T> /*c*/ ::/*c*/ template /*c*/ B "
814                "/*c*/<T> /*c*/ const v;",
815                Style);
816 
817   verifyFormat("const unsigned /*c*/ long /*c*/ a;",
818                "unsigned /*c*/ long /*c*/ const a;", Style);
819   verifyFormat("const unsigned /*c*/ long /*c*/ long a;",
820                "unsigned /*c*/ long /*c*/ long const a;", Style);
821 
822   // Not changed
823   verifyFormat("foo() /*c*/ const", Style);
824   verifyFormat("struct /*c*/ const a;", Style);
825   verifyFormat("class /*c*/ const a;", Style);
826   verifyFormat("decltype(v) /*c*/ const a;", Style);
827   verifyFormat("typeof(v) /*c*/ const a;", Style);
828   verifyFormat("_Atomic(v) /*c*/ const a;", Style);
829   verifyFormat("decltype /*c*/ (v) const a;", Style);
830   verifyFormat("const /*c*/ class {\n} /*c*/ volatile /*c*/ foo = {};", Style);
831 
832   Style.ColumnLimit = ColumnLimit;
833 
834   // Don't adjust macros
835   verifyFormat("INTPTR const a;", Style);
836 
837   // Pointers to members
838   verifyFormat("int S::*a;", Style);
839   verifyFormat("const int S::*a;", "int const S::*a;", Style);
840   verifyFormat("const int S::*const a;", "int const S::*const a;", Style);
841   verifyFormat("int A::*const A::*p1;", Style);
842   verifyFormat("float (C::*p)(int);", Style);
843   verifyFormat("float (C::*const p)(int);", Style);
844   verifyFormat("float (C::*p)(int) const;", Style);
845   verifyFormat("const float (C::*p)(int);", "float const (C::*p)(int);", Style);
846 }
847 
848 TEST_F(QualifierFixerTest, ConstVolatileQualifiersOrder) {
849   FormatStyle Style = getLLVMStyle();
850   Style.QualifierAlignment = FormatStyle::QAS_Left;
851   Style.QualifierOrder = {"inline", "static", "const", "volatile", "type"};
852 
853   // The Default
854   EXPECT_EQ(Style.QualifierOrder.size(), (size_t)5);
855 
856   verifyFormat("const volatile int a;", Style);
857   verifyFormat("const volatile int a;", "volatile const int a;", Style);
858   verifyFormat("const volatile int a;", "int const volatile a;", Style);
859   verifyFormat("const volatile int a;", "int volatile const a;", Style);
860   verifyFormat("const volatile int a;", "const int volatile a;", Style);
861 
862   verifyFormat("const volatile Foo a;", Style);
863   verifyFormat("const volatile Foo a;", "volatile const Foo a;", Style);
864   verifyFormat("const volatile Foo a;", "Foo const volatile a;", Style);
865   verifyFormat("const volatile Foo a;", "Foo volatile const a;", Style);
866   verifyFormat("const volatile Foo a;", "const Foo volatile a;", Style);
867 
868   Style.QualifierAlignment = FormatStyle::QAS_Right;
869   Style.QualifierOrder = {"type", "const", "volatile"};
870 
871   verifyFormat("int const volatile a;", "const volatile int a;", Style);
872   verifyFormat("int const volatile a;", "volatile const int a;", Style);
873   verifyFormat("int const volatile a;", Style);
874   verifyFormat("int const volatile a;", "int volatile const a;", Style);
875   verifyFormat("int const volatile a;", "const int volatile a;", Style);
876 
877   verifyFormat("Foo const volatile a;", "const volatile Foo a;", Style);
878   verifyFormat("Foo const volatile a;", "volatile const Foo a;", Style);
879   verifyFormat("Foo const volatile a;", Style);
880   verifyFormat("Foo const volatile a;", "Foo volatile const a;", Style);
881   verifyFormat("Foo const volatile a;", "const Foo volatile a;", Style);
882 
883   Style.QualifierAlignment = FormatStyle::QAS_Left;
884   Style.QualifierOrder = {"volatile", "const", "type"};
885 
886   verifyFormat("volatile const int a;", "const volatile int a;", Style);
887   verifyFormat("volatile const int a;", Style);
888   verifyFormat("volatile const int a;", "int const volatile a;", Style);
889   verifyFormat("volatile const int a;", "int volatile const a;", Style);
890   verifyFormat("volatile const int a;", "const int volatile a;", Style);
891 
892   verifyFormat("volatile const Foo a;", "const volatile Foo a;", Style);
893   verifyFormat("volatile const Foo a;", Style);
894   verifyFormat("volatile const Foo a;", "Foo const volatile a;", Style);
895   verifyFormat("volatile const Foo a;", "Foo volatile const a;", Style);
896   verifyFormat("volatile const Foo a;", "const Foo volatile a;", Style);
897 
898   Style.QualifierAlignment = FormatStyle::QAS_Right;
899   Style.QualifierOrder = {"type", "volatile", "const"};
900 
901   verifyFormat("int volatile const a;", "const volatile int a;", Style);
902   verifyFormat("int volatile const a;", "volatile const int a;", Style);
903   verifyFormat("int volatile const a;", "int const volatile a;", Style);
904   verifyFormat("int volatile const a;", Style);
905   verifyFormat("int volatile const a;", "const int volatile a;", Style);
906 
907   verifyFormat("Foo volatile const a;", "const volatile Foo a;", Style);
908   verifyFormat("Foo volatile const a;", "volatile const Foo a;", Style);
909   verifyFormat("Foo volatile const a;", "Foo const volatile a;", Style);
910   verifyFormat("Foo volatile const a;", Style);
911   verifyFormat("Foo volatile const a;", "const Foo volatile a;", Style);
912 
913   Style.QualifierAlignment = FormatStyle::QAS_Custom;
914   Style.QualifierOrder = {"type", "volatile", "const"};
915 
916   verifyFormat("int volatile const a;", "const volatile int a;", Style);
917   verifyFormat("int volatile const a;", "volatile const int a;", Style);
918   verifyFormat("int volatile const a;", "int const volatile a;", Style);
919   verifyFormat("int volatile const a;", Style);
920   verifyFormat("int volatile const a;", "const int volatile a;", Style);
921 
922   verifyFormat("Foo volatile const a;", "const volatile Foo a;", Style);
923   verifyFormat("Foo volatile const a;", "volatile const Foo a;", Style);
924   verifyFormat("Foo volatile const a;", "Foo const volatile a;", Style);
925   verifyFormat("Foo volatile const a;", Style);
926   verifyFormat("Foo volatile const a;", "const Foo volatile a;", Style);
927 }
928 
929 TEST_F(QualifierFixerTest, InlineStatics) {
930   FormatStyle Style = getLLVMStyle();
931   Style.QualifierAlignment = FormatStyle::QAS_Left;
932   Style.QualifierOrder = {"inline", "static", "const", "volatile", "type"};
933   EXPECT_EQ(Style.QualifierOrder.size(), (size_t)5);
934 
935   verifyFormat("inline static const volatile int a;",
936                "const inline static volatile int a;", Style);
937   verifyFormat("inline static const volatile int a;",
938                "volatile inline static const int a;", Style);
939   verifyFormat("inline static const volatile int a;",
940                "int const inline static  volatile a;", Style);
941   verifyFormat("inline static const volatile int a;",
942                "int volatile inline static  const a;", Style);
943   verifyFormat("inline static const volatile int a;",
944                "const int inline static  volatile a;", Style);
945 }
946 
947 TEST_F(QualifierFixerTest, AmpEqual) {
948   FormatStyle Style = getLLVMStyle();
949   Style.QualifierAlignment = FormatStyle::QAS_Custom;
950   Style.QualifierOrder = {"static", "type", "const"};
951   EXPECT_EQ(Style.QualifierOrder.size(), (size_t)3);
952 
953   verifyFormat("foo(std::string const & = std::string()) const",
954                "foo(const std::string & = std::string()) const", Style);
955   verifyFormat("foo(std::string const & = std::string())",
956                "foo(const std::string & = std::string())", Style);
957 }
958 
959 TEST_F(QualifierFixerTest, MoveConstBeyondTypeSmall) {
960 
961   FormatStyle Style = getLLVMStyle();
962   Style.QualifierAlignment = FormatStyle::QAS_Custom;
963   Style.QualifierOrder = {"type", "const"};
964   EXPECT_EQ(Style.QualifierOrder.size(), (size_t)2);
965 
966   verifyFormat("int const a;", "const int a;", Style);
967   verifyFormat("int const *a;", "const int*a;", Style);
968   verifyFormat("int const *a;", "const int *a;", Style);
969   verifyFormat("int const &a;", "const int &a;", Style);
970   verifyFormat("int const &&a;", "const int &&a;", Style);
971 }
972 
973 TEST_F(QualifierFixerTest, MoveConstBeforeTypeSmall) {
974 
975   FormatStyle Style = getLLVMStyle();
976   Style.QualifierAlignment = FormatStyle::QAS_Custom;
977   Style.QualifierOrder = {"const", "type"};
978   EXPECT_EQ(Style.QualifierOrder.size(), (size_t)2);
979 
980   verifyFormat("const int a;", "int const a;", Style);
981   verifyFormat("const int *a;", "int const *a;", Style);
982   verifyFormat("const int *const a;", "int const *const a;", Style);
983 
984   verifyFormat("const int a = foo();", "int const a = foo();", Style);
985   verifyFormat("const int *a = foo();", "int const *a = foo();", Style);
986   verifyFormat("const int *const a = foo();", "int const *const a = foo();",
987                Style);
988 
989   verifyFormat("const auto a = foo();", "auto const a = foo();", Style);
990   verifyFormat("const auto *a = foo();", "auto const *a = foo();", Style);
991   verifyFormat("const auto *const a = foo();", "auto const *const a = foo();",
992                Style);
993 }
994 
995 TEST_F(QualifierFixerTest, MoveConstBeyondType) {
996 
997   FormatStyle Style = getLLVMStyle();
998   Style.QualifierAlignment = FormatStyle::QAS_Custom;
999   Style.QualifierOrder = {"static", "inline", "type", "const", "volatile"};
1000   EXPECT_EQ(Style.QualifierOrder.size(), (size_t)5);
1001 
1002   verifyFormat("static inline int const volatile a;",
1003                "const inline static volatile int a;", Style);
1004   verifyFormat("static inline int const volatile a;",
1005                "volatile inline static const int a;", Style);
1006   verifyFormat("static inline int const volatile a;",
1007                "int const inline static  volatile a;", Style);
1008   verifyFormat("static inline int const volatile a;",
1009                "int volatile inline static  const a;", Style);
1010   verifyFormat("static inline int const volatile a;",
1011                "const int inline static  volatile a;", Style);
1012 
1013   verifyFormat("static inline int const volatile *const a;",
1014                "const int inline static  volatile *const a;", Style);
1015 
1016   verifyFormat("static inline Foo const volatile a;",
1017                "const inline static volatile Foo a;", Style);
1018   verifyFormat("static inline Foo const volatile a;",
1019                "volatile inline static const Foo a;", Style);
1020   verifyFormat("static inline Foo const volatile a;",
1021                "Foo const inline static  volatile a;", Style);
1022   verifyFormat("static inline Foo const volatile a;",
1023                "Foo volatile inline static  const a;", Style);
1024   verifyFormat("static inline Foo const volatile a;",
1025                "const Foo inline static  volatile a;", Style);
1026 
1027   verifyFormat("static inline Foo const volatile *const a;",
1028                "const Foo inline static volatile *const a;", Style);
1029 }
1030 
1031 TEST_F(QualifierFixerTest, PrepareLeftRightOrdering) {
1032   FormatStyle Style = getLLVMStyle();
1033   Style.QualifierAlignment = FormatStyle::QAS_Custom;
1034   Style.QualifierOrder = {"static", "inline", "type", "const", "volatile"};
1035 
1036   std::vector<std::string> Left;
1037   std::vector<std::string> Right;
1038   std::vector<tok::TokenKind> ConfiguredTokens;
1039   prepareLeftRightOrderingForQualifierAlignmentFixer(Style.QualifierOrder, Left,
1040                                                      Right, ConfiguredTokens);
1041 
1042   EXPECT_EQ(Left.size(), (size_t)2);
1043   EXPECT_EQ(Right.size(), (size_t)2);
1044 
1045   std::vector<std::string> LeftResult = {"inline", "static"};
1046   std::vector<std::string> RightResult = {"const", "volatile"};
1047   EXPECT_EQ(Left, LeftResult);
1048   EXPECT_EQ(Right, RightResult);
1049 }
1050 
1051 TEST_F(QualifierFixerTest, IsQualifierType) {
1052 
1053   std::vector<tok::TokenKind> ConfiguredTokens;
1054   ConfiguredTokens.push_back(tok::kw_const);
1055   ConfiguredTokens.push_back(tok::kw_static);
1056   ConfiguredTokens.push_back(tok::kw_inline);
1057   ConfiguredTokens.push_back(tok::kw_restrict);
1058   ConfiguredTokens.push_back(tok::kw_constexpr);
1059   ConfiguredTokens.push_back(tok::kw_friend);
1060 
1061   TestLexer lexer{Allocator, Buffers};
1062   const auto LangOpts = getFormattingLangOpts();
1063 
1064   auto Tokens = lexer.lex(
1065       "const static inline auto restrict int double long constexpr friend");
1066   ASSERT_EQ(Tokens.size(), 11u) << Tokens;
1067 
1068   EXPECT_TRUE(
1069       isConfiguredQualifierOrType(Tokens[0], ConfiguredTokens, LangOpts));
1070   EXPECT_TRUE(
1071       isConfiguredQualifierOrType(Tokens[1], ConfiguredTokens, LangOpts));
1072   EXPECT_TRUE(
1073       isConfiguredQualifierOrType(Tokens[2], ConfiguredTokens, LangOpts));
1074   EXPECT_TRUE(
1075       isConfiguredQualifierOrType(Tokens[3], ConfiguredTokens, LangOpts));
1076   EXPECT_TRUE(
1077       isConfiguredQualifierOrType(Tokens[4], ConfiguredTokens, LangOpts));
1078   EXPECT_TRUE(
1079       isConfiguredQualifierOrType(Tokens[5], ConfiguredTokens, LangOpts));
1080   EXPECT_TRUE(
1081       isConfiguredQualifierOrType(Tokens[6], ConfiguredTokens, LangOpts));
1082   EXPECT_TRUE(
1083       isConfiguredQualifierOrType(Tokens[7], ConfiguredTokens, LangOpts));
1084   EXPECT_TRUE(
1085       isConfiguredQualifierOrType(Tokens[8], ConfiguredTokens, LangOpts));
1086   EXPECT_TRUE(
1087       isConfiguredQualifierOrType(Tokens[9], ConfiguredTokens, LangOpts));
1088 
1089   EXPECT_TRUE(isQualifierOrType(Tokens[0], LangOpts));
1090   EXPECT_TRUE(isQualifierOrType(Tokens[1], LangOpts));
1091   EXPECT_TRUE(isQualifierOrType(Tokens[2], LangOpts));
1092   EXPECT_TRUE(isQualifierOrType(Tokens[3], LangOpts));
1093   EXPECT_TRUE(isQualifierOrType(Tokens[4], LangOpts));
1094   EXPECT_TRUE(isQualifierOrType(Tokens[5], LangOpts));
1095   EXPECT_TRUE(isQualifierOrType(Tokens[6], LangOpts));
1096   EXPECT_TRUE(isQualifierOrType(Tokens[7], LangOpts));
1097   EXPECT_TRUE(isQualifierOrType(Tokens[8], LangOpts));
1098   EXPECT_TRUE(isQualifierOrType(Tokens[9], LangOpts));
1099 
1100   auto NotTokens = lexer.lex("for while do Foo Bar ");
1101   ASSERT_EQ(NotTokens.size(), 6u) << Tokens;
1102 
1103   EXPECT_FALSE(
1104       isConfiguredQualifierOrType(NotTokens[0], ConfiguredTokens, LangOpts));
1105   EXPECT_FALSE(
1106       isConfiguredQualifierOrType(NotTokens[1], ConfiguredTokens, LangOpts));
1107   EXPECT_FALSE(
1108       isConfiguredQualifierOrType(NotTokens[2], ConfiguredTokens, LangOpts));
1109   EXPECT_FALSE(
1110       isConfiguredQualifierOrType(NotTokens[3], ConfiguredTokens, LangOpts));
1111   EXPECT_FALSE(
1112       isConfiguredQualifierOrType(NotTokens[4], ConfiguredTokens, LangOpts));
1113   EXPECT_FALSE(
1114       isConfiguredQualifierOrType(NotTokens[5], ConfiguredTokens, LangOpts));
1115 
1116   EXPECT_FALSE(isQualifierOrType(NotTokens[0], LangOpts));
1117   EXPECT_FALSE(isQualifierOrType(NotTokens[1], LangOpts));
1118   EXPECT_FALSE(isQualifierOrType(NotTokens[2], LangOpts));
1119   EXPECT_FALSE(isQualifierOrType(NotTokens[3], LangOpts));
1120   EXPECT_FALSE(isQualifierOrType(NotTokens[4], LangOpts));
1121   EXPECT_FALSE(isQualifierOrType(NotTokens[5], LangOpts));
1122 }
1123 
1124 TEST_F(QualifierFixerTest, IsMacro) {
1125 
1126   auto Tokens = annotate("INT INTPR Foo int");
1127   ASSERT_EQ(Tokens.size(), 5u) << Tokens;
1128 
1129   EXPECT_TRUE(isPossibleMacro(Tokens[0]));
1130   EXPECT_TRUE(isPossibleMacro(Tokens[1]));
1131   EXPECT_FALSE(isPossibleMacro(Tokens[2]));
1132   EXPECT_FALSE(isPossibleMacro(Tokens[3]));
1133 }
1134 
1135 TEST_F(QualifierFixerTest, OverlappingQualifier) {
1136 
1137   FormatStyle Style = getLLVMStyle();
1138   Style.QualifierAlignment = FormatStyle::QAS_Left;
1139   Style.QualifierOrder = {"const", "type"};
1140 
1141   verifyFormat("Foo(const Bar &name);", "Foo(Bar const &name);", Style);
1142 }
1143 
1144 TEST_F(QualifierFixerTest, DontPushQualifierThroughNonSpecifiedTypes) {
1145 
1146   FormatStyle Style = getLLVMStyle();
1147   Style.QualifierAlignment = FormatStyle::QAS_Left;
1148   Style.QualifierOrder = {"const", "volatile", "type"};
1149 
1150   verifyFormat("inline static const int a;", Style);
1151 
1152   Style.QualifierOrder = {"static", "const", "type"};
1153 
1154   verifyFormat("inline static const int a;", Style);
1155   verifyFormat("static inline const int a;", Style);
1156 
1157   verifyFormat("static const int a;", "const static int a;", Style);
1158 
1159   Style.QualifierOrder = {"const", "volatile", "type"};
1160   // static is not configured, unchanged at right hand qualifiers.
1161   verifyFormat("const volatile int static;", "int volatile static const;",
1162                Style);
1163   verifyFormat("const volatile int static;", "int const static volatile;",
1164                Style);
1165   verifyFormat("const volatile int static;", "const int static volatile;",
1166                Style);
1167   verifyFormat("const volatile Foo static;", "Foo volatile static const;",
1168                Style);
1169   verifyFormat("const volatile Foo static;", "Foo const static volatile;",
1170                Style);
1171   verifyFormat("const volatile Foo static;", "const Foo static volatile;",
1172                Style);
1173 
1174   verifyFormat("inline static const Foo;", "inline static Foo const;", Style);
1175   verifyFormat("inline static const Foo;", Style);
1176 
1177   // Don't move qualifiers to the right for aestethics only.
1178   verifyFormat("inline const static Foo;", Style);
1179   verifyFormat("const inline static Foo;", Style);
1180 }
1181 
1182 TEST_F(QualifierFixerTest, QualifiersBrokenUpByPPDirectives) {
1183   auto Style = getLLVMStyle();
1184   Style.QualifierAlignment = FormatStyle::QAS_Custom;
1185   Style.QualifierOrder = {"constexpr", "inline", "type"};
1186 
1187   verifyFormat("inline\n"
1188                "#if FOO\n"
1189                "    constexpr\n"
1190                "#endif\n"
1191                "    int i = 0;",
1192                Style);
1193 }
1194 
1195 TEST_F(QualifierFixerTest, UnsignedQualifier) {
1196 
1197   FormatStyle Style = getLLVMStyle();
1198   Style.QualifierAlignment = FormatStyle::QAS_Left;
1199   Style.QualifierOrder = {"const", "type"};
1200 
1201   verifyFormat("Foo(const unsigned char *bytes)",
1202                "Foo(unsigned const char *bytes)", Style);
1203 
1204   Style.QualifierAlignment = FormatStyle::QAS_Right;
1205   Style.QualifierOrder = {"type", "const"};
1206 
1207   verifyFormat("Foo(unsigned char const *bytes)",
1208                "Foo(unsigned const char *bytes)", Style);
1209 }
1210 
1211 TEST_F(QualifierFixerTest, NoOpQualifierReplacements) {
1212 
1213   FormatStyle Style = getLLVMStyle();
1214   Style.QualifierAlignment = FormatStyle::QAS_Custom;
1215   Style.QualifierOrder = {"static", "const", "type"};
1216 
1217   verifyFormat("static const uint32 foo[] = {0, 31};", Style);
1218   EXPECT_EQ(ReplacementCount, 0);
1219 
1220   verifyFormat("#define MACRO static const", Style);
1221   EXPECT_EQ(ReplacementCount, 0);
1222 
1223   verifyFormat("using sc = static const", Style);
1224   EXPECT_EQ(ReplacementCount, 0);
1225 }
1226 
1227 TEST_F(QualifierFixerTest, QualifierTemplates) {
1228   FormatStyle Style = getLLVMStyle();
1229   Style.QualifierAlignment = FormatStyle::QAS_Custom;
1230   Style.QualifierOrder = {"static", "const", "type"};
1231 
1232   ReplacementCount = 0;
1233   EXPECT_EQ(ReplacementCount, 0);
1234   verifyFormat("using A = B<>;", Style);
1235   verifyFormat("using A = B /**/<>;", Style);
1236   verifyFormat("template <class C> using A = B<Foo<C>, 1>;", Style);
1237   verifyFormat("template <class C> using A = B /**/<Foo<C>, 1>;", Style);
1238   verifyFormat("template <class C> using A = B /* */<Foo<C>, 1>;", Style);
1239   verifyFormat("template <class C> using A = B /*foo*/<Foo<C>, 1>;", Style);
1240   verifyFormat("template <class C> using A = B /**/ /**/<Foo<C>, 1>;", Style);
1241   verifyFormat("template <class C> using A = B<Foo</**/ C>, 1>;", Style);
1242   verifyFormat("template <class C> using A = /**/ B<Foo<C>, 1>;", Style);
1243   EXPECT_EQ(ReplacementCount, 0);
1244   verifyFormat("template <class C>\n"
1245                "using A = B // foo\n"
1246                "    <Foo<C>, 1>;",
1247                Style);
1248 
1249   ReplacementCount = 0;
1250   Style.QualifierOrder = {"type", "static", "const"};
1251   verifyFormat("using A = B<>;", Style);
1252   verifyFormat("using A = B /**/<>;", Style);
1253   verifyFormat("template <class C> using A = B<Foo<C>, 1>;", Style);
1254   verifyFormat("template <class C> using A = B /**/<Foo<C>, 1>;", Style);
1255   verifyFormat("template <class C> using A = B /* */<Foo<C>, 1>;", Style);
1256   verifyFormat("template <class C> using A = B /*foo*/<Foo<C>, 1>;", Style);
1257   verifyFormat("template <class C> using A = B /**/ /**/<Foo<C>, 1>;", Style);
1258   verifyFormat("template <class C> using A = B<Foo</**/ C>, 1>;", Style);
1259   verifyFormat("template <class C> using A = /**/ B<Foo<C>, 1>;", Style);
1260   EXPECT_EQ(ReplacementCount, 0);
1261   verifyFormat("template <class C>\n"
1262                "using A = B // foo\n"
1263                "    <Foo<C>, 1>;",
1264                Style);
1265 }
1266 
1267 TEST_F(QualifierFixerTest, WithConstraints) {
1268   FormatStyle Style = getLLVMStyle();
1269   Style.QualifierAlignment = FormatStyle::QAS_Custom;
1270   Style.QualifierOrder = {"constexpr", "type"};
1271 
1272   verifyFormat("template <typename T>\n"
1273                "  requires Concept<F>\n"
1274                "constexpr constructor();",
1275                Style);
1276   verifyFormat("template <typename T>\n"
1277                "  requires Concept1<F> && Concept2<F>\n"
1278                "constexpr constructor();",
1279                Style);
1280 }
1281 
1282 TEST_F(QualifierFixerTest, WithCpp11Attribute) {
1283   FormatStyle Style = getLLVMStyle();
1284   Style.QualifierAlignment = FormatStyle::QAS_Custom;
1285   Style.QualifierOrder = {"static", "constexpr", "inline", "type"};
1286 
1287   verifyFormat("[[nodiscard]] static constexpr inline int func() noexcept {}",
1288                "[[nodiscard]] inline constexpr static int func() noexcept {}",
1289                Style);
1290   verifyFormat("[[maybe_unused]] static constexpr int A",
1291                "[[maybe_unused]] constexpr static int A", Style);
1292 }
1293 
1294 TEST_F(QualifierFixerTest, DisableRegions) {
1295   FormatStyle Style = getLLVMStyle();
1296   Style.QualifierAlignment = FormatStyle::QAS_Custom;
1297   Style.QualifierOrder = {"inline", "static", "const", "type"};
1298 
1299   ReplacementCount = 0;
1300   verifyFormat("// clang-format off\n"
1301                "int const inline static a = 0;\n"
1302                "// clang-format on",
1303                Style);
1304   EXPECT_EQ(ReplacementCount, 0);
1305   verifyFormat("// clang-format off\n"
1306                "int const inline static a = 0;\n"
1307                "// clang-format on\n"
1308                "inline static const int a = 0;",
1309                "// clang-format off\n"
1310                "int const inline static a = 0;\n"
1311                "// clang-format on\n"
1312                "int const inline static a = 0;",
1313                Style);
1314 }
1315 
1316 TEST_F(QualifierFixerTest, TemplatesRight) {
1317   FormatStyle Style = getLLVMStyle();
1318   Style.QualifierAlignment = FormatStyle::QAS_Custom;
1319   Style.QualifierOrder = {"type", "const"};
1320 
1321   verifyFormat("template <typename T> Foo const f();",
1322                "template <typename T> const Foo f();", Style);
1323   verifyFormat("template <typename T> int const f();",
1324                "template <typename T> const int f();", Style);
1325 
1326   verifyFormat("template <T const> t;", "template <const T> t;", Style);
1327   verifyFormat("template <typename T>\n"
1328                "  requires Concept<T const>\n"
1329                "Foo const f();",
1330                "template <typename T>\n"
1331                "  requires Concept<const T>\n"
1332                "const Foo f();",
1333                Style);
1334   verifyFormat("TemplateType<T const> t;", "TemplateType<const T> t;", Style);
1335   verifyFormat("TemplateType<Container const> t;",
1336                "TemplateType<const Container> t;", Style);
1337 }
1338 
1339 TEST_F(QualifierFixerTest, TemplatesLeft) {
1340   FormatStyle Style = getLLVMStyle();
1341   Style.QualifierAlignment = FormatStyle::QAS_Custom;
1342   Style.QualifierOrder = {"const", "volatile", "type"};
1343 
1344   verifyFormat("template <typename T> const Foo f();",
1345                "template <typename T> Foo const f();", Style);
1346   verifyFormat("template <typename T> const int f();",
1347                "template <typename T> int const f();", Style);
1348 
1349   verifyFormat("template <const T> t;", "template <T const> t;", Style);
1350   verifyFormat("template <typename T>\n"
1351                "  requires Concept<const T>\n"
1352                "const Foo f();",
1353                "template <typename T>\n"
1354                "  requires Concept<T const>\n"
1355                "Foo const f();",
1356                Style);
1357   verifyFormat("template <typename T>\n"
1358                "  requires Concept<const T>\n"
1359                "const volatile Foo f();",
1360                "template <typename T>\n"
1361                "  requires Concept<T const>\n"
1362                "volatile const Foo f();",
1363                Style);
1364   verifyFormat("TemplateType<const T> t;", "TemplateType<T const> t;", Style);
1365   verifyFormat("TemplateType<const Container> t;",
1366                "TemplateType<Container const> t;", Style);
1367 }
1368 
1369 TEST_F(QualifierFixerTest, Ranges) {
1370   FormatStyle Style = getLLVMStyle();
1371   Style.QualifierAlignment = FormatStyle::QAS_Custom;
1372   Style.QualifierOrder = {"const", "volatile", "type"};
1373 
1374   // Only the first line should be formatted; the second should remain as is.
1375   verifyFormat("template <typename T> const Foo f();\n"
1376                "template <typename T> Foo const f();",
1377                "template <typename T> Foo const f();\n"
1378                "template <typename T> Foo const f();",
1379                Style, {tooling::Range(0, 36)});
1380 
1381   // Only the middle line should be formatted; the first and last should remain
1382   // as is.
1383   verifyFormat("template <typename T> Foo const f();\n"
1384                "template <typename T> const Foo f();\n"
1385                "template <typename T> Foo const f();",
1386                "template <typename T> Foo const f();\n"
1387                "template <typename T> Foo const f();\n"
1388                "template <typename T> Foo const f();",
1389                Style, {tooling::Range(37, 36)});
1390 }
1391 
1392 } // namespace
1393 } // namespace test
1394 } // namespace format
1395 } // namespace clang
1396