Debian Codegraph @

llvm-project/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp

raw
1 //===- unittests/Lex/DependencyDirectivesScannerTest.cpp ------------------===//
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/Lex/DependencyDirectivesScanner.h"
10 #include "llvm/ADT/SmallString.h"
11 #include "gtest/gtest.h"
12
13 using namespace llvm;
14 using namespace clang;
15 using namespace clang::dependency_directives_scan;
16
17 static bool minimizeSourceToDependencyDirectives(
18 StringRef Input, SmallVectorImpl<char> &Out,
19 SmallVectorImpl<dependency_directives_scan::Token> &Tokens,
20 SmallVectorImpl<Directive> &Directives) {
21 Out.clear();
22 Tokens.clear();
23 Directives.clear();
24 if (scanSourceForDependencyDirectives(Input, Tokens, Directives))
25 return true;
26
27 raw_svector_ostream OS(Out);
28 printDependencyDirectivesAsSource(Input, Directives, OS);
29 if (!Out.empty() && Out.back() != '\n')
30 Out.push_back('\n');
31 Out.push_back('\0');
32 Out.pop_back();
33
34 return false;
35 }
36
37 static bool minimizeSourceToDependencyDirectives(StringRef Input,
38 SmallVectorImpl<char> &Out) {
39 SmallVector<dependency_directives_scan::Token, 16> Tokens;
40 SmallVector<Directive, 32> Directives;
41 return minimizeSourceToDependencyDirectives(Input, Out, Tokens, Directives);
42 }
43
44 namespace {
45
46 TEST(MinimizeSourceToDependencyDirectivesTest, Empty) {
47 SmallVector<char, 128> Out;
48 SmallVector<dependency_directives_scan::Token, 4> Tokens;
49 SmallVector<Directive, 4> Directives;
50
51 ASSERT_FALSE(
52 minimizeSourceToDependencyDirectives("", Out, Tokens, Directives));
53 EXPECT_TRUE(Out.empty());
54 EXPECT_TRUE(Tokens.empty());
55 ASSERT_EQ(1u, Directives.size());
56 ASSERT_EQ(pp_eof, Directives.back().Kind);
57
58 ASSERT_FALSE(minimizeSourceToDependencyDirectives("abc def\nxyz", Out, Tokens,
59 Directives));
60 EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
61 EXPECT_TRUE(Tokens.empty());
62 ASSERT_EQ(2u, Directives.size());
63 EXPECT_EQ(tokens_present_before_eof, Directives[0].Kind);
64 EXPECT_EQ(pp_eof, Directives[1].Kind);
65 }
66
67 TEST(MinimizeSourceToDependencyDirectivesTest, AllTokens) {
68 SmallVector<char, 128> Out;
69 SmallVector<dependency_directives_scan::Token, 4> Tokens;
70 SmallVector<Directive, 4> Directives;
71
72 ASSERT_FALSE(
73 minimizeSourceToDependencyDirectives("#define A\n"
74 "#undef A\n"
75 "#endif\n"
76 "#if A\n"
77 "#ifdef A\n"
78 "#ifndef A\n"
79 "#elifdef A\n"
80 "#elifndef A\n"
81 "#elif A\n"
82 "#else\n"
83 "#include <A>\n"
84 "#include_next <A>\n"
85 "#__include_macros <A>\n"
86 "#import <A>\n"
87 "@import A;\n"
88 "#pragma clang module import A\n"
89 "#pragma push_macro(A)\n"
90 "#pragma pop_macro(A)\n"
91 "#pragma include_alias(<A>, <B>)\n"
92 "export module m;\n"
93 "import m;\n",
94 Out, Tokens, Directives));
95 EXPECT_EQ(pp_define, Directives[0].Kind);
96 EXPECT_EQ(pp_undef, Directives[1].Kind);
97 EXPECT_EQ(pp_endif, Directives[2].Kind);
98 EXPECT_EQ(pp_if, Directives[3].Kind);
99 EXPECT_EQ(pp_ifdef, Directives[4].Kind);
100 EXPECT_EQ(pp_ifndef, Directives[5].Kind);
101 EXPECT_EQ(pp_elifdef, Directives[6].Kind);
102 EXPECT_EQ(pp_elifndef, Directives[7].Kind);
103 EXPECT_EQ(pp_elif, Directives[8].Kind);
104 EXPECT_EQ(pp_else, Directives[9].Kind);
105 EXPECT_EQ(pp_include, Directives[10].Kind);
106 EXPECT_EQ(pp_include_next, Directives[11].Kind);
107 EXPECT_EQ(pp___include_macros, Directives[12].Kind);
108 EXPECT_EQ(pp_import, Directives[13].Kind);
109 EXPECT_EQ(decl_at_import, Directives[14].Kind);
110 EXPECT_EQ(pp_pragma_import, Directives[15].Kind);
111 EXPECT_EQ(pp_pragma_push_macro, Directives[16].Kind);
112 EXPECT_EQ(pp_pragma_pop_macro, Directives[17].Kind);
113 EXPECT_EQ(pp_pragma_include_alias, Directives[18].Kind);
114 EXPECT_EQ(cxx_export_module_decl, Directives[19].Kind);
115 EXPECT_EQ(cxx_import_decl, Directives[20].Kind);
116 EXPECT_EQ(pp_eof, Directives[21].Kind);
117 }
118
119 TEST(MinimizeSourceToDependencyDirectivesTest, EmptyHash) {
120 SmallVector<char, 128> Out;
121
122 ASSERT_FALSE(
123 minimizeSourceToDependencyDirectives("#\n#define MACRO a\n", Out));
124 EXPECT_STREQ("#define MACRO a\n", Out.data());
125 }
126
127 TEST(MinimizeSourceToDependencyDirectivesTest, HashHash) {
128 SmallVector<char, 128> Out;
129
130 StringRef Source = R"(
131 #define S
132 #if 0
133 ##pragma cool
134 ##include "t.h"
135 #endif
136 #define E
137 )";
138 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
139 EXPECT_STREQ("#define S\n#if 0\n#endif\n#define E\n", Out.data());
140 }
141
142 TEST(MinimizeSourceToDependencyDirectivesTest, Define) {
143 SmallVector<char, 128> Out;
144 SmallVector<dependency_directives_scan::Token, 4> Tokens;
145 SmallVector<Directive, 4> Directives;
146
147 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define MACRO", Out,
148 Tokens, Directives));
149 EXPECT_STREQ("#define MACRO\n", Out.data());
150 ASSERT_EQ(4u, Tokens.size());
151 ASSERT_EQ(2u, Directives.size());
152 ASSERT_EQ(pp_define, Directives.front().Kind);
153 }
154
155 TEST(MinimizeSourceToDependencyDirectivesTest, DefineSpacing) {
156 SmallVector<char, 128> Out;
157
158 ASSERT_FALSE(
159 minimizeSourceToDependencyDirectives("#define MACRO\n\n\n", Out));
160 EXPECT_STREQ("#define MACRO\n", Out.data());
161
162 ASSERT_FALSE(
163 minimizeSourceToDependencyDirectives("#define MACRO \n\n\n", Out));
164 EXPECT_STREQ("#define MACRO\n", Out.data());
165
166 ASSERT_FALSE(
167 minimizeSourceToDependencyDirectives("#define MACRO a \n\n\n", Out));
168 EXPECT_STREQ("#define MACRO a\n", Out.data());
169
170 ASSERT_FALSE(
171 minimizeSourceToDependencyDirectives("#define MACRO\n\n\n", Out));
172 EXPECT_STREQ("#define MACRO\n", Out.data());
173 }
174
175 TEST(MinimizeSourceToDependencyDirectivesTest, DefineMacroArguments) {
176 SmallVector<char, 128> Out;
177
178 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define MACRO()", Out));
179 EXPECT_STREQ("#define MACRO()\n", Out.data());
180
181 ASSERT_FALSE(
182 minimizeSourceToDependencyDirectives("#define MACRO(a, b...)", Out));
183 EXPECT_STREQ("#define MACRO(a,b...)\n", Out.data());
184
185 ASSERT_FALSE(
186 minimizeSourceToDependencyDirectives("#define MACRO content", Out));
187 EXPECT_STREQ("#define MACRO content\n", Out.data());
188
189 ASSERT_FALSE(minimizeSourceToDependencyDirectives(
190 "#define MACRO con tent ", Out));
191 EXPECT_STREQ("#define MACRO con tent\n", Out.data());
192
193 ASSERT_FALSE(minimizeSourceToDependencyDirectives(
194 "#define MACRO() con tent ", Out));
195 EXPECT_STREQ("#define MACRO() con tent\n", Out.data());
196 }
197
198 TEST(MinimizeSourceToDependencyDirectivesTest, DefineInvalidMacroArguments) {
199 SmallVector<char, 128> Out;
200
201 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define MACRO((a))", Out));
202 EXPECT_STREQ("#define MACRO((a))\n", Out.data());
203
204 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define MACRO(", Out));
205 EXPECT_STREQ("#define MACRO(\n", Out.data());
206
207 ASSERT_FALSE(
208 minimizeSourceToDependencyDirectives("#define MACRO(a * b)", Out));
209 EXPECT_STREQ("#define MACRO(a*b)\n", Out.data());
210 }
211
212 TEST(MinimizeSourceToDependencyDirectivesTest, DefineHorizontalWhitespace) {
213 SmallVector<char, 128> Out;
214
215 ASSERT_FALSE(minimizeSourceToDependencyDirectives(
216 "#define MACRO(\t)\tcon \t tent\t", Out));
217 EXPECT_STREQ("#define MACRO() con tent\n", Out.data());
218
219 ASSERT_FALSE(minimizeSourceToDependencyDirectives(
220 "#define MACRO(\f)\fcon \f tent\f", Out));
221 EXPECT_STREQ("#define MACRO() con tent\n", Out.data());
222
223 ASSERT_FALSE(minimizeSourceToDependencyDirectives(
224 "#define MACRO(\v)\vcon \v tent\v", Out));
225 EXPECT_STREQ("#define MACRO() con tent\n", Out.data());
226
227 ASSERT_FALSE(minimizeSourceToDependencyDirectives(
228 "#define MACRO \t\v\f\v\t con\f\t\vtent\v\f \v", Out));
229 EXPECT_STREQ("#define MACRO con tent\n", Out.data());
230 }
231
232 TEST(MinimizeSourceToDependencyDirectivesTest, DefineMultilineArgs) {
233 SmallVector<char, 128> Out;
234
235 ASSERT_FALSE(
236 minimizeSourceToDependencyDirectives("#define MACRO(a \\\n"
237 " )",
238 Out));
239 EXPECT_STREQ("#define MACRO(a)\n", Out.data());
240
241 ASSERT_FALSE(
242 minimizeSourceToDependencyDirectives("#define MACRO(a, \\\n"
243 " b) \\\n"
244 " call((a), \\\n"
245 " (b))",
246 Out));
247 EXPECT_STREQ("#define MACRO(a,b) call((a), (b))\n", Out.data());
248 }
249
250 TEST(MinimizeSourceToDependencyDirectivesTest,
251 DefineMultilineArgsCarriageReturn) {
252 SmallVector<char, 128> Out;
253
254 ASSERT_FALSE(
255 minimizeSourceToDependencyDirectives("#define MACRO(a, \\\r"
256 " b) \\\r"
257 " call((a), \\\r"
258 " (b))",
259 Out));
260 EXPECT_STREQ("#define MACRO(a,b) call((a), (b))\n", Out.data());
261 }
262
263 TEST(MinimizeSourceToDependencyDirectivesTest, DefineMultilineArgsStringize) {
264 SmallVector<char, 128> Out;
265
266 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define MACRO(a,b) \\\n"
267 " #a \\\n"
268 " #b",
269 Out));
270 EXPECT_STREQ("#define MACRO(a,b) #a #b\n", Out.data());
271 }
272
273 TEST(MinimizeSourceToDependencyDirectivesTest,
274 DefineMultilineArgsCarriageReturnNewline) {
275 SmallVector<char, 128> Out;
276
277 ASSERT_FALSE(
278 minimizeSourceToDependencyDirectives("#define MACRO(a, \\\r\n"
279 " b) \\\r\n"
280 " call((a), \\\r\n"
281 " (b))",
282 Out));
283 EXPECT_STREQ("#define MACRO(a,b) call((a), (b))\n", Out.data());
284 }
285
286 TEST(MinimizeSourceToDependencyDirectivesTest,
287 DefineMultilineArgsNewlineCarriageReturn) {
288 SmallVector<char, 128> Out;
289
290 ASSERT_FALSE(
291 minimizeSourceToDependencyDirectives("#define MACRO(a, \\\n\r"
292 " b) \\\n\r"
293 " call((a), \\\n\r"
294 " (b))",
295 Out));
296 EXPECT_STREQ("#define MACRO(a,b) call((a), (b))\n", Out.data());
297 }
298
299 TEST(MinimizeSourceToDependencyDirectivesTest, DefineNumber) {
300 SmallVector<char, 128> Out;
301
302 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define 0\n", Out));
303 }
304
305 TEST(MinimizeSourceToDependencyDirectivesTest, DefineNoName) {
306 SmallVector<char, 128> Out;
307
308 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define &\n", Out));
309 }
310
311 TEST(MinimizeSourceToDependencyDirectivesTest, DefineNoWhitespace) {
312 SmallVector<char, 128> Out;
313
314 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define AND&\n", Out));
315 EXPECT_STREQ("#define AND&\n", Out.data());
316
317 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define AND\\\n"
318 "&\n",
319 Out));
320 EXPECT_STREQ("#define AND\\\n"
321 "&\n",
322 Out.data());
323 }
324
325 TEST(MinimizeSourceToDependencyDirectivesTest, MultilineComment) {
326 SmallVector<char, 128> Out;
327
328 ASSERT_FALSE(
329 minimizeSourceToDependencyDirectives("#define MACRO a/*\n"
330 " /*\n"
331 "#define MISSING abc\n"
332 " /*\n"
333 " /* something */ \n"
334 "#include /* \"def\" */ <abc> \n",
335 Out));
336 EXPECT_STREQ("#define MACRO a\n"
337 "#include <abc>\n",
338 Out.data());
339 }
340
341 TEST(MinimizeSourceToDependencyDirectivesTest, MultilineCommentInStrings) {
342 SmallVector<char, 128> Out;
343
344 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define MACRO1 \"/*\"\n"
345 "#define MACRO2 \"*/\"\n",
346 Out));
347 EXPECT_STREQ("#define MACRO1 \"/*\"\n"
348 "#define MACRO2 \"*/\"\n",
349 Out.data());
350 }
351
352 TEST(MinimizeSourceToDependencyDirectivesTest, CommentSlashSlashStar) {
353 SmallVector<char, 128> Out;
354
355 ASSERT_FALSE(minimizeSourceToDependencyDirectives(
356 "#define MACRO 1 //* blah */\n", Out));
357 EXPECT_STREQ("#define MACRO 1\n", Out.data());
358 }
359
360 TEST(MinimizeSourceToDependencyDirectivesTest, Ifdef) {
361 SmallVector<char, 128> Out;
362
363 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#ifdef A\n"
364 "#define B\n"
365 "#endif\n",
366 Out));
367 EXPECT_STREQ("#ifdef A\n"
368 "#define B\n"
369 "#endif\n",
370 Out.data());
371
372 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#ifdef A\n"
373 "#define B\n"
374 "#elif B\n"
375 "#define C\n"
376 "#elif C\n"
377 "#define D\n"
378 "#else\n"
379 "#define E\n"
380 "#endif\n",
381 Out));
382 EXPECT_STREQ("#ifdef A\n"
383 "#define B\n"
384 "#elif B\n"
385 "#define C\n"
386 "#elif C\n"
387 "#define D\n"
388 "#else\n"
389 "#define E\n"
390 "#endif\n",
391 Out.data());
392 }
393
394 TEST(MinimizeSourceToDependencyDirectivesTest, Elifdef) {
395 SmallVector<char, 128> Out;
396
397 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#ifdef A\n"
398 "#define B\n"
399 "#elifdef C\n"
400 "#define D\n"
401 "#endif\n",
402 Out));
403 EXPECT_STREQ("#ifdef A\n"
404 "#define B\n"
405 "#elifdef C\n"
406 "#define D\n"
407 "#endif\n",
408 Out.data());
409
410 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#ifdef A\n"
411 "#define B\n"
412 "#elifdef B\n"
413 "#define C\n"
414 "#elifndef C\n"
415 "#define D\n"
416 "#else\n"
417 "#define E\n"
418 "#endif\n",
419 Out));
420 EXPECT_STREQ("#ifdef A\n"
421 "#define B\n"
422 "#elifdef B\n"
423 "#define C\n"
424 "#elifndef C\n"
425 "#define D\n"
426 "#else\n"
427 "#define E\n"
428 "#endif\n",
429 Out.data());
430 }
431
432 TEST(MinimizeSourceToDependencyDirectivesTest, EmptyIfdef) {
433 SmallVector<char, 128> Out;
434
435 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#ifdef A\n"
436 "void skip();\n"
437 "#elif B\n"
438 "#elif C\n"
439 "#else D\n"
440 "#endif\n",
441 Out));
442 EXPECT_STREQ("#ifdef A\n"
443 "#elif B\n"
444 "#elif C\n"
445 "#endif\n",
446 Out.data());
447 }
448
449 TEST(MinimizeSourceToDependencyDirectivesTest, EmptyElifdef) {
450 SmallVector<char, 128> Out;
451
452 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#ifdef A\n"
453 "void skip();\n"
454 "#elifdef B\n"
455 "#elifndef C\n"
456 "#else D\n"
457 "#endif\n",
458 Out));
459 EXPECT_STREQ("#ifdef A\n"
460 "#elifdef B\n"
461 "#elifndef C\n"
462 "#endif\n",
463 Out.data());
464 }
465
466 TEST(MinimizeSourceToDependencyDirectivesTest, Pragma) {
467 SmallVector<char, 128> Out;
468
469 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#pragma A\n", Out));
470 EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
471
472 ASSERT_FALSE(minimizeSourceToDependencyDirectives(
473 "#pragma push_macro(\"MACRO\")\n", Out));
474 EXPECT_STREQ("#pragma push_macro(\"MACRO\")\n", Out.data());
475
476 ASSERT_FALSE(minimizeSourceToDependencyDirectives(
477 "#pragma pop_macro(\"MACRO\")\n", Out));
478 EXPECT_STREQ("#pragma pop_macro(\"MACRO\")\n", Out.data());
479
480 ASSERT_FALSE(minimizeSourceToDependencyDirectives(
481 "#pragma include_alias(\"A\", \"B\")\n", Out));
482 EXPECT_STREQ("#pragma include_alias(\"A\", \"B\")\n", Out.data());
483
484 ASSERT_FALSE(minimizeSourceToDependencyDirectives(
485 "#pragma include_alias(<A>, <B>)\n", Out));
486 EXPECT_STREQ("#pragma include_alias(<A>, <B>)\n", Out.data());
487
488 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#pragma clang\n", Out));
489 EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
490
491 ASSERT_FALSE(
492 minimizeSourceToDependencyDirectives("#pragma clang module\n", Out));
493 EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
494
495 ASSERT_FALSE(minimizeSourceToDependencyDirectives(
496 "#pragma clang module impor\n", Out));
497 EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
498
499 ASSERT_FALSE(minimizeSourceToDependencyDirectives(
500 "#pragma clang module import\n", Out));
501 EXPECT_STREQ("#pragma clang module import\n", Out.data());
502 }
503
504 TEST(MinimizeSourceToDependencyDirectivesTest, Include) {
505 SmallVector<char, 128> Out;
506
507 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#include \"A\"\n", Out));
508 EXPECT_STREQ("#include \"A\"\n", Out.data());
509
510 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#include <A>\n", Out));
511 EXPECT_STREQ("#include <A>\n", Out.data());
512
513 ASSERT_FALSE(
514 minimizeSourceToDependencyDirectives("#include <A//A.h>\n", Out));
515 EXPECT_STREQ("#include <A//A.h>\n", Out.data());
516
517 ASSERT_FALSE(
518 minimizeSourceToDependencyDirectives("#include \"A//A.h\"\n", Out));
519 EXPECT_STREQ("#include \"A//A.h\"\n", Out.data());
520
521 ASSERT_FALSE(
522 minimizeSourceToDependencyDirectives("#include_next <A>\n", Out));
523 EXPECT_STREQ("#include_next <A>\n", Out.data());
524
525 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#import <A>\n", Out));
526 EXPECT_STREQ("#import <A>\n", Out.data());
527
528 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#import <A//A.h>\n", Out));
529 EXPECT_STREQ("#import <A//A.h>\n", Out.data());
530
531 ASSERT_FALSE(
532 minimizeSourceToDependencyDirectives("#import \"A//A.h\"\n", Out));
533 EXPECT_STREQ("#import \"A//A.h\"\n", Out.data());
534
535 ASSERT_FALSE(
536 minimizeSourceToDependencyDirectives("#__include_macros <A>\n", Out));
537 EXPECT_STREQ("#__include_macros <A>\n", Out.data());
538
539 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#include MACRO\n", Out));
540 EXPECT_STREQ("#include MACRO\n", Out.data());
541 }
542
543 TEST(MinimizeSourceToDependencyDirectivesTest, AtImport) {
544 SmallVector<char, 128> Out;
545
546 ASSERT_FALSE(minimizeSourceToDependencyDirectives("@import A;\n", Out));
547 EXPECT_STREQ("@import A;\n", Out.data());
548
549 ASSERT_FALSE(minimizeSourceToDependencyDirectives(" @ import A;\n", Out));
550 EXPECT_STREQ("@import A;\n", Out.data());
551
552 ASSERT_FALSE(minimizeSourceToDependencyDirectives("@import A\n;", Out));
553 EXPECT_STREQ("@import A;\n", Out.data());
554
555 ASSERT_FALSE(minimizeSourceToDependencyDirectives("@import A.B;\n", Out));
556 EXPECT_STREQ("@import A.B;\n", Out.data());
557
558 ASSERT_FALSE(minimizeSourceToDependencyDirectives(
559 "@import /*x*/ A /*x*/ . /*x*/ B /*x*/ \n /*x*/ ; /*x*/", Out));
560 EXPECT_STREQ("@import A.B;\n", Out.data());
561 }
562
563 TEST(MinimizeSourceToDependencyDirectivesTest, AtImportFailures) {
564 SmallVector<char, 128> Out;
565
566 ASSERT_TRUE(minimizeSourceToDependencyDirectives("@import A\n", Out));
567 ASSERT_FALSE(
568 minimizeSourceToDependencyDirectives("@import MACRO(A);\n", Out));
569 ASSERT_FALSE(minimizeSourceToDependencyDirectives("@import \" \";\n", Out));
570 }
571
572 TEST(MinimizeSourceToDependencyDirectivesTest, RawStringLiteral) {
573 SmallVector<char, 128> Out;
574
575 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#ifndef GUARD\n"
576 "#define GUARD\n"
577 "R\"()\"\n"
578 "#endif\n",
579 Out));
580 EXPECT_STREQ("#ifndef GUARD\n"
581 "#define GUARD\n"
582 "#endif\n",
583 Out.data());
584
585 bool RawStringLiteralResult = minimizeSourceToDependencyDirectives(
586 "#ifndef GUARD\n"
587 "#define GUARD\n"
588 R"raw(static constexpr char bytes[] = R"(-?:\,[]{}#&*!|>'"%@`)";)raw"
589 "\n"
590 "#endif\n",
591 Out);
592 ASSERT_FALSE(RawStringLiteralResult);
593 EXPECT_STREQ("#ifndef GUARD\n"
594 "#define GUARD\n"
595 "#endif\n",
596 Out.data());
597
598 bool RawStringLiteralResult2 = minimizeSourceToDependencyDirectives(
599 "#ifndef GUARD\n"
600 "#define GUARD\n"
601 R"raw(static constexpr char bytes[] = R"abc(-?:\,[]{}#&*!|>'"%@`)abc";)raw"
602 "\n"
603 "#endif\n",
604 Out);
605 ASSERT_FALSE(RawStringLiteralResult2);
606 EXPECT_STREQ("#ifndef GUARD\n"
607 "#define GUARD\n"
608 "#endif\n",
609 Out.data());
610 }
611
612 TEST(MinimizeSourceToDependencyDirectivesTest, SplitIdentifier) {
613 SmallVector<char, 128> Out;
614
615 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#if\\\n"
616 "ndef GUARD\n"
617 "#define GUARD\n"
618 "#endif\n",
619 Out));
620 EXPECT_STREQ("#if\\\n"
621 "ndef GUARD\n"
622 "#define GUARD\n"
623 "#endif\n",
624 Out.data());
625
626 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define GUA\\\n"
627 "RD\n",
628 Out));
629 EXPECT_STREQ("#define GUA\\\n"
630 "RD\n",
631 Out.data());
632
633 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define GUA\\\r"
634 "RD\n",
635 Out));
636 EXPECT_STREQ("#define GUA\\\r"
637 "RD\n",
638 Out.data());
639
640 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define GUA\\\n"
641 " RD\n",
642 Out));
643 EXPECT_STREQ("#define GUA RD\n", Out.data());
644 }
645
646 TEST(MinimizeSourceToDependencyDirectivesTest,
647 WhitespaceAfterLineContinuationSlash) {
648 SmallVector<char, 128> Out;
649
650 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define A 1 + \\ \n"
651 "2 + \\\t\n"
652 "3\n",
653 Out));
654 EXPECT_STREQ("#define A 1+\\ \n"
655 "2+\\\t\n"
656 "3\n",
657 Out.data());
658 }
659
660 TEST(MinimizeSourceToDependencyDirectivesTest, PoundWarningAndError) {
661 SmallVector<char, 128> Out;
662
663 for (auto Source : {
664 "#warning '\n#include <t.h>\n",
665 "#warning \"\n#include <t.h>\n",
666 "#warning /*\n#include <t.h>\n",
667 "#warning \\\n#include <t.h>\n#include <t.h>\n",
668 "#error '\n#include <t.h>\n",
669 "#error \"\n#include <t.h>\n",
670 "#error /*\n#include <t.h>\n",
671 "#error \\\n#include <t.h>\n#include <t.h>\n",
672 }) {
673 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
674 EXPECT_STREQ("#include <t.h>\n", Out.data());
675 }
676
677 for (auto Source : {
678 "#warning \\\n#include <t.h>\n",
679 "#error \\\n#include <t.h>\n",
680 }) {
681 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
682 EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
683 }
684
685 for (auto Source : {
686 "#if MACRO\n#warning '\n#endif\n",
687 "#if MACRO\n#warning \"\n#endif\n",
688 "#if MACRO\n#warning /*\n#endif\n",
689 "#if MACRO\n#error '\n#endif\n",
690 "#if MACRO\n#error \"\n#endif\n",
691 "#if MACRO\n#error /*\n#endif\n",
692 }) {
693 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
694 EXPECT_STREQ("#if MACRO\n#endif\n", Out.data());
695 }
696 }
697
698 TEST(MinimizeSourceToDependencyDirectivesTest, CharacterLiteral) {
699 SmallVector<char, 128> Out;
700
701 StringRef Source = R"(
702 #include <bob>
703 int a = 0'1;
704 int b = 0xfa'af'fa;
705 int c = 12 ' ';
706 #include <foo>
707 )";
708 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
709 EXPECT_STREQ("#include <bob>\n#include <foo>\n", Out.data());
710 }
711
712 TEST(MinimizeSourceToDependencyDirectivesTest, CharacterLiteralPrefixL) {
713 SmallVector<char, 128> Out;
714
715 StringRef Source = R"(L'P'
716 #if DEBUG
717 // '
718 #endif
719 #include <test.h>
720 )";
721 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
722 EXPECT_STREQ("#if DEBUG\n#endif\n#include <test.h>\n", Out.data());
723 }
724
725 TEST(MinimizeSourceToDependencyDirectivesTest, CharacterLiteralPrefixU) {
726 SmallVector<char, 128> Out;
727
728 StringRef Source = R"(int x = U'P';
729 #include <test.h>
730 // '
731 )";
732 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
733 EXPECT_STREQ("#include <test.h>\n", Out.data());
734 }
735
736 TEST(MinimizeSourceToDependencyDirectivesTest, CharacterLiteralPrefixu) {
737 SmallVector<char, 128> Out;
738
739 StringRef Source = R"(int x = u'b';
740 int y = u8'a';
741 int z = 128'78;
742 #include <test.h>
743 // '
744 )";
745 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
746 EXPECT_STREQ("#include <test.h>\n", Out.data());
747 }
748
749 TEST(MinimizeSourceToDependencyDirectivesTest, PragmaOnce) {
750 SmallVector<char, 128> Out;
751 SmallVector<dependency_directives_scan::Token, 4> Tokens;
752 SmallVector<Directive, 4> Directives;
753
754 StringRef Source = R"(// comment
755 #pragma once
756 // another comment
757 #include <test.h>
758 )";
759 ASSERT_FALSE(
760 minimizeSourceToDependencyDirectives(Source, Out, Tokens, Directives));
761 EXPECT_STREQ("#pragma once\n#include <test.h>\n", Out.data());
762 ASSERT_EQ(Directives.size(), 3u);
763 EXPECT_EQ(Directives[0].Kind, dependency_directives_scan::pp_pragma_once);
764
765 Source = R"(// comment
766 #pragma once extra tokens
767 // another comment
768 #include <test.h>
769 )";
770 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
771 EXPECT_STREQ("#pragma once extra tokens\n#include <test.h>\n", Out.data());
772 }
773
774 TEST(MinimizeSourceToDependencyDirectivesTest,
775 SkipLineStringCharLiteralsUntilNewline) {
776 SmallVector<char, 128> Out;
777
778 StringRef Source = R"(#if NEVER_ENABLED
779 #define why(fmt, ...) #error don't try me
780 #endif
781
782 void foo();
783 )";
784 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
785 EXPECT_STREQ(
786 "#if NEVER_ENABLED\n#define why(fmt,...) #error don't try me\n#endif\n"
787 "<TokBeforeEOF>\n",
788 Out.data());
789
790 Source = R"(#if NEVER_ENABLED
791 #define why(fmt, ...) "quote dropped
792 #endif
793
794 void foo();
795 )";
796 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
797 EXPECT_STREQ(
798 "#if NEVER_ENABLED\n#define why(fmt,...) \"quote dropped\n#endif\n"
799 "<TokBeforeEOF>\n",
800 Out.data());
801 }
802
803 TEST(MinimizeSourceToDependencyDirectivesTest,
804 SupportWhitespaceBeforeLineContinuation) {
805 SmallVector<char, 128> Out;
806
807 ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define FOO(BAR) \\\n"
808 " #BAR\\\n"
809 " baz\n",
810 Out));
811 EXPECT_STREQ("#define FOO(BAR) #BAR baz\n", Out.data());
812 }
813
814 TEST(MinimizeSourceToDependencyDirectivesTest,
815 SupportWhitespaceBeforeLineContinuationInStringSkipping) {
816 SmallVector<char, 128> Out;
817
818 StringRef Source = "#define X '\\ \t\nx'\nvoid foo() {}";
819 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
820 EXPECT_STREQ("#define X '\\ \t\nx'\n<TokBeforeEOF>\n", Out.data());
821
822 Source = "#define X \"\\ \r\nx\"\nvoid foo() {}";
823 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
824 EXPECT_STREQ("#define X \"\\ \r\nx\"\n<TokBeforeEOF>\n", Out.data());
825
826 Source = "#define X \"\\ \r\nx\n#include <x>\n";
827 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
828 EXPECT_STREQ("#define X\"\\ \r\nx\n#include <x>\n", Out.data());
829 }
830
831 TEST(MinimizeSourceToDependencyDirectivesTest, CxxModules) {
832 SmallVector<char, 128> Out;
833 SmallVector<dependency_directives_scan::Token, 4> Tokens;
834 SmallVector<Directive, 4> Directives;
835
836 StringRef Source = R"(
837 module;
838 #include "textual-header.h"
839
840 export module m;
841 exp\
842 ort \
843 import \
844 :l [[rename]];
845
846 export void f();
847
848 void h() {
849 import.a = 3;
850 import = 3;
851 import <<= 3;
852 import->a = 3;
853 import();
854 import . a();
855
856 import a b d e d e f e;
857 import foo [[no_unique_address]];
858 import foo();
859 import f(:sefse);
860 import f(->a = 3);
861 }
862 )";
863 ASSERT_FALSE(
864 minimizeSourceToDependencyDirectives(Source, Out, Tokens, Directives));
865 EXPECT_STREQ("#include \"textual-header.h\"\nexport module m;"
866 "exp\\\nort import:l[[rename]];"
867 "import<<=3;import a b d e d e f e;"
868 "import foo[[no_unique_address]];import foo();"
869 "import f(:sefse);import f(->a=3);"
870 "<TokBeforeEOF>\n",
871 Out.data());
872 ASSERT_EQ(Directives.size(), 11u);
873 EXPECT_EQ(Directives[0].Kind, pp_include);
874 EXPECT_EQ(Directives[1].Kind, cxx_export_module_decl);
875 }
876
877 TEST(MinimizeSourceToDependencyDirectivesTest, TokensBeforeEOF) {
878 SmallString<128> Out;
879
880 StringRef Source = R"(
881 #define A
882 #ifdef B
883 int x;
884 #endif
885 )";
886 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
887 EXPECT_STREQ("#define A\n<TokBeforeEOF>\n", Out.data());
888
889 Source = R"(
890 #ifndef A
891 #define A
892 #endif // some comment
893
894 // other comment
895 )";
896 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
897 EXPECT_STREQ("#ifndef A\n#define A\n#endif\n", Out.data());
898
899 Source = R"(
900 #ifndef A
901 #define A
902 #endif /* some comment
903
904 */
905 )";
906 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
907 EXPECT_STREQ("#ifndef A\n#define A\n#endif\n", Out.data());
908
909 Source = R"(
910 #ifndef A
911 #define A
912 #endif /* some comment
913
914 */
915 int x;
916 )";
917 ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
918 EXPECT_STREQ("#ifndef A\n#define A\n#endif\n<TokBeforeEOF>\n", Out.data());
919 }
920
921 } // end anonymous namespace