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
15namespace clang {
16namespace format {
17namespace test {
18namespace {
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
29class QualifierFixerTest : public FormatTestBase {
30protected:
31 TokenList annotate(llvm::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
39TEST_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
55TEST_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
64TEST_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
73TEST_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
85TEST_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
92TEST_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
99TEST_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
106TEST_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
113TEST_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
147TEST_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
180TEST_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);",
534 Style);
535}
536
537TEST_F(QualifierFixerTest, LeftQualifier) {
538 FormatStyle Style = getLLVMStyle();
539 Style.QualifierAlignment = FormatStyle::QAS_Left;
540 Style.QualifierOrder = {"inline", "static", "const", "volatile", "type"};
541
542 verifyFormat("const int a;", Style);
543 verifyFormat("const int *a;", Style);
544 verifyFormat("const int &a;", Style);
545 verifyFormat("const int &&a;", Style);
546 verifyFormat("const int b;", Style);
547 verifyFormat("const int *b;", Style);
548 verifyFormat("const int &b;", Style);
549 verifyFormat("const int &&b;", Style);
550 verifyFormat("const int *const b;", Style);
551 verifyFormat("int *const c;", Style);
552
553 verifyFormat("const Foo a;", Style);
554 verifyFormat("const Foo *a;", Style);
555 verifyFormat("const Foo &a;", Style);
556 verifyFormat("const Foo &&a;", Style);
557 verifyFormat("const Foo b;", Style);
558 verifyFormat("const Foo *b;", Style);
559 verifyFormat("const Foo &b;", Style);
560 verifyFormat("const Foo &&b;", Style);
561 verifyFormat("const Foo *const b;", Style);
562 verifyFormat("Foo *const b;", Style);
563 verifyFormat("const Foo *const b;", Style);
564
565 verifyFormat("LLVM_NODISCARD const int &Foo();", Style);
566
567 verifyFormat("const char a[];", Style);
568 verifyFormat("const auto v = get_value();", Style);
569 verifyFormat("const long long &a;", Style);
570 verifyFormat("const unsigned char *a;", Style);
571 verifyFormat("const unsigned char *a;", "unsigned char const *a;", Style);
572 verifyFormat("const Foo<int> &a", "Foo<int> const &a", Style);
573 verifyFormat("const Foo<int>::iterator &a", "Foo<int>::iterator const &a",
574 Style);
575 verifyFormat("const ::Foo<int>::iterator &a", "::Foo<int>::iterator const &a",
576 Style);
577
578 verifyFormat("const int a;", "int const a;", Style);
579 verifyFormat("const int *a;", "int const *a;", Style);
580 verifyFormat("const int &a;", "int const &a;", Style);
581 verifyFormat("foo(const int &a)", "foo(int const &a)", Style);
582 verifyFormat("unsigned char *a;", Style);
583 verifyFormat("const unsigned int &get_nu() const",
584 "unsigned int const &get_nu() const", Style);
585
586 verifyFormat("const volatile int;", "volatile const int;", Style);
587 verifyFormat("const volatile int;", Style);
588 verifyFormat("const volatile int;", "const int volatile;", Style);
589
590 verifyFormat("const volatile int *restrict;", "volatile const int *restrict;",
591 Style);
592 verifyFormat("const volatile int *restrict;", Style);
593 verifyFormat("const volatile int *restrict;", "const int volatile *restrict;",
594 Style);
595
596 verifyFormat("const volatile long long int;", "volatile long long int const;",
597 Style);
598 verifyFormat("const volatile long long int;", "volatile long long const int;",
599 Style);
600 verifyFormat("const volatile long long int;", "long long volatile int const;",
601 Style);
602 verifyFormat("const volatile long long int;", "long volatile long int const;",
603 Style);
604 verifyFormat("const volatile long long int;", "const long long volatile int;",
605 Style);
606
607 verifyFormat("SourceRange getSourceRange() const override LLVM_READONLY;",
608 Style);
609
610 verifyFormat("void foo() const override;", Style);
611 verifyFormat("void foo() const override LLVM_READONLY;", Style);
612 verifyFormat("void foo() const final;", Style);
613 verifyFormat("void foo() const final LLVM_READONLY;", Style);
614 verifyFormat("void foo() const LLVM_READONLY;", Style);
615
616 verifyFormat(
617 "template <typename Func> explicit Action(const Action<Func> &action);",
618 Style);
619 verifyFormat(
620 "template <typename Func> explicit Action(const Action<Func> &action);",
621 "template <typename Func> explicit Action(Action<Func> const &action);",
622 Style);
623
624 verifyFormat("static const int bat;", Style);
625 verifyFormat("static const int bat;", "static int const bat;", Style);
626
627 verifyFormat("static const int Foo<int>::bat = 0;", Style);
628 verifyFormat("static const int Foo<int>::bat = 0;",
629 "static int const Foo<int>::bat = 0;", Style);
630
631 verifyFormat("void fn(const Foo<T> &i);");
632
633 verifyFormat("const int Foo<int>::bat = 0;", Style);
634 verifyFormat("const int Foo<int>::bat = 0;", "int const Foo<int>::bat = 0;",
635 Style);
636 verifyFormat("void fn(const Foo<T> &i);", "void fn( Foo<T> const &i);",
637 Style);
638 verifyFormat("const int Foo<int>::fn() {", "int const Foo<int>::fn() {",
639 Style);
640 verifyFormat("const Foo<Foo<int>> *p;", "Foo<Foo<int>> const *p;", Style);
641 verifyFormat(
642 "const Foo<Foo<int>> *p = const_cast<const Foo<Foo<int>> *>(&ffi);",
643 "const Foo<Foo<int>> *p = const_cast<Foo<Foo<int>> const *>(&ffi);",
644 Style);
645
646 verifyFormat("void fn(const Foo<T> &i);", "void fn(Foo<T> const &i);", Style);
647 verifyFormat("void fns(const ns::S &s);", "void fns(ns::S const &s);", Style);
648 verifyFormat("void fns(const ::ns::S &s);", "void fns(::ns::S const &s);",
649 Style);
650 verifyFormat("void fn(const ns::Foo<T> &i);", "void fn(ns::Foo<T> const &i);",
651 Style);
652 verifyFormat("void fns(const ns::ns2::S &s);",
653 "void fns(ns::ns2::S const &s);", Style);
654 verifyFormat("void fn(const ns::Foo<Bar<T>> &i);",
655 "void fn(ns::Foo<Bar<T>> const &i);", Style);
656 verifyFormat("void fn(const ns::ns2::Foo<Bar<T>> &i);",
657 "void fn(ns::ns2::Foo<Bar<T>> const &i);", Style);
658 verifyFormat("void fn(const ns::ns2::Foo<Bar<T, U>> &i);",
659 "void fn(ns::ns2::Foo<Bar<T, U>> const &i);", Style);
660
661 verifyFormat("const auto i = 0;", "auto const i = 0;", Style);
662 verifyFormat("const auto &ir = i;", "auto const &ir = i;", Style);
663 verifyFormat("const auto *ip = &i;", "auto const *ip = &i;", Style);
664
665 verifyFormat("void f(const Concept auto &x);",
666 "void f(Concept auto const &x);", Style);
667 verifyFormat("void f(const std::integral auto &x);",
668 "void f(std::integral auto const &x);", Style);
669
670 verifyFormat("auto lambda = [] { const int i = 0; };",
671 "auto lambda = [] { int const i = 0; };", Style);
672
673 verifyFormat("Foo<const Foo<int>> P;\n#if 0\n#else\n#endif",
674 "Foo<Foo<int> const> P;\n#if 0\n#else\n#endif", Style);
675
676 verifyFormat("Foo<Foo<const int>> P;\n#if 0\n#else\n#endif",
677 "Foo<Foo<int const>> P;\n#if 0\n#else\n#endif", Style);
678
679 verifyFormat("const int P;\n#if 0\n#else\n#endif",
680 "int const P;\n#if 0\n#else\n#endif", Style);
681
682 verifyFormat("const unsigned long a;", "unsigned long const a;", Style);
683 verifyFormat("const unsigned long long a;", "unsigned long long const a;",
684 Style);
685
686 verifyFormat("const long long unsigned a;", "long const long unsigned a;",
687 Style);
688
689 verifyFormat("const std::Foo", Style);
690 verifyFormat("const std::Foo<>", Style);
691 verifyFormat("const std::Foo < int", "const std::Foo<int", Style);
692 verifyFormat("const std::Foo<int>", Style);
693
694 // Multiple template parameters.
695 verifyFormat("Bar<const std::Foo, 32>;", "Bar<std::Foo const, 32>;", Style);
696
697 // Variable declaration based on template type.
698 verifyFormat("Bar<const std::Foo> bar;", "Bar<std::Foo const> bar;", Style);
699
700 // Using typename for a dependent name.
701 verifyFormat("const typename Foo::iterator;", "typename Foo::iterator const;",
702 Style);
703
704 // Don't move past C-style struct/class.
705 verifyFormat("void foo(struct A const a);", Style);
706 verifyFormat("void foo(class A const a);", Style);
707
708 // Don't move past struct/class combined declaration and variable
709 // definition.
710 verifyFormat("const struct {\n} var;", Style);
711 verifyFormat("struct {\n} const var;", Style);
712 verifyFormat("const class {\n} var;", Style);
713 verifyFormat("class {\n} const var;", Style);
714
715 // Sort left qualifiers for struct/class combined declaration and variable
716 // definition.
717 verifyFormat("const volatile class {\n} var;", Style);
718 verifyFormat("const volatile class {\n} var;",
719 "volatile const class {\n} var;", Style);
720 // Leave right qualifers unchanged for struct/class combined declaration and
721 // variable definition.
722 verifyFormat("class {\n} const volatile var;", Style);
723 verifyFormat("class {\n} volatile const var;", Style);
724
725 verifyFormat("foo<const Bar<Baz<T>>>();", "foo<Bar<Baz<T>> const>();", Style);
726 verifyFormat("foo<const Bar<Baz<T>>>();", "foo<Bar<Baz<T> > const>();",
727 Style);
728 verifyFormat("Bar<32, const Foo<25>>;", "Bar<32, Foo<25> const>;", Style);
729 verifyFormat("A<const B<const C<D>>>;", "A<B<C<D> const> const>;", Style);
730 verifyFormat("A<const B<const C<const D>>>;", "A<B<C<D const> const> const>;",
731 Style);
732
733 // Don't move past decltype, typeof, or _Atomic.
734 verifyFormat("decltype(foo) const", Style);
735 verifyFormat("typeof(foo) const", Style);
736 verifyFormat("_Atomic(foo) const", Style);
737
738 // ::template for dependent names
739 verifyFormat("const volatile ::template Foo<T> var;",
740 "::template Foo<T> const volatile var;", Style);
741 verifyFormat("const volatile typename ::template Foo<T> var;",
742 "typename ::template Foo<T> const volatile var;", Style);
743 verifyFormat("const typename Bar::template Foo<T>::T;",
744 "typename Bar::template Foo<T>::T const;", Style);
745 verifyFormat("const volatile typename Bar::template Foo<T>::T;",
746 "typename Bar::template Foo<T>::T const volatile;", Style);
747
748 // typename ::
749 verifyFormat("const typename ::Bar<int>;", "typename ::Bar<int> const;",
750 Style);
751 // typename ::template
752 verifyFormat("const typename ::template Bar<int>;",
753 "typename ::template Bar<int> const;", Style);
754
755 // Comments
756 const int ColumnLimit = Style.ColumnLimit;
757 Style.ColumnLimit = 200;
758 verifyFormat("/*c*/ const Foo *foo;", "/*c*/ Foo const *foo;", Style);
759 verifyFormat("const Foo /*c*/ *foo;", "Foo const /*c*/ *foo;", Style);
760 verifyFormat("const Foo * /*c*/ foo;", "Foo const * /*c*/ foo;", Style);
761
762 verifyFormat("/*comment*/ const std::vector<int> v;",
763 "/*comment*/ std::vector<int> const v;", Style);
764 verifyFormat("const std /*comment*/ ::vector<int> v;",
765 "std /*comment*/ ::vector<int> const v;", Style);
766 verifyFormat("const std::/*comment*/ vector<int> v;",
767 "std::/*comment*/ vector<int> const v;", Style);
768 verifyFormat("const std::vector /*comment*/<int> v;",
769 "std::vector /*comment*/<int> const v;", Style);
770 verifyFormat("const std::vector</*comment*/ int> v;",
771 "std::vector</*comment*/ int> const v;", Style);
772 verifyFormat("const std::vector<int /*comment*/> v;",
773 "std::vector<int /*comment*/> const v;", Style);
774 verifyFormat("const std::vector<int> /*comment*/ v;",
775 "std::vector<int> /*comment*/ const v;", Style);
776
777 verifyFormat("std::vector</*comment*/ const int> v;",
778 "std::vector</*comment*/ int const> v;", Style);
779 verifyFormat("std::vector<const int /*comment*/> v;",
780 "std::vector<int /*comment*/ const> v;", Style);
781 verifyFormat("std::vector<const int /*comment*/> v;",
782 "std::vector<int const /*comment*/> v;", Style);
783 verifyFormat("std::vector</*comment*/ const Foo> v;",
784 "std::vector</*comment*/ Foo const> v;", Style);
785 verifyFormat("std::vector<const Foo /*comment*/> v;",
786 "std::vector<Foo /*comment*/ const> v;", Style);
787 verifyFormat("std::vector<const Foo /*comment*/> v;",
788 "std::vector<Foo const /*comment*/> v;", Style);
789
790 verifyFormat("const typename C<T>::template B<T>;",
791 "typename C<T>::template B<T> const;", Style);
792 verifyFormat("/*c*/ const typename C<T>::template B<T>;",
793 "/*c*/ typename C<T>::template B<T> const;", Style);
794 verifyFormat("const typename /*c*/ C<T>::template B<T>;",
795 "typename /*c*/ C<T>::template B<T> const;", Style);
796 verifyFormat("const typename C /*c*/<T>::template B<T>;",
797 "typename C /*c*/<T>::template B<T> const;", Style);
798 verifyFormat("const typename C<T> /*c*/ ::template B<T>;",
799 "typename C<T> /*c*/ ::template B<T> const;", Style);
800 verifyFormat("const typename C<T>::/*c*/ template B<T>;",
801 "typename C<T>::/*c*/ template B<T> const;", Style);
802 verifyFormat("const typename C<T>::template /*c*/ B<T>;",
803 "typename C<T>::template /*c*/ B<T> const;", Style);
804 verifyFormat("const typename C<T>::template B<T> /*c*/;",
805 "typename C<T>::template B<T> /*c*/ const;", Style);
806
807 verifyFormat("/*c*/ const typename /*c*/ C /*c*/<T> /*c*/ ::/*c*/ template "
808 "/*c*/ B /*c*/<T> /*c*/ v;",
809 "/*c*/ typename /*c*/ C /*c*/<T> /*c*/ ::/*c*/ template /*c*/ B "
810 "/*c*/<T> /*c*/ const v;",
811 Style);
812
813 verifyFormat("const unsigned /*c*/ long /*c*/ a;",
814 "unsigned /*c*/ long /*c*/ const a;", Style);
815 verifyFormat("const unsigned /*c*/ long /*c*/ long a;",
816 "unsigned /*c*/ long /*c*/ long const a;", Style);
817
818 // Not changed
819 verifyFormat("foo() /*c*/ const", Style);
820 verifyFormat("struct /*c*/ const a;", Style);
821 verifyFormat("class /*c*/ const a;", Style);
822 verifyFormat("decltype(v) /*c*/ const a;", Style);
823 verifyFormat("typeof(v) /*c*/ const a;", Style);
824 verifyFormat("_Atomic(v) /*c*/ const a;", Style);
825 verifyFormat("decltype /*c*/ (v) const a;", Style);
826 verifyFormat("const /*c*/ class {\n} /*c*/ volatile /*c*/ foo = {};", Style);
827
828 Style.ColumnLimit = ColumnLimit;
829
830 // Don't adjust macros
831 verifyFormat("INTPTR const a;", Style);
832
833 // Pointers to members
834 verifyFormat("int S:: *a;", Style);
835 verifyFormat("const int S:: *a;", "int const S:: *a;", Style);
836 verifyFormat("const int S:: *const a;", "int const S::* const a;", Style);
837 verifyFormat("int A:: *const A:: *p1;", Style);
838 verifyFormat("float (C:: *p)(int);", Style);
839 verifyFormat("float (C:: *const p)(int);", Style);
840 verifyFormat("float (C:: *p)(int) const;", Style);
841 verifyFormat("const float (C:: *p)(int);", "float const (C::*p)(int);",
842 Style);
843}
844
845TEST_F(QualifierFixerTest, ConstVolatileQualifiersOrder) {
846 FormatStyle Style = getLLVMStyle();
847 Style.QualifierAlignment = FormatStyle::QAS_Left;
848 Style.QualifierOrder = {"inline", "static", "const", "volatile", "type"};
849
850 // The Default
851 EXPECT_EQ(Style.QualifierOrder.size(), (size_t)5);
852
853 verifyFormat("const volatile int a;", Style);
854 verifyFormat("const volatile int a;", "volatile const int a;", Style);
855 verifyFormat("const volatile int a;", "int const volatile a;", Style);
856 verifyFormat("const volatile int a;", "int volatile const a;", Style);
857 verifyFormat("const volatile int a;", "const int volatile a;", Style);
858
859 verifyFormat("const volatile Foo a;", Style);
860 verifyFormat("const volatile Foo a;", "volatile const Foo a;", Style);
861 verifyFormat("const volatile Foo a;", "Foo const volatile a;", Style);
862 verifyFormat("const volatile Foo a;", "Foo volatile const a;", Style);
863 verifyFormat("const volatile Foo a;", "const Foo volatile a;", Style);
864
865 Style.QualifierAlignment = FormatStyle::QAS_Right;
866 Style.QualifierOrder = {"type", "const", "volatile"};
867
868 verifyFormat("int const volatile a;", "const volatile int a;", Style);
869 verifyFormat("int const volatile a;", "volatile const int a;", Style);
870 verifyFormat("int const volatile a;", Style);
871 verifyFormat("int const volatile a;", "int volatile const a;", Style);
872 verifyFormat("int const volatile a;", "const int volatile a;", Style);
873
874 verifyFormat("Foo const volatile a;", "const volatile Foo a;", Style);
875 verifyFormat("Foo const volatile a;", "volatile const Foo a;", Style);
876 verifyFormat("Foo const volatile a;", Style);
877 verifyFormat("Foo const volatile a;", "Foo volatile const a;", Style);
878 verifyFormat("Foo const volatile a;", "const Foo volatile a;", Style);
879
880 Style.QualifierAlignment = FormatStyle::QAS_Left;
881 Style.QualifierOrder = {"volatile", "const", "type"};
882
883 verifyFormat("volatile const int a;", "const volatile int a;", Style);
884 verifyFormat("volatile const int a;", Style);
885 verifyFormat("volatile const int a;", "int const volatile a;", Style);
886 verifyFormat("volatile const int a;", "int volatile const a;", Style);
887 verifyFormat("volatile const int a;", "const int volatile a;", Style);
888
889 verifyFormat("volatile const Foo a;", "const volatile Foo a;", Style);
890 verifyFormat("volatile const Foo a;", Style);
891 verifyFormat("volatile const Foo a;", "Foo const volatile a;", Style);
892 verifyFormat("volatile const Foo a;", "Foo volatile const a;", Style);
893 verifyFormat("volatile const Foo a;", "const Foo volatile a;", Style);
894
895 Style.QualifierAlignment = FormatStyle::QAS_Right;
896 Style.QualifierOrder = {"type", "volatile", "const"};
897
898 verifyFormat("int volatile const a;", "const volatile int a;", Style);
899 verifyFormat("int volatile const a;", "volatile const int a;", Style);
900 verifyFormat("int volatile const a;", "int const volatile a;", Style);
901 verifyFormat("int volatile const a;", Style);
902 verifyFormat("int volatile const a;", "const int volatile a;", Style);
903
904 verifyFormat("Foo volatile const a;", "const volatile Foo a;", Style);
905 verifyFormat("Foo volatile const a;", "volatile const Foo a;", Style);
906 verifyFormat("Foo volatile const a;", "Foo const volatile a;", Style);
907 verifyFormat("Foo volatile const a;", Style);
908 verifyFormat("Foo volatile const a;", "const Foo volatile a;", Style);
909
910 Style.QualifierAlignment = FormatStyle::QAS_Custom;
911 Style.QualifierOrder = {"type", "volatile", "const"};
912
913 verifyFormat("int volatile const a;", "const volatile int a;", Style);
914 verifyFormat("int volatile const a;", "volatile const int a;", Style);
915 verifyFormat("int volatile const a;", "int const volatile a;", Style);
916 verifyFormat("int volatile const a;", Style);
917 verifyFormat("int volatile const a;", "const int volatile a;", Style);
918
919 verifyFormat("Foo volatile const a;", "const volatile Foo a;", Style);
920 verifyFormat("Foo volatile const a;", "volatile const Foo a;", Style);
921 verifyFormat("Foo volatile const a;", "Foo const volatile a;", Style);
922 verifyFormat("Foo volatile const a;", Style);
923 verifyFormat("Foo volatile const a;", "const Foo volatile a;", Style);
924}
925
926TEST_F(QualifierFixerTest, InlineStatics) {
927 FormatStyle Style = getLLVMStyle();
928 Style.QualifierAlignment = FormatStyle::QAS_Left;
929 Style.QualifierOrder = {"inline", "static", "const", "volatile", "type"};
930 EXPECT_EQ(Style.QualifierOrder.size(), (size_t)5);
931
932 verifyFormat("inline static const volatile int a;",
933 "const inline static volatile int a;", Style);
934 verifyFormat("inline static const volatile int a;",
935 "volatile inline static const int a;", Style);
936 verifyFormat("inline static const volatile int a;",
937 "int const inline static volatile a;", Style);
938 verifyFormat("inline static const volatile int a;",
939 "int volatile inline static const a;", Style);
940 verifyFormat("inline static const volatile int a;",
941 "const int inline static volatile a;", Style);
942}
943
944TEST_F(QualifierFixerTest, AmpEqual) {
945 FormatStyle Style = getLLVMStyle();
946 Style.QualifierAlignment = FormatStyle::QAS_Custom;
947 Style.QualifierOrder = {"static", "type", "const"};
948 EXPECT_EQ(Style.QualifierOrder.size(), (size_t)3);
949
950 verifyFormat("foo(std::string const & = std::string()) const",
951 "foo(const std::string & = std::string()) const", Style);
952 verifyFormat("foo(std::string const & = std::string())",
953 "foo(const std::string & = std::string())", Style);
954}
955
956TEST_F(QualifierFixerTest, MoveConstBeyondTypeSmall) {
957
958 FormatStyle Style = getLLVMStyle();
959 Style.QualifierAlignment = FormatStyle::QAS_Custom;
960 Style.QualifierOrder = {"type", "const"};
961 EXPECT_EQ(Style.QualifierOrder.size(), (size_t)2);
962
963 verifyFormat("int const a;", "const int a;", Style);
964 verifyFormat("int const *a;", "const int*a;", Style);
965 verifyFormat("int const *a;", "const int *a;", Style);
966 verifyFormat("int const &a;", "const int &a;", Style);
967 verifyFormat("int const &&a;", "const int &&a;", Style);
968}
969
970TEST_F(QualifierFixerTest, MoveConstBeforeTypeSmall) {
971
972 FormatStyle Style = getLLVMStyle();
973 Style.QualifierAlignment = FormatStyle::QAS_Custom;
974 Style.QualifierOrder = {"const", "type"};
975 EXPECT_EQ(Style.QualifierOrder.size(), (size_t)2);
976
977 verifyFormat("const int a;", "int const a;", Style);
978 verifyFormat("const int *a;", "int const *a;", Style);
979 verifyFormat("const int *const a;", "int const *const a;", Style);
980
981 verifyFormat("const int a = foo();", "int const a = foo();", Style);
982 verifyFormat("const int *a = foo();", "int const *a = foo();", Style);
983 verifyFormat("const int *const a = foo();", "int const *const a = foo();",
984 Style);
985
986 verifyFormat("const auto a = foo();", "auto const a = foo();", Style);
987 verifyFormat("const auto *a = foo();", "auto const *a = foo();", Style);
988 verifyFormat("const auto *const a = foo();", "auto const *const a = foo();",
989 Style);
990}
991
992TEST_F(QualifierFixerTest, MoveConstBeyondType) {
993
994 FormatStyle Style = getLLVMStyle();
995 Style.QualifierAlignment = FormatStyle::QAS_Custom;
996 Style.QualifierOrder = {"static", "inline", "type", "const", "volatile"};
997 EXPECT_EQ(Style.QualifierOrder.size(), (size_t)5);
998
999 verifyFormat("static inline int const volatile a;",
1000 "const inline static volatile int a;", Style);
1001 verifyFormat("static inline int const volatile a;",
1002 "volatile inline static const int a;", Style);
1003 verifyFormat("static inline int const volatile a;",
1004 "int const inline static volatile a;", Style);
1005 verifyFormat("static inline int const volatile a;",
1006 "int volatile inline static const a;", Style);
1007 verifyFormat("static inline int const volatile a;",
1008 "const int inline static volatile a;", Style);
1009
1010 verifyFormat("static inline int const volatile *const a;",
1011 "const int inline static volatile *const a;", Style);
1012
1013 verifyFormat("static inline Foo const volatile a;",
1014 "const inline static volatile Foo a;", Style);
1015 verifyFormat("static inline Foo const volatile a;",
1016 "volatile inline static const Foo a;", Style);
1017 verifyFormat("static inline Foo const volatile a;",
1018 "Foo const inline static volatile a;", Style);
1019 verifyFormat("static inline Foo const volatile a;",
1020 "Foo volatile inline static const a;", Style);
1021 verifyFormat("static inline Foo const volatile a;",
1022 "const Foo inline static volatile a;", Style);
1023
1024 verifyFormat("static inline Foo const volatile *const a;",
1025 "const Foo inline static volatile *const a;", Style);
1026}
1027
1028TEST_F(QualifierFixerTest, PrepareLeftRightOrdering) {
1029 FormatStyle Style = getLLVMStyle();
1030 Style.QualifierAlignment = FormatStyle::QAS_Custom;
1031 Style.QualifierOrder = {"static", "inline", "type", "const", "volatile"};
1032
1033 std::vector<std::string> Left;
1034 std::vector<std::string> Right;
1035 std::vector<tok::TokenKind> ConfiguredTokens;
1036 prepareLeftRightOrderingForQualifierAlignmentFixer(Order: Style.QualifierOrder, LeftOrder&: Left,
1037 RightOrder&: Right, Qualifiers&: ConfiguredTokens);
1038
1039 EXPECT_EQ(Left.size(), (size_t)2);
1040 EXPECT_EQ(Right.size(), (size_t)2);
1041
1042 std::vector<std::string> LeftResult = {"inline", "static"};
1043 std::vector<std::string> RightResult = {"const", "volatile"};
1044 EXPECT_EQ(Left, LeftResult);
1045 EXPECT_EQ(Right, RightResult);
1046}
1047
1048TEST_F(QualifierFixerTest, IsQualifierType) {
1049
1050 std::vector<tok::TokenKind> ConfiguredTokens;
1051 ConfiguredTokens.push_back(x: tok::kw_const);
1052 ConfiguredTokens.push_back(x: tok::kw_static);
1053 ConfiguredTokens.push_back(x: tok::kw_inline);
1054 ConfiguredTokens.push_back(x: tok::kw_restrict);
1055 ConfiguredTokens.push_back(x: tok::kw_constexpr);
1056 ConfiguredTokens.push_back(x: tok::kw_friend);
1057
1058 auto Tokens = annotate(
1059 Code: "const static inline auto restrict int double long constexpr friend");
1060 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
1061
1062 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1063 Tokens[0], ConfiguredTokens));
1064 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1065 Tokens[1], ConfiguredTokens));
1066 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1067 Tokens[2], ConfiguredTokens));
1068 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1069 Tokens[3], ConfiguredTokens));
1070 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1071 Tokens[4], ConfiguredTokens));
1072 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1073 Tokens[5], ConfiguredTokens));
1074 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1075 Tokens[6], ConfiguredTokens));
1076 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1077 Tokens[7], ConfiguredTokens));
1078 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1079 Tokens[8], ConfiguredTokens));
1080 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1081 Tokens[9], ConfiguredTokens));
1082
1083 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[0]));
1084 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[1]));
1085 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[2]));
1086 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[3]));
1087 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[4]));
1088 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[5]));
1089 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[6]));
1090 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[7]));
1091 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[8]));
1092 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[9]));
1093
1094 auto NotTokens = annotate(Code: "for while do Foo Bar ");
1095 ASSERT_EQ(NotTokens.size(), 6u) << Tokens;
1096
1097 EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1098 NotTokens[0], ConfiguredTokens));
1099 EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1100 NotTokens[1], ConfiguredTokens));
1101 EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1102 NotTokens[2], ConfiguredTokens));
1103 EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1104 NotTokens[3], ConfiguredTokens));
1105 EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1106 NotTokens[4], ConfiguredTokens));
1107 EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
1108 NotTokens[5], ConfiguredTokens));
1109
1110 EXPECT_FALSE(
1111 LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[0]));
1112 EXPECT_FALSE(
1113 LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[1]));
1114 EXPECT_FALSE(
1115 LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[2]));
1116 EXPECT_FALSE(
1117 LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[3]));
1118 EXPECT_FALSE(
1119 LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[4]));
1120 EXPECT_FALSE(
1121 LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[5]));
1122}
1123
1124TEST_F(QualifierFixerTest, IsMacro) {
1125
1126 auto Tokens = annotate(Code: "INT INTPR Foo int");
1127 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
1128
1129 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isPossibleMacro(Tokens[0]));
1130 EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isPossibleMacro(Tokens[1]));
1131 EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isPossibleMacro(Tokens[2]));
1132 EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isPossibleMacro(Tokens[3]));
1133}
1134
1135TEST_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
1144TEST_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
1182TEST_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
1195TEST_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
1211TEST_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
1227TEST_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
1267TEST_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
1282TEST_F(QualifierFixerTest, DisableRegions) {
1283 FormatStyle Style = getLLVMStyle();
1284 Style.QualifierAlignment = FormatStyle::QAS_Custom;
1285 Style.QualifierOrder = {"inline", "static", "const", "type"};
1286
1287 ReplacementCount = 0;
1288 verifyFormat("// clang-format off\n"
1289 "int const inline static a = 0;\n"
1290 "// clang-format on",
1291 Style);
1292 EXPECT_EQ(ReplacementCount, 0);
1293 verifyFormat("// clang-format off\n"
1294 "int const inline static a = 0;\n"
1295 "// clang-format on\n"
1296 "inline static const int a = 0;",
1297 "// clang-format off\n"
1298 "int const inline static a = 0;\n"
1299 "// clang-format on\n"
1300 "int const inline static a = 0;",
1301 Style);
1302}
1303
1304TEST_F(QualifierFixerTest, TemplatesRight) {
1305 FormatStyle Style = getLLVMStyle();
1306 Style.QualifierAlignment = FormatStyle::QAS_Custom;
1307 Style.QualifierOrder = {"type", "const"};
1308
1309 verifyFormat("template <typename T> Foo const f();",
1310 "template <typename T> const Foo f();", Style);
1311 verifyFormat("template <typename T> int const f();",
1312 "template <typename T> const int f();", Style);
1313
1314 verifyFormat("template <T const> t;", "template <const T> t;", Style);
1315 verifyFormat("template <typename T>\n"
1316 " requires Concept<T const>\n"
1317 "Foo const f();",
1318 "template <typename T>\n"
1319 " requires Concept<const T>\n"
1320 "const Foo f();",
1321 Style);
1322 verifyFormat("TemplateType<T const> t;", "TemplateType<const T> t;", Style);
1323 verifyFormat("TemplateType<Container const> t;",
1324 "TemplateType<const Container> t;", Style);
1325}
1326
1327TEST_F(QualifierFixerTest, TemplatesLeft) {
1328 FormatStyle Style = getLLVMStyle();
1329 Style.QualifierAlignment = FormatStyle::QAS_Custom;
1330 Style.QualifierOrder = {"const", "volatile", "type"};
1331
1332 verifyFormat("template <typename T> const Foo f();",
1333 "template <typename T> Foo const f();", Style);
1334 verifyFormat("template <typename T> const int f();",
1335 "template <typename T> int const f();", Style);
1336
1337 verifyFormat("template <const T> t;", "template <T const> t;", Style);
1338 verifyFormat("template <typename T>\n"
1339 " requires Concept<const T>\n"
1340 "const Foo f();",
1341 "template <typename T>\n"
1342 " requires Concept<T const>\n"
1343 "Foo const f();",
1344 Style);
1345 verifyFormat("template <typename T>\n"
1346 " requires Concept<const T>\n"
1347 "const volatile Foo f();",
1348 "template <typename T>\n"
1349 " requires Concept<T const>\n"
1350 "volatile const Foo f();",
1351 Style);
1352 verifyFormat("TemplateType<const T> t;", "TemplateType<T const> t;", Style);
1353 verifyFormat("TemplateType<const Container> t;",
1354 "TemplateType<Container const> t;", Style);
1355}
1356
1357TEST_F(QualifierFixerTest, Ranges) {
1358 FormatStyle Style = getLLVMStyle();
1359 Style.QualifierAlignment = FormatStyle::QAS_Custom;
1360 Style.QualifierOrder = {"const", "volatile", "type"};
1361
1362 // Only the first line should be formatted; the second should remain as is.
1363 verifyFormat("template <typename T> const Foo f();\n"
1364 "template <typename T> Foo const f();",
1365 "template <typename T> Foo const f();\n"
1366 "template <typename T> Foo const f();",
1367 Style, {tooling::Range(0, 36)});
1368
1369 // Only the middle line should be formatted; the first and last should remain
1370 // as is.
1371 verifyFormat("template <typename T> Foo const f();\n"
1372 "template <typename T> const Foo f();\n"
1373 "template <typename T> Foo const f();",
1374 "template <typename T> Foo const f();\n"
1375 "template <typename T> Foo const f();\n"
1376 "template <typename T> Foo const f();",
1377 Style, {tooling::Range(37, 36)});
1378}
1379
1380} // namespace
1381} // namespace test
1382} // namespace format
1383} // namespace clang
1384

source code of clang/unittests/Format/QualifierFixerTest.cpp