Debian Codegraph @

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