Debian Codegraph @

llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp

raw
1 //===--- PreambleTests.cpp --------------------------------------*- C++ -*-===//
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 "Annotations.h"
10 #include "Compiler.h"
11 #include "Headers.h"
12 #include "Hover.h"
13 #include "Preamble.h"
14 #include "SourceCode.h"
15 #include "TestFS.h"
16 #include "TestTU.h"
17 #include "XRefs.h"
18 #include "clang/Format/Format.h"
19 #include "clang/Frontend/FrontendActions.h"
20 #include "clang/Frontend/PrecompiledPreamble.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/VirtualFileSystem.h"
25 #include "gmock/gmock.h"
26 #include "gtest/gtest.h"
27 #include <memory>
28 #include <optional>
29 #include <string>
30 #include <vector>
31
32 using testing::Contains;
33 using testing::Field;
34 using testing::Matcher;
35 using testing::MatchesRegex;
36
37 namespace clang {
38 namespace clangd {
39 namespace {
40
41 MATCHER_P2(Distance, File, D, "") {
42 return arg.first() == File && arg.second == D;
43 }
44
45 // Builds a preamble for BaselineContents, patches it for ModifiedContents and
46 // returns the includes in the patch.
47 IncludeStructure
48 collectPatchedIncludes(llvm::StringRef ModifiedContents,
49 llvm::StringRef BaselineContents,
50 llvm::StringRef MainFileName = "main.cpp") {
51 MockFS FS;
52 auto TU = TestTU::withCode(BaselineContents);
53 TU.Filename = MainFileName.str();
54 // ms-compatibility changes meaning of #import, make sure it is turned off.
55 TU.ExtraArgs = {"-fno-ms-compatibility"};
56 auto BaselinePreamble = TU.preamble();
57 // Create the patch.
58 TU.Code = ModifiedContents.str();
59 auto PI = TU.inputs(FS);
60 auto PP = PreamblePatch::createFullPatch(testPath(TU.Filename), PI,
61 *BaselinePreamble);
62 // Collect patch contents.
63 IgnoreDiagnostics Diags;
64 auto CI = buildCompilerInvocation(PI, Diags);
65 PP.apply(*CI);
66 // Run preprocessor over the modified contents with patched Invocation. We
67 // provide a preamble and trim contents to ensure only the implicit header
68 // introduced by the patch is parsed and nothing else.
69 // We don't run PP directly over the patch cotents to test production
70 // behaviour.
71 auto Bounds = Lexer::ComputePreamble(ModifiedContents, *CI->getLangOpts());
72 auto Clang =
73 prepareCompilerInstance(std::move(CI), &BaselinePreamble->Preamble,
74 llvm::MemoryBuffer::getMemBufferCopy(
75 ModifiedContents.slice(0, Bounds.Size).str()),
76 PI.TFS->view(PI.CompileCommand.Directory), Diags);
77 PreprocessOnlyAction Action;
78 if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
79 ADD_FAILURE() << "failed begin source file";
80 return {};
81 }
82 IncludeStructure Includes;
83 Includes.collect(*Clang);
84 if (llvm::Error Err = Action.Execute()) {
85 ADD_FAILURE() << "failed to execute action: " << std::move(Err);
86 return {};
87 }
88 Action.EndSourceFile();
89 return Includes;
90 }
91
92 // Check preamble lexing logic by building an empty preamble and patching it
93 // with all the contents.
94 TEST(PreamblePatchTest, IncludeParsing) {
95 // We expect any line with a point to show up in the patch.
96 llvm::StringRef Cases[] = {
97 // Only preamble
98 R"cpp(^#include "a.h")cpp",
99 // Both preamble and mainfile
100 R"cpp(
101 ^#include "a.h"
102 garbage, finishes preamble
103 #include "a.h")cpp",
104 // Mixed directives
105 R"cpp(
106 ^#include "a.h"
107 #pragma directive
108 // some comments
109 ^#include_next <a.h>
110 #ifdef skipped
111 ^#import "a.h"
112 #endif)cpp",
113 // Broken directives
114 R"cpp(
115 #include "a
116 ^#include "a.h"
117 #include <b
118 ^#include <b.h>)cpp",
119 // Directive is not part of preamble if it is not the token immediately
120 // followed by the hash (#).
121 R"cpp(
122 ^#include "a.h"
123 #/**/include <b.h>)cpp",
124 };
125
126 for (const auto &Case : Cases) {
127 Annotations Test(Case);
128 const auto Code = Test.code();
129 SCOPED_TRACE(Code);
130
131 auto Includes =
132 collectPatchedIncludes(Code, /*BaselineContents=*/"").MainFileIncludes;
133 auto Points = Test.points();
134 ASSERT_EQ(Includes.size(), Points.size());
135 for (size_t I = 0, E = Includes.size(); I != E; ++I)
136 EXPECT_EQ(Includes[I].HashLine, Points[I].line);
137 }
138 }
139
140 TEST(PreamblePatchTest, ContainsNewIncludes) {
141 constexpr llvm::StringLiteral BaselineContents = R"cpp(
142 #include <a.h>
143 #include <b.h> // This will be removed
144 #include <c.h>
145 )cpp";
146 constexpr llvm::StringLiteral ModifiedContents = R"cpp(
147 #include <a.h>
148 #include <c.h> // This has changed a line.
149 #include <c.h> // This is a duplicate.
150 #include <d.h> // This is newly introduced.
151 )cpp";
152 auto Includes = collectPatchedIncludes(ModifiedContents, BaselineContents)
153 .MainFileIncludes;
154 EXPECT_THAT(Includes, ElementsAre(AllOf(Field(&Inclusion::Written, "<d.h>"),
155 Field(&Inclusion::HashLine, 4))));
156 }
157
158 TEST(PreamblePatchTest, MainFileIsEscaped) {
159 auto Includes = collectPatchedIncludes("#include <a.h>", "", "file\"name.cpp")
160 .MainFileIncludes;
161 EXPECT_THAT(Includes, ElementsAre(AllOf(Field(&Inclusion::Written, "<a.h>"),
162 Field(&Inclusion::HashLine, 0))));
163 }
164
165 TEST(PreamblePatchTest, PatchesPreambleIncludes) {
166 MockFS FS;
167 IgnoreDiagnostics Diags;
168 auto TU = TestTU::withCode(R"cpp(
169 #include "a.h"
170 #include "c.h"
171 )cpp");
172 TU.AdditionalFiles["a.h"] = "#include \"b.h\"";
173 TU.AdditionalFiles["b.h"] = "";
174 TU.AdditionalFiles["c.h"] = "";
175 auto PI = TU.inputs(FS);
176 auto BaselinePreamble = buildPreamble(
177 TU.Filename, *buildCompilerInvocation(PI, Diags), PI, true, nullptr);
178 // We drop c.h from modified and add a new header. Since the latter is patched
179 // we should only get a.h in preamble includes.
180 TU.Code = R"cpp(
181 #include "a.h"
182 #include "b.h"
183 )cpp";
184 auto PP = PreamblePatch::createFullPatch(testPath(TU.Filename), TU.inputs(FS),
185 *BaselinePreamble);
186 // Only a.h should exists in the preamble, as c.h has been dropped and b.h was
187 // newly introduced.
188 EXPECT_THAT(PP.preambleIncludes(),
189 ElementsAre(AllOf(Field(&Inclusion::Written, "\"a.h\""),
190 Field(&Inclusion::Resolved, testPath("a.h")))));
191 }
192
193 std::optional<ParsedAST> createPatchedAST(llvm::StringRef Baseline,
194 llvm::StringRef Modified) {
195 auto BaselinePreamble = TestTU::withCode(Baseline).preamble();
196 if (!BaselinePreamble) {
197 ADD_FAILURE() << "Failed to build baseline preamble";
198 return std::nullopt;
199 }
200
201 IgnoreDiagnostics Diags;
202 MockFS FS;
203 auto TU = TestTU::withCode(Modified);
204 auto CI = buildCompilerInvocation(TU.inputs(FS), Diags);
205 if (!CI) {
206 ADD_FAILURE() << "Failed to build compiler invocation";
207 return std::nullopt;
208 }
209 return ParsedAST::build(testPath(TU.Filename), TU.inputs(FS), std::move(CI),
210 {}, BaselinePreamble);
211 }
212
213 std::string getPreamblePatch(llvm::StringRef Baseline,
214 llvm::StringRef Modified) {
215 auto BaselinePreamble = TestTU::withCode(Baseline).preamble();
216 if (!BaselinePreamble) {
217 ADD_FAILURE() << "Failed to build baseline preamble";
218 return "";
219 }
220 MockFS FS;
221 auto TU = TestTU::withCode(Modified);
222 return PreamblePatch::createFullPatch(testPath("main.cpp"), TU.inputs(FS),
223 *BaselinePreamble)
224 .text()
225 .str();
226 }
227
228 TEST(PreamblePatchTest, Define) {
229 // BAR should be defined while parsing the AST.
230 struct {
231 const char *const Contents;
232 const char *const ExpectedPatch;
233 } Cases[] = {
234 {
235 R"cpp(
236 #define BAR
237 [[BAR]])cpp",
238 R"cpp(#line 0 ".*main.cpp"
239 #line 2
240 #define BAR
241 )cpp",
242 },
243 // multiline macro
244 {
245 R"cpp(
246 #define BAR \
247
248 [[BAR]])cpp",
249 R"cpp(#line 0 ".*main.cpp"
250 #line 2
251 #define BAR
252 )cpp",
253 },
254 // multiline macro
255 {
256 R"cpp(
257 #define \
258 BAR
259 [[BAR]])cpp",
260 R"cpp(#line 0 ".*main.cpp"
261 #line 3
262 #define BAR
263 )cpp",
264 },
265 };
266
267 for (const auto &Case : Cases) {
268 SCOPED_TRACE(Case.Contents);
269 Annotations Modified(Case.Contents);
270 EXPECT_THAT(getPreamblePatch("", Modified.code()),
271 MatchesRegex(Case.ExpectedPatch));
272
273 auto AST = createPatchedAST("", Modified.code());
274 ASSERT_TRUE(AST);
275 std::vector<Range> MacroRefRanges;
276 for (auto &M : AST->getMacros().MacroRefs) {
277 for (auto &O : M.getSecond())
278 MacroRefRanges.push_back(O.Rng);
279 }
280 EXPECT_THAT(MacroRefRanges, Contains(Modified.range()));
281 }
282 }
283
284 TEST(PreamblePatchTest, OrderingPreserved) {
285 llvm::StringLiteral Baseline = "#define BAR(X) X";
286 Annotations Modified(R"cpp(
287 #define BAR(X, Y) X Y
288 #define BAR(X) X
289 [[BAR]](int y);
290 )cpp");
291
292 llvm::StringLiteral ExpectedPatch(R"cpp(#line 0 ".*main.cpp"
293 #line 2
294 #define BAR\(X, Y\) X Y
295 #line 3
296 #define BAR\(X\) X
297 )cpp");
298 EXPECT_THAT(getPreamblePatch(Baseline, Modified.code()),
299 MatchesRegex(ExpectedPatch.str()));
300
301 auto AST = createPatchedAST(Baseline, Modified.code());
302 ASSERT_TRUE(AST);
303 }
304
305 TEST(PreamblePatchTest, LocateMacroAtWorks) {
306 struct {
307 const char *const Baseline;
308 const char *const Modified;
309 } Cases[] = {
310 // Addition of new directive
311 {
312 "",
313 R"cpp(
314 #define $def^FOO
315 $use^FOO)cpp",
316 },
317 // Available inside preamble section
318 {
319 "",
320 R"cpp(
321 #define $def^FOO
322 #undef $use^FOO)cpp",
323 },
324 // Available after undef, as we don't patch those
325 {
326 "",
327 R"cpp(
328 #define $def^FOO
329 #undef FOO
330 $use^FOO)cpp",
331 },
332 // Identifier on a different line
333 {
334 "",
335 R"cpp(
336 #define \
337 $def^FOO
338 $use^FOO)cpp",
339 },
340 // In presence of comment tokens
341 {
342 "",
343 R"cpp(
344 #\
345 define /* FOO */\
346 /* FOO */ $def^FOO
347 $use^FOO)cpp",
348 },
349 // Moved around
350 {
351 "#define FOO",
352 R"cpp(
353 #define BAR
354 #define $def^FOO
355 $use^FOO)cpp",
356 },
357 };
358 for (const auto &Case : Cases) {
359 SCOPED_TRACE(Case.Modified);
360 llvm::Annotations Modified(Case.Modified);
361 auto AST = createPatchedAST(Case.Baseline, Modified.code());
362 ASSERT_TRUE(AST);
363
364 const auto &SM = AST->getSourceManager();
365 auto *MacroTok = AST->getTokens().spelledTokenAt(
366 SM.getComposedLoc(SM.getMainFileID(), Modified.point("use")));
367 ASSERT_TRUE(MacroTok);
368
369 auto FoundMacro = locateMacroAt(*MacroTok, AST->getPreprocessor());
370 ASSERT_TRUE(FoundMacro);
371 EXPECT_THAT(FoundMacro->Name, "FOO");
372
373 auto MacroLoc = FoundMacro->NameLoc;
374 EXPECT_EQ(SM.getFileID(MacroLoc), SM.getMainFileID());
375 EXPECT_EQ(SM.getFileOffset(MacroLoc), Modified.point("def"));
376 }
377 }
378
379 TEST(PreamblePatchTest, LocateMacroAtDeletion) {
380 {
381 // We don't patch deleted define directives, make sure we don't crash.
382 llvm::StringLiteral Baseline = "#define FOO";
383 llvm::Annotations Modified("^FOO");
384
385 auto AST = createPatchedAST(Baseline, Modified.code());
386 ASSERT_TRUE(AST);
387
388 const auto &SM = AST->getSourceManager();
389 auto *MacroTok = AST->getTokens().spelledTokenAt(
390 SM.getComposedLoc(SM.getMainFileID(), Modified.point()));
391 ASSERT_TRUE(MacroTok);
392
393 auto FoundMacro = locateMacroAt(*MacroTok, AST->getPreprocessor());
394 ASSERT_TRUE(FoundMacro);
395 EXPECT_THAT(FoundMacro->Name, "FOO");
396 auto HI =
397 getHover(*AST, offsetToPosition(Modified.code(), Modified.point()),
398 format::getLLVMStyle(), nullptr);
399 ASSERT_TRUE(HI);
400 EXPECT_THAT(HI->Definition, testing::IsEmpty());
401 }
402
403 {
404 // Offset is valid, but underlying text is different.
405 llvm::StringLiteral Baseline = "#define FOO";
406 Annotations Modified(R"cpp(#define BAR
407 ^FOO")cpp");
408
409 auto AST = createPatchedAST(Baseline, Modified.code());
410 ASSERT_TRUE(AST);
411
412 auto HI = getHover(*AST, Modified.point(), format::getLLVMStyle(), nullptr);
413 ASSERT_TRUE(HI);
414 EXPECT_THAT(HI->Definition, "#define BAR");
415 }
416 }
417
418 MATCHER_P(referenceRangeIs, R, "") { return arg.Loc.range == R; }
419
420 TEST(PreamblePatchTest, RefsToMacros) {
421 struct {
422 const char *const Baseline;
423 const char *const Modified;
424 } Cases[] = {
425 // Newly added
426 {
427 "",
428 R"cpp(
429 #define ^FOO
430 ^[[FOO]])cpp",
431 },
432 // Moved around
433 {
434 "#define FOO",
435 R"cpp(
436 #define BAR
437 #define ^FOO
438 ^[[FOO]])cpp",
439 },
440 // Ref in preamble section
441 {
442 "",
443 R"cpp(
444 #define ^FOO
445 #undef ^FOO)cpp",
446 },
447 };
448
449 for (const auto &Case : Cases) {
450 Annotations Modified(Case.Modified);
451 auto AST = createPatchedAST("", Modified.code());
452 ASSERT_TRUE(AST);
453
454 const auto &SM = AST->getSourceManager();
455 std::vector<Matcher<ReferencesResult::Reference>> ExpectedLocations;
456 for (const auto &R : Modified.ranges())
457 ExpectedLocations.push_back(referenceRangeIs(R));
458
459 for (const auto &P : Modified.points()) {
460 auto *MacroTok = AST->getTokens().spelledTokenAt(SM.getComposedLoc(
461 SM.getMainFileID(),
462 llvm::cantFail(positionToOffset(Modified.code(), P))));
463 ASSERT_TRUE(MacroTok);
464 EXPECT_THAT(findReferences(*AST, P, 0).References,
465 testing::ElementsAreArray(ExpectedLocations));
466 }
467 }
468 }
469
470 TEST(TranslatePreamblePatchLocation, Simple) {
471 auto TU = TestTU::withHeaderCode(R"cpp(
472 #line 3 "main.cpp"
473 int foo();)cpp");
474 // Presumed line/col needs to be valid in the main file.
475 TU.Code = R"cpp(// line 1
476 // line 2
477 // line 3
478 // line 4)cpp";
479 TU.Filename = "main.cpp";
480 TU.HeaderFilename = "__preamble_patch__.h";
481 TU.ImplicitHeaderGuard = false;
482
483 auto AST = TU.build();
484 auto &SM = AST.getSourceManager();
485 auto &ND = findDecl(AST, "foo");
486 EXPECT_NE(SM.getFileID(ND.getLocation()), SM.getMainFileID());
487
488 auto TranslatedLoc = translatePreamblePatchLocation(ND.getLocation(), SM);
489 auto DecompLoc = SM.getDecomposedLoc(TranslatedLoc);
490 EXPECT_EQ(DecompLoc.first, SM.getMainFileID());
491 EXPECT_EQ(SM.getLineNumber(DecompLoc.first, DecompLoc.second), 3U);
492 }
493
494 TEST(PreamblePatch, ModifiedBounds) {
495 struct {
496 const char *const Baseline;
497 const char *const Modified;
498 } Cases[] = {
499 // Size increased
500 {
501 "",
502 R"cpp(
503 #define FOO
504 FOO)cpp",
505 },
506 // Stayed same
507 {"#define FOO", "#define BAR"},
508 // Got smaller
509 {
510 R"cpp(
511 #define FOO
512 #undef FOO)cpp",
513 "#define FOO"},
514 };
515
516 for (const auto &Case : Cases) {
517 auto TU = TestTU::withCode(Case.Baseline);
518 auto BaselinePreamble = TU.preamble();
519 ASSERT_TRUE(BaselinePreamble);
520
521 Annotations Modified(Case.Modified);
522 TU.Code = Modified.code().str();
523 MockFS FS;
524 auto PP = PreamblePatch::createFullPatch(testPath(TU.Filename),
525 TU.inputs(FS), *BaselinePreamble);
526
527 IgnoreDiagnostics Diags;
528 auto CI = buildCompilerInvocation(TU.inputs(FS), Diags);
529 ASSERT_TRUE(CI);
530
531 const auto ExpectedBounds =
532 Lexer::ComputePreamble(Case.Modified, *CI->getLangOpts());
533 EXPECT_EQ(PP.modifiedBounds().Size, ExpectedBounds.Size);
534 EXPECT_EQ(PP.modifiedBounds().PreambleEndsAtStartOfLine,
535 ExpectedBounds.PreambleEndsAtStartOfLine);
536 }
537 }
538
539 TEST(PreamblePatch, DropsDiagnostics) {
540 llvm::StringLiteral Code = "#define FOO\nx;/* error-ok */";
541 // First check that this code generates diagnostics.
542 EXPECT_THAT(*TestTU::withCode(Code).build().getDiagnostics(),
543 testing::Not(testing::IsEmpty()));
544 // Ensure they are dropeed when a patched preamble is used.
545 EXPECT_FALSE(createPatchedAST("", Code)->getDiagnostics());
546 }
547
548 TEST(PreamblePatch, MacroLoc) {
549 llvm::StringLiteral Baseline = "\n#define MACRO 12\nint num = MACRO;";
550 llvm::StringLiteral Modified = " \n#define MACRO 12\nint num = MACRO;";
551 auto AST = createPatchedAST(Baseline, Modified);
552 ASSERT_TRUE(AST);
553 }
554
555 TEST(PreamblePatch, NoopWhenNotRequested) {
556 llvm::StringLiteral Baseline = "#define M\nint num = M;";
557 llvm::StringLiteral Modified = "#define M\n#include <foo.h>\nint num = M;";
558 auto TU = TestTU::withCode(Baseline);
559 auto BaselinePreamble = TU.preamble();
560 ASSERT_TRUE(BaselinePreamble);
561
562 TU.Code = Modified.str();
563 MockFS FS;
564 auto PP = PreamblePatch::createMacroPatch(testPath(TU.Filename),
565 TU.inputs(FS), *BaselinePreamble);
566 EXPECT_TRUE(PP.text().empty());
567 }
568 } // namespace
569 } // namespace clangd
570 } // namespace clang