clang-tools  14.0.0git
ParsedAST.cpp
Go to the documentation of this file.
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 "IncludeFixer.h"
22 #include "Preamble.h"
23 #include "SourceCode.h"
24 #include "TidyProvider.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 
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:
79  DeclTrackingASTConsumer(std::vector<Decl *> &TopLevelDecls)
80  : TopLevelDecls(TopLevelDecls) {}
81 
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))
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:
106  std::vector<Decl *> takeTopLevelDecls() { return std::move(TopLevelDecls); }
107 
108 protected:
109  std::unique_ptr<ASTConsumer>
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.
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:
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.
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 
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>
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  // We might use an ignoring diagnostic consumer if they are going to be
287  // dropped later on to not pay for extra latency by processing them.
288  DiagnosticConsumer *DiagConsumer = &ASTDiags;
289  IgnoreDiagnostics DropDiags;
290  if (Preamble) {
292  Patch->apply(*CI);
293  PreserveDiags = Patch->preserveDiagnostics();
294  if (!PreserveDiags)
295  DiagConsumer = &DropDiags;
296  }
297  auto Clang = prepareCompilerInstance(
298  std::move(CI), PreamblePCH,
299  llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, Filename), VFS,
300  *DiagConsumer);
301  if (!Clang) {
302  // The last diagnostic contains information about the reason of this
303  // failure.
304  std::vector<Diag> Diags(ASTDiags.take());
305  elog("Failed to prepare a compiler instance: {0}",
306  !Diags.empty() ? static_cast<DiagBase &>(Diags.back()).Message
307  : "unknown error");
308  return None;
309  }
310  if (!PreserveDiags) {
311  // Skips some analysis.
312  Clang->getDiagnosticOpts().IgnoreWarnings = true;
313  }
314 
315  auto Action = std::make_unique<ClangdFrontendAction>();
316  const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0];
317  if (!Action->BeginSourceFile(*Clang, MainInput)) {
318  log("BeginSourceFile() failed when building AST for {0}",
319  MainInput.getFile());
320  return None;
321  }
322  // If we saw an include guard in the preamble section of the main file,
323  // mark the main-file as include-guarded.
324  // This information is part of the HeaderFileInfo but is not loaded from the
325  // preamble as the file's size is part of its identity and may have changed.
326  // (The rest of HeaderFileInfo is not relevant for our purposes).
327  if (Preamble && Preamble->MainIsIncludeGuarded) {
328  const SourceManager &SM = Clang->getSourceManager();
329  const FileEntry *MainFE = SM.getFileEntryForID(SM.getMainFileID());
330  Clang->getPreprocessor().getHeaderSearchInfo().MarkFileIncludeOnce(MainFE);
331  }
332 
333  // Set up ClangTidy. Must happen after BeginSourceFile() so ASTContext exists.
334  // Clang-tidy has some limitations to ensure reasonable performance:
335  // - checks don't see all preprocessor events in the preamble
336  // - matchers run only over the main-file top-level decls (and can't see
337  // ancestors outside this scope).
338  // In practice almost all checks work well without modifications.
339  std::vector<std::unique_ptr<tidy::ClangTidyCheck>> CTChecks;
340  ast_matchers::MatchFinder CTFinder;
341  llvm::Optional<tidy::ClangTidyContext> CTContext;
342  llvm::Optional<IncludeFixer> FixIncludes;
343  // No need to run clang-tidy or IncludeFixerif we are not going to surface
344  // diagnostics.
345  if (PreserveDiags) {
346  trace::Span Tracer("ClangTidyInit");
347  tidy::ClangTidyOptions ClangTidyOpts =
348  getTidyOptionsForFile(Inputs.ClangTidyProvider, Filename);
349  dlog("ClangTidy configuration for file {0}: {1}", Filename,
350  tidy::configurationAsText(ClangTidyOpts));
351  tidy::ClangTidyCheckFactories CTFactories;
352  for (const auto &E : tidy::ClangTidyModuleRegistry::entries())
353  E.instantiate()->addCheckFactories(CTFactories);
354  CTContext.emplace(std::make_unique<tidy::DefaultOptionsProvider>(
355  tidy::ClangTidyGlobalOptions(), ClangTidyOpts));
356  CTContext->setDiagnosticsEngine(&Clang->getDiagnostics());
357  CTContext->setASTContext(&Clang->getASTContext());
358  CTContext->setCurrentFile(Filename);
359  CTChecks = CTFactories.createChecks(CTContext.getPointer());
360  llvm::erase_if(CTChecks, [&](const auto &Check) {
361  return !Check->isLanguageVersionSupported(CTContext->getLangOpts());
362  });
363  Preprocessor *PP = &Clang->getPreprocessor();
364  for (const auto &Check : CTChecks) {
365  Check->registerPPCallbacks(Clang->getSourceManager(), PP, PP);
366  Check->registerMatchers(&CTFinder);
367  }
368 
369  const Config &Cfg = Config::current();
370  ASTDiags.setLevelAdjuster([&](DiagnosticsEngine::Level DiagLevel,
371  const clang::Diagnostic &Info) {
372  if (Cfg.Diagnostics.SuppressAll ||
374  return DiagnosticsEngine::Ignored;
375  if (!CTChecks.empty()) {
376  std::string CheckName = CTContext->getCheckName(Info.getID());
377  bool IsClangTidyDiag = !CheckName.empty();
378  if (IsClangTidyDiag) {
379  if (Cfg.Diagnostics.Suppress.contains(CheckName))
380  return DiagnosticsEngine::Ignored;
381  // Check for suppression comment. Skip the check for diagnostics not
382  // in the main file, because we don't want that function to query the
383  // source buffer for preamble files. For the same reason, we ask
384  // shouldSuppressDiagnostic to avoid I/O.
385  // We let suppression comments take precedence over warning-as-error
386  // to match clang-tidy's behaviour.
387  bool IsInsideMainFile =
388  Info.hasSourceManager() &&
389  isInsideMainFile(Info.getLocation(), Info.getSourceManager());
390  if (IsInsideMainFile &&
391  tidy::shouldSuppressDiagnostic(DiagLevel, Info, *CTContext,
392  /*AllowIO=*/false)) {
393  return DiagnosticsEngine::Ignored;
394  }
395 
396  // Check for warning-as-error.
397  if (DiagLevel == DiagnosticsEngine::Warning &&
398  CTContext->treatAsError(CheckName)) {
399  return DiagnosticsEngine::Error;
400  }
401  }
402  }
403  return DiagLevel;
404  });
405 
406  // Add IncludeFixer which can recover diagnostics caused by missing includes
407  // (e.g. incomplete type) and attach include insertion fixes to diagnostics.
408  auto BuildDir = VFS->getCurrentWorkingDirectory();
409  if (Inputs.Index && !BuildDir.getError()) {
410  auto Style =
411  getFormatStyleForFile(Filename, Inputs.Contents, *Inputs.TFS);
412  auto Inserter = std::make_shared<IncludeInserter>(
413  Filename, Inputs.Contents, Style, BuildDir.get(),
414  &Clang->getPreprocessor().getHeaderSearchInfo());
415  if (Preamble) {
416  for (const auto &Inc : Preamble->Includes.MainFileIncludes)
417  Inserter->addExisting(Inc);
418  }
419  FixIncludes.emplace(Filename, Inserter, *Inputs.Index,
420  /*IndexRequestLimit=*/5);
421  ASTDiags.contributeFixes([&FixIncludes](DiagnosticsEngine::Level DiagLevl,
422  const clang::Diagnostic &Info) {
423  return FixIncludes->fix(DiagLevl, Info);
424  });
425  Clang->setExternalSemaSource(FixIncludes->unresolvedNameRecorder());
426  }
427  }
428 
429  IncludeStructure Includes;
430  // If we are using a preamble, copy existing includes.
431  if (Preamble) {
432  Includes = Preamble->Includes;
433  Includes.MainFileIncludes = Patch->preambleIncludes();
434  // Replay the preamble includes so that clang-tidy checks can see them.
435  ReplayPreamble::attach(Patch->preambleIncludes(), *Clang,
436  Patch->modifiedBounds());
437  }
438  // Important: collectIncludeStructure is registered *after* ReplayPreamble!
439  // Otherwise we would collect the replayed includes again...
440  // (We can't *just* use the replayed includes, they don't have Resolved path).
441  Clang->getPreprocessor().addPPCallbacks(
442  collectIncludeStructureCallback(Clang->getSourceManager(), &Includes));
443  // Copy over the macros in the preamble region of the main file, and combine
444  // with non-preamble macros below.
445  MainFileMacros Macros;
446  if (Preamble)
447  Macros = Preamble->Macros;
448  Clang->getPreprocessor().addPPCallbacks(
449  std::make_unique<CollectMainFileMacros>(Clang->getSourceManager(),
450  Macros));
451 
452  // Copy over the includes from the preamble, then combine with the
453  // non-preamble includes below.
454  CanonicalIncludes CanonIncludes;
455  if (Preamble)
456  CanonIncludes = Preamble->CanonIncludes;
457  else
458  CanonIncludes.addSystemHeadersMapping(Clang->getLangOpts());
459  std::unique_ptr<CommentHandler> IWYUHandler =
460  collectIWYUHeaderMaps(&CanonIncludes);
461  Clang->getPreprocessor().addCommentHandler(IWYUHandler.get());
462 
463  // Collect tokens of the main file.
464  syntax::TokenCollector CollectTokens(Clang->getPreprocessor());
465 
466  if (llvm::Error Err = Action->Execute())
467  log("Execute() failed when building AST for {0}: {1}", MainInput.getFile(),
468  toString(std::move(Err)));
469 
470  // We have to consume the tokens before running clang-tidy to avoid collecting
471  // tokens from running the preprocessor inside the checks (only
472  // modernize-use-trailing-return-type does that today).
473  syntax::TokenBuffer Tokens = std::move(CollectTokens).consume();
474  // Makes SelectionTree build much faster.
475  Tokens.indexExpandedTokens();
476  std::vector<Decl *> ParsedDecls = Action->takeTopLevelDecls();
477  // AST traversals should exclude the preamble, to avoid performance cliffs.
478  Clang->getASTContext().setTraversalScope(ParsedDecls);
479  if (!CTChecks.empty()) {
480  // Run the AST-dependent part of the clang-tidy checks.
481  // (The preprocessor part ran already, via PPCallbacks).
482  trace::Span Tracer("ClangTidyMatch");
483  CTFinder.matchAST(Clang->getASTContext());
484  }
485 
486  // XXX: This is messy: clang-tidy checks flush some diagnostics at EOF.
487  // However Action->EndSourceFile() would destroy the ASTContext!
488  // So just inform the preprocessor of EOF, while keeping everything alive.
489  Clang->getPreprocessor().EndSourceFile();
490  // UnitDiagsConsumer is local, we can not store it in CompilerInstance that
491  // has a longer lifetime.
492  Clang->getDiagnostics().setClient(new IgnoreDiagnostics);
493  // CompilerInstance won't run this callback, do it directly.
494  ASTDiags.EndSourceFile();
495 
496  llvm::Optional<std::vector<Diag>> Diags;
497  // FIXME: Also skip generation of diagnostics alltogether to speed up ast
498  // builds when we are patching a stale preamble.
499  if (PreserveDiags) {
500  Diags = CompilerInvocationDiags;
501  // Add diagnostics from the preamble, if any.
502  if (Preamble)
503  Diags->insert(Diags->end(), Preamble->Diags.begin(),
504  Preamble->Diags.end());
505  // Finally, add diagnostics coming from the AST.
506  {
507  std::vector<Diag> D = ASTDiags.take(CTContext.getPointer());
508  Diags->insert(Diags->end(), D.begin(), D.end());
509  }
510  }
511  return ParsedAST(Inputs.Version, std::move(Preamble), std::move(Clang),
512  std::move(Action), std::move(Tokens), std::move(Macros),
513  std::move(ParsedDecls), std::move(Diags),
514  std::move(Includes), std::move(CanonIncludes));
515 }
516 
517 ParsedAST::ParsedAST(ParsedAST &&Other) = default;
518 
519 ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default;
520 
521 ParsedAST::~ParsedAST() {
522  if (Action) {
523  // We already notified the PP of end-of-file earlier, so detach it first.
524  // We must keep it alive until after EndSourceFile(), Sema relies on this.
525  auto PP = Clang->getPreprocessorPtr(); // Keep PP alive for now.
526  Clang->setPreprocessor(nullptr); // Detach so we don't send EOF again.
527  Action->EndSourceFile(); // Destroy ASTContext and Sema.
528  // Now Sema is gone, it's safe for PP to go out of scope.
529  }
530 }
531 
532 ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); }
533 
534 const ASTContext &ParsedAST::getASTContext() const {
535  return Clang->getASTContext();
536 }
537 
538 Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); }
539 
540 std::shared_ptr<Preprocessor> ParsedAST::getPreprocessorPtr() {
541  return Clang->getPreprocessorPtr();
542 }
543 
544 const Preprocessor &ParsedAST::getPreprocessor() const {
545  return Clang->getPreprocessor();
546 }
547 
548 llvm::ArrayRef<Decl *> ParsedAST::getLocalTopLevelDecls() {
549  return LocalTopLevelDecls;
550 }
551 
552 const MainFileMacros &ParsedAST::getMacros() const { return Macros; }
553 
554 std::size_t ParsedAST::getUsedBytes() const {
555  auto &AST = getASTContext();
556  // FIXME(ibiryukov): we do not account for the dynamically allocated part of
557  // Message and Fixes inside each diagnostic.
558  std::size_t Total = clangd::getUsedBytes(LocalTopLevelDecls) +
559  (Diags ? clangd::getUsedBytes(*Diags) : 0);
560 
561  // FIXME: the rest of the function is almost a direct copy-paste from
562  // libclang's clang_getCXTUResourceUsage. We could share the implementation.
563 
564  // Sum up various allocators inside the ast context and the preprocessor.
565  Total += AST.getASTAllocatedMemory();
566  Total += AST.getSideTableAllocatedMemory();
567  Total += AST.Idents.getAllocator().getTotalMemory();
568  Total += AST.Selectors.getTotalMemory();
569 
570  Total += AST.getSourceManager().getContentCacheSize();
571  Total += AST.getSourceManager().getDataStructureSizes();
572  Total += AST.getSourceManager().getMemoryBufferSizes().malloc_bytes;
573 
574  if (ExternalASTSource *Ext = AST.getExternalSource())
575  Total += Ext->getMemoryBufferSizes().malloc_bytes;
576 
577  const Preprocessor &PP = getPreprocessor();
578  Total += PP.getTotalMemory();
579  if (PreprocessingRecord *PRec = PP.getPreprocessingRecord())
580  Total += PRec->getTotalMemory();
581  Total += PP.getHeaderSearchInfo().getTotalMemory();
582 
583  return Total;
584 }
585 
586 const IncludeStructure &ParsedAST::getIncludeStructure() const {
587  return Includes;
588 }
589 
590 const CanonicalIncludes &ParsedAST::getCanonicalIncludes() const {
591  return CanonIncludes;
592 }
593 
594 ParsedAST::ParsedAST(llvm::StringRef Version,
595  std::shared_ptr<const PreambleData> Preamble,
596  std::unique_ptr<CompilerInstance> Clang,
597  std::unique_ptr<FrontendAction> Action,
598  syntax::TokenBuffer Tokens, MainFileMacros Macros,
599  std::vector<Decl *> LocalTopLevelDecls,
600  llvm::Optional<std::vector<Diag>> Diags,
601  IncludeStructure Includes, CanonicalIncludes CanonIncludes)
602  : Version(Version), Preamble(std::move(Preamble)), Clang(std::move(Clang)),
603  Action(std::move(Action)), Tokens(std::move(Tokens)),
604  Macros(std::move(Macros)), Diags(std::move(Diags)),
605  LocalTopLevelDecls(std::move(LocalTopLevelDecls)),
606  Includes(std::move(Includes)), CanonIncludes(std::move(CanonIncludes)) {
607  Resolver = std::make_unique<HeuristicResolver>(getASTContext());
608  assert(this->Clang);
609  assert(this->Action);
610 }
611 
612 llvm::Optional<llvm::StringRef> ParsedAST::preambleVersion() const {
613  if (!Preamble)
614  return llvm::None;
615  return llvm::StringRef(Preamble->Version);
616 }
617 } // namespace clangd
618 } // namespace clang
clang::clangd::CanonicalIncludes::addSystemHeadersMapping
void addSystemHeadersMapping(const LangOptions &Language)
Adds mapping for system headers and some special symbols (e.g.
Definition: CanonicalIncludes.cpp:84
dlog
#define dlog(...)
Definition: Logger.h:102
clang::clangd::ParseInputs::Version
std::string Version
Definition: Compiler.h:52
Total
unsigned Total
Definition: FunctionCognitiveComplexityCheck.cpp:145
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:45
clang::clangd::collectIWYUHeaderMaps
std::unique_ptr< CommentHandler > collectIWYUHeaderMaps(CanonicalIncludes *Includes)
Returns a CommentHandler that parses pragma comment on include files to determine when we should incl...
Definition: CanonicalIncludes.cpp:59
SyntaxOnlyAction
Headers.h
E
const Expr * E
Definition: AvoidBindCheck.cpp:88
clang::clangd::prepareCompilerInstance
std::unique_ptr< CompilerInstance > prepareCompilerInstance(std::unique_ptr< clang::CompilerInvocation > CI, const PrecompiledPreamble *Preamble, std::unique_ptr< llvm::MemoryBuffer > Buffer, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, DiagnosticConsumer &DiagsClient)
Definition: Compiler.cpp:110
Tracer
std::unique_ptr< trace::EventTracer > Tracer
Definition: TraceTests.cpp:164
DiagnosticConsumer
clang::clangd::getTidyOptionsForFile
tidy::ClangTidyOptions getTidyOptionsForFile(TidyProviderRef Provider, llvm::StringRef Filename)
Definition: TidyProvider.cpp:277
clang::clangd::ParsedAST::getASTContext
ASTContext & getASTContext()
Note that the returned ast will not contain decls from the preamble that were not deserialized during...
Definition: ParsedAST.cpp:532
clang::clangd::isImplicitTemplateInstantiation
bool isImplicitTemplateInstantiation(const NamedDecl *D)
Indicates if D is a template instantiation implicitly generated by the compiler, e....
Definition: AST.cpp:154
clang::clangd::PreambleData::Macros
MainFileMacros Macros
Definition: Preamble.h:63
Index.h
CI
std::unique_ptr< CompilerInvocation > CI
Definition: TUScheduler.cpp:450
Kind
BindArgumentKind Kind
Definition: AvoidBindCheck.cpp:59
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:39
Preamble.h
Feature.h
Trace.h
clang::clangd::Config
Settings that express user/project preferences and control clangd behavior.
Definition: Config.h:43
clang::clangd::PreambleData::Includes
IncludeStructure Includes
Definition: Preamble.h:59
IncludeFixer.h
Action
llvm::unique_function< void()> Action
Definition: TUScheduler.cpp:596
clang::clangd::IncludeStructure::MainFileIncludes
std::vector< Inclusion > MainFileIncludes
Definition: Headers.h:115
clang::tidy::ClangTidyOptions
Contains options for clang-tidy.
Definition: ClangTidyOptions.h:50
Preamble
const PreambleData & Preamble
Definition: CodeComplete.cpp:1104
clang::tidy::ClangTidyCheckFactories
A collection of ClangTidyCheckFactory instances.
Definition: ClangTidyModule.h:28
M
const google::protobuf::Message & M
Definition: Server.cpp:309
clang::clangd::ParsedAST::build
static 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)
Attempts to run Clang and store the parsed AST.
Definition: ParsedAST.cpp:248
clang::clangd::IncludeStructure
Definition: Headers.h:113
Inputs
ParseInputs Inputs
Definition: TUScheduler.cpp:451
clang::clangd::ParseInputs
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:47
CanonicalIncludes.h
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
Diagnostic
DiagnosticCallback Diagnostic
Definition: ConfigCompile.cpp:101
clang::clangd::DiagBase::Message
std::string Message
Definition: Diagnostics.h:62
Decl
const FunctionDecl * Decl
Definition: AvoidBindCheck.cpp:100
clang::clangd::CanonicalIncludes
Maps a definition location onto an #include file, based on a set of filename rules.
Definition: CanonicalIncludes.h:36
TidyProvider.h
clang::clangd::Diag
A top-level diagnostic that may have Notes and Fixes.
Definition: Diagnostics.h:97
clang::clangd::Config::Suppress
llvm::StringSet Suppress
Definition: Config.h:92
Logger.h
clang::clangd::MainFileMacros
Definition: CollectMacros.h:31
if
if(CLANGD_ENABLE_REMOTE) generate_protos(RemoteIndexProto "Index.proto") generate_protos(MonitoringServiceProto "MonitoringService.proto" GRPC) generate_protos(RemoteIndexServiceProto "Service.proto" DEPENDS "Index.proto" GRPC) target_link_libraries(RemoteIndexServiceProto PRIVATE RemoteIndexProto MonitoringServiceProto) include_directories($
Definition: clangd/index/remote/CMakeLists.txt:1
Patch
const llvm::Optional< PreamblePatch > Patch
Definition: CodeComplete.cpp:1105
clang::clangd::PreambleData::Diags
std::vector< Diag > Diags
Definition: Preamble.h:56
SPAN_ATTACH
#define SPAN_ATTACH(S, Name, Expr)
Attach a key-value pair to a Span event.
Definition: Trace.h:164
clang::clangd::toString
static const char * toString(OffsetEncoding OE)
Definition: Protocol.cpp:1338
Diagnostics.h
clang::tidy::configurationAsText
std::string configurationAsText(const ClangTidyOptions &Options)
Serializes configuration to a YAML-encoded string.
Definition: ClangTidyOptions.cpp:409
clang::clangd::collectIncludeStructureCallback
std::unique_ptr< PPCallbacks > collectIncludeStructureCallback(const SourceManager &SM, IncludeStructure *Out)
Returns a PPCallback that visits all inclusions in the main file.
Definition: Headers.cpp:152
clang::clangd::Config::SuppressAll
bool SuppressAll
Definition: Config.h:91
clang::clangd::getFormatStyleForFile
format::FormatStyle getFormatStyleForFile(llvm::StringRef File, llvm::StringRef Content, const ThreadsafeFS &TFS)
Choose the clang-format style we should apply to a certain file.
Definition: SourceCode.cpp:573
clang::clangd::log
void log(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:68
clang::tidy::ClangTidyGlobalOptions
Global options.
Definition: ClangTidyOptions.h:42
SourceCode.h
HeuristicResolver.h
clang::clangd::StoreDiags::contributeFixes
void contributeFixes(DiagFixer Fixer)
If set, possibly adds fixes for diagnostics using Fixer.
Definition: Diagnostics.h:153
Config.h
Compiler.h
FeatureModule.h
clang::clangd::PreamblePatch::createFullPatch
static PreamblePatch createFullPatch(llvm::StringRef FileName, const ParseInputs &Modified, const PreambleData &Baseline)
Builds a patch that contains new PP directives introduced to the preamble section of Modified compare...
Definition: Preamble.cpp:535
Info
FunctionInfo Info
Definition: FunctionSizeCheck.cpp:120
ASTConsumer
PPCallbacks
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::isInsideMainFile
bool isInsideMainFile(SourceLocation Loc, const SourceManager &SM)
Returns true iff Loc is inside the main file.
Definition: SourceCode.cpp:417
clang::clangd::StoreDiags::setDiagCallback
void setDiagCallback(DiagCallback CB)
Invokes a callback every time a diagnostics is completely formed.
Definition: Diagnostics.h:160
clang::clangd::StoreDiags
StoreDiags collects the diagnostics that can later be reported by clangd.
Definition: Diagnostics.h:135
Diags
CapturedDiags Diags
Definition: ConfigCompileTests.cpp:38
clang::clangd::Config::current
static const Config & current()
Returns the Config of the current Context, or an empty configuration.
Definition: Config.cpp:17
clang::clangd::Config::Diagnostics
struct clang::clangd::Config::@4 Diagnostics
Controls warnings and errors when parsing code.
clang::clangd::PreambleData::CanonIncludes
CanonicalIncludes CanonIncludes
Definition: Preamble.h:67
clang::tidy::ClangTidyCheckFactories::createChecks
std::vector< std::unique_ptr< ClangTidyCheck > > createChecks(ClangTidyContext *Context)
Create instances of checks that are enabled.
Definition: ClangTidyModule.cpp:25
clang::clangd::StoreDiags::setLevelAdjuster
void setLevelAdjuster(LevelAdjuster Adjuster)
If set, this allows the client of this class to adjust the level of diagnostics, such as promoting wa...
Definition: Diagnostics.h:157
clang::clangd::elog
void elog(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:62
clang::clangd::DiagBase
Contains basic information about a diagnostic.
Definition: Diagnostics.h:61
clang::clangd::StoreDiags::take
std::vector< Diag > take(const clang::tidy::ClangTidyContext *Tidy=nullptr)
Definition: Diagnostics.cpp:558
clang::clangd::ParsedAST::preambleVersion
llvm::Optional< llvm::StringRef > preambleVersion() const
Returns the version of the ParseInputs used to build Preamble part of this AST.
Definition: ParsedAST.cpp:612
clang::clangd::IgnoreDiagnostics
Definition: Compiler.h:32
AST.h
ParsedAST.h
clang::clangd::trace::Span
Records an event whose duration is the lifetime of the Span object.
Definition: Trace.h:143
clang::clangd::isBuiltinDiagnosticSuppressed
bool isBuiltinDiagnosticSuppressed(unsigned ID, const llvm::StringSet<> &Suppress)
Determine whether a (non-clang-tidy) diagnostic is suppressed by config.
Definition: Diagnostics.cpp:865