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