[GlobalISel] Add G_ABDS and G_ABDU instructions (#118122)
[llvm-project.git] / clang / unittests / Format / QualifierFixerTest.cpp
blobf9255c6e4c70883921d1e5fdeaf91ff2d6bfcb36
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 //===----------------------------------------------------------------------===//
9 #include "../lib/Format/QualifierAlignmentFixer.h"
10 #include "FormatTestBase.h"
11 #include "TestLexer.h"
13 #define DEBUG_TYPE "format-qualifier-fixer-test"
15 namespace clang {
16 namespace format {
17 namespace test {
18 namespace {
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 {
30 protected:
31 TokenList annotate(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) {
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);
55 TEST_F(QualifierFixerTest, FailQualifierInvalidConfiguration) {
56 FormatStyle Style = {};
57 Style.Language = FormatStyle::LK_Cpp;
58 FAIL_PARSE("QualifierAlignment: Custom\n"
59 "QualifierOrder: [const, volatile, apples, type]",
60 QualifierOrder,
61 std::vector<std::string>({"const", "volatile", "apples", "type"}));
64 TEST_F(QualifierFixerTest, FailQualifierDuplicateConfiguration) {
65 FormatStyle Style = {};
66 Style.Language = FormatStyle::LK_Cpp;
67 FAIL_PARSE("QualifierAlignment: Custom\n"
68 "QualifierOrder: [const, volatile, const, type]",
69 QualifierOrder,
70 std::vector<std::string>({"const", "volatile", "const", "type"}));
73 TEST_F(QualifierFixerTest, FailQualifierMissingType) {
74 FormatStyle Style = {};
75 Style.Language = FormatStyle::LK_Cpp;
76 FAIL_PARSE("QualifierAlignment: Custom\n"
77 "QualifierOrder: [const, volatile ]",
78 QualifierOrder,
79 std::vector<std::string>({
80 "const",
81 "volatile",
82 }));
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;",
126 Style);
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;",
134 Style);
135 verifyFormat("const const int a;", "const int const a;", Style);
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);
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)",
211 Style);
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);
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);
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);
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);
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;",
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);
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);
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
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);
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::*;",
310 Style);
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);
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);
338 verifyFormat("LocalScope const *Scope = nullptr;",
339 "const LocalScope* Scope = nullptr;", Style);
340 verifyFormat("struct DOTGraphTraits<Stmt const *>",
341 "struct DOTGraphTraits<const Stmt *>", Style);
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);
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("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);
360 verifyFormat("auto lambda = [] { int const i = 0; };",
361 "auto lambda = [] { const int i = 0; };", Style);
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);
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);
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);
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);
375 verifyFormat("int const P;\n#if 0\n#else\n#endif",
376 "const int P;\n#if 0\n#else\n#endif", Style);
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);
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);
387 // Using typename for a nested dependent type name.
388 verifyFormat("typename Foo::iterator const;", "const typename Foo::iterator;",
389 Style);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
520 Style.ColumnLimit = ColumnLimit;
522 // Don't adjust macros
523 verifyFormat("const INTPTR a;", Style);
525 // Pointers to members
526 verifyFormat("int S::*a;", Style);
527 verifyFormat("int const S::*a;", "const int S::*a;", Style);
528 verifyFormat("int const S::*const a;", "const int S::* const a;", Style);
529 verifyFormat("int A::*const A::*p1;", Style);
530 verifyFormat("float (C::*p)(int);", Style);
531 verifyFormat("float (C::*const p)(int);", Style);
532 verifyFormat("float (C::*p)(int) const;", Style);
533 verifyFormat("float const (C::*p)(int);", "const float (C::*p)(int);", Style);
536 TEST_F(QualifierFixerTest, LeftQualifier) {
537 FormatStyle Style = getLLVMStyle();
538 Style.QualifierAlignment = FormatStyle::QAS_Left;
539 Style.QualifierOrder = {"inline", "static", "const", "volatile", "type"};
541 verifyFormat("const int a;", Style);
542 verifyFormat("const int *a;", Style);
543 verifyFormat("const int &a;", Style);
544 verifyFormat("const int &&a;", Style);
545 verifyFormat("const int b;", Style);
546 verifyFormat("const int *b;", Style);
547 verifyFormat("const int &b;", Style);
548 verifyFormat("const int &&b;", Style);
549 verifyFormat("const int *const b;", Style);
550 verifyFormat("int *const c;", Style);
552 verifyFormat("const Foo a;", Style);
553 verifyFormat("const Foo *a;", Style);
554 verifyFormat("const Foo &a;", Style);
555 verifyFormat("const Foo &&a;", Style);
556 verifyFormat("const Foo b;", Style);
557 verifyFormat("const Foo *b;", Style);
558 verifyFormat("const Foo &b;", Style);
559 verifyFormat("const Foo &&b;", Style);
560 verifyFormat("const Foo *const b;", Style);
561 verifyFormat("Foo *const b;", Style);
562 verifyFormat("const Foo *const b;", Style);
564 verifyFormat("LLVM_NODISCARD const int &Foo();", Style);
566 verifyFormat("const char a[];", Style);
567 verifyFormat("const auto v = get_value();", Style);
568 verifyFormat("const long long &a;", Style);
569 verifyFormat("const unsigned char *a;", Style);
570 verifyFormat("const unsigned char *a;", "unsigned char const *a;", Style);
571 verifyFormat("const Foo<int> &a", "Foo<int> const &a", Style);
572 verifyFormat("const Foo<int>::iterator &a", "Foo<int>::iterator const &a",
573 Style);
574 verifyFormat("const ::Foo<int>::iterator &a", "::Foo<int>::iterator const &a",
575 Style);
577 verifyFormat("const int a;", "int const a;", Style);
578 verifyFormat("const int *a;", "int const *a;", Style);
579 verifyFormat("const int &a;", "int const &a;", Style);
580 verifyFormat("foo(const int &a)", "foo(int const &a)", Style);
581 verifyFormat("unsigned char *a;", Style);
582 verifyFormat("const unsigned int &get_nu() const",
583 "unsigned int const &get_nu() const", Style);
585 verifyFormat("const volatile int;", "volatile const int;", Style);
586 verifyFormat("const volatile int;", Style);
587 verifyFormat("const volatile int;", "const int volatile;", Style);
589 verifyFormat("const volatile int *restrict;", "volatile const int *restrict;",
590 Style);
591 verifyFormat("const volatile int *restrict;", Style);
592 verifyFormat("const volatile int *restrict;", "const int volatile *restrict;",
593 Style);
595 verifyFormat("const volatile long long int;", "volatile long long int const;",
596 Style);
597 verifyFormat("const volatile long long int;", "volatile long long const int;",
598 Style);
599 verifyFormat("const volatile long long int;", "long long volatile int const;",
600 Style);
601 verifyFormat("const volatile long long int;", "long volatile long int const;",
602 Style);
603 verifyFormat("const volatile long long int;", "const long long volatile int;",
604 Style);
606 verifyFormat("SourceRange getSourceRange() const override LLVM_READONLY;",
607 Style);
609 verifyFormat("void foo() const override;", Style);
610 verifyFormat("void foo() const override LLVM_READONLY;", Style);
611 verifyFormat("void foo() const final;", Style);
612 verifyFormat("void foo() const final LLVM_READONLY;", Style);
613 verifyFormat("void foo() const LLVM_READONLY;", Style);
615 verifyFormat(
616 "template <typename Func> explicit Action(const Action<Func> &action);",
617 Style);
618 verifyFormat(
619 "template <typename Func> explicit Action(const Action<Func> &action);",
620 "template <typename Func> explicit Action(Action<Func> const &action);",
621 Style);
623 verifyFormat("static const int bat;", Style);
624 verifyFormat("static const int bat;", "static int const bat;", Style);
626 verifyFormat("static const int Foo<int>::bat = 0;", Style);
627 verifyFormat("static const int Foo<int>::bat = 0;",
628 "static int const Foo<int>::bat = 0;", Style);
630 verifyFormat("void fn(const Foo<T> &i);");
632 verifyFormat("const int Foo<int>::bat = 0;", Style);
633 verifyFormat("const int Foo<int>::bat = 0;", "int const Foo<int>::bat = 0;",
634 Style);
635 verifyFormat("void fn(const Foo<T> &i);", "void fn( Foo<T> const &i);",
636 Style);
637 verifyFormat("const int Foo<int>::fn() {", "int const Foo<int>::fn() {",
638 Style);
639 verifyFormat("const Foo<Foo<int>> *p;", "Foo<Foo<int>> const *p;", Style);
640 verifyFormat(
641 "const Foo<Foo<int>> *p = const_cast<const Foo<Foo<int>> *>(&ffi);",
642 "const Foo<Foo<int>> *p = const_cast<Foo<Foo<int>> const *>(&ffi);",
643 Style);
645 verifyFormat("void fn(const Foo<T> &i);", "void fn(Foo<T> const &i);", Style);
646 verifyFormat("void fns(const ns::S &s);", "void fns(ns::S const &s);", Style);
647 verifyFormat("void fns(const ::ns::S &s);", "void fns(::ns::S const &s);",
648 Style);
649 verifyFormat("void fn(const ns::Foo<T> &i);", "void fn(ns::Foo<T> const &i);",
650 Style);
651 verifyFormat("void fns(const ns::ns2::S &s);",
652 "void fns(ns::ns2::S const &s);", Style);
653 verifyFormat("void fn(const ns::Foo<Bar<T>> &i);",
654 "void fn(ns::Foo<Bar<T>> const &i);", Style);
655 verifyFormat("void fn(const ns::ns2::Foo<Bar<T>> &i);",
656 "void fn(ns::ns2::Foo<Bar<T>> const &i);", Style);
657 verifyFormat("void fn(const ns::ns2::Foo<Bar<T, U>> &i);",
658 "void fn(ns::ns2::Foo<Bar<T, U>> const &i);", Style);
660 verifyFormat("const auto i = 0;", "auto const i = 0;", Style);
661 verifyFormat("const auto &ir = i;", "auto const &ir = i;", Style);
662 verifyFormat("const auto *ip = &i;", "auto const *ip = &i;", Style);
664 verifyFormat("void f(const Concept auto &x);",
665 "void f(Concept auto const &x);", Style);
666 verifyFormat("void f(const std::integral auto &x);",
667 "void f(std::integral auto const &x);", Style);
669 verifyFormat("auto lambda = [] { const int i = 0; };",
670 "auto lambda = [] { int const i = 0; };", Style);
672 verifyFormat("Foo<const Foo<int>> P;\n#if 0\n#else\n#endif",
673 "Foo<Foo<int> const> P;\n#if 0\n#else\n#endif", Style);
675 verifyFormat("Foo<Foo<const int>> P;\n#if 0\n#else\n#endif",
676 "Foo<Foo<int const>> P;\n#if 0\n#else\n#endif", Style);
678 verifyFormat("const int P;\n#if 0\n#else\n#endif",
679 "int const P;\n#if 0\n#else\n#endif", Style);
681 verifyFormat("const unsigned long a;", "unsigned long const a;", Style);
682 verifyFormat("const unsigned long long a;", "unsigned long long const a;",
683 Style);
685 verifyFormat("const long long unsigned a;", "long const long unsigned a;",
686 Style);
688 verifyFormat("const std::Foo", Style);
689 verifyFormat("const std::Foo<>", Style);
690 verifyFormat("const std::Foo < int", "const std::Foo<int", Style);
691 verifyFormat("const std::Foo<int>", Style);
693 // Multiple template parameters.
694 verifyFormat("Bar<const std::Foo, 32>;", "Bar<std::Foo const, 32>;", Style);
696 // Variable declaration based on template type.
697 verifyFormat("Bar<const std::Foo> bar;", "Bar<std::Foo const> bar;", Style);
699 // Using typename for a dependent name.
700 verifyFormat("const typename Foo::iterator;", "typename Foo::iterator const;",
701 Style);
703 // Don't move past C-style struct/class.
704 verifyFormat("void foo(struct A const a);", Style);
705 verifyFormat("void foo(class A const a);", Style);
707 // Don't move past struct/class combined declaration and variable
708 // definition.
709 verifyFormat("const struct {\n} var;", Style);
710 verifyFormat("struct {\n} const var;", Style);
711 verifyFormat("const class {\n} var;", Style);
712 verifyFormat("class {\n} const var;", Style);
714 // Sort left qualifiers for struct/class combined declaration and variable
715 // definition.
716 verifyFormat("const volatile class {\n} var;", Style);
717 verifyFormat("const volatile class {\n} var;",
718 "volatile const class {\n} var;", Style);
719 // Leave right qualifers unchanged for struct/class combined declaration and
720 // variable definition.
721 verifyFormat("class {\n} const volatile var;", Style);
722 verifyFormat("class {\n} volatile const var;", Style);
724 verifyFormat("foo<const Bar<Baz<T>>>();", "foo<Bar<Baz<T>> const>();", Style);
725 verifyFormat("foo<const Bar<Baz<T>>>();", "foo<Bar<Baz<T> > const>();",
726 Style);
727 verifyFormat("Bar<32, const Foo<25>>;", "Bar<32, Foo<25> const>;", Style);
728 verifyFormat("A<const B<const C<D>>>;", "A<B<C<D> const> const>;", Style);
729 verifyFormat("A<const B<const C<const D>>>;", "A<B<C<D const> const> const>;",
730 Style);
732 // Don't move past decltype, typeof, or _Atomic.
733 verifyFormat("decltype(foo) const", Style);
734 verifyFormat("typeof(foo) const", Style);
735 verifyFormat("_Atomic(foo) const", Style);
737 // ::template for dependent names
738 verifyFormat("const volatile ::template Foo<T> var;",
739 "::template Foo<T> const volatile var;", Style);
740 verifyFormat("const volatile typename ::template Foo<T> var;",
741 "typename ::template Foo<T> const volatile var;", Style);
742 verifyFormat("const typename Bar::template Foo<T>::T;",
743 "typename Bar::template Foo<T>::T const;", Style);
744 verifyFormat("const volatile typename Bar::template Foo<T>::T;",
745 "typename Bar::template Foo<T>::T const volatile;", Style);
747 // typename ::
748 verifyFormat("const typename ::Bar<int>;", "typename ::Bar<int> const;",
749 Style);
750 // typename ::template
751 verifyFormat("const typename ::template Bar<int>;",
752 "typename ::template Bar<int> const;", Style);
754 // Comments
755 const int ColumnLimit = Style.ColumnLimit;
756 Style.ColumnLimit = 200;
757 verifyFormat("/*c*/ const Foo *foo;", "/*c*/ Foo const *foo;", Style);
758 verifyFormat("const Foo /*c*/ *foo;", "Foo const /*c*/ *foo;", Style);
759 verifyFormat("const Foo * /*c*/ foo;", "Foo const * /*c*/ foo;", Style);
761 verifyFormat("/*comment*/ const std::vector<int> v;",
762 "/*comment*/ std::vector<int> const v;", Style);
763 verifyFormat("const std /*comment*/ ::vector<int> v;",
764 "std /*comment*/ ::vector<int> const v;", Style);
765 verifyFormat("const std::/*comment*/ vector<int> v;",
766 "std::/*comment*/ vector<int> const v;", Style);
767 verifyFormat("const std::vector /*comment*/<int> v;",
768 "std::vector /*comment*/<int> const v;", Style);
769 verifyFormat("const std::vector</*comment*/ int> v;",
770 "std::vector</*comment*/ int> const v;", Style);
771 verifyFormat("const std::vector<int /*comment*/> v;",
772 "std::vector<int /*comment*/> const v;", Style);
773 verifyFormat("const std::vector<int> /*comment*/ v;",
774 "std::vector<int> /*comment*/ const v;", Style);
776 verifyFormat("std::vector</*comment*/ const int> v;",
777 "std::vector</*comment*/ int const> v;", Style);
778 verifyFormat("std::vector<const int /*comment*/> v;",
779 "std::vector<int /*comment*/ const> v;", Style);
780 verifyFormat("std::vector<const int /*comment*/> v;",
781 "std::vector<int const /*comment*/> v;", Style);
782 verifyFormat("std::vector</*comment*/ const Foo> v;",
783 "std::vector</*comment*/ Foo const> v;", Style);
784 verifyFormat("std::vector<const Foo /*comment*/> v;",
785 "std::vector<Foo /*comment*/ const> v;", Style);
786 verifyFormat("std::vector<const Foo /*comment*/> v;",
787 "std::vector<Foo const /*comment*/> v;", Style);
789 verifyFormat("const typename C<T>::template B<T>;",
790 "typename C<T>::template B<T> const;", Style);
791 verifyFormat("/*c*/ const typename C<T>::template B<T>;",
792 "/*c*/ typename C<T>::template B<T> const;", Style);
793 verifyFormat("const typename /*c*/ C<T>::template B<T>;",
794 "typename /*c*/ C<T>::template B<T> const;", Style);
795 verifyFormat("const typename C /*c*/<T>::template B<T>;",
796 "typename C /*c*/<T>::template B<T> const;", Style);
797 verifyFormat("const typename C<T> /*c*/ ::template B<T>;",
798 "typename C<T> /*c*/ ::template B<T> const;", Style);
799 verifyFormat("const typename C<T>::/*c*/ template B<T>;",
800 "typename C<T>::/*c*/ template B<T> const;", Style);
801 verifyFormat("const typename C<T>::template /*c*/ B<T>;",
802 "typename C<T>::template /*c*/ B<T> const;", Style);
803 verifyFormat("const typename C<T>::template B<T> /*c*/;",
804 "typename C<T>::template B<T> /*c*/ const;", Style);
806 verifyFormat("/*c*/ const typename /*c*/ C /*c*/<T> /*c*/ ::/*c*/ template "
807 "/*c*/ B /*c*/<T> /*c*/ v;",
808 "/*c*/ typename /*c*/ C /*c*/<T> /*c*/ ::/*c*/ template /*c*/ B "
809 "/*c*/<T> /*c*/ const v;",
810 Style);
812 verifyFormat("const unsigned /*c*/ long /*c*/ a;",
813 "unsigned /*c*/ long /*c*/ const a;", Style);
814 verifyFormat("const unsigned /*c*/ long /*c*/ long a;",
815 "unsigned /*c*/ long /*c*/ long const a;", Style);
817 // Not changed
818 verifyFormat("foo() /*c*/ const", Style);
819 verifyFormat("struct /*c*/ const a;", Style);
820 verifyFormat("class /*c*/ const a;", Style);
821 verifyFormat("decltype(v) /*c*/ const a;", Style);
822 verifyFormat("typeof(v) /*c*/ const a;", Style);
823 verifyFormat("_Atomic(v) /*c*/ const a;", Style);
824 verifyFormat("decltype /*c*/ (v) const a;", Style);
825 verifyFormat("const /*c*/ class {\n} /*c*/ volatile /*c*/ foo = {};", Style);
827 Style.ColumnLimit = ColumnLimit;
829 // Don't adjust macros
830 verifyFormat("INTPTR const a;", Style);
832 // Pointers to members
833 verifyFormat("int S::*a;", Style);
834 verifyFormat("const int S::*a;", "int const S::*a;", Style);
835 verifyFormat("const int S::*const a;", "int const S::*const a;", Style);
836 verifyFormat("int A::*const A::*p1;", Style);
837 verifyFormat("float (C::*p)(int);", Style);
838 verifyFormat("float (C::*const p)(int);", Style);
839 verifyFormat("float (C::*p)(int) const;", Style);
840 verifyFormat("const float (C::*p)(int);", "float const (C::*p)(int);", Style);
843 TEST_F(QualifierFixerTest, ConstVolatileQualifiersOrder) {
844 FormatStyle Style = getLLVMStyle();
845 Style.QualifierAlignment = FormatStyle::QAS_Left;
846 Style.QualifierOrder = {"inline", "static", "const", "volatile", "type"};
848 // The Default
849 EXPECT_EQ(Style.QualifierOrder.size(), (size_t)5);
851 verifyFormat("const volatile int a;", Style);
852 verifyFormat("const volatile int a;", "volatile const int a;", Style);
853 verifyFormat("const volatile int a;", "int const volatile a;", Style);
854 verifyFormat("const volatile int a;", "int volatile const a;", Style);
855 verifyFormat("const volatile int a;", "const int volatile a;", Style);
857 verifyFormat("const volatile Foo a;", Style);
858 verifyFormat("const volatile Foo a;", "volatile const Foo a;", Style);
859 verifyFormat("const volatile Foo a;", "Foo const volatile a;", Style);
860 verifyFormat("const volatile Foo a;", "Foo volatile const a;", Style);
861 verifyFormat("const volatile Foo a;", "const Foo volatile a;", Style);
863 Style.QualifierAlignment = FormatStyle::QAS_Right;
864 Style.QualifierOrder = {"type", "const", "volatile"};
866 verifyFormat("int const volatile a;", "const volatile int a;", Style);
867 verifyFormat("int const volatile a;", "volatile const int a;", Style);
868 verifyFormat("int const volatile a;", Style);
869 verifyFormat("int const volatile a;", "int volatile const a;", Style);
870 verifyFormat("int const volatile a;", "const int volatile a;", Style);
872 verifyFormat("Foo const volatile a;", "const volatile Foo a;", Style);
873 verifyFormat("Foo const volatile a;", "volatile const Foo a;", Style);
874 verifyFormat("Foo const volatile a;", Style);
875 verifyFormat("Foo const volatile a;", "Foo volatile const a;", Style);
876 verifyFormat("Foo const volatile a;", "const Foo volatile a;", Style);
878 Style.QualifierAlignment = FormatStyle::QAS_Left;
879 Style.QualifierOrder = {"volatile", "const", "type"};
881 verifyFormat("volatile const int a;", "const volatile int a;", Style);
882 verifyFormat("volatile const int a;", Style);
883 verifyFormat("volatile const int a;", "int const volatile a;", Style);
884 verifyFormat("volatile const int a;", "int volatile const a;", Style);
885 verifyFormat("volatile const int a;", "const int volatile a;", Style);
887 verifyFormat("volatile const Foo a;", "const volatile Foo a;", Style);
888 verifyFormat("volatile const Foo a;", Style);
889 verifyFormat("volatile const Foo a;", "Foo const volatile a;", Style);
890 verifyFormat("volatile const Foo a;", "Foo volatile const a;", Style);
891 verifyFormat("volatile const Foo a;", "const Foo volatile a;", Style);
893 Style.QualifierAlignment = FormatStyle::QAS_Right;
894 Style.QualifierOrder = {"type", "volatile", "const"};
896 verifyFormat("int volatile const a;", "const volatile int a;", Style);
897 verifyFormat("int volatile const a;", "volatile const int a;", Style);
898 verifyFormat("int volatile const a;", "int const volatile a;", Style);
899 verifyFormat("int volatile const a;", Style);
900 verifyFormat("int volatile const a;", "const int volatile a;", Style);
902 verifyFormat("Foo volatile const a;", "const volatile Foo a;", Style);
903 verifyFormat("Foo volatile const a;", "volatile const Foo a;", Style);
904 verifyFormat("Foo volatile const a;", "Foo const volatile a;", Style);
905 verifyFormat("Foo volatile const a;", Style);
906 verifyFormat("Foo volatile const a;", "const Foo volatile a;", Style);
908 Style.QualifierAlignment = FormatStyle::QAS_Custom;
909 Style.QualifierOrder = {"type", "volatile", "const"};
911 verifyFormat("int volatile const a;", "const volatile int a;", Style);
912 verifyFormat("int volatile const a;", "volatile const int a;", Style);
913 verifyFormat("int volatile const a;", "int const volatile a;", Style);
914 verifyFormat("int volatile const a;", Style);
915 verifyFormat("int volatile const a;", "const int volatile a;", Style);
917 verifyFormat("Foo volatile const a;", "const volatile Foo a;", Style);
918 verifyFormat("Foo volatile const a;", "volatile const Foo a;", Style);
919 verifyFormat("Foo volatile const a;", "Foo const volatile a;", Style);
920 verifyFormat("Foo volatile const a;", Style);
921 verifyFormat("Foo volatile const a;", "const Foo volatile a;", Style);
924 TEST_F(QualifierFixerTest, InlineStatics) {
925 FormatStyle Style = getLLVMStyle();
926 Style.QualifierAlignment = FormatStyle::QAS_Left;
927 Style.QualifierOrder = {"inline", "static", "const", "volatile", "type"};
928 EXPECT_EQ(Style.QualifierOrder.size(), (size_t)5);
930 verifyFormat("inline static const volatile int a;",
931 "const inline static volatile int a;", Style);
932 verifyFormat("inline static const volatile int a;",
933 "volatile inline static const int a;", Style);
934 verifyFormat("inline static const volatile int a;",
935 "int const inline static volatile a;", Style);
936 verifyFormat("inline static const volatile int a;",
937 "int volatile inline static const a;", Style);
938 verifyFormat("inline static const volatile int a;",
939 "const int inline static volatile a;", Style);
942 TEST_F(QualifierFixerTest, AmpEqual) {
943 FormatStyle Style = getLLVMStyle();
944 Style.QualifierAlignment = FormatStyle::QAS_Custom;
945 Style.QualifierOrder = {"static", "type", "const"};
946 EXPECT_EQ(Style.QualifierOrder.size(), (size_t)3);
948 verifyFormat("foo(std::string const & = std::string()) const",
949 "foo(const std::string & = std::string()) const", Style);
950 verifyFormat("foo(std::string const & = std::string())",
951 "foo(const std::string & = std::string())", Style);
954 TEST_F(QualifierFixerTest, MoveConstBeyondTypeSmall) {
956 FormatStyle Style = getLLVMStyle();
957 Style.QualifierAlignment = FormatStyle::QAS_Custom;
958 Style.QualifierOrder = {"type", "const"};
959 EXPECT_EQ(Style.QualifierOrder.size(), (size_t)2);
961 verifyFormat("int const a;", "const int a;", Style);
962 verifyFormat("int const *a;", "const int*a;", Style);
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);
968 TEST_F(QualifierFixerTest, MoveConstBeforeTypeSmall) {
970 FormatStyle Style = getLLVMStyle();
971 Style.QualifierAlignment = FormatStyle::QAS_Custom;
972 Style.QualifierOrder = {"const", "type"};
973 EXPECT_EQ(Style.QualifierOrder.size(), (size_t)2);
975 verifyFormat("const int a;", "int const a;", Style);
976 verifyFormat("const int *a;", "int const *a;", Style);
977 verifyFormat("const int *const a;", "int const *const a;", Style);
979 verifyFormat("const int a = foo();", "int const a = foo();", Style);
980 verifyFormat("const int *a = foo();", "int const *a = foo();", Style);
981 verifyFormat("const int *const a = foo();", "int const *const a = foo();",
982 Style);
984 verifyFormat("const auto a = foo();", "auto const a = foo();", Style);
985 verifyFormat("const auto *a = foo();", "auto const *a = foo();", Style);
986 verifyFormat("const auto *const a = foo();", "auto const *const a = foo();",
987 Style);
990 TEST_F(QualifierFixerTest, MoveConstBeyondType) {
992 FormatStyle Style = getLLVMStyle();
993 Style.QualifierAlignment = FormatStyle::QAS_Custom;
994 Style.QualifierOrder = {"static", "inline", "type", "const", "volatile"};
995 EXPECT_EQ(Style.QualifierOrder.size(), (size_t)5);
997 verifyFormat("static inline int const volatile a;",
998 "const inline static volatile int a;", Style);
999 verifyFormat("static inline int const volatile a;",
1000 "volatile inline static const int a;", Style);
1001 verifyFormat("static inline int const volatile a;",
1002 "int const inline static volatile a;", Style);
1003 verifyFormat("static inline int const volatile a;",
1004 "int volatile inline static const a;", Style);
1005 verifyFormat("static inline int const volatile a;",
1006 "const int inline static volatile a;", Style);
1008 verifyFormat("static inline int const volatile *const a;",
1009 "const int inline static volatile *const a;", Style);
1011 verifyFormat("static inline Foo const volatile a;",
1012 "const inline static volatile Foo a;", Style);
1013 verifyFormat("static inline Foo const volatile a;",
1014 "volatile inline static const Foo a;", Style);
1015 verifyFormat("static inline Foo const volatile a;",
1016 "Foo const inline static volatile a;", Style);
1017 verifyFormat("static inline Foo const volatile a;",
1018 "Foo volatile inline static const a;", Style);
1019 verifyFormat("static inline Foo const volatile a;",
1020 "const Foo inline static volatile a;", Style);
1022 verifyFormat("static inline Foo const volatile *const a;",
1023 "const Foo inline static volatile *const a;", Style);
1026 TEST_F(QualifierFixerTest, PrepareLeftRightOrdering) {
1027 FormatStyle Style = getLLVMStyle();
1028 Style.QualifierAlignment = FormatStyle::QAS_Custom;
1029 Style.QualifierOrder = {"static", "inline", "type", "const", "volatile"};
1031 std::vector<std::string> Left;
1032 std::vector<std::string> Right;
1033 std::vector<tok::TokenKind> ConfiguredTokens;
1034 prepareLeftRightOrderingForQualifierAlignmentFixer(Style.QualifierOrder, Left,
1035 Right, ConfiguredTokens);
1037 EXPECT_EQ(Left.size(), (size_t)2);
1038 EXPECT_EQ(Right.size(), (size_t)2);
1040 std::vector<std::string> LeftResult = {"inline", "static"};
1041 std::vector<std::string> RightResult = {"const", "volatile"};
1042 EXPECT_EQ(Left, LeftResult);
1043 EXPECT_EQ(Right, RightResult);
1046 TEST_F(QualifierFixerTest, IsQualifierType) {
1048 std::vector<tok::TokenKind> ConfiguredTokens;
1049 ConfiguredTokens.push_back(tok::kw_const);
1050 ConfiguredTokens.push_back(tok::kw_static);
1051 ConfiguredTokens.push_back(tok::kw_inline);
1052 ConfiguredTokens.push_back(tok::kw_restrict);
1053 ConfiguredTokens.push_back(tok::kw_constexpr);
1054 ConfiguredTokens.push_back(tok::kw_friend);
1056 TestLexer lexer{Allocator, Buffers};
1057 const auto LangOpts = getFormattingLangOpts();
1059 auto Tokens = lexer.lex(
1060 "const static inline auto restrict int double long constexpr friend");
1061 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
1063 EXPECT_TRUE(
1064 isConfiguredQualifierOrType(Tokens[0], ConfiguredTokens, LangOpts));
1065 EXPECT_TRUE(
1066 isConfiguredQualifierOrType(Tokens[1], ConfiguredTokens, LangOpts));
1067 EXPECT_TRUE(
1068 isConfiguredQualifierOrType(Tokens[2], ConfiguredTokens, LangOpts));
1069 EXPECT_TRUE(
1070 isConfiguredQualifierOrType(Tokens[3], ConfiguredTokens, LangOpts));
1071 EXPECT_TRUE(
1072 isConfiguredQualifierOrType(Tokens[4], ConfiguredTokens, LangOpts));
1073 EXPECT_TRUE(
1074 isConfiguredQualifierOrType(Tokens[5], ConfiguredTokens, LangOpts));
1075 EXPECT_TRUE(
1076 isConfiguredQualifierOrType(Tokens[6], ConfiguredTokens, LangOpts));
1077 EXPECT_TRUE(
1078 isConfiguredQualifierOrType(Tokens[7], ConfiguredTokens, LangOpts));
1079 EXPECT_TRUE(
1080 isConfiguredQualifierOrType(Tokens[8], ConfiguredTokens, LangOpts));
1081 EXPECT_TRUE(
1082 isConfiguredQualifierOrType(Tokens[9], ConfiguredTokens, LangOpts));
1084 EXPECT_TRUE(isQualifierOrType(Tokens[0], LangOpts));
1085 EXPECT_TRUE(isQualifierOrType(Tokens[1], LangOpts));
1086 EXPECT_TRUE(isQualifierOrType(Tokens[2], LangOpts));
1087 EXPECT_TRUE(isQualifierOrType(Tokens[3], LangOpts));
1088 EXPECT_TRUE(isQualifierOrType(Tokens[4], LangOpts));
1089 EXPECT_TRUE(isQualifierOrType(Tokens[5], LangOpts));
1090 EXPECT_TRUE(isQualifierOrType(Tokens[6], LangOpts));
1091 EXPECT_TRUE(isQualifierOrType(Tokens[7], LangOpts));
1092 EXPECT_TRUE(isQualifierOrType(Tokens[8], LangOpts));
1093 EXPECT_TRUE(isQualifierOrType(Tokens[9], LangOpts));
1095 auto NotTokens = lexer.lex("for while do Foo Bar ");
1096 ASSERT_EQ(NotTokens.size(), 6u) << Tokens;
1098 EXPECT_FALSE(
1099 isConfiguredQualifierOrType(NotTokens[0], ConfiguredTokens, LangOpts));
1100 EXPECT_FALSE(
1101 isConfiguredQualifierOrType(NotTokens[1], ConfiguredTokens, LangOpts));
1102 EXPECT_FALSE(
1103 isConfiguredQualifierOrType(NotTokens[2], ConfiguredTokens, LangOpts));
1104 EXPECT_FALSE(
1105 isConfiguredQualifierOrType(NotTokens[3], ConfiguredTokens, LangOpts));
1106 EXPECT_FALSE(
1107 isConfiguredQualifierOrType(NotTokens[4], ConfiguredTokens, LangOpts));
1108 EXPECT_FALSE(
1109 isConfiguredQualifierOrType(NotTokens[5], ConfiguredTokens, LangOpts));
1111 EXPECT_FALSE(isQualifierOrType(NotTokens[0], LangOpts));
1112 EXPECT_FALSE(isQualifierOrType(NotTokens[1], LangOpts));
1113 EXPECT_FALSE(isQualifierOrType(NotTokens[2], LangOpts));
1114 EXPECT_FALSE(isQualifierOrType(NotTokens[3], LangOpts));
1115 EXPECT_FALSE(isQualifierOrType(NotTokens[4], LangOpts));
1116 EXPECT_FALSE(isQualifierOrType(NotTokens[5], LangOpts));
1119 TEST_F(QualifierFixerTest, IsMacro) {
1121 auto Tokens = annotate("INT INTPR Foo int");
1122 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
1124 EXPECT_TRUE(isPossibleMacro(Tokens[0]));
1125 EXPECT_TRUE(isPossibleMacro(Tokens[1]));
1126 EXPECT_FALSE(isPossibleMacro(Tokens[2]));
1127 EXPECT_FALSE(isPossibleMacro(Tokens[3]));
1130 TEST_F(QualifierFixerTest, OverlappingQualifier) {
1132 FormatStyle Style = getLLVMStyle();
1133 Style.QualifierAlignment = FormatStyle::QAS_Left;
1134 Style.QualifierOrder = {"const", "type"};
1136 verifyFormat("Foo(const Bar &name);", "Foo(Bar const &name);", Style);
1139 TEST_F(QualifierFixerTest, DontPushQualifierThroughNonSpecifiedTypes) {
1141 FormatStyle Style = getLLVMStyle();
1142 Style.QualifierAlignment = FormatStyle::QAS_Left;
1143 Style.QualifierOrder = {"const", "volatile", "type"};
1145 verifyFormat("inline static const int a;", Style);
1147 Style.QualifierOrder = {"static", "const", "type"};
1149 verifyFormat("inline static const int a;", Style);
1150 verifyFormat("static inline const int a;", Style);
1152 verifyFormat("static const int a;", "const static int a;", Style);
1154 Style.QualifierOrder = {"const", "volatile", "type"};
1155 // static is not configured, unchanged at right hand qualifiers.
1156 verifyFormat("const volatile int static;", "int volatile static const;",
1157 Style);
1158 verifyFormat("const volatile int static;", "int const static volatile;",
1159 Style);
1160 verifyFormat("const volatile int static;", "const int static volatile;",
1161 Style);
1162 verifyFormat("const volatile Foo static;", "Foo volatile static const;",
1163 Style);
1164 verifyFormat("const volatile Foo static;", "Foo const static volatile;",
1165 Style);
1166 verifyFormat("const volatile Foo static;", "const Foo static volatile;",
1167 Style);
1169 verifyFormat("inline static const Foo;", "inline static Foo const;", Style);
1170 verifyFormat("inline static const Foo;", Style);
1172 // Don't move qualifiers to the right for aestethics only.
1173 verifyFormat("inline const static Foo;", Style);
1174 verifyFormat("const inline static Foo;", Style);
1177 TEST_F(QualifierFixerTest, QualifiersBrokenUpByPPDirectives) {
1178 auto Style = getLLVMStyle();
1179 Style.QualifierAlignment = FormatStyle::QAS_Custom;
1180 Style.QualifierOrder = {"constexpr", "inline", "type"};
1182 verifyFormat("inline\n"
1183 "#if FOO\n"
1184 " constexpr\n"
1185 "#endif\n"
1186 " int i = 0;",
1187 Style);
1190 TEST_F(QualifierFixerTest, UnsignedQualifier) {
1192 FormatStyle Style = getLLVMStyle();
1193 Style.QualifierAlignment = FormatStyle::QAS_Left;
1194 Style.QualifierOrder = {"const", "type"};
1196 verifyFormat("Foo(const unsigned char *bytes)",
1197 "Foo(unsigned const char *bytes)", Style);
1199 Style.QualifierAlignment = FormatStyle::QAS_Right;
1200 Style.QualifierOrder = {"type", "const"};
1202 verifyFormat("Foo(unsigned char const *bytes)",
1203 "Foo(unsigned const char *bytes)", Style);
1206 TEST_F(QualifierFixerTest, NoOpQualifierReplacements) {
1208 FormatStyle Style = getLLVMStyle();
1209 Style.QualifierAlignment = FormatStyle::QAS_Custom;
1210 Style.QualifierOrder = {"static", "const", "type"};
1212 verifyFormat("static const uint32 foo[] = {0, 31};", Style);
1213 EXPECT_EQ(ReplacementCount, 0);
1215 verifyFormat("#define MACRO static const", Style);
1216 EXPECT_EQ(ReplacementCount, 0);
1218 verifyFormat("using sc = static const", Style);
1219 EXPECT_EQ(ReplacementCount, 0);
1222 TEST_F(QualifierFixerTest, QualifierTemplates) {
1223 FormatStyle Style = getLLVMStyle();
1224 Style.QualifierAlignment = FormatStyle::QAS_Custom;
1225 Style.QualifierOrder = {"static", "const", "type"};
1227 ReplacementCount = 0;
1228 EXPECT_EQ(ReplacementCount, 0);
1229 verifyFormat("using A = B<>;", Style);
1230 verifyFormat("using A = B /**/<>;", Style);
1231 verifyFormat("template <class C> using A = B<Foo<C>, 1>;", Style);
1232 verifyFormat("template <class C> using A = B /**/<Foo<C>, 1>;", Style);
1233 verifyFormat("template <class C> using A = B /* */<Foo<C>, 1>;", Style);
1234 verifyFormat("template <class C> using A = B /*foo*/<Foo<C>, 1>;", Style);
1235 verifyFormat("template <class C> using A = B /**/ /**/<Foo<C>, 1>;", 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 EXPECT_EQ(ReplacementCount, 0);
1239 verifyFormat("template <class C>\n"
1240 "using A = B // foo\n"
1241 " <Foo<C>, 1>;",
1242 Style);
1244 ReplacementCount = 0;
1245 Style.QualifierOrder = {"type", "static", "const"};
1246 verifyFormat("using A = B<>;", Style);
1247 verifyFormat("using A = B /**/<>;", Style);
1248 verifyFormat("template <class C> using A = B<Foo<C>, 1>;", Style);
1249 verifyFormat("template <class C> using A = B /**/<Foo<C>, 1>;", Style);
1250 verifyFormat("template <class C> using A = B /* */<Foo<C>, 1>;", Style);
1251 verifyFormat("template <class C> using A = B /*foo*/<Foo<C>, 1>;", Style);
1252 verifyFormat("template <class C> using A = B /**/ /**/<Foo<C>, 1>;", 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 EXPECT_EQ(ReplacementCount, 0);
1256 verifyFormat("template <class C>\n"
1257 "using A = B // foo\n"
1258 " <Foo<C>, 1>;",
1259 Style);
1262 TEST_F(QualifierFixerTest, WithConstraints) {
1263 FormatStyle Style = getLLVMStyle();
1264 Style.QualifierAlignment = FormatStyle::QAS_Custom;
1265 Style.QualifierOrder = {"constexpr", "type"};
1267 verifyFormat("template <typename T>\n"
1268 " requires Concept<F>\n"
1269 "constexpr constructor();",
1270 Style);
1271 verifyFormat("template <typename T>\n"
1272 " requires Concept1<F> && Concept2<F>\n"
1273 "constexpr constructor();",
1274 Style);
1277 TEST_F(QualifierFixerTest, DisableRegions) {
1278 FormatStyle Style = getLLVMStyle();
1279 Style.QualifierAlignment = FormatStyle::QAS_Custom;
1280 Style.QualifierOrder = {"inline", "static", "const", "type"};
1282 ReplacementCount = 0;
1283 verifyFormat("// clang-format off\n"
1284 "int const inline static a = 0;\n"
1285 "// clang-format on",
1286 Style);
1287 EXPECT_EQ(ReplacementCount, 0);
1288 verifyFormat("// clang-format off\n"
1289 "int const inline static a = 0;\n"
1290 "// clang-format on\n"
1291 "inline static const int a = 0;",
1292 "// clang-format off\n"
1293 "int const inline static a = 0;\n"
1294 "// clang-format on\n"
1295 "int const inline static a = 0;",
1296 Style);
1299 TEST_F(QualifierFixerTest, TemplatesRight) {
1300 FormatStyle Style = getLLVMStyle();
1301 Style.QualifierAlignment = FormatStyle::QAS_Custom;
1302 Style.QualifierOrder = {"type", "const"};
1304 verifyFormat("template <typename T> Foo const f();",
1305 "template <typename T> const Foo f();", Style);
1306 verifyFormat("template <typename T> int const f();",
1307 "template <typename T> const int f();", Style);
1309 verifyFormat("template <T const> t;", "template <const T> t;", Style);
1310 verifyFormat("template <typename T>\n"
1311 " requires Concept<T const>\n"
1312 "Foo const f();",
1313 "template <typename T>\n"
1314 " requires Concept<const T>\n"
1315 "const Foo f();",
1316 Style);
1317 verifyFormat("TemplateType<T const> t;", "TemplateType<const T> t;", Style);
1318 verifyFormat("TemplateType<Container const> t;",
1319 "TemplateType<const Container> t;", Style);
1322 TEST_F(QualifierFixerTest, TemplatesLeft) {
1323 FormatStyle Style = getLLVMStyle();
1324 Style.QualifierAlignment = FormatStyle::QAS_Custom;
1325 Style.QualifierOrder = {"const", "volatile", "type"};
1327 verifyFormat("template <typename T> const Foo f();",
1328 "template <typename T> Foo const f();", Style);
1329 verifyFormat("template <typename T> const int f();",
1330 "template <typename T> int const f();", Style);
1332 verifyFormat("template <const T> t;", "template <T const> t;", Style);
1333 verifyFormat("template <typename T>\n"
1334 " requires Concept<const T>\n"
1335 "const Foo f();",
1336 "template <typename T>\n"
1337 " requires Concept<T const>\n"
1338 "Foo const f();",
1339 Style);
1340 verifyFormat("template <typename T>\n"
1341 " requires Concept<const T>\n"
1342 "const volatile Foo f();",
1343 "template <typename T>\n"
1344 " requires Concept<T const>\n"
1345 "volatile const Foo f();",
1346 Style);
1347 verifyFormat("TemplateType<const T> t;", "TemplateType<T const> t;", Style);
1348 verifyFormat("TemplateType<const Container> t;",
1349 "TemplateType<Container const> t;", Style);
1352 TEST_F(QualifierFixerTest, Ranges) {
1353 FormatStyle Style = getLLVMStyle();
1354 Style.QualifierAlignment = FormatStyle::QAS_Custom;
1355 Style.QualifierOrder = {"const", "volatile", "type"};
1357 // Only the first line should be formatted; the second should remain as is.
1358 verifyFormat("template <typename T> const Foo f();\n"
1359 "template <typename T> Foo const f();",
1360 "template <typename T> Foo const f();\n"
1361 "template <typename T> Foo const f();",
1362 Style, {tooling::Range(0, 36)});
1364 // Only the middle line should be formatted; the first and last should remain
1365 // as is.
1366 verifyFormat("template <typename T> Foo const f();\n"
1367 "template <typename T> const Foo f();\n"
1368 "template <typename T> Foo const f();",
1369 "template <typename T> Foo const f();\n"
1370 "template <typename T> Foo const f();\n"
1371 "template <typename T> Foo const f();",
1372 Style, {tooling::Range(37, 36)});
1375 } // namespace
1376 } // namespace test
1377 } // namespace format
1378 } // namespace clang