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