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