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("ID3({, ID(a *b),\n"
55 verifyFormat("ID(CALL(CALL(return a * b;)));", Style
);
57 verifyFormat("ASSIGN_OR_RETURN(MySomewhatLongType *variable,\n"
58 " MySomewhatLongFunction(SomethingElse()));",
60 verifyFormat("ASSIGN_OR_RETURN(MySomewhatLongType *variable,\n"
61 " MySomewhatLongFunction(SomethingElse()), "
66 #define MACRO(a, b) ID(a + b)
89 ID(namespace foo { int a; }) // namespace k
98 Style
.ColumnLimit
= 35;
99 // FIXME: Arbitrary formatting of macros where the end of the logical
100 // line is in the middle of a macro call are not working yet.
110 Style
.ColumnLimit
= 10;
111 verifyFormat("STMT\n"
121 ID(CALL(CALL(a * b)));
125 // FIXME: If we want to support unbalanced braces or parens from macro
126 // expansions we need to re-think how we propagate errors in
127 // TokenAnnotator::parseLine; for investigation, switching the inner loop of
128 // TokenAnnotator::parseLine to return LT_Other instead of LT_Invalid in case
129 // of !consumeToken() changes the formatting of the test below and makes it
130 // believe it has a fully correct formatting.
146 ID3({CLASS a*b; };}, ID(x*y);, STMT STMT STMT)
151 verifyFormat("ID(a(\n"
159 Style
.ColumnLimit
= 80;
160 verifyFormat(R
"(ASSIGN_OR_RETURN(
165 Style
.ColumnLimit
= 30;
166 verifyFormat(R
"(ASSIGN_OR_RETURN(
179 verifyFormat(R
"(int a = []() {
189 R
"(ASSIGN_OR_RETURN((
193 format(R
"(ASSIGN_OR_RETURN((
197 Style
, SC_ExpectIncomplete
));
198 EXPECT_EQ(R
"(ASSIGN_OR_RETURN(
204 format(R
"(ASSIGN_OR_RETURN(
210 Style
, SC_ExpectIncomplete
));
211 EXPECT_EQ(R
"(ASSIGN_OR_RETURN(a
216 format(R
"(ASSIGN_OR_RETURN(a
222 verifyFormat("class C {\n"
223 " MOCK_METHOD(R, f,\n"
230 TEST_F(FormatTestMacroExpansion
, KeepParensWhenExpandingObjectLikeMacros
) {
231 FormatStyle Style
= getLLVMStyle();
232 Style
.Macros
.push_back("FN=class C { int f");
233 verifyFormat("void f() {\n"
240 TEST_F(FormatTestMacroExpansion
, DoesNotExpandFunctionLikeMacrosWithoutParens
) {
241 FormatStyle Style
= getLLVMStyle();
242 Style
.Macros
.push_back("CLASS()=class C {");
243 verifyFormat("CLASS void f();\n"
249 TEST_F(FormatTestMacroExpansion
,
250 ContinueFormattingAfterUnclosedParensAfterObjectLikeMacro
) {
251 FormatStyle Style
= getLLVMStyle();
252 Style
.Macros
.push_back("O=class {");
253 verifyIncompleteFormat("O(auto x = [](){\n"
260 } // namespace format