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