1 //===- unittest/Format/FormatMacroExpansion.cpp - Formatting unit tests ---===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "FormatTestBase.h"
11 #define DEBUG_TYPE "format-test-macro-expansion"
18 class FormatTestMacroExpansion
: public FormatTestBase
{};
20 TEST_F(FormatTestMacroExpansion
, UnexpandConfiguredMacros
) {
21 FormatStyle Style
= getLLVMStyle();
22 Style
.Macros
.push_back("CLASS=class C {");
23 Style
.Macros
.push_back("SEMI=;");
24 Style
.Macros
.push_back("STMT=f();");
25 Style
.Macros
.push_back("ID(x)=x");
26 Style
.Macros
.push_back("ID3(x, y, z)=x y z");
27 Style
.Macros
.push_back("CALL(x)=f([] { x })");
28 Style
.Macros
.push_back("ASSIGN_OR_RETURN(a, b)=a = (b)");
29 Style
.Macros
.push_back("ASSIGN_OR_RETURN(a, b, c)=a = (b); if (x) return c");
30 Style
.Macros
.push_back("MOCK_METHOD(r, n, a, s)=r n a s");
32 verifyFormat("ID(nested(a(b, c), d))", Style
);
33 verifyFormat("CLASS\n"
45 verifyFormat("void f() { ID(a *b); }", Style
);
50 verifyIncompleteFormat(R
"(ID3({, ID(a *b),
56 verifyFormat("ID(CALL(CALL(return a * b;)));", Style
);
58 verifyFormat("ASSIGN_OR_RETURN(MySomewhatLongType *variable,\n"
59 " MySomewhatLongFunction(SomethingElse()));\n",
61 verifyFormat("ASSIGN_OR_RETURN(MySomewhatLongType *variable,\n"
62 " MySomewhatLongFunction(SomethingElse()), "
67 #define MACRO(a, b) ID(a + b)
90 ID(namespace foo { int a; }) // namespace k
99 Style
.ColumnLimit
= 35;
100 // FIXME: Arbitrary formatting of macros where the end of the logical
101 // line is in the middle of a macro call are not working yet.
111 Style
.ColumnLimit
= 10;
112 verifyFormat("STMT\n"
122 ID(CALL(CALL(a * b)));
126 // FIXME: If we want to support unbalanced braces or parens from macro
127 // expansions we need to re-think how we propagate errors in
128 // TokenAnnotator::parseLine; for investigation, switching the inner loop of
129 // TokenAnnotator::parseLine to return LT_Other instead of LT_Invalid in case
130 // of !consumeToken() changes the formatting of the test below and makes it
131 // believe it has a fully correct formatting.
147 ID3({CLASS a*b; };}, ID(x*y);, STMT STMT STMT)
152 verifyFormat("ID(a(\n"
160 Style
.ColumnLimit
= 80;
161 verifyFormat(R
"(ASSIGN_OR_RETURN(
166 Style
.ColumnLimit
= 30;
167 verifyFormat(R
"(ASSIGN_OR_RETURN(
180 verifyFormat(R
"(int a = []() {
190 R
"(ASSIGN_OR_RETURN((
194 format(R
"(ASSIGN_OR_RETURN((
198 Style
, SC_ExpectIncomplete
));
199 EXPECT_EQ(R
"(ASSIGN_OR_RETURN(
205 format(R
"(ASSIGN_OR_RETURN(
211 Style
, SC_ExpectIncomplete
));
212 EXPECT_EQ(R
"(ASSIGN_OR_RETURN(a
217 format(R
"(ASSIGN_OR_RETURN(a
223 verifyFormat("class C {\n"
224 " MOCK_METHOD(R, f,\n"
231 TEST_F(FormatTestMacroExpansion
, KeepParensWhenExpandingObjectLikeMacros
) {
232 FormatStyle Style
= getLLVMStyle();
233 Style
.Macros
.push_back("FN=class C { int f");
234 verifyFormat("void f() {\n"
241 TEST_F(FormatTestMacroExpansion
, DoesNotExpandFunctionLikeMacrosWithoutParens
) {
242 FormatStyle Style
= getLLVMStyle();
243 Style
.Macros
.push_back("CLASS()=class C {");
244 verifyFormat("CLASS void f();\n"
250 TEST_F(FormatTestMacroExpansion
,
251 ContinueFormattingAfterUnclosedParensAfterObjectLikeMacro
) {
252 FormatStyle Style
= getLLVMStyle();
253 Style
.Macros
.push_back("O=class {");
254 verifyIncompleteFormat("O(auto x = [](){\n"
261 } // namespace format