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 "clang/Format/Format.h"
10 
11 #include "FormatTestUtils.h"
12 #include "TestLexer.h"
13 #include "gtest/gtest.h"
14 
15 #include "../../lib/Format/QualifierAlignmentFixer.h"
16 
17 #define DEBUG_TYPE "format-qualifier-fixer-test"
18 
19 using testing::ScopedTrace;
20 
21 namespace clang {
22 namespace format {
23 namespace {
24 
25 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
26   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
27   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
28   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
29 
30 #define FAIL_PARSE(TEXT, FIELD, VALUE)                                         \
31   EXPECT_NE(0, parseConfiguration(TEXT, &Style).value());                      \
32   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
33 
34 class QualifierFixerTest : public ::testing::Test {
35 protected:
36   enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck };
37 
annotate(llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle ())38   TokenList annotate(llvm::StringRef Code,
39                      const FormatStyle &Style = getLLVMStyle()) {
40     return TestLexer(Allocator, Buffers, Style).annotate(Code);
41   }
42   llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
43   std::vector<std::unique_ptr<llvm::MemoryBuffer>> Buffers;
44 
format(llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle (),StatusCheck CheckComplete=SC_ExpectComplete)45   std::string format(llvm::StringRef Code,
46                      const FormatStyle &Style = getLLVMStyle(),
47                      StatusCheck CheckComplete = SC_ExpectComplete) {
48     LLVM_DEBUG(llvm::errs() << "---\n");
49     LLVM_DEBUG(llvm::errs() << Code << "\n\n");
50     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
51     FormattingAttemptStatus Status;
52     tooling::Replacements Replaces =
53         reformat(Style, Code, Ranges, "<stdin>", &Status);
54     if (CheckComplete != SC_DoNotCheck) {
55       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
56       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
57           << Code << "\n\n";
58     }
59     ReplacementCount = Replaces.size();
60     auto Result = applyAllReplacements(Code, Replaces);
61     EXPECT_TRUE(static_cast<bool>(Result));
62     LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
63     return *Result;
64   }
65 
getStyleWithColumns(FormatStyle Style,unsigned ColumnLimit)66   FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
67     Style.ColumnLimit = ColumnLimit;
68     return Style;
69   }
70 
getLLVMStyleWithColumns(unsigned ColumnLimit)71   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
72     return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
73   }
74 
_verifyFormat(const char * File,int Line,llvm::StringRef Expected,llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle ())75   void _verifyFormat(const char *File, int Line, llvm::StringRef Expected,
76                      llvm::StringRef Code,
77                      const FormatStyle &Style = getLLVMStyle()) {
78     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
79     EXPECT_EQ(Expected.str(), format(Expected, Style))
80         << "Expected code is not stable";
81     EXPECT_EQ(Expected.str(), format(Code, Style));
82     if (Style.Language == FormatStyle::LK_Cpp) {
83       // Objective-C++ is a superset of C++, so everything checked for C++
84       // needs to be checked for Objective-C++ as well.
85       FormatStyle ObjCStyle = Style;
86       ObjCStyle.Language = FormatStyle::LK_ObjC;
87       EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle));
88     }
89   }
90 
_verifyFormat(const char * File,int Line,llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle ())91   void _verifyFormat(const char *File, int Line, llvm::StringRef Code,
92                      const FormatStyle &Style = getLLVMStyle()) {
93     _verifyFormat(File, Line, Code, test::messUp(Code), Style);
94   }
95 
_verifyIncompleteFormat(const char * File,int Line,llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle ())96   void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code,
97                                const FormatStyle &Style = getLLVMStyle()) {
98     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
99     EXPECT_EQ(Code.str(),
100               format(test::messUp(Code), Style, SC_ExpectIncomplete));
101   }
102 
_verifyIndependentOfContext(const char * File,int Line,llvm::StringRef Text,const FormatStyle & Style=getLLVMStyle ())103   void _verifyIndependentOfContext(const char *File, int Line,
104                                    llvm::StringRef Text,
105                                    const FormatStyle &Style = getLLVMStyle()) {
106     _verifyFormat(File, Line, Text, Style);
107     _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(),
108                   Style);
109   }
110 
111   /// \brief Verify that clang-format does not crash on the given input.
verifyNoCrash(llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle ())112   void verifyNoCrash(llvm::StringRef Code,
113                      const FormatStyle &Style = getLLVMStyle()) {
114     format(Code, Style, SC_DoNotCheck);
115   }
116 
117   int ReplacementCount;
118 };
119 
120 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__)
121 
122 } // namespace
123 
TEST_F(QualifierFixerTest,RotateTokens)124 TEST_F(QualifierFixerTest, RotateTokens) {
125   // TODO add test
126   EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("const"),
127             tok::kw_const);
128   EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("volatile"),
129             tok::kw_volatile);
130   EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("inline"),
131             tok::kw_inline);
132   EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("static"),
133             tok::kw_static);
134   EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("restrict"),
135             tok::kw_restrict);
136 }
137 
TEST_F(QualifierFixerTest,FailQualifierInvalidConfiguration)138 TEST_F(QualifierFixerTest, FailQualifierInvalidConfiguration) {
139   FormatStyle Style = {};
140   Style.Language = FormatStyle::LK_Cpp;
141   FAIL_PARSE("QualifierAlignment: Custom\n"
142              "QualifierOrder: [const, volatile, apples, type]",
143              QualifierOrder,
144              std::vector<std::string>({"const", "volatile", "apples", "type"}));
145 }
146 
TEST_F(QualifierFixerTest,FailQualifierDuplicateConfiguration)147 TEST_F(QualifierFixerTest, FailQualifierDuplicateConfiguration) {
148   FormatStyle Style = {};
149   Style.Language = FormatStyle::LK_Cpp;
150   FAIL_PARSE("QualifierAlignment: Custom\n"
151              "QualifierOrder: [const, volatile, const, type]",
152              QualifierOrder,
153              std::vector<std::string>({"const", "volatile", "const", "type"}));
154 }
155 
TEST_F(QualifierFixerTest,FailQualifierMissingType)156 TEST_F(QualifierFixerTest, FailQualifierMissingType) {
157   FormatStyle Style = {};
158   Style.Language = FormatStyle::LK_Cpp;
159   FAIL_PARSE("QualifierAlignment: Custom\n"
160              "QualifierOrder: [const, volatile ]",
161              QualifierOrder,
162              std::vector<std::string>({
163                  "const",
164                  "volatile",
165              }));
166 }
167 
TEST_F(QualifierFixerTest,FailQualifierEmptyOrder)168 TEST_F(QualifierFixerTest, FailQualifierEmptyOrder) {
169   FormatStyle Style = {};
170   Style.Language = FormatStyle::LK_Cpp;
171   FAIL_PARSE("QualifierAlignment: Custom\nQualifierOrder: []", QualifierOrder,
172              std::vector<std::string>({}));
173 }
174 
TEST_F(QualifierFixerTest,FailQualifierMissingOrder)175 TEST_F(QualifierFixerTest, FailQualifierMissingOrder) {
176   FormatStyle Style = {};
177   Style.Language = FormatStyle::LK_Cpp;
178   FAIL_PARSE("QualifierAlignment: Custom", QualifierOrder,
179              std::vector<std::string>());
180 }
181 
TEST_F(QualifierFixerTest,QualifierLeft)182 TEST_F(QualifierFixerTest, QualifierLeft) {
183   FormatStyle Style = {};
184   Style.Language = FormatStyle::LK_Cpp;
185   CHECK_PARSE("QualifierAlignment: Left", QualifierOrder,
186               std::vector<std::string>({"const", "volatile", "type"}));
187 }
188 
TEST_F(QualifierFixerTest,QualifierRight)189 TEST_F(QualifierFixerTest, QualifierRight) {
190   FormatStyle Style = {};
191   Style.Language = FormatStyle::LK_Cpp;
192   CHECK_PARSE("QualifierAlignment: Right", QualifierOrder,
193               std::vector<std::string>({"type", "const", "volatile"}));
194 }
195 
TEST_F(QualifierFixerTest,QualifiersCustomOrder)196 TEST_F(QualifierFixerTest, QualifiersCustomOrder) {
197   FormatStyle Style = getLLVMStyle();
198   Style.QualifierAlignment = FormatStyle::QAS_Left;
199   Style.QualifierOrder = {"inline", "constexpr", "static",
200                           "const",  "volatile",  "type"};
201 
202   verifyFormat("const volatile int a;", "const volatile int a;", Style);
203   verifyFormat("const volatile int a;", "volatile const int a;", Style);
204   verifyFormat("const volatile int a;", "int const volatile a;", Style);
205   verifyFormat("const volatile int a;", "int volatile const a;", Style);
206   verifyFormat("const volatile int a;", "const int volatile a;", Style);
207 
208   verifyFormat("static const volatile int a;", "const static int volatile a;",
209                Style);
210   verifyFormat("inline static const volatile int a;",
211                "const static inline int volatile a;", Style);
212 
213   verifyFormat("constexpr static int a;", "static constexpr int a;", Style);
214   verifyFormat("constexpr static int A;", "static constexpr int A;", Style);
215   verifyFormat("constexpr static int Bar;", "static constexpr int Bar;", Style);
216   verifyFormat("constexpr static LPINT Bar;", "static constexpr LPINT Bar;",
217                Style);
218   verifyFormat("const const int a;", "const int const a;", Style);
219 }
220 
TEST_F(QualifierFixerTest,LeftRightQualifier)221 TEST_F(QualifierFixerTest, LeftRightQualifier) {
222   FormatStyle Style = getLLVMStyle();
223 
224   // keep the const style unaltered
225   verifyFormat("const int a;", Style);
226   verifyFormat("const int *a;", Style);
227   verifyFormat("const int &a;", Style);
228   verifyFormat("const int &&a;", Style);
229   verifyFormat("int const b;", Style);
230   verifyFormat("int const *b;", Style);
231   verifyFormat("int const &b;", Style);
232   verifyFormat("int const &&b;", Style);
233   verifyFormat("int const *b const;", Style);
234   verifyFormat("int *const c;", Style);
235 
236   verifyFormat("const Foo a;", Style);
237   verifyFormat("const Foo *a;", Style);
238   verifyFormat("const Foo &a;", Style);
239   verifyFormat("const Foo &&a;", Style);
240   verifyFormat("Foo const b;", Style);
241   verifyFormat("Foo const *b;", Style);
242   verifyFormat("Foo const &b;", Style);
243   verifyFormat("Foo const &&b;", Style);
244   verifyFormat("Foo const *b const;", Style);
245 
246   verifyFormat("LLVM_NODISCARD const int &Foo();", Style);
247   verifyFormat("LLVM_NODISCARD int const &Foo();", Style);
248 
249   verifyFormat("volatile const int *restrict;", Style);
250   verifyFormat("const volatile int *restrict;", Style);
251   verifyFormat("const int volatile *restrict;", Style);
252 }
253 
TEST_F(QualifierFixerTest,RightQualifier)254 TEST_F(QualifierFixerTest, RightQualifier) {
255   FormatStyle Style = getLLVMStyle();
256   Style.QualifierAlignment = FormatStyle::QAS_Right;
257   Style.QualifierOrder = {"type", "const", "volatile"};
258 
259   verifyFormat("int const a;", Style);
260   verifyFormat("int const *a;", Style);
261   verifyFormat("int const &a;", Style);
262   verifyFormat("int const &&a;", Style);
263   verifyFormat("int const b;", Style);
264   verifyFormat("int const *b;", Style);
265   verifyFormat("int const &b;", Style);
266   verifyFormat("int const &&b;", Style);
267   verifyFormat("int const *b const;", Style);
268   verifyFormat("int *const c;", Style);
269 
270   verifyFormat("Foo const a;", Style);
271   verifyFormat("Foo const *a;", Style);
272   verifyFormat("Foo const &a;", Style);
273   verifyFormat("Foo const &&a;", Style);
274   verifyFormat("Foo const b;", Style);
275   verifyFormat("Foo const *b;", Style);
276   verifyFormat("Foo const &b;", Style);
277   verifyFormat("Foo const &&b;", Style);
278   verifyFormat("Foo const *b const;", Style);
279   verifyFormat("Foo *const b;", Style);
280   verifyFormat("Foo const *const b;", Style);
281   verifyFormat("auto const v = get_value();", Style);
282   verifyFormat("long long const &a;", Style);
283   verifyFormat("unsigned char const *a;", Style);
284   verifyFormat("int main(int const argc, char const *const *const argv)",
285                Style);
286 
287   verifyFormat("LLVM_NODISCARD int const &Foo();", Style);
288   verifyFormat("SourceRange getSourceRange() const override LLVM_READONLY",
289                Style);
290   verifyFormat("void foo() const override;", Style);
291   verifyFormat("void foo() const override LLVM_READONLY;", Style);
292   verifyFormat("void foo() const final;", Style);
293   verifyFormat("void foo() const final LLVM_READONLY;", Style);
294   verifyFormat("void foo() const LLVM_READONLY;", Style);
295 
296   verifyFormat(
297       "template <typename Func> explicit Action(Action<Func> const &action);",
298       Style);
299   verifyFormat(
300       "template <typename Func> explicit Action(Action<Func> const &action);",
301       "template <typename Func> explicit Action(const Action<Func>& action);",
302       Style);
303   verifyFormat(
304       "template <typename Func> explicit Action(Action<Func> const &action);",
305       "template <typename Func>\nexplicit Action(const Action<Func>& action);",
306       Style);
307 
308   verifyFormat("int const a;", "const int a;", Style);
309   verifyFormat("int const *a;", "const int *a;", Style);
310   verifyFormat("int const &a;", "const int &a;", Style);
311   verifyFormat("foo(int const &a)", "foo(const int &a)", Style);
312   verifyFormat("unsigned char *a;", "unsigned char *a;", Style);
313   verifyFormat("unsigned char const *a;", "const unsigned char *a;", Style);
314   verifyFormat("vector<int, int const, int &, int const &> args1",
315                "vector<int, const int, int &, const int &> args1", Style);
316   verifyFormat("unsigned int const &get_nu() const",
317                "const unsigned int &get_nu() const", Style);
318   verifyFormat("Foo<int> const &a", "const Foo<int> &a", Style);
319   verifyFormat("Foo<int>::iterator const &a", "const Foo<int>::iterator &a",
320                Style);
321 
322   verifyFormat("Foo(int a, "
323                "unsigned b, // c-style args\n"
324                "    Bar const &c);",
325                "Foo(int a, "
326                "unsigned b, // c-style args\n"
327                "    const Bar &c);",
328                Style);
329 
330   verifyFormat("int const volatile;", "volatile const int;", Style);
331   verifyFormat("int const volatile;", "const volatile int;", Style);
332   verifyFormat("int const volatile;", "const int volatile;", Style);
333   verifyFormat("int const volatile *restrict;", "volatile const int *restrict;",
334                Style);
335   verifyFormat("int const volatile *restrict;", "const volatile int *restrict;",
336                Style);
337   verifyFormat("int const volatile *restrict;", "const int volatile *restrict;",
338                Style);
339 
340   verifyFormat("static int const bat;", "static const int bat;", Style);
341   verifyFormat("static int const bat;", "static int const bat;", Style);
342 
343   verifyFormat("int const Foo<int>::bat = 0;", "const int Foo<int>::bat = 0;",
344                Style);
345   verifyFormat("int const Foo<int>::bat = 0;", "int const Foo<int>::bat = 0;",
346                Style);
347   verifyFormat("void fn(Foo<T> const &i);", "void fn(const Foo<T> &i);", Style);
348   verifyFormat("int const Foo<int>::fn() {", "int const Foo<int>::fn() {",
349                Style);
350   verifyFormat("Foo<Foo<int>> const *p;", "const Foo<Foo<int>> *p;", Style);
351   verifyFormat(
352       "Foo<Foo<int>> const *p = const_cast<Foo<Foo<int>> const *>(&ffi);",
353       "const Foo<Foo<int>> *p = const_cast<const Foo<Foo<int>> *>(&ffi);",
354       Style);
355 
356   verifyFormat("void fn(Foo<T> const &i);", "void fn(const Foo<T> &i);", Style);
357   verifyFormat("void fns(ns::S const &s);", "void fns(const ns::S &s);", Style);
358   verifyFormat("void fn(ns::Foo<T> const &i);", "void fn(const ns::Foo<T> &i);",
359                Style);
360   verifyFormat("void fns(ns::ns2::S const &s);",
361                "void fns(const ns::ns2::S &s);", Style);
362   verifyFormat("void fn(ns::Foo<Bar<T>> const &i);",
363                "void fn(const ns::Foo<Bar<T>> &i);", Style);
364   verifyFormat("void fn(ns::ns2::Foo<Bar<T>> const &i);",
365                "void fn(const ns::ns2::Foo<Bar<T>> &i);", Style);
366   verifyFormat("void fn(ns::ns2::Foo<Bar<T, U>> const &i);",
367                "void fn(const ns::ns2::Foo<Bar<T, U>> &i);", Style);
368 
369   verifyFormat("LocalScope const *Scope = nullptr;",
370                "const LocalScope* Scope = nullptr;", Style);
371   verifyFormat("struct DOTGraphTraits<Stmt const *>",
372                "struct DOTGraphTraits<const Stmt *>", Style);
373 
374   verifyFormat(
375       "bool tools::addXRayRuntime(ToolChain const &TC, ArgList const &Args) {",
376       "bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args) {",
377       Style);
378   verifyFormat("Foo<Foo<int> const> P;", "Foo<const Foo<int>> P;", Style);
379   verifyFormat("Foo<Foo<int> const> P;\n", "Foo<const Foo<int>> P;\n", Style);
380   verifyFormat("Foo<Foo<int> const> P;\n#if 0\n#else\n#endif",
381                "Foo<const Foo<int>> P;\n#if 0\n#else\n#endif", Style);
382 
383   verifyFormat("auto const i = 0;", "const auto i = 0;", Style);
384   verifyFormat("auto const &ir = i;", "const auto &ir = i;", Style);
385   verifyFormat("auto const *ip = &i;", "const auto *ip = &i;", Style);
386 
387   verifyFormat("Foo<Foo<int> const> P;\n#if 0\n#else\n#endif",
388                "Foo<const Foo<int>> P;\n#if 0\n#else\n#endif", Style);
389 
390   verifyFormat("Bar<Bar<int const> const> P;\n#if 0\n#else\n#endif",
391                "Bar<Bar<const int> const> P;\n#if 0\n#else\n#endif", Style);
392 
393   verifyFormat("Baz<Baz<int const> const> P;\n#if 0\n#else\n#endif",
394                "Baz<const Baz<const int>> P;\n#if 0\n#else\n#endif", Style);
395 
396   // verifyFormat("#if 0\nBoo<Boo<int const> const> P;\n#else\n#endif",
397   //             "#if 0\nBoo<const Boo<const int>> P;\n#else\n#endif", Style);
398 
399   verifyFormat("int const P;\n#if 0\n#else\n#endif",
400                "const int P;\n#if 0\n#else\n#endif", Style);
401 
402   verifyFormat("unsigned long const a;", "const unsigned long a;", Style);
403   verifyFormat("unsigned long long const a;", "const unsigned long long a;",
404                Style);
405 
406   // don't adjust macros
407   verifyFormat("const INTPTR a;", "const INTPTR a;", Style);
408 }
409 
TEST_F(QualifierFixerTest,LeftQualifier)410 TEST_F(QualifierFixerTest, LeftQualifier) {
411   FormatStyle Style = getLLVMStyle();
412   Style.QualifierAlignment = FormatStyle::QAS_Left;
413   Style.QualifierOrder = {"inline", "static", "const", "volatile", "type"};
414 
415   verifyFormat("const int a;", Style);
416   verifyFormat("const int *a;", Style);
417   verifyFormat("const int &a;", Style);
418   verifyFormat("const int &&a;", Style);
419   verifyFormat("const int b;", Style);
420   verifyFormat("const int *b;", Style);
421   verifyFormat("const int &b;", Style);
422   verifyFormat("const int &&b;", Style);
423   verifyFormat("const int *b const;", Style);
424   verifyFormat("int *const c;", Style);
425 
426   verifyFormat("const Foo a;", Style);
427   verifyFormat("const Foo *a;", Style);
428   verifyFormat("const Foo &a;", Style);
429   verifyFormat("const Foo &&a;", Style);
430   verifyFormat("const Foo b;", Style);
431   verifyFormat("const Foo *b;", Style);
432   verifyFormat("const Foo &b;", Style);
433   verifyFormat("const Foo &&b;", Style);
434   verifyFormat("const Foo *b const;", Style);
435   verifyFormat("Foo *const b;", Style);
436   verifyFormat("const Foo *const b;", Style);
437 
438   verifyFormat("LLVM_NODISCARD const int &Foo();", Style);
439 
440   verifyFormat("const char a[];", Style);
441   verifyFormat("const auto v = get_value();", Style);
442   verifyFormat("const long long &a;", Style);
443   verifyFormat("const unsigned char *a;", Style);
444   verifyFormat("const unsigned char *a;", "unsigned char const *a;", Style);
445   verifyFormat("const Foo<int> &a", "Foo<int> const &a", Style);
446   verifyFormat("const Foo<int>::iterator &a", "Foo<int>::iterator const &a",
447                Style);
448 
449   verifyFormat("const int a;", "int const a;", Style);
450   verifyFormat("const int *a;", "int const *a;", Style);
451   verifyFormat("const int &a;", "int const &a;", Style);
452   verifyFormat("foo(const int &a)", "foo(int const &a)", Style);
453   verifyFormat("unsigned char *a;", "unsigned char *a;", Style);
454   verifyFormat("const unsigned int &get_nu() const",
455                "unsigned int const &get_nu() const", Style);
456 
457   verifyFormat("const volatile int;", "volatile const int;", Style);
458   verifyFormat("const volatile int;", "const volatile int;", Style);
459   verifyFormat("const volatile int;", "const int volatile;", Style);
460 
461   verifyFormat("const volatile int *restrict;", "volatile const int *restrict;",
462                Style);
463   verifyFormat("const volatile int *restrict;", "const volatile int *restrict;",
464                Style);
465   verifyFormat("const volatile int *restrict;", "const int volatile *restrict;",
466                Style);
467 
468   verifyFormat("SourceRange getSourceRange() const override LLVM_READONLY;",
469                Style);
470 
471   verifyFormat("void foo() const override;", Style);
472   verifyFormat("void foo() const override LLVM_READONLY;", Style);
473   verifyFormat("void foo() const final;", Style);
474   verifyFormat("void foo() const final LLVM_READONLY;", Style);
475   verifyFormat("void foo() const LLVM_READONLY;", Style);
476 
477   verifyFormat(
478       "template <typename Func> explicit Action(const Action<Func> &action);",
479       Style);
480   verifyFormat(
481       "template <typename Func> explicit Action(const Action<Func> &action);",
482       "template <typename Func> explicit Action(Action<Func> const &action);",
483       Style);
484 
485   verifyFormat("static const int bat;", "static const int bat;", Style);
486   verifyFormat("static const int bat;", "static int const bat;", Style);
487 
488   verifyFormat("static const int Foo<int>::bat = 0;",
489                "static const int Foo<int>::bat = 0;", Style);
490   verifyFormat("static const int Foo<int>::bat = 0;",
491                "static int const Foo<int>::bat = 0;", Style);
492 
493   verifyFormat("void fn(const Foo<T> &i);");
494 
495   verifyFormat("const int Foo<int>::bat = 0;", "const int Foo<int>::bat = 0;",
496                Style);
497   verifyFormat("const int Foo<int>::bat = 0;", "int const Foo<int>::bat = 0;",
498                Style);
499   verifyFormat("void fn(const Foo<T> &i);", "void fn( Foo<T> const &i);",
500                Style);
501   verifyFormat("const int Foo<int>::fn() {", "int const Foo<int>::fn() {",
502                Style);
503   verifyFormat("const Foo<Foo<int>> *p;", "Foo<Foo<int>> const *p;", Style);
504   verifyFormat(
505       "const Foo<Foo<int>> *p = const_cast<const Foo<Foo<int>> *>(&ffi);",
506       "const Foo<Foo<int>> *p = const_cast<Foo<Foo<int>> const *>(&ffi);",
507       Style);
508 
509   verifyFormat("void fn(const Foo<T> &i);", "void fn(Foo<T> const &i);", Style);
510   verifyFormat("void fns(const ns::S &s);", "void fns(ns::S const &s);", Style);
511   verifyFormat("void fn(const ns::Foo<T> &i);", "void fn(ns::Foo<T> const &i);",
512                Style);
513   verifyFormat("void fns(const ns::ns2::S &s);",
514                "void fns(ns::ns2::S const &s);", Style);
515   verifyFormat("void fn(const ns::Foo<Bar<T>> &i);",
516                "void fn(ns::Foo<Bar<T>> const &i);", Style);
517   verifyFormat("void fn(const ns::ns2::Foo<Bar<T>> &i);",
518                "void fn(ns::ns2::Foo<Bar<T>> const &i);", Style);
519   verifyFormat("void fn(const ns::ns2::Foo<Bar<T, U>> &i);",
520                "void fn(ns::ns2::Foo<Bar<T, U>> const &i);", Style);
521 
522   verifyFormat("const auto i = 0;", "auto const i = 0;", Style);
523   verifyFormat("const auto &ir = i;", "auto const &ir = i;", Style);
524   verifyFormat("const auto *ip = &i;", "auto const *ip = &i;", Style);
525 
526   verifyFormat("Foo<const Foo<int>> P;\n#if 0\n#else\n#endif",
527                "Foo<Foo<int> const> P;\n#if 0\n#else\n#endif", Style);
528 
529   verifyFormat("Foo<Foo<const int>> P;\n#if 0\n#else\n#endif",
530                "Foo<Foo<int const>> P;\n#if 0\n#else\n#endif", Style);
531 
532   verifyFormat("const int P;\n#if 0\n#else\n#endif",
533                "int const P;\n#if 0\n#else\n#endif", Style);
534 
535   verifyFormat("const unsigned long a;", "unsigned long const a;", Style);
536   verifyFormat("const unsigned long long a;", "unsigned long long const a;",
537                Style);
538 
539   verifyFormat("const long long unsigned a;", "long const long unsigned a;",
540                Style);
541 
542   verifyFormat("const std::Foo", "const std::Foo", Style);
543   verifyFormat("const std::Foo<>", "const std::Foo<>", Style);
544   verifyFormat("const std::Foo < int", "const std::Foo<int", Style);
545   verifyFormat("const std::Foo<int>", "const std::Foo<int>", Style);
546 
547   // don't adjust macros
548   verifyFormat("INTPTR const a;", "INTPTR const a;", Style);
549 }
550 
TEST_F(QualifierFixerTest,ConstVolatileQualifiersOrder)551 TEST_F(QualifierFixerTest, ConstVolatileQualifiersOrder) {
552   FormatStyle Style = getLLVMStyle();
553   Style.QualifierAlignment = FormatStyle::QAS_Left;
554   Style.QualifierOrder = {"inline", "static", "const", "volatile", "type"};
555 
556   // The Default
557   EXPECT_EQ(Style.QualifierOrder.size(), (size_t)5);
558 
559   verifyFormat("const volatile int a;", "const volatile int a;", Style);
560   verifyFormat("const volatile int a;", "volatile const int a;", Style);
561   verifyFormat("const volatile int a;", "int const volatile a;", Style);
562   verifyFormat("const volatile int a;", "int volatile const a;", Style);
563   verifyFormat("const volatile int a;", "const int volatile a;", Style);
564 
565   Style.QualifierAlignment = FormatStyle::QAS_Right;
566   Style.QualifierOrder = {"type", "const", "volatile"};
567 
568   verifyFormat("int const volatile a;", "const volatile int a;", Style);
569   verifyFormat("int const volatile a;", "volatile const int a;", Style);
570   verifyFormat("int const volatile a;", "int const volatile a;", Style);
571   verifyFormat("int const volatile a;", "int volatile const a;", Style);
572   verifyFormat("int const volatile a;", "const int volatile a;", Style);
573 
574   Style.QualifierAlignment = FormatStyle::QAS_Left;
575   Style.QualifierOrder = {"volatile", "const", "type"};
576 
577   verifyFormat("volatile const int a;", "const volatile int a;", Style);
578   verifyFormat("volatile const int a;", "volatile const int a;", Style);
579   verifyFormat("volatile const int a;", "int const volatile a;", Style);
580   verifyFormat("volatile const int a;", "int volatile const a;", Style);
581   verifyFormat("volatile const int a;", "const int volatile a;", Style);
582 
583   Style.QualifierAlignment = FormatStyle::QAS_Right;
584   Style.QualifierOrder = {"type", "volatile", "const"};
585 
586   verifyFormat("int volatile const a;", "const volatile int a;", Style);
587   verifyFormat("int volatile const a;", "volatile const int a;", Style);
588   verifyFormat("int volatile const a;", "int const volatile a;", Style);
589   verifyFormat("int volatile const a;", "int volatile const a;", Style);
590   verifyFormat("int volatile const a;", "const int volatile a;", Style);
591 
592   Style.QualifierAlignment = FormatStyle::QAS_Custom;
593   Style.QualifierOrder = {"type", "volatile", "const"};
594 
595   verifyFormat("int volatile const a;", "const volatile int a;", Style);
596   verifyFormat("int volatile const a;", "volatile const int a;", Style);
597   verifyFormat("int volatile const a;", "int const volatile a;", Style);
598   verifyFormat("int volatile const a;", "int volatile const a;", Style);
599   verifyFormat("int volatile const a;", "const int volatile a;", Style);
600 }
601 
TEST_F(QualifierFixerTest,InlineStatics)602 TEST_F(QualifierFixerTest, InlineStatics) {
603   FormatStyle Style = getLLVMStyle();
604   Style.QualifierAlignment = FormatStyle::QAS_Left;
605   Style.QualifierOrder = {"inline", "static", "const", "volatile", "type"};
606   EXPECT_EQ(Style.QualifierOrder.size(), (size_t)5);
607 
608   verifyFormat("inline static const volatile int a;",
609                "const inline static volatile int a;", Style);
610   verifyFormat("inline static const volatile int a;",
611                "volatile inline static const int a;", Style);
612   verifyFormat("inline static const volatile int a;",
613                "int const inline static  volatile a;", Style);
614   verifyFormat("inline static const volatile int a;",
615                "int volatile inline static  const a;", Style);
616   verifyFormat("inline static const volatile int a;",
617                "const int inline static  volatile a;", Style);
618 }
619 
TEST_F(QualifierFixerTest,AmpEqual)620 TEST_F(QualifierFixerTest, AmpEqual) {
621   FormatStyle Style = getLLVMStyle();
622   Style.QualifierAlignment = FormatStyle::QAS_Custom;
623   Style.QualifierOrder = {"static", "type", "const"};
624   EXPECT_EQ(Style.QualifierOrder.size(), (size_t)3);
625 
626   verifyFormat("foo(std::string const & = std::string()) const",
627                "foo(const std::string & = std::string()) const", Style);
628   verifyFormat("foo(std::string const & = std::string())",
629                "foo(const std::string & = std::string())", Style);
630 }
631 
TEST_F(QualifierFixerTest,MoveConstBeyondTypeSmall)632 TEST_F(QualifierFixerTest, MoveConstBeyondTypeSmall) {
633 
634   FormatStyle Style = getLLVMStyle();
635   Style.QualifierAlignment = FormatStyle::QAS_Custom;
636   Style.QualifierOrder = {"type", "const"};
637   EXPECT_EQ(Style.QualifierOrder.size(), (size_t)2);
638 
639   verifyFormat("int const a;", "const int a;", Style);
640   verifyFormat("int const *a;", "const int*a;", Style);
641   verifyFormat("int const *a;", "const int *a;", Style);
642   verifyFormat("int const &a;", "const int &a;", Style);
643   verifyFormat("int const &&a;", "const int &&a;", Style);
644 }
645 
TEST_F(QualifierFixerTest,MoveConstBeforeTypeSmall)646 TEST_F(QualifierFixerTest, MoveConstBeforeTypeSmall) {
647 
648   FormatStyle Style = getLLVMStyle();
649   Style.QualifierAlignment = FormatStyle::QAS_Custom;
650   Style.QualifierOrder = {"const", "type"};
651   EXPECT_EQ(Style.QualifierOrder.size(), (size_t)2);
652 
653   verifyFormat("const int a;", "int const a;", Style);
654   verifyFormat("const int *a;", "int const *a;", Style);
655   verifyFormat("const int *a const;", "int const *a const;", Style);
656 
657   verifyFormat("const int a = foo();", "int const a = foo();", Style);
658   verifyFormat("const int *a = foo();", "int const *a = foo();", Style);
659   verifyFormat("const int *a const = foo();", "int const *a const = foo();",
660                Style);
661 
662   verifyFormat("const auto a = foo();", "auto const a = foo();", Style);
663   verifyFormat("const auto *a = foo();", "auto const *a = foo();", Style);
664   verifyFormat("const auto *a const = foo();", "auto const *a const = foo();",
665                Style);
666 }
667 
TEST_F(QualifierFixerTest,MoveConstBeyondType)668 TEST_F(QualifierFixerTest, MoveConstBeyondType) {
669 
670   FormatStyle Style = getLLVMStyle();
671   Style.QualifierAlignment = FormatStyle::QAS_Custom;
672   Style.QualifierOrder = {"static", "inline", "type", "const", "volatile"};
673   EXPECT_EQ(Style.QualifierOrder.size(), (size_t)5);
674 
675   verifyFormat("static inline int const volatile a;",
676                "const inline static volatile int a;", Style);
677   verifyFormat("static inline int const volatile a;",
678                "volatile inline static const int a;", Style);
679   verifyFormat("static inline int const volatile a;",
680                "int const inline static  volatile a;", Style);
681   verifyFormat("static inline int const volatile a;",
682                "int volatile inline static  const a;", Style);
683   verifyFormat("static inline int const volatile a;",
684                "const int inline static  volatile a;", Style);
685 
686   verifyFormat("static inline int const volatile *a const;",
687                "const int inline static  volatile *a const;", Style);
688 }
689 
TEST_F(QualifierFixerTest,PrepareLeftRightOrdering)690 TEST_F(QualifierFixerTest, PrepareLeftRightOrdering) {
691   FormatStyle Style = getLLVMStyle();
692   Style.QualifierAlignment = FormatStyle::QAS_Custom;
693   Style.QualifierOrder = {"static", "inline", "type", "const", "volatile"};
694 
695   std::vector<std::string> Left;
696   std::vector<std::string> Right;
697   std::vector<tok::TokenKind> ConfiguredTokens;
698   QualifierAlignmentFixer::PrepareLeftRightOrdering(Style.QualifierOrder, Left,
699                                                     Right, ConfiguredTokens);
700 
701   EXPECT_EQ(Left.size(), (size_t)2);
702   EXPECT_EQ(Right.size(), (size_t)2);
703 
704   std::vector<std::string> LeftResult = {"inline", "static"};
705   std::vector<std::string> RightResult = {"const", "volatile"};
706   EXPECT_EQ(Left, LeftResult);
707   EXPECT_EQ(Right, RightResult);
708 }
709 
TEST_F(QualifierFixerTest,IsQualifierType)710 TEST_F(QualifierFixerTest, IsQualifierType) {
711 
712   std::vector<tok::TokenKind> ConfiguredTokens;
713   ConfiguredTokens.push_back(tok::kw_const);
714   ConfiguredTokens.push_back(tok::kw_static);
715   ConfiguredTokens.push_back(tok::kw_inline);
716   ConfiguredTokens.push_back(tok::kw_restrict);
717   ConfiguredTokens.push_back(tok::kw_constexpr);
718 
719   auto Tokens =
720       annotate("const static inline auto restrict int double long constexpr");
721 
722   EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
723       Tokens[0], ConfiguredTokens));
724   EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
725       Tokens[1], ConfiguredTokens));
726   EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
727       Tokens[2], ConfiguredTokens));
728   EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
729       Tokens[3], ConfiguredTokens));
730   EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
731       Tokens[4], ConfiguredTokens));
732   EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
733       Tokens[5], ConfiguredTokens));
734   EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
735       Tokens[6], ConfiguredTokens));
736   EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
737       Tokens[7], ConfiguredTokens));
738   EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
739       Tokens[8], ConfiguredTokens));
740 
741   auto NotTokens = annotate("for while do Foo Bar ");
742 
743   EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
744       NotTokens[0], ConfiguredTokens));
745   EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
746       NotTokens[1], ConfiguredTokens));
747   EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
748       NotTokens[2], ConfiguredTokens));
749   EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
750       NotTokens[3], ConfiguredTokens));
751   EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
752       NotTokens[4], ConfiguredTokens));
753   EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
754       NotTokens[5], ConfiguredTokens));
755 }
756 
TEST_F(QualifierFixerTest,IsMacro)757 TEST_F(QualifierFixerTest, IsMacro) {
758 
759   auto Tokens = annotate("INT INTPR Foo int");
760 
761   EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isPossibleMacro(Tokens[0]));
762   EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isPossibleMacro(Tokens[1]));
763   EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isPossibleMacro(Tokens[2]));
764   EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isPossibleMacro(Tokens[3]));
765 }
766 
TEST_F(QualifierFixerTest,OverlappingQualifier)767 TEST_F(QualifierFixerTest, OverlappingQualifier) {
768 
769   FormatStyle Style = getLLVMStyle();
770   Style.QualifierAlignment = FormatStyle::QAS_Left;
771   Style.QualifierOrder = {"const", "type"};
772 
773   verifyFormat("Foo(const Bar &name);", "Foo(Bar const &name);", Style);
774 }
775 
TEST_F(QualifierFixerTest,DontPushQualifierThroughNonSpecifiedTypes)776 TEST_F(QualifierFixerTest, DontPushQualifierThroughNonSpecifiedTypes) {
777 
778   FormatStyle Style = getLLVMStyle();
779   Style.QualifierAlignment = FormatStyle::QAS_Left;
780   Style.QualifierOrder = {"const", "type"};
781 
782   verifyFormat("inline static const int a;", Style);
783 
784   Style.QualifierOrder = {"static", "const", "type"};
785 
786   verifyFormat("inline static const int a;", Style);
787   verifyFormat("static inline const int a;", "static inline const int a;",
788                Style);
789 
790   verifyFormat("static const int a;", "const static int a;", Style);
791 }
792 
TEST_F(QualifierFixerTest,UnsignedQualifier)793 TEST_F(QualifierFixerTest, UnsignedQualifier) {
794 
795   FormatStyle Style = getLLVMStyle();
796   Style.QualifierAlignment = FormatStyle::QAS_Left;
797   Style.QualifierOrder = {"const", "type"};
798 
799   verifyFormat("Foo(const unsigned char *bytes)",
800                "Foo(unsigned const char *bytes)", Style);
801 
802   Style.QualifierAlignment = FormatStyle::QAS_Right;
803   Style.QualifierOrder = {"type", "const"};
804 
805   verifyFormat("Foo(unsigned char const *bytes)",
806                "Foo(unsigned const char *bytes)", Style);
807 }
808 
TEST_F(QualifierFixerTest,NoOpQualifierReplacements)809 TEST_F(QualifierFixerTest, NoOpQualifierReplacements) {
810 
811   FormatStyle Style = getLLVMStyle();
812   Style.QualifierAlignment = FormatStyle::QAS_Custom;
813   Style.QualifierOrder = {"static", "const", "type"};
814 
815   ReplacementCount = 0;
816   EXPECT_EQ(ReplacementCount, 0);
817   verifyFormat("static const uint32 foo[] = {0, 31};", Style);
818   EXPECT_EQ(ReplacementCount, 0);
819 }
820 
821 } // namespace format
822 } // namespace clang
823