clang-tools 24.0.0git
ParsedASTTests.cpp
Go to the documentation of this file.
1//===-- ParsedASTTests.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// These tests cover clangd's logic to build a TU, which generally uses the APIs
10// in ParsedAST and Preamble, via the TestTU helper.
11//
12//===----------------------------------------------------------------------===//
13
15#include "AST.h"
16#include "Compiler.h"
17#include "Config.h"
18#include "Diagnostics.h"
19#include "Headers.h"
20#include "ParsedAST.h"
21#include "Preamble.h"
22#include "SourceCode.h"
23#include "TestFS.h"
24#include "TestTU.h"
25#include "TidyProvider.h"
26#include "support/Context.h"
27#include "clang/AST/DeclTemplate.h"
28#include "clang/Basic/FileEntry.h"
29#include "clang/Basic/SourceLocation.h"
30#include "clang/Basic/SourceManager.h"
31#include "clang/Basic/TokenKinds.h"
32#include "clang/Tooling/Syntax/Tokens.h"
33#include "llvm/ADT/StringRef.h"
34#include "llvm/Testing/Annotations/Annotations.h"
35#include "llvm/Testing/Support/Error.h"
36#include "gmock/gmock-matchers.h"
37#include "gmock/gmock.h"
38#include "gtest/gtest.h"
39#include <memory>
40#include <string_view>
41#include <utility>
42#include <vector>
43
44namespace clang {
45namespace clangd {
46namespace {
47
48using ::testing::AllOf;
49using ::testing::Contains;
50using ::testing::ElementsAre;
51using ::testing::ElementsAreArray;
52using ::testing::IsEmpty;
53
54MATCHER_P(declNamed, Name, "") {
55 if (NamedDecl *ND = dyn_cast<NamedDecl>(arg))
56 if (ND->getName() == Name)
57 return true;
58 if (auto *Stream = result_listener->stream()) {
59 llvm::raw_os_ostream OS(*Stream);
60 arg->dump(OS);
61 }
62 return false;
63}
64
65MATCHER_P(declKind, Kind, "") {
66 if (NamedDecl *ND = dyn_cast<NamedDecl>(arg))
67 if (ND->getDeclKindName() == llvm::StringRef(Kind))
68 return true;
69 if (auto *Stream = result_listener->stream()) {
70 llvm::raw_os_ostream OS(*Stream);
71 arg->dump(OS);
72 }
73 return false;
74}
75
76// Matches if the Decl has template args equal to ArgName. If the decl is a
77// NamedDecl and ArgName is an empty string it also matches.
78MATCHER_P(withTemplateArgs, ArgName, "") {
79 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(arg)) {
80 if (const auto *Args = FD->getTemplateSpecializationArgs()) {
81 std::string SpecializationArgs;
82 // Without the PrintingPolicy "bool" will be printed as "_Bool".
83 LangOptions LO;
84 PrintingPolicy Policy(LO);
85 Policy.adjustForCPlusPlus();
86 for (const auto &Arg : Args->asArray()) {
87 if (SpecializationArgs.size() > 0)
88 SpecializationArgs += ",";
89 SpecializationArgs += Arg.getAsType().getAsString(Policy);
90 }
91 if (Args->size() == 0)
92 return ArgName == SpecializationArgs;
93 return ArgName == "<" + SpecializationArgs + ">";
94 }
95 }
96 if (const NamedDecl *ND = dyn_cast<NamedDecl>(arg))
98 return false;
99}
100
101MATCHER_P(pragmaTrivia, P, "") { return arg.Trivia == P; }
102
103MATCHER(eqInc, "") {
104 Inclusion Actual = testing::get<0>(arg);
105 Inclusion Expected = testing::get<1>(arg);
106 return std::tie(Actual.HashLine, Actual.Written) ==
107 std::tie(Expected.HashLine, Expected.Written);
108}
109
110TEST(ParsedASTTest, TopLevelDecls) {
111 TestTU TU;
112 TU.HeaderCode = R"(
113 int header1();
114 int header2;
115 )";
116 TU.Code = R"cpp(
117 int main();
118 template <typename> bool X = true;
119 )cpp";
120 auto AST = TU.build();
121 EXPECT_THAT(AST.getLocalTopLevelDecls(),
122 testing::UnorderedElementsAreArray(
123 {AllOf(declNamed("main"), declKind("Function")),
124 AllOf(declNamed("X"), declKind("VarTemplate"))}));
125}
126
127TEST(ParsedASTTest, DoesNotGetIncludedTopDecls) {
128 TestTU TU;
129 TU.HeaderCode = R"cpp(
130 #define LL void foo(){}
131 template<class T>
132 struct H {
133 H() {}
134 LL
135 };
136 )cpp";
137 TU.Code = R"cpp(
138 int main() {
139 H<int> h;
140 h.foo();
141 }
142 )cpp";
143 auto AST = TU.build();
144 EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(declNamed("main")));
145}
146
147TEST(ParsedASTTest, DoesNotGetImplicitTemplateTopDecls) {
148 TestTU TU;
149 TU.Code = R"cpp(
150 template<typename T>
151 void f(T) {}
152 void s() {
153 f(10UL);
154 }
155 )cpp";
156
157 auto AST = TU.build();
158 EXPECT_THAT(AST.getLocalTopLevelDecls(),
159 ElementsAre(declNamed("f"), declNamed("s")));
160}
161
162TEST(ParsedASTTest,
163 GetsExplicitInstantiationAndSpecializationTemplateTopDecls) {
164 TestTU TU;
165 TU.Code = R"cpp(
166 template <typename T>
167 void f(T) {}
168 template<>
169 void f(bool);
170 template void f(double);
171
172 template <class T>
173 struct V {};
174 template<class T>
175 struct V<T*> {};
176 template <>
177 struct V<bool> {};
178
179 template<class T>
180 T foo = T(10);
181 int i = foo<int>;
182 double d = foo<double>;
183
184 template <class T>
185 int foo<T*> = 0;
186 template <>
187 int foo<bool> = 0;
188 )cpp";
189
190 auto AST = TU.build();
191 EXPECT_THAT(
192 AST.getLocalTopLevelDecls(),
193 ElementsAreArray({AllOf(declNamed("f"), withTemplateArgs("")),
194 AllOf(declNamed("f"), withTemplateArgs("<bool>")),
195 AllOf(declNamed("f"), withTemplateArgs("<double>")),
196 AllOf(declNamed("V"), withTemplateArgs("")),
197 AllOf(declNamed("V"), withTemplateArgs("<T *>")),
198 AllOf(declNamed("V"), withTemplateArgs("<bool>")),
199 AllOf(declNamed("foo"), withTemplateArgs("")),
200 AllOf(declNamed("i"), withTemplateArgs("")),
201 AllOf(declNamed("d"), withTemplateArgs("")),
202 AllOf(declNamed("foo"), withTemplateArgs("<T *>")),
203 AllOf(declNamed("foo"), withTemplateArgs("<bool>"))}));
204}
205
206TEST(ParsedASTTest, IgnoresDelayedTemplateParsing) {
207 auto TU = TestTU::withCode(R"cpp(
208 template <typename T> void xxx() {
209 int yyy = 0;
210 }
211 )cpp");
212 TU.ExtraArgs.push_back("-fdelayed-template-parsing");
213 auto AST = TU.build();
214 EXPECT_EQ(Decl::Var, findUnqualifiedDecl(AST, "yyy").getKind());
215}
216
217TEST(ParsedASTTest, TokensAfterPreamble) {
218 TestTU TU;
219 TU.AdditionalFiles["foo.h"] = R"(
220 int foo();
221 )";
222 TU.Code = R"cpp(
223 #include "foo.h"
224 first_token;
225 void test() {
226 // error-ok: invalid syntax, just examining token stream
227 }
228 last_token
229)cpp";
230 auto AST = TU.build();
231 const syntax::TokenBuffer &T = AST.getTokens();
232 const auto &SM = AST.getSourceManager();
233
234 ASSERT_GT(T.expandedTokens().size(), 2u);
235 // Check first token after the preamble.
236 EXPECT_EQ(T.expandedTokens().front().text(SM), "first_token");
237 // Last token is always 'eof'.
238 EXPECT_EQ(T.expandedTokens().back().kind(), tok::eof);
239 // Check the token before 'eof'.
240 EXPECT_EQ(T.expandedTokens().drop_back().back().text(SM), "last_token");
241
242 // The spelled tokens for the main file should have everything.
243 auto Spelled = T.spelledTokens(SM.getMainFileID());
244 ASSERT_FALSE(Spelled.empty());
245 EXPECT_EQ(Spelled.front().kind(), tok::hash);
246 EXPECT_EQ(Spelled.back().text(SM), "last_token");
247}
248
249TEST(ParsedASTTest, NoCrashOnTokensWithTidyCheck) {
250 TestTU TU;
251 // this check runs the preprocessor, we need to make sure it does not break
252 // our recording logic.
253 TU.ClangTidyProvider = addTidyChecks("modernize-use-trailing-return-type");
254 TU.Code = "inline int foo() { return 0; }";
255
256 auto AST = TU.build();
257 const syntax::TokenBuffer &T = AST.getTokens();
258 const auto &SM = AST.getSourceManager();
259
260 ASSERT_GT(T.expandedTokens().size(), 7u);
261 // Check first token after the preamble.
262 EXPECT_EQ(T.expandedTokens().front().text(SM), "inline");
263 // Last token is always 'eof'.
264 EXPECT_EQ(T.expandedTokens().back().kind(), tok::eof);
265 // Check the token before 'eof'.
266 EXPECT_EQ(T.expandedTokens().drop_back().back().text(SM), "}");
267}
268
269TEST(ParsedASTTest, CanBuildInvocationWithUnknownArgs) {
270 MockFS FS;
271 FS.Files = {{testPath("foo.cpp"), "void test() {}"}};
272 // Unknown flags should not prevent a build of compiler invocation.
273 ParseInputs Inputs;
274 Inputs.TFS = &FS;
275 Inputs.CompileCommand.CommandLine = {"clang", "-fsome-unknown-flag",
276 testPath("foo.cpp")};
277 IgnoreDiagnostics IgnoreDiags;
278 EXPECT_NE(buildCompilerInvocation(Inputs, IgnoreDiags), nullptr);
279
280 // Unknown forwarded to -cc1 should not a failure either.
281 Inputs.CompileCommand.CommandLine = {
282 "clang", "-Xclang", "-fsome-unknown-flag", testPath("foo.cpp")};
283 EXPECT_NE(buildCompilerInvocation(Inputs, IgnoreDiags), nullptr);
284}
285
286TEST(ParsedASTTest, CollectsMainFileMacroExpansions) {
287 llvm::Annotations TestCase(R"cpp(
288 #define ^MACRO_ARGS(X, Y) X Y
289 // - preamble ends
290 ^ID(int A);
291 // Macro arguments included.
292 ^MACRO_ARGS(^MACRO_ARGS(^MACRO_EXP(int), E), ^ID(= 2));
293
294 // Macro names inside other macros not included.
295 #define ^MACRO_ARGS2(X, Y) X Y
296 #define ^FOO BAR
297 #define ^BAR 1
298 int F = ^FOO;
299
300 // Macros from token concatenations not included.
301 #define ^CONCAT(X) X##A()
302 #define ^PREPEND(X) MACRO##X()
303 #define ^MACROA() 123
304 int G = ^CONCAT(MACRO);
305 int H = ^PREPEND(A);
306
307 // Macros included not from preamble not included.
308 #include "foo.inc"
309
310 int printf(const char*, ...);
311 void exit(int);
312 #define ^assert(COND) if (!(COND)) { printf("%s", #COND); exit(0); }
313
314 void test() {
315 // Includes macro expansions in arguments that are expressions
316 ^assert(0 <= ^BAR);
317 }
318
319 #ifdef ^UNDEFINED
320 #endif
321
322 #define ^MULTIPLE_DEFINITION 1
323 #undef ^MULTIPLE_DEFINITION
324
325 #define ^MULTIPLE_DEFINITION 2
326 #undef ^MULTIPLE_DEFINITION
327 )cpp");
328 auto TU = TestTU::withCode(TestCase.code());
329 TU.HeaderCode = R"cpp(
330 #define ID(X) X
331 #define MACRO_EXP(X) ID(X)
332 MACRO_EXP(int B);
333 )cpp";
334 TU.AdditionalFiles["foo.inc"] = R"cpp(
335 int C = ID(1);
336 #define DEF 1
337 int D = DEF;
338 )cpp";
339 ParsedAST AST = TU.build();
340 std::vector<size_t> MacroExpansionPositions;
341 for (const auto &SIDToRefs : AST.getMacros().MacroRefs) {
342 for (const auto &R : SIDToRefs.second)
343 MacroExpansionPositions.push_back(R.StartOffset);
344 }
345 for (const auto &R : AST.getMacros().UnknownMacros)
346 MacroExpansionPositions.push_back(R.StartOffset);
347 EXPECT_THAT(MacroExpansionPositions,
348 testing::UnorderedElementsAreArray(TestCase.points()));
349}
350
351TEST(ParsedASTTest, PatchesAdditionalIncludes) {
352 llvm::StringLiteral ModifiedContents = R"cpp(
353 #include "baz.h"
354 #include "foo.h"
355 #include "sub/aux.h"
356 void bar() {
357 foo();
358 baz();
359 aux();
360 })cpp";
361 // Build expected ast with symbols coming from headers.
362 TestTU TU;
363 TU.Filename = "foo.cpp";
364 TU.AdditionalFiles["foo.h"] = "void foo();";
365 TU.AdditionalFiles["sub/baz.h"] = "void baz();";
366 TU.AdditionalFiles["sub/aux.h"] = "void aux();";
367 TU.ExtraArgs = {"-I" + testPath("sub")};
368 TU.Code = ModifiedContents.str();
369 auto ExpectedAST = TU.build();
370
371 // Build preamble with no includes.
372 TU.Code = "";
373 StoreDiags Diags;
374 MockFS FS;
375 auto Inputs = TU.inputs(FS);
376 auto CI = buildCompilerInvocation(Inputs, Diags);
377 auto EmptyPreamble =
378 buildPreamble(testPath("foo.cpp"), *CI, Inputs, true, nullptr);
379 ASSERT_TRUE(EmptyPreamble);
380 EXPECT_THAT(EmptyPreamble->Includes.MainFileIncludes, IsEmpty());
381
382 // Now build an AST using empty preamble and ensure patched includes worked.
383 TU.Code = ModifiedContents.str();
384 Inputs = TU.inputs(FS);
385 auto PatchedAST = ParsedAST::build(testPath("foo.cpp"), Inputs, std::move(CI),
386 {}, EmptyPreamble);
387 ASSERT_TRUE(PatchedAST);
388
389 // Ensure source location information is correct, including resolved paths.
390 EXPECT_THAT(PatchedAST->getIncludeStructure().MainFileIncludes,
391 testing::Pointwise(
392 eqInc(), ExpectedAST.getIncludeStructure().MainFileIncludes));
393 // Ensure file proximity signals are correct.
394 auto &SM = PatchedAST->getSourceManager();
395 auto &FM = SM.getFileManager();
396 // Copy so that we can use operator[] to get the children.
397 IncludeStructure Includes = PatchedAST->getIncludeStructure();
398 auto MainFE = FM.getOptionalFileRef(testPath("foo.cpp"));
399 ASSERT_TRUE(MainFE);
400 auto MainID = Includes.getID(*MainFE);
401 auto AuxFE = FM.getOptionalFileRef(testPath("sub/aux.h"));
402 ASSERT_TRUE(AuxFE);
403 auto AuxID = Includes.getID(*AuxFE);
404 EXPECT_THAT(Includes.IncludeChildren[*MainID], Contains(*AuxID));
405}
406
407TEST(ParsedASTTest, PatchesDeletedIncludes) {
408 TestTU TU;
409 TU.Filename = "foo.cpp";
410 TU.Code = "";
411 auto ExpectedAST = TU.build();
412
413 // Build preamble with no includes.
414 TU.Code = R"cpp(#include <foo.h>)cpp";
415 StoreDiags Diags;
416 MockFS FS;
417 auto Inputs = TU.inputs(FS);
418 auto CI = buildCompilerInvocation(Inputs, Diags);
419 auto BaselinePreamble =
420 buildPreamble(testPath("foo.cpp"), *CI, Inputs, true, nullptr);
421 ASSERT_TRUE(BaselinePreamble);
422 EXPECT_THAT(BaselinePreamble->Includes.MainFileIncludes,
423 ElementsAre(testing::Field(&Inclusion::Written, "<foo.h>")));
424
425 // Now build an AST using additional includes and check that locations are
426 // correctly parsed.
427 TU.Code = "";
428 Inputs = TU.inputs(FS);
429 auto PatchedAST = ParsedAST::build(testPath("foo.cpp"), Inputs, std::move(CI),
430 {}, BaselinePreamble);
431 ASSERT_TRUE(PatchedAST);
432
433 // Ensure source location information is correct.
434 EXPECT_THAT(PatchedAST->getIncludeStructure().MainFileIncludes,
435 testing::Pointwise(
436 eqInc(), ExpectedAST.getIncludeStructure().MainFileIncludes));
437 // Ensure file proximity signals are correct.
438 auto &SM = ExpectedAST.getSourceManager();
439 auto &FM = SM.getFileManager();
440 // Copy so that we can getOrCreateID().
441 IncludeStructure Includes = ExpectedAST.getIncludeStructure();
442 auto MainFE = FM.getFileRef(testPath("foo.cpp"));
443 ASSERT_THAT_EXPECTED(MainFE, llvm::Succeeded());
444 auto MainID = Includes.getOrCreateID(*MainFE);
445 auto &PatchedFM = PatchedAST->getSourceManager().getFileManager();
446 IncludeStructure PatchedIncludes = PatchedAST->getIncludeStructure();
447 auto PatchedMainFE = PatchedFM.getFileRef(testPath("foo.cpp"));
448 ASSERT_THAT_EXPECTED(PatchedMainFE, llvm::Succeeded());
449 auto PatchedMainID = PatchedIncludes.getOrCreateID(*PatchedMainFE);
450 EXPECT_EQ(Includes.includeDepth(MainID)[MainID],
451 PatchedIncludes.includeDepth(PatchedMainID)[PatchedMainID]);
452}
453
454// Returns Code guarded by #ifndef guards
455std::string guard(llvm::StringRef Code) {
456 static int GuardID = 0;
457 std::string GuardName = ("GUARD_" + llvm::Twine(++GuardID)).str();
458 return llvm::formatv("#ifndef {0}\n#define {0}\n{1}\n#endif\n", GuardName,
459 Code);
460}
461
462std::string once(llvm::StringRef Code) {
463 return llvm::formatv("#pragma once\n{0}\n", Code);
464}
465
466bool mainIsGuarded(const ParsedAST &AST) {
467 const auto &SM = AST.getSourceManager();
468 OptionalFileEntryRef MainFE = SM.getFileEntryRefForID(SM.getMainFileID());
469 return AST.getPreprocessor()
470 .getHeaderSearchInfo()
471 .isFileMultipleIncludeGuarded(*MainFE);
472}
473
474MATCHER_P(diag, Desc, "") {
475 return llvm::StringRef(arg.Message).contains(Desc);
476}
477
478// Check our understanding of whether the main file is header guarded or not.
479TEST(ParsedASTTest, HeaderGuards) {
480 TestTU TU;
481 TU.ImplicitHeaderGuard = false;
482
483 TU.Code = ";";
484 EXPECT_FALSE(mainIsGuarded(TU.build()));
485
486 TU.Code = guard(";");
487 EXPECT_TRUE(mainIsGuarded(TU.build()));
488
489 TU.Code = once(";");
490 EXPECT_TRUE(mainIsGuarded(TU.build()));
491
492 TU.Code = R"cpp(
493 ;
494 #pragma once
495 )cpp";
496 EXPECT_FALSE(mainIsGuarded(TU.build())); // FIXME: true
497
498 TU.Code = R"cpp(
499 ;
500 #ifndef GUARD
501 #define GUARD
502 ;
503 #endif
504 )cpp";
505 EXPECT_FALSE(mainIsGuarded(TU.build()));
506}
507
508// Check our handling of files that include themselves.
509// Ideally we allow this if the file has header guards.
510//
511// Note: the semicolons (empty statements) are significant!
512// - they force the preamble to end and the body to begin. Directives can have
513// different effects in the preamble vs main file (which we try to hide).
514// - if the preamble would otherwise cover the whole file, a trailing semicolon
515// forces their sizes to be different. This is significant because the file
516// size is part of the lookup key for HeaderFileInfo, and we don't want to
517// rely on the preamble's HFI being looked up when parsing the main file.
518TEST(ParsedASTTest, HeaderGuardsSelfInclude) {
519 // Disable include cleaner diagnostics to prevent them from interfering with
520 // other diagnostics.
521 Config Cfg;
523 Cfg.Diagnostics.UnusedIncludes = Config::IncludesPolicy::None;
524 WithContextValue Ctx(Config::Key, std::move(Cfg));
525
526 TestTU TU;
527 TU.ImplicitHeaderGuard = false;
528 TU.Filename = "self.h";
529
530 TU.Code = R"cpp(
531 #include "self.h" // error-ok
532 ;
533 )cpp";
534 auto AST = TU.build();
535 EXPECT_THAT(AST.getDiagnostics(),
536 ElementsAre(diag("recursively when building a preamble")));
537 EXPECT_FALSE(mainIsGuarded(AST));
538
539 TU.Code = R"cpp(
540 ;
541 #include "self.h" // error-ok
542 )cpp";
543 AST = TU.build();
544 EXPECT_THAT(AST.getDiagnostics(), ElementsAre(diag("nested too deeply")));
545 EXPECT_FALSE(mainIsGuarded(AST));
546
547 TU.Code = R"cpp(
548 #pragma once
549 #include "self.h"
550 ;
551 )cpp";
552 AST = TU.build();
553 EXPECT_THAT(AST.getDiagnostics(), IsEmpty());
554 EXPECT_TRUE(mainIsGuarded(AST));
555
556 TU.Code = R"cpp(
557 #pragma once
558 ;
559 #include "self.h"
560 )cpp";
561 AST = TU.build();
562 EXPECT_THAT(AST.getDiagnostics(), IsEmpty());
563 EXPECT_TRUE(mainIsGuarded(AST));
564
565 TU.Code = R"cpp(
566 ;
567 #pragma once
568 #include "self.h"
569 )cpp";
570 AST = TU.build();
571 EXPECT_THAT(AST.getDiagnostics(), IsEmpty());
572 EXPECT_TRUE(mainIsGuarded(AST));
573
574 TU.Code = R"cpp(
575 #ifndef GUARD
576 #define GUARD
577 #include "self.h" // error-ok: FIXME, this would be nice to support
578 #endif
579 ;
580 )cpp";
581 AST = TU.build();
582 EXPECT_THAT(AST.getDiagnostics(),
583 ElementsAre(diag("recursively when building a preamble")));
584 EXPECT_TRUE(mainIsGuarded(AST));
585
586 TU.Code = R"cpp(
587 #ifndef GUARD
588 #define GUARD
589 ;
590 #include "self.h"
591 #endif
592 )cpp";
593 AST = TU.build();
594 EXPECT_THAT(AST.getDiagnostics(), IsEmpty());
595 EXPECT_TRUE(mainIsGuarded(AST));
596
597 // Guarded too late...
598 TU.Code = R"cpp(
599 #include "self.h" // error-ok
600 #ifndef GUARD
601 #define GUARD
602 ;
603 #endif
604 )cpp";
605 AST = TU.build();
606 EXPECT_THAT(AST.getDiagnostics(),
607 ElementsAre(diag("recursively when building a preamble")));
608 EXPECT_FALSE(mainIsGuarded(AST));
609
610 TU.Code = R"cpp(
611 #include "self.h" // error-ok
612 ;
613 #ifndef GUARD
614 #define GUARD
615 #endif
616 )cpp";
617 AST = TU.build();
618 EXPECT_THAT(AST.getDiagnostics(),
619 ElementsAre(diag("recursively when building a preamble")));
620 EXPECT_FALSE(mainIsGuarded(AST));
621
622 TU.Code = R"cpp(
623 ;
624 #ifndef GUARD
625 #define GUARD
626 #include "self.h"
627 #endif
628 )cpp";
629 AST = TU.build();
630 EXPECT_THAT(AST.getDiagnostics(), IsEmpty());
631 EXPECT_FALSE(mainIsGuarded(AST));
632
633 TU.Code = R"cpp(
634 #include "self.h" // error-ok
635 #pragma once
636 ;
637 )cpp";
638 AST = TU.build();
639 EXPECT_THAT(AST.getDiagnostics(),
640 ElementsAre(diag("recursively when building a preamble")));
641 EXPECT_TRUE(mainIsGuarded(AST));
642
643 TU.Code = R"cpp(
644 #include "self.h" // error-ok
645 ;
646 #pragma once
647 )cpp";
648 AST = TU.build();
649 EXPECT_THAT(AST.getDiagnostics(),
650 ElementsAre(diag("recursively when building a preamble")));
651 EXPECT_TRUE(mainIsGuarded(AST));
652}
653
654// Tests how we handle common idioms for splitting a header-only library
655// into interface and implementation files (e.g. *.h vs *.inl).
656// These files mutually include each other, and need careful handling of include
657// guards (which interact with preambles).
658TEST(ParsedASTTest, HeaderGuardsImplIface) {
659 std::string Interface = R"cpp(
660 // error-ok: we assert on diagnostics explicitly
661 template <class T> struct Traits {
662 unsigned size();
663 };
664 #include "impl.h"
665 )cpp";
666 std::string Implementation = R"cpp(
667 // error-ok: we assert on diagnostics explicitly
668 #include "iface.h"
669 template <class T> unsigned Traits<T>::size() {
670 return sizeof(T);
671 }
672 )cpp";
673
674 TestTU TU;
675 TU.ImplicitHeaderGuard = false; // We're testing include guard handling!
676 TU.ExtraArgs.push_back("-xc++-header");
677
678 // Editing the interface file, which is include guarded (easy case).
679 // We mostly get this right via PP if we don't recognize the include guard.
680 TU.Filename = "iface.h";
681 TU.Code = guard(Interface);
682 TU.AdditionalFiles = {{"impl.h", Implementation}};
683 auto AST = TU.build();
684 EXPECT_THAT(AST.getDiagnostics(), IsEmpty());
685 EXPECT_TRUE(mainIsGuarded(AST));
686 // Slightly harder: the `#pragma once` is part of the preamble, and we
687 // need to transfer it to the main file's HeaderFileInfo.
688 TU.Code = once(Interface);
689 AST = TU.build();
690 EXPECT_THAT(AST.getDiagnostics(), IsEmpty());
691 EXPECT_TRUE(mainIsGuarded(AST));
692
693 // Editing the implementation file, which is not include guarded.
694 TU.Filename = "impl.h";
695 TU.Code = Implementation;
696 TU.AdditionalFiles = {{"iface.h", guard(Interface)}};
697 AST = TU.build();
698 // The diagnostic is unfortunate in this case, but correct per our model.
699 // Ultimately the include is skipped and the code is parsed correctly though.
700 EXPECT_THAT(AST.getDiagnostics(),
701 ElementsAre(diag("in included file: main file cannot be included "
702 "recursively when building a preamble")));
703 EXPECT_FALSE(mainIsGuarded(AST));
704 // Interface is pragma once guarded, same thing.
705 TU.AdditionalFiles = {{"iface.h", once(Interface)}};
706 AST = TU.build();
707 EXPECT_THAT(AST.getDiagnostics(),
708 ElementsAre(diag("in included file: main file cannot be included "
709 "recursively when building a preamble")));
710 EXPECT_FALSE(mainIsGuarded(AST));
711}
712
713TEST(ParsedASTTest, DiscoversPragmaMarks) {
714 TestTU TU;
715 TU.AdditionalFiles["Header.h"] = R"(
716 #pragma mark - Something API
717 int something();
718 #pragma mark Something else
719 )";
720 TU.Code = R"cpp(
721 #include "Header.h"
722 #pragma mark In Preamble
723 #pragma mark - Something Impl
724 int something() { return 1; }
725 #pragma mark End
726 )cpp";
727 auto AST = TU.build();
728
729 EXPECT_THAT(AST.getMarks(), ElementsAre(pragmaTrivia(" In Preamble"),
730 pragmaTrivia(" - Something Impl"),
731 pragmaTrivia(" End")));
732}
733
734TEST(ParsedASTTest, GracefulFailureOnAssemblyFile) {
735 std::string Filename = "TestTU.S";
736 std::string Code = R"S(
737main:
738 # test comment
739 bx lr
740 )S";
741
742 // The rest is a simplified version of TestTU::build().
743 // Don't call TestTU::build() itself because it would assert on
744 // failure to build an AST.
745 MockFS FS;
746 std::string FullFilename = testPath(Filename);
747 FS.Files[FullFilename] = Code;
748 ParseInputs Inputs;
749 auto &Argv = Inputs.CompileCommand.CommandLine;
750 Argv = {"clang"};
751 Argv.push_back(FullFilename);
752 Inputs.CompileCommand.Filename = FullFilename;
753 Inputs.CompileCommand.Directory = testRoot();
754 Inputs.Contents = Code;
755 Inputs.TFS = &FS;
756 StoreDiags Diags;
757 auto CI = buildCompilerInvocation(Inputs, Diags);
758 assert(CI && "Failed to build compilation invocation.");
759 auto AST = ParsedAST::build(FullFilename, Inputs, std::move(CI), {}, nullptr);
760
761 EXPECT_FALSE(AST.has_value())
762 << "Should not try to build AST for assembly source file";
763}
764
765TEST(ParsedASTTest, PreambleWithDifferentTarget) {
766 constexpr std::string_view kPreambleTarget = "x86_64";
767 // Specifically picking __builtin_va_list as it triggers crashes when
768 // switching to wasm.
769 // It's due to different predefined types in different targets.
770 auto TU = TestTU::withHeaderCode("void foo(__builtin_va_list);");
771 TU.Code = "void bar() { foo(2); }";
772 TU.ExtraArgs.emplace_back("-target");
773 TU.ExtraArgs.emplace_back(kPreambleTarget);
774 const auto Preamble = TU.preamble();
775
776 // Switch target to wasm.
777 TU.ExtraArgs.pop_back();
778 TU.ExtraArgs.emplace_back("wasm32");
779
780 IgnoreDiagnostics Diags;
781 MockFS FS;
782 auto Inputs = TU.inputs(FS);
783 auto CI = buildCompilerInvocation(Inputs, Diags);
784 ASSERT_TRUE(CI) << "Failed to build compiler invocation";
785
786 auto AST = ParsedAST::build(testPath(TU.Filename), std::move(Inputs),
787 std::move(CI), {}, Preamble);
788
789 ASSERT_TRUE(AST);
790 // We use the target from preamble, not with the most-recent flags.
791 EXPECT_EQ(AST->getASTContext().getTargetInfo().getTriple().getArchName(),
792 llvm::StringRef(kPreambleTarget));
793}
794} // namespace
795} // namespace clangd
796} // namespace clang
llvm::StringMap< std::string > Files
Definition TestFS.h:45
Stores and provides access to parsed AST.
Definition ParsedAST.h:47
static std::optional< ParsedAST > build(llvm::StringRef Filename, const ParseInputs &Inputs, std::unique_ptr< clang::CompilerInvocation > CI, llvm::ArrayRef< Diag > CompilerInvocationDiags, std::shared_ptr< const PreambleData > Preamble)
Attempts to run Clang and store the parsed AST.
StoreDiags collects the diagnostics that can later be reported by clangd.
WithContextValue extends Context::current() with a single value.
Definition Context.h:200
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:44
std::string printTemplateSpecializationArgs(const NamedDecl &ND)
Prints template arguments of a decl as written in the source code, including enclosing '<' and '>',...
Definition AST.cpp:287
std::unique_ptr< CompilerInvocation > buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D, std::vector< std::string > *CC1Args)
Builds compiler invocation that could be used to build AST or preamble.
Definition Compiler.cpp:96
MATCHER_P(named, N, "")
std::string testPath(PathRef File, llvm::sys::path::Style Style)
Definition TestFS.cpp:94
std::shared_ptr< const PreambleData > buildPreamble(PathRef FileName, CompilerInvocation CI, const ParseInputs &Inputs, bool StoreInMemory, PreambleParsedCallback PreambleCallback, PreambleBuildStats *Stats)
Build a preamble for the new inputs unless an old one can be reused.
Definition Preamble.cpp:573
TidyProvider addTidyChecks(llvm::StringRef Checks, llvm::StringRef WarningsAsErrors)
Provider the enables a specific set of checks and warnings as errors.
TEST(BackgroundQueueTest, Priority)
const NamedDecl & findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name)
Definition TestTU.cpp:261
const char * testRoot()
Definition TestFS.cpp:85
cppcoreguidelines::ProBoundsAvoidUncheckedContainerAccessCheck P
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Settings that express user/project preferences and control clangd behavior.
Definition Config.h:45
static clangd::Key< Config > Key
Context key which can be used to set the current Config.
Definition Config.h:49
struct clang::clangd::Config::@343034053122374337352226322054223376344037116252 Diagnostics
Controls warnings and errors when parsing code.
IncludesPolicy MissingIncludes
Definition Config.h:118
std::string Written
Definition Headers.h:72
Information required to run clang, e.g. to parse AST or do code completion.
Definition Compiler.h:51
tooling::CompileCommand CompileCommand
Definition Compiler.h:52
const ThreadsafeFS * TFS
Definition Compiler.h:53
TidyProvider ClangTidyProvider
Definition TestTU.h:65
std::string Code
Definition TestTU.h:49
static TestTU withHeaderCode(llvm::StringRef HeaderCode)
Definition TestTU.h:42
std::string Filename
Definition TestTU.h:50
static TestTU withCode(llvm::StringRef Code)
Definition TestTU.h:36
llvm::StringMap< std::string > AdditionalFiles
Definition TestTU.h:57
std::string HeaderCode
Definition TestTU.h:53
static constexpr const char ArgName[]