1 //===- unittest/Format/QualifierFixerTest.cpp - Formatting unit tests -----===//
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
7 //===----------------------------------------------------------------------===//
9 #include "../lib/Format/QualifierAlignmentFixer.h"
10 #include "FormatTestBase.h"
11 #include "TestLexer.h"
13 #define DEBUG_TYPE "format-qualifier-fixer-test"
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!"
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!"
29 class QualifierFixerTest
: public FormatTestBase
{
31 TokenList
annotate(llvm::StringRef Code
,
32 const FormatStyle
&Style
= getLLVMStyle()) {
33 return TestLexer(Allocator
, Buffers
, Style
).annotate(Code
);
35 llvm::SpecificBumpPtrAllocator
<FormatToken
> Allocator
;
36 std::vector
<std::unique_ptr
<llvm::MemoryBuffer
>> Buffers
;
39 TEST_F(QualifierFixerTest
, RotateTokens
) {
41 EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("const"),
43 EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("volatile"),
45 EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("inline"),
47 EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("static"),
49 EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("restrict"),
51 EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("friend"),
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]",
61 std::vector
<std::string
>({"const", "volatile", "apples", "type"}));
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]",
70 std::vector
<std::string
>({"const", "volatile", "const", "type"}));
73 TEST_F(QualifierFixerTest
, FailQualifierMissingType
) {
74 FormatStyle Style
= {};
75 Style
.Language
= FormatStyle::LK_Cpp
;
76 FAIL_PARSE("QualifierAlignment: Custom\n"
77 "QualifierOrder: [const, volatile ]",
79 std::vector
<std::string
>({
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
>({}));
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
>());
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"}));
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"}));
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"};
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
);
125 verifyFormat("static const volatile int a;", "const static int volatile a;",
127 verifyFormat("inline static const volatile int a;",
128 "const static inline int volatile a;", Style
);
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;",
135 verifyFormat("const const int a;", "const int const a;", Style
);
138 "friend constexpr auto operator<=>(const foo &, const foo &) = default;",
139 "constexpr friend auto operator<=>(const foo &, const foo &) = default;",
142 "friend constexpr bool operator==(const foo &, const foo &) = default;",
143 "constexpr bool friend operator==(const foo &, const foo &) = default;",
147 TEST_F(QualifierFixerTest
, LeftRightQualifier
) {
148 FormatStyle Style
= getLLVMStyle();
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
);
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
);
172 verifyFormat("LLVM_NODISCARD const int &Foo();", Style
);
173 verifyFormat("LLVM_NODISCARD int const &Foo();", Style
);
175 verifyFormat("volatile const int *restrict;", Style
);
176 verifyFormat("const volatile int *restrict;", Style
);
177 verifyFormat("const int volatile *restrict;", Style
);
180 TEST_F(QualifierFixerTest
, RightQualifier
) {
181 FormatStyle Style
= getLLVMStyle();
182 Style
.QualifierAlignment
= FormatStyle::QAS_Right
;
183 Style
.QualifierOrder
= {"type", "const", "volatile"};
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
);
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)",
213 verifyFormat("LLVM_NODISCARD int const &Foo();", Style
);
214 verifyFormat("SourceRange getSourceRange() const override LLVM_READONLY",
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
);
228 "template <typename Func> explicit Action(Action<Func> const &action);",
231 "template <typename Func> explicit Action(Action<Func> const &action);",
232 "template <typename Func> explicit Action(const Action<Func>& action);",
235 "template <typename Func> explicit Action(Action<Func> const &action);",
236 "template <typename Func>\nexplicit Action(const Action<Func>& action);",
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",
252 verifyFormat("::Foo<int>::iterator const &a", "const ::Foo<int>::iterator &a",
255 verifyFormat("Foo(int a, "
256 "unsigned b, // c-style args\n"
259 "unsigned b, // c-style args\n"
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
);
267 verifyFormat("int const volatile *restrict;", "volatile const int *restrict;",
269 verifyFormat("int const volatile *restrict;", "const volatile int *restrict;",
271 verifyFormat("int const volatile *restrict;", "const int volatile *restrict;",
274 verifyFormat("long long int const volatile;", "const long long int volatile;",
276 verifyFormat("long long int const volatile;", "long const long int volatile;",
278 verifyFormat("long long int const volatile;", "long long volatile int const;",
280 verifyFormat("long long int const volatile;", "long volatile long const int;",
282 verifyFormat("long long int const volatile;", "const long long volatile int;",
285 verifyFormat("static int const bat;", "static const int bat;", Style
);
286 verifyFormat("static int const bat;", Style
);
288 // static is not configured, unchanged on the left of the right hand
290 verifyFormat("int static const volatile;", "volatile const int static;",
292 verifyFormat("int static const volatile;", "const volatile int static;",
294 verifyFormat("int static const volatile;", "const int volatile static;",
296 verifyFormat("Foo static const volatile;", "volatile const Foo static;",
298 verifyFormat("Foo static const volatile;", "const volatile Foo static;",
300 verifyFormat("Foo static const volatile;", "const Foo volatile static;",
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
);
308 verifyFormat("Foo<T volatile>::Bar<Type const, 5> const volatile A::*;",
309 "volatile const Foo<volatile T>::Bar<const Type, 5> A::*;",
312 verifyFormat("int const Foo<int>::bat = 0;", "const int Foo<int>::bat = 0;",
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
);
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);",
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);",
327 verifyFormat("void fn(ns::Foo<T> const &i);", "void fn(const ns::Foo<T> &i);",
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
);
338 verifyFormat("LocalScope const *Scope = nullptr;",
339 "const LocalScope* Scope = nullptr;", Style
);
340 verifyFormat("struct DOTGraphTraits<Stmt const *>",
341 "struct DOTGraphTraits<const Stmt *>", Style
);
344 "bool tools::addXRayRuntime(ToolChain const &TC, ArgList const &Args) {",
345 "bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args) {",
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
);
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
);
355 verifyFormat("Foo<Foo<int> const> P;\n#if 0\n#else\n#endif",
356 "Foo<const Foo<int>> P;\n#if 0\n#else\n#endif", Style
);
358 verifyFormat("Bar<Bar<int const> const> P;\n#if 0\n#else\n#endif",
359 "Bar<Bar<const int> const> P;\n#if 0\n#else\n#endif", Style
);
361 verifyFormat("Baz<Baz<int const> const> P;\n#if 0\n#else\n#endif",
362 "Baz<const Baz<const int>> P;\n#if 0\n#else\n#endif", Style
);
364 // verifyFormat("#if 0\nBoo<Boo<int const> const> P;\n#else\n#endif",
365 // "#if 0\nBoo<const Boo<const int>> P;\n#else\n#endif", Style);
367 verifyFormat("int const P;\n#if 0\n#else\n#endif",
368 "const int P;\n#if 0\n#else\n#endif", Style
);
370 verifyFormat("unsigned long const a;", "const unsigned long a;", Style
);
371 verifyFormat("unsigned long long const a;", "const unsigned long long a;",
374 // Multiple template parameters.
375 verifyFormat("Bar<std::Foo const, 32>", "Bar<const std::Foo, 32>", Style
);
376 // Variable declaration based on template type.
377 verifyFormat("Bar<std::Foo const> bar", "Bar<const std::Foo> bar", Style
);
379 // Using typename for a nested dependent type name.
380 verifyFormat("typename Foo::iterator const;", "const typename Foo::iterator;",
383 // Don't move past C-style struct/class.
384 verifyFormat("void foo(const struct A a);", Style
);
385 verifyFormat("void foo(const class A a);", Style
);
387 // Don't move past struct/class combined declaration and variable
389 verifyFormat("const struct {\n} var;", Style
);
390 verifyFormat("struct {\n} const var;", Style
);
391 verifyFormat("const class {\n} var;", Style
);
392 verifyFormat("class {\n} const var;", Style
);
394 // Leave left qualifers unchanged for combined declaration and variable
396 verifyFormat("volatile const class {\n} var;", Style
);
397 verifyFormat("const volatile class {\n} var;", Style
);
398 // Also do no sorting with respect to not-configured tokens.
399 verifyFormat("const static volatile class {\n} var;", Style
);
400 // Sort right qualifiers for combined declaration and variable definition.
401 verifyFormat("class {\n} const volatile var;", Style
);
402 verifyFormat("class {\n} const volatile var;",
403 "class {\n} volatile const var;", Style
);
404 // Static keyword is not configured, should end up on the left of the right
406 verifyFormat("class {\n} static const volatile var;", Style
);
407 verifyFormat("class {\n} static const volatile var;",
408 "class {\n} volatile static const var;", Style
);
410 // ::template for dependent names
411 verifyFormat("::template Foo<T> const volatile var;",
412 "const volatile ::template Foo<T> var;", Style
);
413 verifyFormat("typename ::template Foo<T> const volatile var;",
414 "const volatile typename ::template Foo<T> var;", Style
);
415 verifyFormat("typename Bar::template Foo<T>::T const;",
416 "const typename Bar::template Foo<T>::T;", Style
);
417 verifyFormat("typename Bar::template Foo<T>::T const volatile;",
418 "const volatile typename Bar::template Foo<T>::T;", Style
);
421 verifyFormat("typename ::Bar<int> const;", "const typename ::Bar<int>;",
423 // typename ::template
424 verifyFormat("typename ::template Bar<int> const;",
425 "const typename ::template Bar<int>;", Style
);
427 verifyFormat("foo<Bar<Baz> const>();", "foo<const Bar<Baz>>();", Style
);
428 verifyFormat("foo<Bar<Baz> const>();", "foo<const Bar<Baz> >();", Style
);
429 verifyFormat("Bar<32, Foo<25> const>;", "Bar<32, const Foo<25>>;", Style
);
430 verifyFormat("A<B<C<D> const> const>;", "A<const B<const C<D>>>;", Style
);
431 verifyFormat("A<B<C<D const> const> const>;", "A<const B<const C<const D>>>;",
434 // Don't move past decltype, typeof, or _Atomic.
435 verifyFormat("const decltype(foo)", Style
);
436 verifyFormat("const typeof(foo)", Style
);
437 verifyFormat("const _Atomic(foo)", Style
);
440 const int ColumnLimit
= Style
.ColumnLimit
;
441 Style
.ColumnLimit
= 200;
442 verifyFormat("/*c*/ Foo const *foo;", "const /*c*/ Foo *foo;", Style
);
443 verifyFormat("Foo const /*c*/ *foo;", "const Foo /*c*/ *foo;", Style
);
444 verifyFormat("Foo const * /*c*/ foo;", "const Foo * /*c*/ foo;", Style
);
446 verifyFormat("/*comment*/ std::vector<int> const v;",
447 "const /*comment*/ std::vector<int> v;", Style
);
448 verifyFormat("std /*comment*/ ::vector<int> const v;",
449 "const std /*comment*/ ::vector<int> v;", Style
);
450 verifyFormat("std::/*comment*/ vector<int> const v;",
451 "const std::/*comment*/ vector<int> v;", Style
);
452 verifyFormat("std::vector /*comment*/<int> const v;",
453 "const std::vector /*comment*/ <int> v;", Style
);
454 verifyFormat("std::vector</*comment*/ int> const v;",
455 "const std::vector</*comment*/ int> v;", Style
);
456 verifyFormat("std::vector<int /*comment*/> const v;",
457 "const std::vector<int /*comment*/> v;", Style
);
458 verifyFormat("std::vector<int> const /*comment*/ v;",
459 "const std::vector<int> /*comment*/ v;", Style
);
461 verifyFormat("std::vector</*comment*/ int const> v;",
462 "std::vector</*comment*/ const int> v;", Style
);
463 verifyFormat("std::vector</*comment*/ int const> v;",
464 "std::vector<const /*comment*/ int> v;", Style
);
465 verifyFormat("std::vector<int const /*comment*/> v;",
466 "std::vector<const int /*comment*/> v;", Style
);
467 verifyFormat("std::vector</*comment*/ Foo const> v;",
468 "std::vector</*comment*/ const Foo> v;", Style
);
469 verifyFormat("std::vector</*comment*/ Foo const> v;",
470 "std::vector<const /*comment*/ Foo> v;", Style
);
471 verifyFormat("std::vector<Foo const /*comment*/> v;",
472 "std::vector<const Foo /*comment*/> v;", Style
);
474 verifyFormat("typename C<T>::template B<T> const;",
475 "const typename C<T>::template B<T>;", Style
);
476 verifyFormat("/*c*/ typename C<T>::template B<T> const;",
477 "const /*c*/ typename C<T>::template B<T>;", Style
);
478 verifyFormat("typename /*c*/ C<T>::template B<T> const;",
479 "const typename /*c*/ C<T>::template B<T>;", Style
);
480 verifyFormat("typename C /*c*/<T>::template B<T> const;",
481 "const typename C /*c*/<T>::template B<T>;", Style
);
482 verifyFormat("typename C<T> /*c*/ ::template B<T> const;",
483 "const typename C<T> /*c*/ ::template B<T>;", Style
);
484 verifyFormat("typename C<T>::/*c*/ template B<T> const;",
485 "const typename C<T>::/*c*/ template B<T>;", Style
);
486 verifyFormat("typename C<T>::template /*c*/ B<T> const;",
487 "const typename C<T>::template /*c*/B<T>;", Style
);
488 verifyFormat("typename C<T>::template B<T> const /*c*/;",
489 "const typename C<T>::template B<T>/*c*/;", Style
);
491 verifyFormat("/*c*/ /*c*/ typename /*c*/ C /*c*/<T> /*c*/ ::/*c*/ template "
492 "/*c*/ B /*c*/<T> const /*c*/ v;",
493 "/*c*/ const /*c*/ typename /*c*/ C /*c*/<T> /*c*/ "
494 "::/*c*/template /*c*/ B /*c*/<T> /*c*/ v;",
497 verifyFormat("/*c*/ unsigned /*c*/ long const /*c*/ a;",
498 "const /*c*/ unsigned /*c*/ long /*c*/ a;", Style
);
499 verifyFormat("unsigned /*c*/ long /*c*/ long const a;",
500 "const unsigned /*c*/ long /*c*/ long a;", Style
);
503 verifyFormat("foo() /*c*/ const", Style
);
504 verifyFormat("const /*c*/ struct a;", Style
);
505 verifyFormat("const /*c*/ class a;", Style
);
506 verifyFormat("const /*c*/ decltype(v) a;", Style
);
507 verifyFormat("const /*c*/ typeof(v) a;", Style
);
508 verifyFormat("const /*c*/ _Atomic(v) a;", Style
);
509 verifyFormat("const decltype /*c*/ (v) a;", Style
);
510 verifyFormat("const /*c*/ class {\n} volatile /*c*/ foo = {};", Style
);
512 Style
.ColumnLimit
= ColumnLimit
;
514 // Don't adjust macros
515 verifyFormat("const INTPTR a;", Style
);
517 // Pointers to members
518 verifyFormat("int S::*a;", Style
);
519 verifyFormat("int const S::*a;", "const int S:: *a;", Style
);
520 verifyFormat("int const S::*const a;", "const int S::* const a;", Style
);
521 verifyFormat("int A::*const A::*p1;", Style
);
522 verifyFormat("float (C::*p)(int);", Style
);
523 verifyFormat("float (C::*const p)(int);", Style
);
524 verifyFormat("float (C::*p)(int) const;", Style
);
525 verifyFormat("float const (C::*p)(int);", "const float (C::*p)(int);", Style
);
528 TEST_F(QualifierFixerTest
, LeftQualifier
) {
529 FormatStyle Style
= getLLVMStyle();
530 Style
.QualifierAlignment
= FormatStyle::QAS_Left
;
531 Style
.QualifierOrder
= {"inline", "static", "const", "volatile", "type"};
533 verifyFormat("const int a;", Style
);
534 verifyFormat("const int *a;", Style
);
535 verifyFormat("const int &a;", Style
);
536 verifyFormat("const int &&a;", Style
);
537 verifyFormat("const int b;", Style
);
538 verifyFormat("const int *b;", Style
);
539 verifyFormat("const int &b;", Style
);
540 verifyFormat("const int &&b;", Style
);
541 verifyFormat("const int *const b;", Style
);
542 verifyFormat("int *const c;", Style
);
544 verifyFormat("const Foo a;", Style
);
545 verifyFormat("const Foo *a;", Style
);
546 verifyFormat("const Foo &a;", Style
);
547 verifyFormat("const Foo &&a;", Style
);
548 verifyFormat("const Foo b;", Style
);
549 verifyFormat("const Foo *b;", Style
);
550 verifyFormat("const Foo &b;", Style
);
551 verifyFormat("const Foo &&b;", Style
);
552 verifyFormat("const Foo *const b;", Style
);
553 verifyFormat("Foo *const b;", Style
);
554 verifyFormat("const Foo *const b;", Style
);
556 verifyFormat("LLVM_NODISCARD const int &Foo();", Style
);
558 verifyFormat("const char a[];", Style
);
559 verifyFormat("const auto v = get_value();", Style
);
560 verifyFormat("const long long &a;", Style
);
561 verifyFormat("const unsigned char *a;", Style
);
562 verifyFormat("const unsigned char *a;", "unsigned char const *a;", Style
);
563 verifyFormat("const Foo<int> &a", "Foo<int> const &a", Style
);
564 verifyFormat("const Foo<int>::iterator &a", "Foo<int>::iterator const &a",
566 verifyFormat("const ::Foo<int>::iterator &a", "::Foo<int>::iterator const &a",
569 verifyFormat("const int a;", "int const a;", Style
);
570 verifyFormat("const int *a;", "int const *a;", Style
);
571 verifyFormat("const int &a;", "int const &a;", Style
);
572 verifyFormat("foo(const int &a)", "foo(int const &a)", Style
);
573 verifyFormat("unsigned char *a;", Style
);
574 verifyFormat("const unsigned int &get_nu() const",
575 "unsigned int const &get_nu() const", Style
);
577 verifyFormat("const volatile int;", "volatile const int;", Style
);
578 verifyFormat("const volatile int;", Style
);
579 verifyFormat("const volatile int;", "const int volatile;", Style
);
581 verifyFormat("const volatile int *restrict;", "volatile const int *restrict;",
583 verifyFormat("const volatile int *restrict;", Style
);
584 verifyFormat("const volatile int *restrict;", "const int volatile *restrict;",
587 verifyFormat("const volatile long long int;", "volatile long long int const;",
589 verifyFormat("const volatile long long int;", "volatile long long const int;",
591 verifyFormat("const volatile long long int;", "long long volatile int const;",
593 verifyFormat("const volatile long long int;", "long volatile long int const;",
595 verifyFormat("const volatile long long int;", "const long long volatile int;",
598 verifyFormat("SourceRange getSourceRange() const override LLVM_READONLY;",
601 verifyFormat("void foo() const override;", Style
);
602 verifyFormat("void foo() const override LLVM_READONLY;", Style
);
603 verifyFormat("void foo() const final;", Style
);
604 verifyFormat("void foo() const final LLVM_READONLY;", Style
);
605 verifyFormat("void foo() const LLVM_READONLY;", Style
);
608 "template <typename Func> explicit Action(const Action<Func> &action);",
611 "template <typename Func> explicit Action(const Action<Func> &action);",
612 "template <typename Func> explicit Action(Action<Func> const &action);",
615 verifyFormat("static const int bat;", Style
);
616 verifyFormat("static const int bat;", "static int const bat;", Style
);
618 verifyFormat("static const int Foo<int>::bat = 0;", Style
);
619 verifyFormat("static const int Foo<int>::bat = 0;",
620 "static int const Foo<int>::bat = 0;", Style
);
622 verifyFormat("void fn(const Foo<T> &i);");
624 verifyFormat("const int Foo<int>::bat = 0;", Style
);
625 verifyFormat("const int Foo<int>::bat = 0;", "int const Foo<int>::bat = 0;",
627 verifyFormat("void fn(const Foo<T> &i);", "void fn( Foo<T> const &i);",
629 verifyFormat("const int Foo<int>::fn() {", "int const Foo<int>::fn() {",
631 verifyFormat("const Foo<Foo<int>> *p;", "Foo<Foo<int>> const *p;", Style
);
633 "const Foo<Foo<int>> *p = const_cast<const Foo<Foo<int>> *>(&ffi);",
634 "const Foo<Foo<int>> *p = const_cast<Foo<Foo<int>> const *>(&ffi);",
637 verifyFormat("void fn(const Foo<T> &i);", "void fn(Foo<T> const &i);", Style
);
638 verifyFormat("void fns(const ns::S &s);", "void fns(ns::S const &s);", Style
);
639 verifyFormat("void fns(const ::ns::S &s);", "void fns(::ns::S const &s);",
641 verifyFormat("void fn(const ns::Foo<T> &i);", "void fn(ns::Foo<T> const &i);",
643 verifyFormat("void fns(const ns::ns2::S &s);",
644 "void fns(ns::ns2::S const &s);", Style
);
645 verifyFormat("void fn(const ns::Foo<Bar<T>> &i);",
646 "void fn(ns::Foo<Bar<T>> const &i);", Style
);
647 verifyFormat("void fn(const ns::ns2::Foo<Bar<T>> &i);",
648 "void fn(ns::ns2::Foo<Bar<T>> const &i);", Style
);
649 verifyFormat("void fn(const ns::ns2::Foo<Bar<T, U>> &i);",
650 "void fn(ns::ns2::Foo<Bar<T, U>> const &i);", Style
);
652 verifyFormat("const auto i = 0;", "auto const i = 0;", Style
);
653 verifyFormat("const auto &ir = i;", "auto const &ir = i;", Style
);
654 verifyFormat("const auto *ip = &i;", "auto const *ip = &i;", Style
);
656 verifyFormat("Foo<const Foo<int>> P;\n#if 0\n#else\n#endif",
657 "Foo<Foo<int> const> P;\n#if 0\n#else\n#endif", Style
);
659 verifyFormat("Foo<Foo<const int>> P;\n#if 0\n#else\n#endif",
660 "Foo<Foo<int const>> P;\n#if 0\n#else\n#endif", Style
);
662 verifyFormat("const int P;\n#if 0\n#else\n#endif",
663 "int const P;\n#if 0\n#else\n#endif", Style
);
665 verifyFormat("const unsigned long a;", "unsigned long const a;", Style
);
666 verifyFormat("const unsigned long long a;", "unsigned long long const a;",
669 verifyFormat("const long long unsigned a;", "long const long unsigned a;",
672 verifyFormat("const std::Foo", Style
);
673 verifyFormat("const std::Foo<>", Style
);
674 verifyFormat("const std::Foo < int", "const std::Foo<int", Style
);
675 verifyFormat("const std::Foo<int>", Style
);
677 // Multiple template parameters.
678 verifyFormat("Bar<const std::Foo, 32>;", "Bar<std::Foo const, 32>;", Style
);
680 // Variable declaration based on template type.
681 verifyFormat("Bar<const std::Foo> bar;", "Bar<std::Foo const> bar;", Style
);
683 // Using typename for a dependent name.
684 verifyFormat("const typename Foo::iterator;", "typename Foo::iterator const;",
687 // Don't move past C-style struct/class.
688 verifyFormat("void foo(struct A const a);", Style
);
689 verifyFormat("void foo(class A const a);", Style
);
691 // Don't move past struct/class combined declaration and variable
693 verifyFormat("const struct {\n} var;", Style
);
694 verifyFormat("struct {\n} const var;", Style
);
695 verifyFormat("const class {\n} var;", Style
);
696 verifyFormat("class {\n} const var;", Style
);
698 // Sort left qualifiers for struct/class combined declaration and variable
700 verifyFormat("const volatile class {\n} var;", Style
);
701 verifyFormat("const volatile class {\n} var;",
702 "volatile const class {\n} var;", Style
);
703 // Leave right qualifers unchanged for struct/class combined declaration and
704 // variable definition.
705 verifyFormat("class {\n} const volatile var;", Style
);
706 verifyFormat("class {\n} volatile const var;", Style
);
708 verifyFormat("foo<const Bar<Baz<T>>>();", "foo<Bar<Baz<T>> const>();", Style
);
709 verifyFormat("foo<const Bar<Baz<T>>>();", "foo<Bar<Baz<T> > const>();",
711 verifyFormat("Bar<32, const Foo<25>>;", "Bar<32, Foo<25> const>;", Style
);
712 verifyFormat("A<const B<const C<D>>>;", "A<B<C<D> const> const>;", Style
);
713 verifyFormat("A<const B<const C<const D>>>;", "A<B<C<D const> const> const>;",
716 // Don't move past decltype, typeof, or _Atomic.
717 verifyFormat("decltype(foo) const", Style
);
718 verifyFormat("typeof(foo) const", Style
);
719 verifyFormat("_Atomic(foo) const", Style
);
721 // ::template for dependent names
722 verifyFormat("const volatile ::template Foo<T> var;",
723 "::template Foo<T> const volatile var;", Style
);
724 verifyFormat("const volatile typename ::template Foo<T> var;",
725 "typename ::template Foo<T> const volatile var;", Style
);
726 verifyFormat("const typename Bar::template Foo<T>::T;",
727 "typename Bar::template Foo<T>::T const;", Style
);
728 verifyFormat("const volatile typename Bar::template Foo<T>::T;",
729 "typename Bar::template Foo<T>::T const volatile;", Style
);
732 verifyFormat("const typename ::Bar<int>;", "typename ::Bar<int> const;",
734 // typename ::template
735 verifyFormat("const typename ::template Bar<int>;",
736 "typename ::template Bar<int> const;", Style
);
739 const int ColumnLimit
= Style
.ColumnLimit
;
740 Style
.ColumnLimit
= 200;
741 verifyFormat("/*c*/ const Foo *foo;", "/*c*/ Foo const *foo;", Style
);
742 verifyFormat("const Foo /*c*/ *foo;", "Foo const /*c*/ *foo;", Style
);
743 verifyFormat("const Foo * /*c*/ foo;", "Foo const * /*c*/ foo;", Style
);
745 verifyFormat("/*comment*/ const std::vector<int> v;",
746 "/*comment*/ std::vector<int> const v;", Style
);
747 verifyFormat("const std /*comment*/ ::vector<int> v;",
748 "std /*comment*/ ::vector<int> const v;", Style
);
749 verifyFormat("const std::/*comment*/ vector<int> v;",
750 "std::/*comment*/ vector<int> const v;", Style
);
751 verifyFormat("const std::vector /*comment*/<int> v;",
752 "std::vector /*comment*/<int> const v;", Style
);
753 verifyFormat("const std::vector</*comment*/ int> v;",
754 "std::vector</*comment*/ int> const v;", Style
);
755 verifyFormat("const std::vector<int /*comment*/> v;",
756 "std::vector<int /*comment*/> const v;", Style
);
757 verifyFormat("const std::vector<int> /*comment*/ v;",
758 "std::vector<int> /*comment*/ const v;", Style
);
760 verifyFormat("std::vector</*comment*/ const int> v;",
761 "std::vector</*comment*/ int const> v;", Style
);
762 verifyFormat("std::vector<const int /*comment*/> v;",
763 "std::vector<int /*comment*/ const> v;", Style
);
764 verifyFormat("std::vector<const int /*comment*/> v;",
765 "std::vector<int const /*comment*/> v;", Style
);
766 verifyFormat("std::vector</*comment*/ const Foo> v;",
767 "std::vector</*comment*/ Foo const> v;", Style
);
768 verifyFormat("std::vector<const Foo /*comment*/> v;",
769 "std::vector<Foo /*comment*/ const> v;", Style
);
770 verifyFormat("std::vector<const Foo /*comment*/> v;",
771 "std::vector<Foo const /*comment*/> v;", Style
);
773 verifyFormat("const typename C<T>::template B<T>;",
774 "typename C<T>::template B<T> const;", Style
);
775 verifyFormat("/*c*/ const typename C<T>::template B<T>;",
776 "/*c*/ typename C<T>::template B<T> const;", Style
);
777 verifyFormat("const typename /*c*/ C<T>::template B<T>;",
778 "typename /*c*/ C<T>::template B<T> const;", Style
);
779 verifyFormat("const typename C /*c*/<T>::template B<T>;",
780 "typename C /*c*/<T>::template B<T> const;", Style
);
781 verifyFormat("const typename C<T> /*c*/ ::template B<T>;",
782 "typename C<T> /*c*/ ::template B<T> const;", Style
);
783 verifyFormat("const typename C<T>::/*c*/ template B<T>;",
784 "typename C<T>::/*c*/ template B<T> const;", Style
);
785 verifyFormat("const typename C<T>::template /*c*/ B<T>;",
786 "typename C<T>::template /*c*/ B<T> const;", Style
);
787 verifyFormat("const typename C<T>::template B<T> /*c*/;",
788 "typename C<T>::template B<T> /*c*/ const;", Style
);
790 verifyFormat("/*c*/ const typename /*c*/ C /*c*/<T> /*c*/ ::/*c*/ template "
791 "/*c*/ B /*c*/<T> /*c*/ v;",
792 "/*c*/ typename /*c*/ C /*c*/<T> /*c*/ ::/*c*/ template /*c*/ B "
793 "/*c*/<T> /*c*/ const v;",
796 verifyFormat("const unsigned /*c*/ long /*c*/ a;",
797 "unsigned /*c*/ long /*c*/ const a;", Style
);
798 verifyFormat("const unsigned /*c*/ long /*c*/ long a;",
799 "unsigned /*c*/ long /*c*/ long const a;", Style
);
802 verifyFormat("foo() /*c*/ const", Style
);
803 verifyFormat("struct /*c*/ const a;", Style
);
804 verifyFormat("class /*c*/ const a;", Style
);
805 verifyFormat("decltype(v) /*c*/ const a;", Style
);
806 verifyFormat("typeof(v) /*c*/ const a;", Style
);
807 verifyFormat("_Atomic(v) /*c*/ const a;", Style
);
808 verifyFormat("decltype /*c*/ (v) const a;", Style
);
809 verifyFormat("const /*c*/ class {\n} /*c*/ volatile /*c*/ foo = {};", Style
);
811 Style
.ColumnLimit
= ColumnLimit
;
813 // Don't adjust macros
814 verifyFormat("INTPTR const a;", Style
);
816 // Pointers to members
817 verifyFormat("int S::*a;", Style
);
818 verifyFormat("const int S::*a;", "int const S:: *a;", Style
);
819 verifyFormat("const int S::*const a;", "int const S::* const a;", Style
);
820 verifyFormat("int A::*const A::*p1;", Style
);
821 verifyFormat("float (C::*p)(int);", Style
);
822 verifyFormat("float (C::*const p)(int);", Style
);
823 verifyFormat("float (C::*p)(int) const;", Style
);
824 verifyFormat("const float (C::*p)(int);", "float const (C::*p)(int);", Style
);
827 TEST_F(QualifierFixerTest
, ConstVolatileQualifiersOrder
) {
828 FormatStyle Style
= getLLVMStyle();
829 Style
.QualifierAlignment
= FormatStyle::QAS_Left
;
830 Style
.QualifierOrder
= {"inline", "static", "const", "volatile", "type"};
833 EXPECT_EQ(Style
.QualifierOrder
.size(), (size_t)5);
835 verifyFormat("const volatile int a;", Style
);
836 verifyFormat("const volatile int a;", "volatile const int a;", Style
);
837 verifyFormat("const volatile int a;", "int const volatile a;", Style
);
838 verifyFormat("const volatile int a;", "int volatile const a;", Style
);
839 verifyFormat("const volatile int a;", "const int volatile a;", Style
);
841 verifyFormat("const volatile Foo a;", Style
);
842 verifyFormat("const volatile Foo a;", "volatile const Foo a;", Style
);
843 verifyFormat("const volatile Foo a;", "Foo const volatile a;", Style
);
844 verifyFormat("const volatile Foo a;", "Foo volatile const a;", Style
);
845 verifyFormat("const volatile Foo a;", "const Foo volatile a;", Style
);
847 Style
.QualifierAlignment
= FormatStyle::QAS_Right
;
848 Style
.QualifierOrder
= {"type", "const", "volatile"};
850 verifyFormat("int const volatile a;", "const volatile int a;", Style
);
851 verifyFormat("int const volatile a;", "volatile const int a;", Style
);
852 verifyFormat("int const volatile a;", Style
);
853 verifyFormat("int const volatile a;", "int volatile const a;", Style
);
854 verifyFormat("int const volatile a;", "const int volatile a;", Style
);
856 verifyFormat("Foo const volatile a;", "const volatile Foo a;", Style
);
857 verifyFormat("Foo const volatile a;", "volatile const Foo a;", Style
);
858 verifyFormat("Foo const volatile a;", Style
);
859 verifyFormat("Foo const volatile a;", "Foo volatile const a;", Style
);
860 verifyFormat("Foo const volatile a;", "const Foo volatile a;", Style
);
862 Style
.QualifierAlignment
= FormatStyle::QAS_Left
;
863 Style
.QualifierOrder
= {"volatile", "const", "type"};
865 verifyFormat("volatile const int a;", "const volatile int a;", Style
);
866 verifyFormat("volatile const int a;", Style
);
867 verifyFormat("volatile const int a;", "int const volatile a;", Style
);
868 verifyFormat("volatile const int a;", "int volatile const a;", Style
);
869 verifyFormat("volatile const int a;", "const int volatile a;", Style
);
871 verifyFormat("volatile const Foo a;", "const volatile Foo a;", Style
);
872 verifyFormat("volatile const Foo a;", Style
);
873 verifyFormat("volatile const Foo a;", "Foo const volatile a;", Style
);
874 verifyFormat("volatile const Foo a;", "Foo volatile const a;", Style
);
875 verifyFormat("volatile const Foo a;", "const Foo volatile a;", Style
);
877 Style
.QualifierAlignment
= FormatStyle::QAS_Right
;
878 Style
.QualifierOrder
= {"type", "volatile", "const"};
880 verifyFormat("int volatile const a;", "const volatile int a;", Style
);
881 verifyFormat("int volatile const a;", "volatile const int a;", Style
);
882 verifyFormat("int volatile const a;", "int const volatile a;", Style
);
883 verifyFormat("int volatile const a;", Style
);
884 verifyFormat("int volatile const a;", "const int volatile a;", Style
);
886 verifyFormat("Foo volatile const a;", "const volatile Foo a;", Style
);
887 verifyFormat("Foo volatile const a;", "volatile const Foo a;", Style
);
888 verifyFormat("Foo volatile const a;", "Foo const volatile a;", Style
);
889 verifyFormat("Foo volatile const a;", Style
);
890 verifyFormat("Foo volatile const a;", "const Foo volatile a;", Style
);
892 Style
.QualifierAlignment
= FormatStyle::QAS_Custom
;
893 Style
.QualifierOrder
= {"type", "volatile", "const"};
895 verifyFormat("int volatile const a;", "const volatile int a;", Style
);
896 verifyFormat("int volatile const a;", "volatile const int a;", Style
);
897 verifyFormat("int volatile const a;", "int const volatile a;", Style
);
898 verifyFormat("int volatile const a;", Style
);
899 verifyFormat("int volatile const a;", "const int volatile a;", Style
);
901 verifyFormat("Foo volatile const a;", "const volatile Foo a;", Style
);
902 verifyFormat("Foo volatile const a;", "volatile const Foo a;", Style
);
903 verifyFormat("Foo volatile const a;", "Foo const volatile a;", Style
);
904 verifyFormat("Foo volatile const a;", Style
);
905 verifyFormat("Foo volatile const a;", "const Foo volatile a;", Style
);
908 TEST_F(QualifierFixerTest
, InlineStatics
) {
909 FormatStyle Style
= getLLVMStyle();
910 Style
.QualifierAlignment
= FormatStyle::QAS_Left
;
911 Style
.QualifierOrder
= {"inline", "static", "const", "volatile", "type"};
912 EXPECT_EQ(Style
.QualifierOrder
.size(), (size_t)5);
914 verifyFormat("inline static const volatile int a;",
915 "const inline static volatile int a;", Style
);
916 verifyFormat("inline static const volatile int a;",
917 "volatile inline static const int a;", Style
);
918 verifyFormat("inline static const volatile int a;",
919 "int const inline static volatile a;", Style
);
920 verifyFormat("inline static const volatile int a;",
921 "int volatile inline static const a;", Style
);
922 verifyFormat("inline static const volatile int a;",
923 "const int inline static volatile a;", Style
);
926 TEST_F(QualifierFixerTest
, AmpEqual
) {
927 FormatStyle Style
= getLLVMStyle();
928 Style
.QualifierAlignment
= FormatStyle::QAS_Custom
;
929 Style
.QualifierOrder
= {"static", "type", "const"};
930 EXPECT_EQ(Style
.QualifierOrder
.size(), (size_t)3);
932 verifyFormat("foo(std::string const & = std::string()) const",
933 "foo(const std::string & = std::string()) const", Style
);
934 verifyFormat("foo(std::string const & = std::string())",
935 "foo(const std::string & = std::string())", Style
);
938 TEST_F(QualifierFixerTest
, MoveConstBeyondTypeSmall
) {
940 FormatStyle Style
= getLLVMStyle();
941 Style
.QualifierAlignment
= FormatStyle::QAS_Custom
;
942 Style
.QualifierOrder
= {"type", "const"};
943 EXPECT_EQ(Style
.QualifierOrder
.size(), (size_t)2);
945 verifyFormat("int const a;", "const int a;", Style
);
946 verifyFormat("int const *a;", "const int*a;", Style
);
947 verifyFormat("int const *a;", "const int *a;", Style
);
948 verifyFormat("int const &a;", "const int &a;", Style
);
949 verifyFormat("int const &&a;", "const int &&a;", Style
);
952 TEST_F(QualifierFixerTest
, MoveConstBeforeTypeSmall
) {
954 FormatStyle Style
= getLLVMStyle();
955 Style
.QualifierAlignment
= FormatStyle::QAS_Custom
;
956 Style
.QualifierOrder
= {"const", "type"};
957 EXPECT_EQ(Style
.QualifierOrder
.size(), (size_t)2);
959 verifyFormat("const int a;", "int const a;", Style
);
960 verifyFormat("const int *a;", "int const *a;", Style
);
961 verifyFormat("const int *const a;", "int const *const a;", Style
);
963 verifyFormat("const int a = foo();", "int const a = foo();", Style
);
964 verifyFormat("const int *a = foo();", "int const *a = foo();", Style
);
965 verifyFormat("const int *const a = foo();", "int const *const a = foo();",
968 verifyFormat("const auto a = foo();", "auto const a = foo();", Style
);
969 verifyFormat("const auto *a = foo();", "auto const *a = foo();", Style
);
970 verifyFormat("const auto *const a = foo();", "auto const *const a = foo();",
974 TEST_F(QualifierFixerTest
, MoveConstBeyondType
) {
976 FormatStyle Style
= getLLVMStyle();
977 Style
.QualifierAlignment
= FormatStyle::QAS_Custom
;
978 Style
.QualifierOrder
= {"static", "inline", "type", "const", "volatile"};
979 EXPECT_EQ(Style
.QualifierOrder
.size(), (size_t)5);
981 verifyFormat("static inline int const volatile a;",
982 "const inline static volatile int a;", Style
);
983 verifyFormat("static inline int const volatile a;",
984 "volatile inline static const int a;", Style
);
985 verifyFormat("static inline int const volatile a;",
986 "int const inline static volatile a;", Style
);
987 verifyFormat("static inline int const volatile a;",
988 "int volatile inline static const a;", Style
);
989 verifyFormat("static inline int const volatile a;",
990 "const int inline static volatile a;", Style
);
992 verifyFormat("static inline int const volatile *const a;",
993 "const int inline static volatile *const a;", Style
);
995 verifyFormat("static inline Foo const volatile a;",
996 "const inline static volatile Foo a;", Style
);
997 verifyFormat("static inline Foo const volatile a;",
998 "volatile inline static const Foo a;", Style
);
999 verifyFormat("static inline Foo const volatile a;",
1000 "Foo const inline static volatile a;", Style
);
1001 verifyFormat("static inline Foo const volatile a;",
1002 "Foo volatile inline static const a;", Style
);
1003 verifyFormat("static inline Foo const volatile a;",
1004 "const Foo inline static volatile a;", Style
);
1006 verifyFormat("static inline Foo const volatile *const a;",
1007 "const Foo inline static volatile *const a;", Style
);
1010 TEST_F(QualifierFixerTest
, PrepareLeftRightOrdering
) {
1011 FormatStyle Style
= getLLVMStyle();
1012 Style
.QualifierAlignment
= FormatStyle::QAS_Custom
;
1013 Style
.QualifierOrder
= {"static", "inline", "type", "const", "volatile"};
1015 std::vector
<std::string
> Left
;
1016 std::vector
<std::string
> Right
;
1017 std::vector
<tok::TokenKind
> ConfiguredTokens
;
1018 prepareLeftRightOrderingForQualifierAlignmentFixer(Style
.QualifierOrder
, Left
,
1019 Right
, ConfiguredTokens
);
1021 EXPECT_EQ(Left
.size(), (size_t)2);
1022 EXPECT_EQ(Right
.size(), (size_t)2);
1024 std::vector
<std::string
> LeftResult
= {"inline", "static"};
1025 std::vector
<std::string
> RightResult
= {"const", "volatile"};
1026 EXPECT_EQ(Left
, LeftResult
);
1027 EXPECT_EQ(Right
, RightResult
);
1030 TEST_F(QualifierFixerTest
, IsQualifierType
) {
1032 std::vector
<tok::TokenKind
> ConfiguredTokens
;
1033 ConfiguredTokens
.push_back(tok::kw_const
);
1034 ConfiguredTokens
.push_back(tok::kw_static
);
1035 ConfiguredTokens
.push_back(tok::kw_inline
);
1036 ConfiguredTokens
.push_back(tok::kw_restrict
);
1037 ConfiguredTokens
.push_back(tok::kw_constexpr
);
1038 ConfiguredTokens
.push_back(tok::kw_friend
);
1040 auto Tokens
= annotate(
1041 "const static inline auto restrict int double long constexpr friend");
1043 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1044 Tokens
[0], ConfiguredTokens
));
1045 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1046 Tokens
[1], ConfiguredTokens
));
1047 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1048 Tokens
[2], ConfiguredTokens
));
1049 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1050 Tokens
[3], ConfiguredTokens
));
1051 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1052 Tokens
[4], ConfiguredTokens
));
1053 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1054 Tokens
[5], ConfiguredTokens
));
1055 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1056 Tokens
[6], ConfiguredTokens
));
1057 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1058 Tokens
[7], ConfiguredTokens
));
1059 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1060 Tokens
[8], ConfiguredTokens
));
1061 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1062 Tokens
[9], ConfiguredTokens
));
1064 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens
[0]));
1065 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens
[1]));
1066 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens
[2]));
1067 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens
[3]));
1068 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens
[4]));
1069 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens
[5]));
1070 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens
[6]));
1071 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens
[7]));
1072 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens
[8]));
1073 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens
[9]));
1075 auto NotTokens
= annotate("for while do Foo Bar ");
1077 EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1078 NotTokens
[0], ConfiguredTokens
));
1079 EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1080 NotTokens
[1], ConfiguredTokens
));
1081 EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1082 NotTokens
[2], ConfiguredTokens
));
1083 EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1084 NotTokens
[3], ConfiguredTokens
));
1085 EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1086 NotTokens
[4], ConfiguredTokens
));
1087 EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1088 NotTokens
[5], ConfiguredTokens
));
1091 LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens
[0]));
1093 LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens
[1]));
1095 LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens
[2]));
1097 LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens
[3]));
1099 LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens
[4]));
1101 LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens
[5]));
1104 TEST_F(QualifierFixerTest
, IsMacro
) {
1106 auto Tokens
= annotate("INT INTPR Foo int");
1108 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isPossibleMacro(Tokens
[0]));
1109 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isPossibleMacro(Tokens
[1]));
1110 EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isPossibleMacro(Tokens
[2]));
1111 EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isPossibleMacro(Tokens
[3]));
1114 TEST_F(QualifierFixerTest
, OverlappingQualifier
) {
1116 FormatStyle Style
= getLLVMStyle();
1117 Style
.QualifierAlignment
= FormatStyle::QAS_Left
;
1118 Style
.QualifierOrder
= {"const", "type"};
1120 verifyFormat("Foo(const Bar &name);", "Foo(Bar const &name);", Style
);
1123 TEST_F(QualifierFixerTest
, DontPushQualifierThroughNonSpecifiedTypes
) {
1125 FormatStyle Style
= getLLVMStyle();
1126 Style
.QualifierAlignment
= FormatStyle::QAS_Left
;
1127 Style
.QualifierOrder
= {"const", "volatile", "type"};
1129 verifyFormat("inline static const int a;", Style
);
1131 Style
.QualifierOrder
= {"static", "const", "type"};
1133 verifyFormat("inline static const int a;", Style
);
1134 verifyFormat("static inline const int a;", Style
);
1136 verifyFormat("static const int a;", "const static int a;", Style
);
1138 Style
.QualifierOrder
= {"const", "volatile", "type"};
1139 // static is not configured, unchanged at right hand qualifiers.
1140 verifyFormat("const volatile int static;", "int volatile static const;",
1142 verifyFormat("const volatile int static;", "int const static volatile;",
1144 verifyFormat("const volatile int static;", "const int static volatile;",
1146 verifyFormat("const volatile Foo static;", "Foo volatile static const;",
1148 verifyFormat("const volatile Foo static;", "Foo const static volatile;",
1150 verifyFormat("const volatile Foo static;", "const Foo static volatile;",
1153 verifyFormat("inline static const Foo;", "inline static Foo const;", Style
);
1154 verifyFormat("inline static const Foo;", Style
);
1156 // Don't move qualifiers to the right for aestethics only.
1157 verifyFormat("inline const static Foo;", Style
);
1158 verifyFormat("const inline static Foo;", Style
);
1161 TEST_F(QualifierFixerTest
, UnsignedQualifier
) {
1163 FormatStyle Style
= getLLVMStyle();
1164 Style
.QualifierAlignment
= FormatStyle::QAS_Left
;
1165 Style
.QualifierOrder
= {"const", "type"};
1167 verifyFormat("Foo(const unsigned char *bytes)",
1168 "Foo(unsigned const char *bytes)", Style
);
1170 Style
.QualifierAlignment
= FormatStyle::QAS_Right
;
1171 Style
.QualifierOrder
= {"type", "const"};
1173 verifyFormat("Foo(unsigned char const *bytes)",
1174 "Foo(unsigned const char *bytes)", Style
);
1177 TEST_F(QualifierFixerTest
, NoOpQualifierReplacements
) {
1179 FormatStyle Style
= getLLVMStyle();
1180 Style
.QualifierAlignment
= FormatStyle::QAS_Custom
;
1181 Style
.QualifierOrder
= {"static", "const", "type"};
1183 verifyFormat("static const uint32 foo[] = {0, 31};", Style
);
1184 EXPECT_EQ(ReplacementCount
, 0);
1186 verifyFormat("#define MACRO static const", Style
);
1187 EXPECT_EQ(ReplacementCount
, 0);
1189 verifyFormat("using sc = static const", Style
);
1190 EXPECT_EQ(ReplacementCount
, 0);
1193 TEST_F(QualifierFixerTest
, QualifierTemplates
) {
1194 FormatStyle Style
= getLLVMStyle();
1195 Style
.QualifierAlignment
= FormatStyle::QAS_Custom
;
1196 Style
.QualifierOrder
= {"static", "const", "type"};
1198 ReplacementCount
= 0;
1199 EXPECT_EQ(ReplacementCount
, 0);
1200 verifyFormat("using A = B<>;", Style
);
1201 verifyFormat("using A = B /**/<>;", Style
);
1202 verifyFormat("template <class C> using A = B<Foo<C>, 1>;", Style
);
1203 verifyFormat("template <class C> using A = B /**/<Foo<C>, 1>;", Style
);
1204 verifyFormat("template <class C> using A = B /* */<Foo<C>, 1>;", Style
);
1205 verifyFormat("template <class C> using A = B /*foo*/<Foo<C>, 1>;", Style
);
1206 verifyFormat("template <class C> using A = B /**/ /**/<Foo<C>, 1>;", Style
);
1207 verifyFormat("template <class C> using A = B<Foo</**/ C>, 1>;", Style
);
1208 verifyFormat("template <class C> using A = /**/ B<Foo<C>, 1>;", Style
);
1209 EXPECT_EQ(ReplacementCount
, 0);
1210 verifyFormat("template <class C>\n"
1211 "using A = B // foo\n"
1215 ReplacementCount
= 0;
1216 Style
.QualifierOrder
= {"type", "static", "const"};
1217 verifyFormat("using A = B<>;", Style
);
1218 verifyFormat("using A = B /**/<>;", Style
);
1219 verifyFormat("template <class C> using A = B<Foo<C>, 1>;", Style
);
1220 verifyFormat("template <class C> using A = B /**/<Foo<C>, 1>;", Style
);
1221 verifyFormat("template <class C> using A = B /* */<Foo<C>, 1>;", Style
);
1222 verifyFormat("template <class C> using A = B /*foo*/<Foo<C>, 1>;", Style
);
1223 verifyFormat("template <class C> using A = B /**/ /**/<Foo<C>, 1>;", Style
);
1224 verifyFormat("template <class C> using A = B<Foo</**/ C>, 1>;", Style
);
1225 verifyFormat("template <class C> using A = /**/ B<Foo<C>, 1>;", Style
);
1226 EXPECT_EQ(ReplacementCount
, 0);
1227 verifyFormat("template <class C>\n"
1228 "using A = B // foo\n"
1233 TEST_F(QualifierFixerTest
, WithConstraints
) {
1234 FormatStyle Style
= getLLVMStyle();
1235 Style
.QualifierAlignment
= FormatStyle::QAS_Custom
;
1236 Style
.QualifierOrder
= {"constexpr", "type"};
1238 verifyFormat("template <typename T>\n"
1239 " requires Concept<F>\n"
1240 "constexpr constructor();",
1242 verifyFormat("template <typename T>\n"
1243 " requires Concept1<F> && Concept2<F>\n"
1244 "constexpr constructor();",
1248 TEST_F(QualifierFixerTest
, DisableRegions
) {
1249 FormatStyle Style
= getLLVMStyle();
1250 Style
.QualifierAlignment
= FormatStyle::QAS_Custom
;
1251 Style
.QualifierOrder
= {"inline", "static", "const", "type"};
1253 ReplacementCount
= 0;
1254 verifyFormat("// clang-format off\n"
1255 "int const inline static a = 0;\n"
1256 "// clang-format on",
1258 EXPECT_EQ(ReplacementCount
, 0);
1259 verifyFormat("// clang-format off\n"
1260 "int const inline static a = 0;\n"
1261 "// clang-format on\n"
1262 "inline static const int a = 0;",
1263 "// clang-format off\n"
1264 "int const inline static a = 0;\n"
1265 "// clang-format on\n"
1266 "int const inline static a = 0;",
1270 TEST_F(QualifierFixerTest
, TemplatesRight
) {
1271 FormatStyle Style
= getLLVMStyle();
1272 Style
.QualifierAlignment
= FormatStyle::QAS_Custom
;
1273 Style
.QualifierOrder
= {"type", "const"};
1275 verifyFormat("template <typename T> Foo const f();",
1276 "template <typename T> const Foo f();", Style
);
1277 verifyFormat("template <typename T> int const f();",
1278 "template <typename T> const int f();", Style
);
1280 verifyFormat("template <T const> t;", "template <const T> t;", Style
);
1281 verifyFormat("template <typename T>\n"
1282 " requires Concept<T const>\n"
1284 "template <typename T>\n"
1285 " requires Concept<const T>\n"
1288 verifyFormat("TemplateType<T const> t;", "TemplateType<const T> t;", Style
);
1289 verifyFormat("TemplateType<Container const> t;",
1290 "TemplateType<const Container> t;", Style
);
1293 TEST_F(QualifierFixerTest
, TemplatesLeft
) {
1294 FormatStyle Style
= getLLVMStyle();
1295 Style
.QualifierAlignment
= FormatStyle::QAS_Custom
;
1296 Style
.QualifierOrder
= {"const", "volatile", "type"};
1298 verifyFormat("template <typename T> const Foo f();",
1299 "template <typename T> Foo const f();", Style
);
1300 verifyFormat("template <typename T> const int f();",
1301 "template <typename T> int const f();", Style
);
1303 verifyFormat("template <const T> t;", "template <T const> t;", Style
);
1304 verifyFormat("template <typename T>\n"
1305 " requires Concept<const T>\n"
1307 "template <typename T>\n"
1308 " requires Concept<T const>\n"
1311 verifyFormat("template <typename T>\n"
1312 " requires Concept<const T>\n"
1313 "const volatile Foo f();",
1314 "template <typename T>\n"
1315 " requires Concept<T const>\n"
1316 "volatile const Foo f();",
1318 verifyFormat("TemplateType<const T> t;", "TemplateType<T const> t;", Style
);
1319 verifyFormat("TemplateType<const Container> t;",
1320 "TemplateType<Container const> t;", Style
);
1323 TEST_F(QualifierFixerTest
, Ranges
) {
1324 FormatStyle Style
= getLLVMStyle();
1325 Style
.QualifierAlignment
= FormatStyle::QAS_Custom
;
1326 Style
.QualifierOrder
= {"const", "volatile", "type"};
1328 // Only the first line should be formatted; the second should remain as is.
1329 verifyFormat("template <typename T> const Foo f();\n"
1330 "template <typename T> Foo const f();",
1331 "template <typename T> Foo const f();\n"
1332 "template <typename T> Foo const f();",
1333 Style
, {tooling::Range(0, 36)});
1335 // Only the middle line should be formatted; the first and last should remain
1337 verifyFormat("template <typename T> Foo const f();\n"
1338 "template <typename T> const Foo f();\n"
1339 "template <typename T> Foo const f();",
1340 "template <typename T> Foo const f();\n"
1341 "template <typename T> Foo const f();\n"
1342 "template <typename T> Foo const f();",
1343 Style
, {tooling::Range(37, 36)});
1348 } // namespace format
1349 } // namespace clang