1 //===--- ParsedAST.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 "ParsedAST.h"
10 #include "../clang-tidy/ClangTidyCheck.h"
11 #include "../clang-tidy/ClangTidyDiagnosticConsumer.h"
12 #include "../clang-tidy/ClangTidyModuleRegistry.h"
13 #include "AST.h"
14 #include "Compiler.h"
15 #include "Config.h"
16 #include "Diagnostics.h"
17 #include "Feature.h"
18 #include "FeatureModule.h"
19 #include "Headers.h"
20 #include "HeuristicResolver.h"
21 #include "IncludeCleaner.h"
22 #include "IncludeFixer.h"
23 #include "Preamble.h"
24 #include "SourceCode.h"
25 #include "TidyProvider.h"
26 #include "index/CanonicalIncludes.h"
27 #include "index/Index.h"
28 #include "support/Logger.h"
29 #include "support/Trace.h"
30 #include "clang/AST/ASTContext.h"
31 #include "clang/AST/Decl.h"
32 #include "clang/Basic/Diagnostic.h"
33 #include "clang/Basic/LangOptions.h"
34 #include "clang/Basic/SourceLocation.h"
35 #include "clang/Basic/SourceManager.h"
36 #include "clang/Basic/TokenKinds.h"
37 #include "clang/Frontend/CompilerInstance.h"
38 #include "clang/Frontend/CompilerInvocation.h"
39 #include "clang/Frontend/FrontendActions.h"
40 #include "clang/Frontend/Utils.h"
41 #include "clang/Index/IndexDataConsumer.h"
42 #include "clang/Index/IndexingAction.h"
43 #include "clang/Lex/Lexer.h"
44 #include "clang/Lex/MacroInfo.h"
45 #include "clang/Lex/PPCallbacks.h"
46 #include "clang/Lex/Preprocessor.h"
47 #include "clang/Lex/PreprocessorOptions.h"
48 #include "clang/Sema/Sema.h"
49 #include "clang/Serialization/ASTWriter.h"
50 #include "clang/Serialization/PCHContainerOperations.h"
51 #include "clang/Tooling/CompilationDatabase.h"
52 #include "clang/Tooling/Syntax/Tokens.h"
53 #include "llvm/ADT/ArrayRef.h"
54 #include "llvm/ADT/STLExtras.h"
55 #include "llvm/ADT/SmallString.h"
56 #include "llvm/ADT/SmallVector.h"
57 #include "llvm/ADT/StringRef.h"
58 #include "llvm/Support/raw_ostream.h"
59 #include <algorithm>
60 #include <memory>
61 #include <vector>
62 
63 // Force the linker to link in Clang-tidy modules.
64 // clangd doesn't support the static analyzer.
65 #if CLANGD_TIDY_CHECKS
66 #define CLANG_TIDY_DISABLE_STATIC_ANALYZER_CHECKS
67 #include "../clang-tidy/ClangTidyForceLinker.h"
68 #endif
69 
70 namespace clang {
71 namespace clangd {
72 namespace {
73 
74 template <class T> std::size_t getUsedBytes(const std::vector<T> &Vec) {
75   return Vec.capacity() * sizeof(T);
76 }
77 
78 class DeclTrackingASTConsumer : public ASTConsumer {
79 public:
80   DeclTrackingASTConsumer(std::vector<Decl *> &TopLevelDecls)
81       : TopLevelDecls(TopLevelDecls) {}
82 
83   bool HandleTopLevelDecl(DeclGroupRef DG) override {
84     for (Decl *D : DG) {
85       auto &SM = D->getASTContext().getSourceManager();
86       if (!isInsideMainFile(D->getLocation(), SM))
87         continue;
88       if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
89         if (isImplicitTemplateInstantiation(ND))
90           continue;
91 
92       // ObjCMethodDecl are not actually top-level decls.
93       if (isa<ObjCMethodDecl>(D))
94         continue;
95 
96       TopLevelDecls.push_back(D);
97     }
98     return true;
99   }
100 
101 private:
102   std::vector<Decl *> &TopLevelDecls;
103 };
104 
105 class ClangdFrontendAction : public SyntaxOnlyAction {
106 public:
107   std::vector<Decl *> takeTopLevelDecls() { return std::move(TopLevelDecls); }
108 
109 protected:
110   std::unique_ptr<ASTConsumer>
111   CreateASTConsumer(CompilerInstance &CI, llvm::StringRef InFile) override {
112     return std::make_unique<DeclTrackingASTConsumer>(/*ref*/ TopLevelDecls);
113   }
114 
115 private:
116   std::vector<Decl *> TopLevelDecls;
117 };
118 
119 // When using a preamble, only preprocessor events outside its bounds are seen.
120 // This is almost what we want: replaying transitive preprocessing wastes time.
121 // However this confuses clang-tidy checks: they don't see any #includes!
122 // So we replay the *non-transitive* #includes that appear in the main-file.
123 // It would be nice to replay other events (macro definitions, ifdefs etc) but
124 // this addresses the most common cases fairly cheaply.
125 class ReplayPreamble : private PPCallbacks {
126 public:
127   // Attach preprocessor hooks such that preamble events will be injected at
128   // the appropriate time.
129   // Events will be delivered to the *currently registered* PP callbacks.
130   static void attach(std::vector<Inclusion> Includes, CompilerInstance &Clang,
131                      const PreambleBounds &PB) {
132     auto &PP = Clang.getPreprocessor();
133     auto *ExistingCallbacks = PP.getPPCallbacks();
134     // No need to replay events if nobody is listening.
135     if (!ExistingCallbacks)
136       return;
137     PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(new ReplayPreamble(
138         std::move(Includes), ExistingCallbacks, Clang.getSourceManager(), PP,
139         Clang.getLangOpts(), PB)));
140     // We're relying on the fact that addPPCallbacks keeps the old PPCallbacks
141     // around, creating a chaining wrapper. Guard against other implementations.
142     assert(PP.getPPCallbacks() != ExistingCallbacks &&
143            "Expected chaining implementation");
144   }
145 
146 private:
147   ReplayPreamble(std::vector<Inclusion> Includes, PPCallbacks *Delegate,
148                  const SourceManager &SM, Preprocessor &PP,
149                  const LangOptions &LangOpts, const PreambleBounds &PB)
150       : Includes(std::move(Includes)), Delegate(Delegate), SM(SM), PP(PP) {
151     // Only tokenize the preamble section of the main file, as we are not
152     // interested in the rest of the tokens.
153     MainFileTokens = syntax::tokenize(
154         syntax::FileRange(SM.getMainFileID(), 0, PB.Size), SM, LangOpts);
155   }
156 
157   // In a normal compile, the preamble traverses the following structure:
158   //
159   // mainfile.cpp
160   //   <built-in>
161   //     ... macro definitions like __cplusplus ...
162   //     <command-line>
163   //       ... macro definitions for args like -Dfoo=bar ...
164   //   "header1.h"
165   //     ... header file contents ...
166   //   "header2.h"
167   //     ... header file contents ...
168   //   ... main file contents ...
169   //
170   // When using a preamble, the "header1" and "header2" subtrees get skipped.
171   // We insert them right after the built-in header, which still appears.
172   void FileChanged(SourceLocation Loc, FileChangeReason Reason,
173                    SrcMgr::CharacteristicKind Kind, FileID PrevFID) override {
174     // It'd be nice if there was a better way to identify built-in headers...
175     if (Reason == FileChangeReason::ExitFile &&
176         SM.getBufferOrFake(PrevFID).getBufferIdentifier() == "<built-in>")
177       replay();
178   }
179 
180   void replay() {
181     for (const auto &Inc : Includes) {
182       llvm::Optional<FileEntryRef> File;
183       if (Inc.Resolved != "")
184         File = expectedToOptional(SM.getFileManager().getFileRef(Inc.Resolved));
185 
186       // Re-lex the #include directive to find its interesting parts.
187       auto HashLoc = SM.getComposedLoc(SM.getMainFileID(), Inc.HashOffset);
188       auto HashTok = llvm::partition_point(MainFileTokens,
189                                            [&HashLoc](const syntax::Token &T) {
190                                              return T.location() < HashLoc;
191                                            });
192       assert(HashTok != MainFileTokens.end() && HashTok->kind() == tok::hash);
193 
194       auto IncludeTok = std::next(HashTok);
195       assert(IncludeTok != MainFileTokens.end());
196 
197       auto FileTok = std::next(IncludeTok);
198       assert(FileTok != MainFileTokens.end());
199 
200       // Create a fake import/include token, none of the callers seem to care
201       // about clang::Token::Flags.
202       Token SynthesizedIncludeTok;
203       SynthesizedIncludeTok.startToken();
204       SynthesizedIncludeTok.setLocation(IncludeTok->location());
205       SynthesizedIncludeTok.setLength(IncludeTok->length());
206       SynthesizedIncludeTok.setKind(tok::raw_identifier);
207       SynthesizedIncludeTok.setRawIdentifierData(IncludeTok->text(SM).data());
208       PP.LookUpIdentifierInfo(SynthesizedIncludeTok);
209 
210       // Same here, create a fake one for Filename, including angles or quotes.
211       Token SynthesizedFilenameTok;
212       SynthesizedFilenameTok.startToken();
213       SynthesizedFilenameTok.setLocation(FileTok->location());
214       // Note that we can't make use of FileTok->length/text in here as in the
215       // case of angled includes this will contain tok::less instead of
216       // filename. Whereas Inc.Written contains the full header name including
217       // quotes/angles.
218       SynthesizedFilenameTok.setLength(Inc.Written.length());
219       SynthesizedFilenameTok.setKind(tok::header_name);
220       SynthesizedFilenameTok.setLiteralData(Inc.Written.data());
221 
222       const FileEntry *FE = File ? &File->getFileEntry() : nullptr;
223       llvm::StringRef WrittenFilename =
224           llvm::StringRef(Inc.Written).drop_front().drop_back();
225       Delegate->InclusionDirective(
226           HashTok->location(), SynthesizedIncludeTok, WrittenFilename,
227           Inc.Written.front() == '<',
228           syntax::FileRange(SM, SynthesizedFilenameTok.getLocation(),
229                             SynthesizedFilenameTok.getEndLoc())
230               .toCharRange(SM),
231           FE, "SearchPath", "RelPath",
232           /*Imported=*/nullptr, Inc.FileKind);
233       if (File)
234         Delegate->FileSkipped(*File, SynthesizedFilenameTok, Inc.FileKind);
235       else {
236         llvm::SmallString<1> UnusedRecovery;
237         Delegate->FileNotFound(WrittenFilename, UnusedRecovery);
238       }
239     }
240   }
241 
242   const std::vector<Inclusion> Includes;
243   PPCallbacks *Delegate;
244   const SourceManager &SM;
245   Preprocessor &PP;
246   std::vector<syntax::Token> MainFileTokens;
247 };
248 
249 } // namespace
250 
251 llvm::Optional<ParsedAST>
252 ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
253                  std::unique_ptr<clang::CompilerInvocation> CI,
254                  llvm::ArrayRef<Diag> CompilerInvocationDiags,
255                  std::shared_ptr<const PreambleData> Preamble) {
256   trace::Span Tracer("BuildAST");
257   SPAN_ATTACH(Tracer, "File", Filename);
258 
259   auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
260   if (Preamble && Preamble->StatCache)
261     VFS = Preamble->StatCache->getConsumingFS(std::move(VFS));
262 
263   assert(CI);
264   // Command-line parsing sets DisableFree to true by default, but we don't want
265   // to leak memory in clangd.
266   CI->getFrontendOpts().DisableFree = false;
267   const PrecompiledPreamble *PreamblePCH =
268       Preamble ? &Preamble->Preamble : nullptr;
269 
270   // This is on-by-default in windows to allow parsing SDK headers, but it
271   // breaks many features. Disable it for the main-file (not preamble).
272   CI->getLangOpts()->DelayedTemplateParsing = false;
273 
274   std::vector<std::unique_ptr<FeatureModule::ASTListener>> ASTListeners;
275   if (Inputs.FeatureModules) {
276     for (auto &M : *Inputs.FeatureModules) {
277       if (auto Listener = M.astListeners())
278         ASTListeners.emplace_back(std::move(Listener));
279     }
280   }
281   StoreDiags ASTDiags;
282   ASTDiags.setDiagCallback(
283       [&ASTListeners](const clang::Diagnostic &D, clangd::Diag &Diag) {
284         llvm::for_each(ASTListeners,
285                        [&](const auto &L) { L->sawDiagnostic(D, Diag); });
286       });
287 
288   llvm::Optional<PreamblePatch> Patch;
289   bool PreserveDiags = true;
290   // We might use an ignoring diagnostic consumer if they are going to be
291   // dropped later on to not pay for extra latency by processing them.
292   DiagnosticConsumer *DiagConsumer = &ASTDiags;
293   IgnoreDiagnostics DropDiags;
294   if (Preamble) {
295     Patch = PreamblePatch::createFullPatch(Filename, Inputs, *Preamble);
296     Patch->apply(*CI);
297     PreserveDiags = Patch->preserveDiagnostics();
298     if (!PreserveDiags)
299       DiagConsumer = &DropDiags;
300   }
301   auto Clang = prepareCompilerInstance(
302       std::move(CI), PreamblePCH,
303       llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, Filename), VFS,
304       *DiagConsumer);
305   if (!Clang) {
306     // The last diagnostic contains information about the reason of this
307     // failure.
308     std::vector<Diag> Diags(ASTDiags.take());
309     elog("Failed to prepare a compiler instance: {0}",
310          !Diags.empty() ? static_cast<DiagBase &>(Diags.back()).Message
311                         : "unknown error");
312     return None;
313   }
314   if (!PreserveDiags) {
315     // Skips some analysis.
316     Clang->getDiagnosticOpts().IgnoreWarnings = true;
317   }
318 
319   auto Action = std::make_unique<ClangdFrontendAction>();
320   const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0];
321   if (!Action->BeginSourceFile(*Clang, MainInput)) {
322     log("BeginSourceFile() failed when building AST for {0}",
323         MainInput.getFile());
324     return None;
325   }
326   // If we saw an include guard in the preamble section of the main file,
327   // mark the main-file as include-guarded.
328   // This information is part of the HeaderFileInfo but is not loaded from the
329   // preamble as the file's size is part of its identity and may have changed.
330   // (The rest of HeaderFileInfo is not relevant for our purposes).
331   if (Preamble && Preamble->MainIsIncludeGuarded) {
332     const SourceManager &SM = Clang->getSourceManager();
333     const FileEntry *MainFE = SM.getFileEntryForID(SM.getMainFileID());
334     Clang->getPreprocessor().getHeaderSearchInfo().MarkFileIncludeOnce(MainFE);
335   }
336 
337   // Set up ClangTidy. Must happen after BeginSourceFile() so ASTContext exists.
338   // Clang-tidy has some limitations to ensure reasonable performance:
339   //  - checks don't see all preprocessor events in the preamble
340   //  - matchers run only over the main-file top-level decls (and can't see
341   //    ancestors outside this scope).
342   // In practice almost all checks work well without modifications.
343   std::vector<std::unique_ptr<tidy::ClangTidyCheck>> CTChecks;
344   ast_matchers::MatchFinder CTFinder;
345   llvm::Optional<tidy::ClangTidyContext> CTContext;
346   llvm::Optional<IncludeFixer> FixIncludes;
347   // No need to run clang-tidy or IncludeFixerif we are not going to surface
348   // diagnostics.
349   if (PreserveDiags) {
350     trace::Span Tracer("ClangTidyInit");
351     tidy::ClangTidyOptions ClangTidyOpts =
352         getTidyOptionsForFile(Inputs.ClangTidyProvider, Filename);
353     dlog("ClangTidy configuration for file {0}: {1}", Filename,
354          tidy::configurationAsText(ClangTidyOpts));
355     tidy::ClangTidyCheckFactories CTFactories;
356     for (const auto &E : tidy::ClangTidyModuleRegistry::entries())
357       E.instantiate()->addCheckFactories(CTFactories);
358     CTContext.emplace(std::make_unique<tidy::DefaultOptionsProvider>(
359         tidy::ClangTidyGlobalOptions(), ClangTidyOpts));
360     CTContext->setDiagnosticsEngine(&Clang->getDiagnostics());
361     CTContext->setASTContext(&Clang->getASTContext());
362     CTContext->setCurrentFile(Filename);
363     CTChecks = CTFactories.createChecks(CTContext.getPointer());
364     llvm::erase_if(CTChecks, [&](const auto &Check) {
365       return !Check->isLanguageVersionSupported(CTContext->getLangOpts());
366     });
367     Preprocessor *PP = &Clang->getPreprocessor();
368     for (const auto &Check : CTChecks) {
369       Check->registerPPCallbacks(Clang->getSourceManager(), PP, PP);
370       Check->registerMatchers(&CTFinder);
371     }
372 
373     const Config &Cfg = Config::current();
374     ASTDiags.setLevelAdjuster([&](DiagnosticsEngine::Level DiagLevel,
375                                   const clang::Diagnostic &Info) {
376       if (Cfg.Diagnostics.SuppressAll ||
377           isBuiltinDiagnosticSuppressed(Info.getID(), Cfg.Diagnostics.Suppress))
378         return DiagnosticsEngine::Ignored;
379       if (!CTChecks.empty()) {
380         std::string CheckName = CTContext->getCheckName(Info.getID());
381         bool IsClangTidyDiag = !CheckName.empty();
382         if (IsClangTidyDiag) {
383           if (Cfg.Diagnostics.Suppress.contains(CheckName))
384             return DiagnosticsEngine::Ignored;
385           // Check for suppression comment. Skip the check for diagnostics not
386           // in the main file, because we don't want that function to query the
387           // source buffer for preamble files. For the same reason, we ask
388           // shouldSuppressDiagnostic to avoid I/O.
389           // We let suppression comments take precedence over warning-as-error
390           // to match clang-tidy's behaviour.
391           bool IsInsideMainFile =
392               Info.hasSourceManager() &&
393               isInsideMainFile(Info.getLocation(), Info.getSourceManager());
394           SmallVector<tidy::ClangTidyError, 1> TidySuppressedErrors;
395           if (IsInsideMainFile &&
396               tidy::shouldSuppressDiagnostic(DiagLevel, Info, *CTContext,
397                                              TidySuppressedErrors,
398                                              /*AllowIO=*/false,
399                                              /*EnableNolintBlocks=*/false)) {
400             // FIXME: should we expose the suppression error (invalid use of
401             // NOLINT comments)?
402             return DiagnosticsEngine::Ignored;
403           }
404 
405           // Check for warning-as-error.
406           if (DiagLevel == DiagnosticsEngine::Warning &&
407               CTContext->treatAsError(CheckName)) {
408             return DiagnosticsEngine::Error;
409           }
410         }
411       }
412       return DiagLevel;
413     });
414 
415     // Add IncludeFixer which can recover diagnostics caused by missing includes
416     // (e.g. incomplete type) and attach include insertion fixes to diagnostics.
417     auto BuildDir = VFS->getCurrentWorkingDirectory();
418     if (Inputs.Index && !BuildDir.getError()) {
419       auto Style =
420           getFormatStyleForFile(Filename, Inputs.Contents, *Inputs.TFS);
421       auto Inserter = std::make_shared<IncludeInserter>(
422           Filename, Inputs.Contents, Style, BuildDir.get(),
423           &Clang->getPreprocessor().getHeaderSearchInfo());
424       if (Preamble) {
425         for (const auto &Inc : Preamble->Includes.MainFileIncludes)
426           Inserter->addExisting(Inc);
427       }
428       FixIncludes.emplace(Filename, Inserter, *Inputs.Index,
429                           /*IndexRequestLimit=*/5);
430       ASTDiags.contributeFixes([&FixIncludes](DiagnosticsEngine::Level DiagLevl,
431                                               const clang::Diagnostic &Info) {
432         return FixIncludes->fix(DiagLevl, Info);
433       });
434       Clang->setExternalSemaSource(FixIncludes->unresolvedNameRecorder());
435     }
436   }
437 
438   IncludeStructure Includes;
439   // If we are using a preamble, copy existing includes.
440   if (Preamble) {
441     Includes = Preamble->Includes;
442     Includes.MainFileIncludes = Patch->preambleIncludes();
443     // Replay the preamble includes so that clang-tidy checks can see them.
444     ReplayPreamble::attach(Patch->preambleIncludes(), *Clang,
445                            Patch->modifiedBounds());
446   }
447   // Important: collectIncludeStructure is registered *after* ReplayPreamble!
448   // Otherwise we would collect the replayed includes again...
449   // (We can't *just* use the replayed includes, they don't have Resolved path).
450   Includes.collect(*Clang);
451   // Copy over the macros in the preamble region of the main file, and combine
452   // with non-preamble macros below.
453   MainFileMacros Macros;
454   if (Preamble)
455     Macros = Preamble->Macros;
456   Clang->getPreprocessor().addPPCallbacks(
457       std::make_unique<CollectMainFileMacros>(Clang->getSourceManager(),
458                                               Macros));
459 
460   std::vector<PragmaMark> Marks;
461   // FIXME: We need to patch the marks for stale preambles.
462   if (Preamble)
463     Marks = Preamble->Marks;
464   Clang->getPreprocessor().addPPCallbacks(
465       collectPragmaMarksCallback(Clang->getSourceManager(), Marks));
466 
467   // Copy over the includes from the preamble, then combine with the
468   // non-preamble includes below.
469   CanonicalIncludes CanonIncludes;
470   if (Preamble)
471     CanonIncludes = Preamble->CanonIncludes;
472   else
473     CanonIncludes.addSystemHeadersMapping(Clang->getLangOpts());
474   std::unique_ptr<CommentHandler> IWYUHandler =
475       collectIWYUHeaderMaps(&CanonIncludes);
476   Clang->getPreprocessor().addCommentHandler(IWYUHandler.get());
477 
478   // Collect tokens of the main file.
479   syntax::TokenCollector CollectTokens(Clang->getPreprocessor());
480 
481   if (llvm::Error Err = Action->Execute())
482     log("Execute() failed when building AST for {0}: {1}", MainInput.getFile(),
483         toString(std::move(Err)));
484 
485   // We have to consume the tokens before running clang-tidy to avoid collecting
486   // tokens from running the preprocessor inside the checks (only
487   // modernize-use-trailing-return-type does that today).
488   syntax::TokenBuffer Tokens = std::move(CollectTokens).consume();
489   // Makes SelectionTree build much faster.
490   Tokens.indexExpandedTokens();
491   std::vector<Decl *> ParsedDecls = Action->takeTopLevelDecls();
492   // AST traversals should exclude the preamble, to avoid performance cliffs.
493   Clang->getASTContext().setTraversalScope(ParsedDecls);
494   if (!CTChecks.empty()) {
495     // Run the AST-dependent part of the clang-tidy checks.
496     // (The preprocessor part ran already, via PPCallbacks).
497     trace::Span Tracer("ClangTidyMatch");
498     CTFinder.matchAST(Clang->getASTContext());
499   }
500 
501   // XXX: This is messy: clang-tidy checks flush some diagnostics at EOF.
502   // However Action->EndSourceFile() would destroy the ASTContext!
503   // So just inform the preprocessor of EOF, while keeping everything alive.
504   Clang->getPreprocessor().EndSourceFile();
505   // UnitDiagsConsumer is local, we can not store it in CompilerInstance that
506   // has a longer lifetime.
507   Clang->getDiagnostics().setClient(new IgnoreDiagnostics);
508   // CompilerInstance won't run this callback, do it directly.
509   ASTDiags.EndSourceFile();
510 
511   llvm::Optional<std::vector<Diag>> Diags;
512   // FIXME: Also skip generation of diagnostics alltogether to speed up ast
513   // builds when we are patching a stale preamble.
514   if (PreserveDiags) {
515     Diags = CompilerInvocationDiags;
516     // Add diagnostics from the preamble, if any.
517     if (Preamble)
518       Diags->insert(Diags->end(), Preamble->Diags.begin(),
519                     Preamble->Diags.end());
520     // Finally, add diagnostics coming from the AST.
521     {
522       std::vector<Diag> D = ASTDiags.take(CTContext.getPointer());
523       Diags->insert(Diags->end(), D.begin(), D.end());
524     }
525   }
526   ParsedAST Result(Inputs.Version, std::move(Preamble), std::move(Clang),
527                    std::move(Action), std::move(Tokens), std::move(Macros),
528                    std::move(Marks), std::move(ParsedDecls), std::move(Diags),
529                    std::move(Includes), std::move(CanonIncludes));
530   if (Result.Diags) {
531     auto UnusedHeadersDiags =
532         issueUnusedIncludesDiagnostics(Result, Inputs.Contents);
533     Result.Diags->insert(Result.Diags->end(),
534                          make_move_iterator(UnusedHeadersDiags.begin()),
535                          make_move_iterator(UnusedHeadersDiags.end()));
536   }
537   return Result;
538 }
539 
540 ParsedAST::ParsedAST(ParsedAST &&Other) = default;
541 
542 ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default;
543 
544 ParsedAST::~ParsedAST() {
545   if (Action) {
546     // We already notified the PP of end-of-file earlier, so detach it first.
547     // We must keep it alive until after EndSourceFile(), Sema relies on this.
548     auto PP = Clang->getPreprocessorPtr(); // Keep PP alive for now.
549     Clang->setPreprocessor(nullptr);       // Detach so we don't send EOF again.
550     Action->EndSourceFile();               // Destroy ASTContext and Sema.
551     // Now Sema is gone, it's safe for PP to go out of scope.
552   }
553 }
554 
555 ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); }
556 
557 const ASTContext &ParsedAST::getASTContext() const {
558   return Clang->getASTContext();
559 }
560 
561 Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); }
562 
563 std::shared_ptr<Preprocessor> ParsedAST::getPreprocessorPtr() {
564   return Clang->getPreprocessorPtr();
565 }
566 
567 const Preprocessor &ParsedAST::getPreprocessor() const {
568   return Clang->getPreprocessor();
569 }
570 
571 llvm::ArrayRef<Decl *> ParsedAST::getLocalTopLevelDecls() {
572   return LocalTopLevelDecls;
573 }
574 
575 const MainFileMacros &ParsedAST::getMacros() const { return Macros; }
576 const std::vector<PragmaMark> &ParsedAST::getMarks() const { return Marks; }
577 
578 std::size_t ParsedAST::getUsedBytes() const {
579   auto &AST = getASTContext();
580   // FIXME(ibiryukov): we do not account for the dynamically allocated part of
581   // Message and Fixes inside each diagnostic.
582   std::size_t Total = clangd::getUsedBytes(LocalTopLevelDecls) +
583                       (Diags ? clangd::getUsedBytes(*Diags) : 0);
584 
585   // FIXME: the rest of the function is almost a direct copy-paste from
586   // libclang's clang_getCXTUResourceUsage. We could share the implementation.
587 
588   // Sum up various allocators inside the ast context and the preprocessor.
589   Total += AST.getASTAllocatedMemory();
590   Total += AST.getSideTableAllocatedMemory();
591   Total += AST.Idents.getAllocator().getTotalMemory();
592   Total += AST.Selectors.getTotalMemory();
593 
594   Total += AST.getSourceManager().getContentCacheSize();
595   Total += AST.getSourceManager().getDataStructureSizes();
596   Total += AST.getSourceManager().getMemoryBufferSizes().malloc_bytes;
597 
598   if (ExternalASTSource *Ext = AST.getExternalSource())
599     Total += Ext->getMemoryBufferSizes().malloc_bytes;
600 
601   const Preprocessor &PP = getPreprocessor();
602   Total += PP.getTotalMemory();
603   if (PreprocessingRecord *PRec = PP.getPreprocessingRecord())
604     Total += PRec->getTotalMemory();
605   Total += PP.getHeaderSearchInfo().getTotalMemory();
606 
607   return Total;
608 }
609 
610 const IncludeStructure &ParsedAST::getIncludeStructure() const {
611   return Includes;
612 }
613 
614 const CanonicalIncludes &ParsedAST::getCanonicalIncludes() const {
615   return CanonIncludes;
616 }
617 
618 ParsedAST::ParsedAST(llvm::StringRef Version,
619                      std::shared_ptr<const PreambleData> Preamble,
620                      std::unique_ptr<CompilerInstance> Clang,
621                      std::unique_ptr<FrontendAction> Action,
622                      syntax::TokenBuffer Tokens, MainFileMacros Macros,
623                      std::vector<PragmaMark> Marks,
624                      std::vector<Decl *> LocalTopLevelDecls,
625                      llvm::Optional<std::vector<Diag>> Diags,
626                      IncludeStructure Includes, CanonicalIncludes CanonIncludes)
627     : Version(Version), Preamble(std::move(Preamble)), Clang(std::move(Clang)),
628       Action(std::move(Action)), Tokens(std::move(Tokens)),
629       Macros(std::move(Macros)), Marks(std::move(Marks)),
630       Diags(std::move(Diags)),
631       LocalTopLevelDecls(std::move(LocalTopLevelDecls)),
632       Includes(std::move(Includes)), CanonIncludes(std::move(CanonIncludes)) {
633   Resolver = std::make_unique<HeuristicResolver>(getASTContext());
634   assert(this->Clang);
635   assert(this->Action);
636 }
637 
638 llvm::Optional<llvm::StringRef> ParsedAST::preambleVersion() const {
639   if (!Preamble)
640     return llvm::None;
641   return llvm::StringRef(Preamble->Version);
642 }
643 
644 } // namespace clangd
645 } // namespace clang
646