Debian Codegraph @

llvm-project/clang/unittests/Format/MacroCallReconstructorTest.cpp

raw
1 #include "../../lib/Format/Macros.h"
2 #include "../../lib/Format/UnwrappedLineParser.h"
3 #include "TestLexer.h"
4 #include "llvm/ADT/ArrayRef.h"
5 #include "llvm/ADT/SmallVector.h"
6 #include "llvm/ADT/StringRef.h"
7
8 #include "gmock/gmock.h"
9 #include "gtest/gtest.h"
10 #include <map>
11 #include <memory>
12 #include <vector>
13
14 namespace clang {
15 namespace format {
16 namespace {
17
18 using UnexpandedMap =
19 llvm::DenseMap<FormatToken *, std::unique_ptr<UnwrappedLine>>;
20
21 // Keeps track of a sequence of macro expansions.
22 //
23 // The expanded tokens are accessible via getTokens(), while a map of macro call
24 // identifier token to unexpanded token stream is accessible via
25 // getUnexpanded().
26 class Expansion {
27 public:
28 Expansion(TestLexer &Lex, MacroExpander &Macros) : Lex(Lex), Macros(Macros) {}
29
30 // Appends the token stream obtained from expanding the macro Name given
31 // the provided arguments, to be later retrieved with getTokens().
32 // Returns the list of tokens making up the unexpanded macro call.
33 TokenList
34 expand(llvm::StringRef Name,
35 const SmallVector<llvm::SmallVector<FormatToken *, 8>, 1> &Args) {
36 auto *ID = Lex.id(Name);
37 auto UnexpandedLine = std::make_unique<UnwrappedLine>();
38 UnexpandedLine->Tokens.push_back(ID);
39 if (!Args.empty()) {
40 UnexpandedLine->Tokens.push_back(Lex.id("("));
41 for (auto I = Args.begin(), E = Args.end(); I != E; ++I) {
42 if (I != Args.begin())
43 UnexpandedLine->Tokens.push_back(Lex.id(","));
44 UnexpandedLine->Tokens.insert(UnexpandedLine->Tokens.end(), I->begin(),
45 I->end());
46 }
47 UnexpandedLine->Tokens.push_back(Lex.id(")"));
48 }
49 Unexpanded[ID] = std::move(UnexpandedLine);
50
51 auto Expanded = uneof(Macros.expand(ID, Args));
52 Tokens.append(Expanded.begin(), Expanded.end());
53
54 TokenList UnexpandedTokens;
55 for (const UnwrappedLineNode &Node : Unexpanded[ID]->Tokens)
56 UnexpandedTokens.push_back(Node.Tok);
57 return UnexpandedTokens;
58 }
59
60 TokenList expand(llvm::StringRef Name,
61 const std::vector<std::string> &Args = {}) {
62 return expand(Name, lexArgs(Args));
63 }
64
65 const UnexpandedMap &getUnexpanded() const { return Unexpanded; }
66
67 const TokenList &getTokens() const { return Tokens; }
68
69 private:
70 llvm::SmallVector<TokenList, 1>
71 lexArgs(const std::vector<std::string> &Args) {
72 llvm::SmallVector<TokenList, 1> Result;
73 for (const auto &Arg : Args)
74 Result.push_back(uneof(Lex.lex(Arg)));
75 return Result;
76 }
77 llvm::DenseMap<FormatToken *, std::unique_ptr<UnwrappedLine>> Unexpanded;
78 llvm::SmallVector<FormatToken *, 8> Tokens;
79 TestLexer &Lex;
80 MacroExpander &Macros;
81 };
82
83 struct Chunk {
84 Chunk(llvm::ArrayRef<FormatToken *> Tokens)
85 : Tokens(Tokens.begin(), Tokens.end()) {}
86 Chunk(llvm::ArrayRef<UnwrappedLine> Children)
87 : Children(Children.begin(), Children.end()) {}
88 llvm::SmallVector<UnwrappedLineNode, 1> Tokens;
89 llvm::SmallVector<UnwrappedLine, 0> Children;
90 };
91
92 // Allows to produce chunks of a token list by typing the code of equal tokens.
93 //
94 // Created from a list of tokens, users call "consume" to get the next chunk
95 // of tokens, checking that they match the written code.
96 struct Matcher {
97 Matcher(const TokenList &Tokens, TestLexer &Lex)
98 : Tokens(Tokens), It(this->Tokens.begin()), Lex(Lex) {}
99
100 bool tokenMatches(const FormatToken *Left, const FormatToken *Right) {
101 if (Left->getType() == Right->getType() &&
102 Left->TokenText == Right->TokenText) {
103 return true;
104 }
105 llvm::dbgs() << Left->TokenText << " != " << Right->TokenText << "\n";
106 return false;
107 }
108
109 Chunk consume(StringRef Tokens) {
110 TokenList Result;
111 for (const FormatToken *Token : uneof(Lex.lex(Tokens))) {
112 (void)Token; // Fix unused variable warning when asserts are disabled.
113 assert(tokenMatches(*It, Token));
114 Result.push_back(*It);
115 ++It;
116 }
117 return Chunk(Result);
118 }
119
120 TokenList Tokens;
121 TokenList::iterator It;
122 TestLexer &Lex;
123 };
124
125 UnexpandedMap mergeUnexpanded(const UnexpandedMap &M1,
126 const UnexpandedMap &M2) {
127 UnexpandedMap Result;
128 for (const auto &KV : M1)
129 Result[KV.first] = std::make_unique<UnwrappedLine>(*KV.second);
130 for (const auto &KV : M2)
131 Result[KV.first] = std::make_unique<UnwrappedLine>(*KV.second);
132 return Result;
133 }
134
135 class MacroCallReconstructorTest : public ::testing::Test {
136 public:
137 MacroCallReconstructorTest() : Lex(Allocator, Buffers) {}
138
139 std::unique_ptr<MacroExpander>
140 createExpander(const std::vector<std::string> &MacroDefinitions) {
141 return std::make_unique<MacroExpander>(MacroDefinitions,
142 Lex.SourceMgr.get(), Lex.Style,
143 Lex.Allocator, Lex.IdentTable);
144 }
145
146 UnwrappedLine line(llvm::ArrayRef<FormatToken *> Tokens) {
147 UnwrappedLine Result;
148 for (FormatToken *Tok : Tokens)
149 Result.Tokens.push_back(UnwrappedLineNode(Tok));
150 return Result;
151 }
152
153 UnwrappedLine line(llvm::StringRef Text) { return line({lex(Text)}); }
154
155 UnwrappedLine line(llvm::ArrayRef<Chunk> Chunks) {
156 UnwrappedLine Result;
157 for (const Chunk &Chunk : Chunks) {
158 Result.Tokens.insert(Result.Tokens.end(), Chunk.Tokens.begin(),
159 Chunk.Tokens.end());
160 assert(!Result.Tokens.empty());
161 Result.Tokens.back().Children.append(Chunk.Children.begin(),
162 Chunk.Children.end());
163 }
164 return Result;
165 }
166
167 TokenList lex(llvm::StringRef Text) { return uneof(Lex.lex(Text)); }
168
169 Chunk tokens(llvm::StringRef Text) { return Chunk(lex(Text)); }
170
171 Chunk children(llvm::ArrayRef<UnwrappedLine> Children) {
172 return Chunk(Children);
173 }
174
175 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
176 std::vector<std::unique_ptr<llvm::MemoryBuffer>> Buffers;
177 TestLexer Lex;
178 };
179
180 bool matchesTokens(const UnwrappedLine &L1, const UnwrappedLine &L2) {
181 if (L1.Tokens.size() != L2.Tokens.size())
182 return false;
183 for (auto L1It = L1.Tokens.begin(), L2It = L2.Tokens.begin();
184 L1It != L1.Tokens.end(); ++L1It, ++L2It) {
185 if (L1It->Tok != L2It->Tok)
186 return false;
187 if (L1It->Children.size() != L2It->Children.size())
188 return false;
189 for (auto L1ChildIt = L1It->Children.begin(),
190 L2ChildIt = L2It->Children.begin();
191 L1ChildIt != L1It->Children.end(); ++L1ChildIt, ++L2ChildIt) {
192 if (!matchesTokens(*L1ChildIt, *L2ChildIt))
193 return false;
194 }
195 }
196 return true;
197 }
198 MATCHER_P(matchesLine, line, "") { return matchesTokens(arg, line); }
199
200 TEST_F(MacroCallReconstructorTest, Identifier) {
201 auto Macros = createExpander({"X=x"});
202 Expansion Exp(Lex, *Macros);
203 TokenList Call = Exp.expand("X");
204
205 MacroCallReconstructor Unexp(0, Exp.getUnexpanded());
206 Unexp.addLine(line(Exp.getTokens()));
207 EXPECT_TRUE(Unexp.finished());
208 Matcher U(Call, Lex);
209 EXPECT_THAT(std::move(Unexp).takeResult(), matchesLine(line(U.consume("X"))));
210 }
211
212 TEST_F(MacroCallReconstructorTest, NestedLineWithinCall) {
213 auto Macros = createExpander({"C(a)=class X { a; };"});
214 Expansion Exp(Lex, *Macros);
215 TokenList Call = Exp.expand("C", {"void f()"});
216
217 MacroCallReconstructor Unexp(0, Exp.getUnexpanded());
218 Matcher E(Exp.getTokens(), Lex);
219 Unexp.addLine(line(E.consume("class X {")));
220 EXPECT_FALSE(Unexp.finished());
221 Unexp.addLine(line(E.consume("void f();")));
222 EXPECT_FALSE(Unexp.finished());
223 Unexp.addLine(line(E.consume("};")));
224 EXPECT_TRUE(Unexp.finished());
225 Matcher U(Call, Lex);
226 EXPECT_THAT(std::move(Unexp).takeResult(),
227 matchesLine(line(U.consume("C(void f())"))));
228 }
229
230 TEST_F(MacroCallReconstructorTest, MultipleLinesInNestedMultiParamsExpansion) {
231 auto Macros = createExpander({"C(a, b)=a b", "B(a)={a}"});
232 Expansion Exp1(Lex, *Macros);
233 TokenList Call1 = Exp1.expand("B", {"b"});
234 Expansion Exp2(Lex, *Macros);
235 TokenList Call2 = Exp2.expand("C", {uneof(Lex.lex("a")), Exp1.getTokens()});
236
237 UnexpandedMap Unexpanded =
238 mergeUnexpanded(Exp1.getUnexpanded(), Exp2.getUnexpanded());
239 MacroCallReconstructor Unexp(0, Unexpanded);
240 Matcher E(Exp2.getTokens(), Lex);
241 Unexp.addLine(line(E.consume("a")));
242 EXPECT_FALSE(Unexp.finished());
243 Unexp.addLine(line(E.consume("{")));
244 EXPECT_FALSE(Unexp.finished());
245 Unexp.addLine(line(E.consume("b")));
246 EXPECT_FALSE(Unexp.finished());
247 Unexp.addLine(line(E.consume("}")));
248 EXPECT_TRUE(Unexp.finished());
249
250 Matcher U1(Call1, Lex);
251 auto Middle = U1.consume("B(b)");
252 Matcher U2(Call2, Lex);
253 auto Chunk1 = U2.consume("C(a, ");
254 auto Chunk2 = U2.consume("{ b }");
255 auto Chunk3 = U2.consume(")");
256
257 EXPECT_THAT(std::move(Unexp).takeResult(),
258 matchesLine(line({Chunk1, Middle, Chunk3})));
259 }
260
261 TEST_F(MacroCallReconstructorTest, StatementSequence) {
262 auto Macros = createExpander({"SEMI=;"});
263 Expansion Exp(Lex, *Macros);
264 TokenList Call1 = Exp.expand("SEMI");
265 TokenList Call2 = Exp.expand("SEMI");
266 TokenList Call3 = Exp.expand("SEMI");
267
268 MacroCallReconstructor Unexp(0, Exp.getUnexpanded());
269 Matcher E(Exp.getTokens(), Lex);
270 Unexp.addLine(line(E.consume(";")));
271 EXPECT_TRUE(Unexp.finished());
272 Unexp.addLine(line(E.consume(";")));
273 EXPECT_TRUE(Unexp.finished());
274 Unexp.addLine(line(E.consume(";")));
275 EXPECT_TRUE(Unexp.finished());
276 Matcher U1(Call1, Lex);
277 Matcher U2(Call2, Lex);
278 Matcher U3(Call3, Lex);
279 EXPECT_THAT(std::move(Unexp).takeResult(),
280 matchesLine(line(
281 {U1.consume("SEMI"),
282 children({line({U2.consume("SEMI"),
283 children({line(U3.consume("SEMI"))})})})})));
284 }
285
286 TEST_F(MacroCallReconstructorTest, NestedBlock) {
287 auto Macros = createExpander({"ID(x)=x"});
288 // Test: ID({ ID(a *b); })
289 // 1. expand ID(a *b) -> a *b
290 Expansion Exp1(Lex, *Macros);
291 TokenList Call1 = Exp1.expand("ID", {"a *b"});
292 // 2. expand ID({ a *b; })
293 TokenList Arg;
294 Arg.push_back(Lex.id("{"));
295 Arg.append(Exp1.getTokens().begin(), Exp1.getTokens().end());
296 Arg.push_back(Lex.id(";"));
297 Arg.push_back(Lex.id("}"));
298 Expansion Exp2(Lex, *Macros);
299 TokenList Call2 = Exp2.expand("ID", {Arg});
300
301 // Consume as-if formatted:
302 // {
303 // a *b;
304 // }
305 UnexpandedMap Unexpanded =
306 mergeUnexpanded(Exp1.getUnexpanded(), Exp2.getUnexpanded());
307 MacroCallReconstructor Unexp(0, Unexpanded);
308 Matcher E(Exp2.getTokens(), Lex);
309 Unexp.addLine(line(E.consume("{")));
310 EXPECT_FALSE(Unexp.finished());
311 Unexp.addLine(line(E.consume("a *b;")));
312 EXPECT_FALSE(Unexp.finished());
313 Unexp.addLine(line(E.consume("}")));
314 EXPECT_TRUE(Unexp.finished());
315
316 // Expect lines:
317 // ID({
318 // ID(a *b);
319 // })
320 Matcher U1(Call1, Lex);
321 Matcher U2(Call2, Lex);
322 auto Chunk2Start = U2.consume("ID(");
323 auto Chunk2LBrace = U2.consume("{");
324 U2.consume("a *b");
325 auto Chunk2Mid = U2.consume(";");
326 auto Chunk2RBrace = U2.consume("}");
327 auto Chunk2End = U2.consume(")");
328 auto Chunk1 = U1.consume("ID(a *b)");
329
330 auto Expected = line({Chunk2Start,
331 children({
332 line(Chunk2LBrace),
333 line({Chunk1, Chunk2Mid}),
334 line(Chunk2RBrace),
335 }),
336 Chunk2End});
337 EXPECT_THAT(std::move(Unexp).takeResult(), matchesLine(Expected));
338 }
339
340 TEST_F(MacroCallReconstructorTest, NestedChildBlocks) {
341 auto Macros = createExpander({"ID(x)=x", "CALL(x)=f([] { x })"});
342 // Test: ID(CALL(CALL(return a * b;)))
343 // 1. expand CALL(return a * b;)
344 Expansion Exp1(Lex, *Macros);
345 TokenList Call1 = Exp1.expand("CALL", {"return a * b;"});
346 // 2. expand CALL(f([] { return a * b; }))
347 Expansion Exp2(Lex, *Macros);
348 TokenList Call2 = Exp2.expand("CALL", {Exp1.getTokens()});
349 // 3. expand ID({ f([] { f([] { return a * b; }) }) })
350 TokenList Arg3;
351 Arg3.push_back(Lex.id("{"));
352 Arg3.append(Exp2.getTokens().begin(), Exp2.getTokens().end());
353 Arg3.push_back(Lex.id("}"));
354 Expansion Exp3(Lex, *Macros);
355 TokenList Call3 = Exp3.expand("ID", {Arg3});
356
357 // Consume as-if formatted in three unwrapped lines:
358 // 0: {
359 // 1: f([] {
360 // f([] {
361 // return a * b;
362 // })
363 // })
364 // 2: }
365 UnexpandedMap Unexpanded = mergeUnexpanded(
366 Exp1.getUnexpanded(),
367 mergeUnexpanded(Exp2.getUnexpanded(), Exp3.getUnexpanded()));
368 MacroCallReconstructor Unexp(0, Unexpanded);
369 Matcher E(Exp3.getTokens(), Lex);
370 Unexp.addLine(line(E.consume("{")));
371 Unexp.addLine(
372 line({E.consume("f([] {"),
373 children({line({E.consume("f([] {"),
374 children({line(E.consume("return a * b;"))}),
375 E.consume("})")})}),
376 E.consume("})")}));
377 Unexp.addLine(line(E.consume("}")));
378 EXPECT_TRUE(Unexp.finished());
379
380 // Expect lines:
381 // ID(
382 // {
383 // CALL(CALL(return a * b;))
384 // }
385 // )
386 Matcher U1(Call1, Lex);
387 Matcher U2(Call2, Lex);
388 Matcher U3(Call3, Lex);
389 auto Chunk3Start = U3.consume("ID(");
390 auto Chunk3LBrace = U3.consume("{");
391 U3.consume("f([] { f([] { return a * b; }) })");
392 auto Chunk3RBrace = U3.consume("}");
393 auto Chunk3End = U3.consume(")");
394 auto Chunk2Start = U2.consume("CALL(");
395 U2.consume("f([] { return a * b; })");
396 auto Chunk2End = U2.consume(")");
397 auto Chunk1 = U1.consume("CALL(return a * b;)");
398
399 auto Expected = line({
400 Chunk3Start,
401 children({
402 line(Chunk3LBrace),
403 line({
404 Chunk2Start,
405 Chunk1,
406 Chunk2End,
407 }),
408 line(Chunk3RBrace),
409 }),
410 Chunk3End,
411 });
412 EXPECT_THAT(std::move(Unexp).takeResult(), matchesLine(Expected));
413 }
414
415 TEST_F(MacroCallReconstructorTest, NestedChildrenMultipleArguments) {
416 auto Macros = createExpander({"CALL(a, b)=f([] { a; b; })"});
417 Expansion Exp(Lex, *Macros);
418 TokenList Call = Exp.expand("CALL", {std::string("int a"), "int b"});
419
420 MacroCallReconstructor Unexp(0, Exp.getUnexpanded());
421 Matcher E(Exp.getTokens(), Lex);
422 Unexp.addLine(line({
423 E.consume("f([] {"),
424 children({
425 line(E.consume("int a;")),
426 line(E.consume("int b;")),
427 }),
428 E.consume("})"),
429 }));
430 EXPECT_TRUE(Unexp.finished());
431 Matcher U(Call, Lex);
432 auto Expected = line(U.consume("CALL(int a, int b)"));
433 EXPECT_THAT(std::move(Unexp).takeResult(), matchesLine(Expected));
434 }
435
436 TEST_F(MacroCallReconstructorTest, ReverseOrderArgumentsInExpansion) {
437 auto Macros = createExpander({"CALL(a, b)=b + a"});
438 Expansion Exp(Lex, *Macros);
439 TokenList Call = Exp.expand("CALL", {std::string("x"), "y"});
440
441 MacroCallReconstructor Unexp(0, Exp.getUnexpanded());
442 Matcher E(Exp.getTokens(), Lex);
443 Unexp.addLine(line(E.consume("y + x")));
444 EXPECT_TRUE(Unexp.finished());
445 Matcher U(Call, Lex);
446 auto Expected = line(U.consume("CALL(x, y)"));
447 EXPECT_THAT(std::move(Unexp).takeResult(), matchesLine(Expected));
448 }
449
450 TEST_F(MacroCallReconstructorTest, MultipleToplevelUnwrappedLines) {
451 auto Macros = createExpander({"ID(a, b)=a b"});
452 Expansion Exp(Lex, *Macros);
453 TokenList Call = Exp.expand("ID", {std::string("x; x"), "y"});
454
455 MacroCallReconstructor Unexp(0, Exp.getUnexpanded());
456 Matcher E(Exp.getTokens(), Lex);
457 Unexp.addLine(line(E.consume("x;")));
458 Unexp.addLine(line(E.consume("x y")));
459 EXPECT_TRUE(Unexp.finished());
460 Matcher U(Call, Lex);
461 auto Expected = line({
462 U.consume("ID("),
463 children({
464 line(U.consume("x;")),
465 line(U.consume("x")),
466 }),
467 U.consume(", y)"),
468 });
469 EXPECT_THAT(std::move(Unexp).takeResult(), matchesLine(Expected));
470 }
471
472 TEST_F(MacroCallReconstructorTest, NestedCallsMultipleLines) {
473 auto Macros = createExpander({"ID(x)=x"});
474 // Test: ID({ID(a * b);})
475 // 1. expand ID(a * b)
476 Expansion Exp1(Lex, *Macros);
477 TokenList Call1 = Exp1.expand("ID", {"a * b"});
478 // 2. expand ID({ a * b; })
479 Expansion Exp2(Lex, *Macros);
480 TokenList Arg2;
481 Arg2.push_back(Lex.id("{"));
482 Arg2.append(Exp1.getTokens().begin(), Exp1.getTokens().end());
483 Arg2.push_back(Lex.id(";"));
484 Arg2.push_back(Lex.id("}"));
485 TokenList Call2 = Exp2.expand("ID", {Arg2});
486
487 // Consume as-if formatted in three unwrapped lines:
488 // 0: {
489 // 1: a * b;
490 // 2: }
491 UnexpandedMap Unexpanded =
492 mergeUnexpanded(Exp1.getUnexpanded(), Exp2.getUnexpanded());
493 MacroCallReconstructor Unexp(0, Unexpanded);
494 Matcher E(Exp2.getTokens(), Lex);
495 Unexp.addLine(line(E.consume("{")));
496 Unexp.addLine(line(E.consume("a * b;")));
497 Unexp.addLine(line(E.consume("}")));
498 EXPECT_TRUE(Unexp.finished());
499
500 // Expect lines:
501 // ID(
502 // {
503 // ID(a * b);
504 // }
505 // )
506 Matcher U1(Call1, Lex);
507 Matcher U2(Call2, Lex);
508 auto Chunk2Start = U2.consume("ID(");
509 auto Chunk2LBrace = U2.consume("{");
510 U2.consume("a * b");
511 auto Chunk2Semi = U2.consume(";");
512 auto Chunk2RBrace = U2.consume("}");
513 auto Chunk2End = U2.consume(")");
514 auto Chunk1 = U1.consume("ID(a * b)");
515
516 auto Expected = line({
517 Chunk2Start,
518 children({
519 line({Chunk2LBrace}),
520 line({Chunk1, Chunk2Semi}),
521 line({Chunk2RBrace}),
522 }),
523 Chunk2End,
524 });
525 EXPECT_THAT(std::move(Unexp).takeResult(), matchesLine(Expected));
526 }
527
528 TEST_F(MacroCallReconstructorTest, ParentOutsideMacroCall) {
529 auto Macros = createExpander({"ID(a)=a"});
530 Expansion Exp(Lex, *Macros);
531 TokenList Call = Exp.expand("ID", {std::string("x; y; z;")});
532
533 auto Prefix = tokens("int a = []() {");
534 auto Postfix = tokens("}();");
535 MacroCallReconstructor Unexp(0, Exp.getUnexpanded());
536 Matcher E(Exp.getTokens(), Lex);
537 Unexp.addLine(line({
538 Prefix,
539 children({
540 line(E.consume("x;")),
541 line(E.consume("y;")),
542 line(E.consume("z;")),
543 }),
544 Postfix,
545 }));
546 EXPECT_TRUE(Unexp.finished());
547 Matcher U(Call, Lex);
548 auto Expected = line({
549 Prefix,
550 children({
551 line({
552 U.consume("ID("),
553 children({
554 line(U.consume("x;")),
555 line(U.consume("y;")),
556 line(U.consume("z;")),
557 }),
558 U.consume(")"),
559 }),
560 }),
561 Postfix,
562 });
563 EXPECT_THAT(std::move(Unexp).takeResult(), matchesLine(Expected));
564 }
565
566 TEST_F(MacroCallReconstructorTest, UnusedMacroArguments) {
567 auto Macros = createExpander({"X=x"});
568 Expansion Exp(Lex, *Macros);
569 TokenList Call = Exp.expand("X", {"a", "b", "c"});
570
571 MacroCallReconstructor Unexp(0, Exp.getUnexpanded());
572 Unexp.addLine(line(Exp.getTokens()));
573 EXPECT_TRUE(Unexp.finished());
574 Matcher U(Call, Lex);
575 EXPECT_THAT(std::move(Unexp).takeResult(),
576 matchesLine(line(U.consume("X(a, b, c)"))));
577 }
578
579 TEST_F(MacroCallReconstructorTest, UnusedEmptyMacroArgument) {
580 auto Macros = createExpander({"X=x"});
581 Expansion Exp(Lex, *Macros);
582 TokenList Call = Exp.expand("X", {std::string("")});
583
584 MacroCallReconstructor Unexp(0, Exp.getUnexpanded());
585 Matcher E(Exp.getTokens(), Lex);
586 auto Semi = tokens(";");
587 Unexp.addLine(line({E.consume("x"), Semi}));
588 EXPECT_TRUE(Unexp.finished());
589 Matcher U(Call, Lex);
590 EXPECT_THAT(std::move(Unexp).takeResult(),
591 matchesLine(line({U.consume("X()"), Semi})));
592 }
593
594 TEST_F(MacroCallReconstructorTest, ChildrenSplitAcrossArguments) {
595 auto Macros = createExpander({"CALL(a, b)=f([]() a b)"});
596 Expansion Exp(Lex, *Macros);
597 TokenList Call = Exp.expand("CALL", {std::string("{ a;"), "b; }"});
598
599 MacroCallReconstructor Unexp(0, Exp.getUnexpanded());
600 Matcher E(Exp.getTokens(), Lex);
601 Unexp.addLine(line({
602 E.consume("f([]() {"),
603 children({
604 line(E.consume("a;")),
605 line(E.consume("b;")),
606 }),
607 E.consume("})"),
608 }));
609 EXPECT_TRUE(Unexp.finished());
610 Matcher U(Call, Lex);
611 auto Expected = line({
612 U.consume("CALL({"),
613 children(line(U.consume("a;"))),
614 U.consume(", b; })"),
615 });
616 EXPECT_THAT(std::move(Unexp).takeResult(), matchesLine(Expected));
617 }
618
619 TEST_F(MacroCallReconstructorTest, ChildrenAfterMacroCall) {
620 auto Macros = createExpander({"CALL(a, b)=f([]() a b"});
621 Expansion Exp(Lex, *Macros);
622 TokenList Call = Exp.expand("CALL", {std::string("{ a"), "b"});
623
624 MacroCallReconstructor Unexp(0, Exp.getUnexpanded());
625 Matcher E(Exp.getTokens(), Lex);
626 auto Semi = tokens(";");
627 auto SecondLine = tokens("c d;");
628 auto ThirdLine = tokens("e f;");
629 auto Postfix = tokens("})");
630 Unexp.addLine(line({
631 E.consume("f([]() {"),
632 children({
633 line({E.consume("a b"), Semi}),
634 line(SecondLine),
635 line(ThirdLine),
636 }),
637 Postfix,
638 }));
639 EXPECT_TRUE(Unexp.finished());
640 Matcher U(Call, Lex);
641 auto Expected = line({
642 U.consume("CALL({"),
643 children(line(U.consume("a"))),
644 U.consume(", b)"),
645 Semi,
646 children(line({
647 SecondLine,
648 children(line({
649 ThirdLine,
650 Postfix,
651 })),
652 })),
653 });
654 EXPECT_THAT(std::move(Unexp).takeResult(), matchesLine(Expected));
655 }
656
657 TEST_F(MacroCallReconstructorTest, InvalidCodeSplittingBracesAcrossArgs) {
658 auto Macros = createExpander({"M(a, b)=(a) (b)"});
659 Expansion Exp(Lex, *Macros);
660 TokenList Call = Exp.expand("M", {std::string("{"), "x", ""});
661
662 MacroCallReconstructor Unexp(0, Exp.getUnexpanded());
663 Matcher E(Exp.getTokens(), Lex);
664 auto Prefix = tokens("({");
665 Unexp.addLine(line({
666 Prefix,
667 children({
668 line({
669 E.consume("({"),
670 children({line(E.consume(")(x)"))}),
671 }),
672 }),
673 }));
674 EXPECT_TRUE(Unexp.finished());
675 Matcher U(Call, Lex);
676 auto Expected = line({
677 Prefix,
678 children({line(U.consume("M({,x,)"))}),
679 });
680 EXPECT_THAT(std::move(Unexp).takeResult(), matchesLine(Expected));
681 }
682
683 } // namespace
684 } // namespace format
685 } // namespace clang