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