clang-tools  16.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 "IncludeCleaner.h"
22 #include "IncludeFixer.h"
23 #include "Preamble.h"
24 #include "SourceCode.h"
25 #include "TidyProvider.h"
27 #include "index/Index.h"
28 #include "support/Logger.h"
29 #include "support/Trace.h"
30 #include "clang/AST/ASTContext.h"
31 #include "clang/AST/Decl.h"
32 #include "clang/Basic/Diagnostic.h"
33 #include "clang/Basic/DiagnosticSema.h"
34 #include "clang/Basic/LangOptions.h"
35 #include "clang/Basic/SourceLocation.h"
36 #include "clang/Basic/SourceManager.h"
37 #include "clang/Basic/TokenKinds.h"
38 #include "clang/Frontend/CompilerInstance.h"
39 #include "clang/Frontend/CompilerInvocation.h"
40 #include "clang/Frontend/FrontendActions.h"
41 #include "clang/Lex/Lexer.h"
42 #include "clang/Lex/PPCallbacks.h"
43 #include "clang/Lex/Preprocessor.h"
44 #include "clang/Serialization/ASTWriter.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/SmallVector.h"
50 #include "llvm/ADT/StringRef.h"
51 #include <algorithm>
52 #include <memory>
53 #include <vector>
54 
55 // Force the linker to link in Clang-tidy modules.
56 // clangd doesn't support the static analyzer.
57 #if CLANGD_TIDY_CHECKS
58 #define CLANG_TIDY_DISABLE_STATIC_ANALYZER_CHECKS
59 #include "../clang-tidy/ClangTidyForceLinker.h"
60 #endif
61 
62 namespace clang {
63 namespace clangd {
64 namespace {
65 
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:
72  DeclTrackingASTConsumer(std::vector<Decl *> &TopLevelDecls)
73  : TopLevelDecls(TopLevelDecls) {}
74 
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))
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:
99  std::vector<Decl *> takeTopLevelDecls() { return std::move(TopLevelDecls); }
100 
101 protected:
102  std::unique_ptr<ASTConsumer>
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.
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:
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.
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 
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  llvm::StringRef WrittenFilename =
215  llvm::StringRef(Inc.Written).drop_front().drop_back();
216  Delegate->InclusionDirective(
217  HashTok->location(), SynthesizedIncludeTok, WrittenFilename,
218  Inc.Written.front() == '<',
219  syntax::FileRange(SM, SynthesizedFilenameTok.getLocation(),
220  SynthesizedFilenameTok.getEndLoc())
221  .toCharRange(SM),
222  File, "SearchPath", "RelPath",
223  /*Imported=*/nullptr, Inc.FileKind);
224  if (File)
225  Delegate->FileSkipped(*File, SynthesizedFilenameTok, Inc.FileKind);
226  }
227  }
228 
229  const std::vector<Inclusion> Includes;
230  PPCallbacks *Delegate;
231  const SourceManager &SM;
232  Preprocessor &PP;
233  std::vector<syntax::Token> MainFileTokens;
234 };
235 
236 // Filter for clang diagnostics groups enabled by CTOptions.Checks.
237 //
238 // These are check names like clang-diagnostics-unused.
239 // Note that unlike -Wunused, clang-diagnostics-unused does not imply
240 // subcategories like clang-diagnostics-unused-function.
241 //
242 // This is used to determine which diagnostics can be enabled by ExtraArgs in
243 // the clang-tidy configuration.
244 class TidyDiagnosticGroups {
245  // Whether all diagnostic groups are enabled by default.
246  // True if we've seen clang-diagnostic-*.
247  bool Default = false;
248  // Set of diag::Group whose enablement != Default.
249  // If Default is false, this is foo where we've seen clang-diagnostic-foo.
250  llvm::DenseSet<unsigned> Exceptions;
251 
252 public:
253  TidyDiagnosticGroups(llvm::StringRef Checks) {
254  constexpr llvm::StringLiteral CDPrefix = "clang-diagnostic-";
255 
256  llvm::StringRef Check;
257  while (!Checks.empty()) {
258  std::tie(Check, Checks) = Checks.split(',');
259  if (Check.empty())
260  continue;
261 
262  bool Enable = !Check.consume_front("-");
263  bool Glob = Check.consume_back("*");
264  if (Glob) {
265  // Is this clang-diagnostic-*, or *, or so?
266  // (We ignore all other types of globs).
267  if (CDPrefix.startswith(Check)) {
268  Default = Enable;
269  Exceptions.clear();
270  }
271  continue;
272  }
273 
274  // In "*,clang-diagnostic-foo", the latter is a no-op.
275  if (Default == Enable)
276  continue;
277  // The only non-glob entries we care about are clang-diagnostic-foo.
278  if (!Check.consume_front(CDPrefix))
279  continue;
280 
281  if (auto Group = DiagnosticIDs::getGroupForWarningOption(Check))
282  Exceptions.insert(static_cast<unsigned>(*Group));
283  }
284  }
285 
286  bool operator()(diag::Group GroupID) const {
287  return Exceptions.contains(static_cast<unsigned>(GroupID)) ? !Default
288  : Default;
289  }
290 };
291 
292 // Find -W<group> and -Wno-<group> options in ExtraArgs and apply them to Diags.
293 //
294 // This is used to handle ExtraArgs in clang-tidy configuration.
295 // We don't use clang's standard handling of this as we want slightly different
296 // behavior (e.g. we want to exclude these from -Wno-error).
297 void applyWarningOptions(llvm::ArrayRef<std::string> ExtraArgs,
298  llvm::function_ref<bool(diag::Group)> EnabledGroups,
299  DiagnosticsEngine &Diags) {
300  for (llvm::StringRef Group : ExtraArgs) {
301  // Only handle args that are of the form -W[no-]<group>.
302  // Other flags are possible but rare and deliberately out of scope.
303  llvm::SmallVector<diag::kind> Members;
304  if (!Group.consume_front("-W") || Group.empty())
305  continue;
306  bool Enable = !Group.consume_front("no-");
307  if (Diags.getDiagnosticIDs()->getDiagnosticsInGroup(
308  diag::Flavor::WarningOrError, Group, Members))
309  continue;
310 
311  // Upgrade (or downgrade) the severity of each diagnostic in the group.
312  // If -Werror is on, newly added warnings will be treated as errors.
313  // We don't want this, so keep track of them to fix afterwards.
314  bool NeedsWerrorExclusion = false;
315  for (diag::kind ID : Members) {
316  if (Enable) {
317  if (Diags.getDiagnosticLevel(ID, SourceLocation()) <
319  auto Group = DiagnosticIDs::getGroupForDiag(ID);
320  if (!Group || !EnabledGroups(*Group))
321  continue;
322  Diags.setSeverity(ID, diag::Severity::Warning, SourceLocation());
323  if (Diags.getWarningsAsErrors())
324  NeedsWerrorExclusion = true;
325  }
326  } else {
327  Diags.setSeverity(ID, diag::Severity::Ignored, SourceLocation());
328  }
329  }
330  if (NeedsWerrorExclusion) {
331  // FIXME: there's no API to suppress -Werror for single diagnostics.
332  // In some cases with sub-groups, we may end up erroneously
333  // downgrading diagnostics that were -Werror in the compile command.
334  Diags.setDiagnosticGroupWarningAsError(Group, false);
335  }
336  }
337 }
338 
339 } // namespace
340 
341 llvm::Optional<ParsedAST>
342 ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
343  std::unique_ptr<clang::CompilerInvocation> CI,
344  llvm::ArrayRef<Diag> CompilerInvocationDiags,
345  std::shared_ptr<const PreambleData> Preamble) {
346  trace::Span Tracer("BuildAST");
347  SPAN_ATTACH(Tracer, "File", Filename);
348 
349  auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
350  if (Preamble && Preamble->StatCache)
351  VFS = Preamble->StatCache->getConsumingFS(std::move(VFS));
352 
353  assert(CI);
354  // Command-line parsing sets DisableFree to true by default, but we don't want
355  // to leak memory in clangd.
356  CI->getFrontendOpts().DisableFree = false;
357  const PrecompiledPreamble *PreamblePCH =
358  Preamble ? &Preamble->Preamble : nullptr;
359 
360  // This is on-by-default in windows to allow parsing SDK headers, but it
361  // breaks many features. Disable it for the main-file (not preamble).
362  CI->getLangOpts()->DelayedTemplateParsing = false;
363 
364  std::vector<std::unique_ptr<FeatureModule::ASTListener>> ASTListeners;
365  if (Inputs.FeatureModules) {
366  for (auto &M : *Inputs.FeatureModules) {
367  if (auto Listener = M.astListeners())
368  ASTListeners.emplace_back(std::move(Listener));
369  }
370  }
371  StoreDiags ASTDiags;
372  ASTDiags.setDiagCallback(
373  [&ASTListeners](const clang::Diagnostic &D, clangd::Diag &Diag) {
374  llvm::for_each(ASTListeners,
375  [&](const auto &L) { L->sawDiagnostic(D, Diag); });
376  });
377 
378  llvm::Optional<PreamblePatch> Patch;
379  bool PreserveDiags = true;
380  // We might use an ignoring diagnostic consumer if they are going to be
381  // dropped later on to not pay for extra latency by processing them.
382  DiagnosticConsumer *DiagConsumer = &ASTDiags;
383  IgnoreDiagnostics DropDiags;
384  if (Preamble) {
386  Patch->apply(*CI);
387  PreserveDiags = Patch->preserveDiagnostics();
388  if (!PreserveDiags)
389  DiagConsumer = &DropDiags;
390  }
392  std::move(CI), PreamblePCH,
393  llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, Filename), VFS,
394  *DiagConsumer);
395  if (!Clang) {
396  // The last diagnostic contains information about the reason of this
397  // failure.
398  std::vector<Diag> Diags(ASTDiags.take());
399  elog("Failed to prepare a compiler instance: {0}",
400  !Diags.empty() ? static_cast<DiagBase &>(Diags.back()).Message
401  : "unknown error");
402  return None;
403  }
404  tidy::ClangTidyOptions ClangTidyOpts;
405  if (PreserveDiags) {
406  trace::Span Tracer("ClangTidyOpts");
407  ClangTidyOpts = getTidyOptionsForFile(Inputs.ClangTidyProvider, Filename);
408  dlog("ClangTidy configuration for file {0}: {1}", Filename,
409  tidy::configurationAsText(ClangTidyOpts));
410 
411  // If clang-tidy is configured to emit clang warnings, we should too.
412  //
413  // Such clang-tidy configuration consists of two parts:
414  // - ExtraArgs: ["-Wfoo"] causes clang to produce the warnings
415  // - Checks: "clang-diagnostic-foo" prevents clang-tidy filtering them out
416  //
417  // In clang-tidy, diagnostics are emitted if they pass both checks.
418  // When groups contain subgroups, -Wparent includes the child, but
419  // clang-diagnostic-parent does not.
420  //
421  // We *don't* want to change the compile command directly. This can have
422  // too many unexpected effects: breaking the command, interactions with
423  // -- and -Werror, etc. Besides, we've already parsed the command.
424  // Instead we parse the -W<group> flags and handle them directly.
425  //
426  // Similarly, we don't want to use Checks to filter clang diagnostics after
427  // they are generated, as this spreads clang-tidy emulation everywhere.
428  // Instead, we just use these to filter which extra diagnostics we enable.
429  auto &Diags = Clang->getDiagnostics();
430  TidyDiagnosticGroups TidyGroups(ClangTidyOpts.Checks ? *ClangTidyOpts.Checks
431  : llvm::StringRef());
432  if (ClangTidyOpts.ExtraArgsBefore)
433  applyWarningOptions(*ClangTidyOpts.ExtraArgsBefore, TidyGroups, Diags);
434  if (ClangTidyOpts.ExtraArgs)
435  applyWarningOptions(*ClangTidyOpts.ExtraArgs, TidyGroups, Diags);
436  } else {
437  // Skips some analysis.
438  Clang->getDiagnosticOpts().IgnoreWarnings = true;
439  }
440 
441  auto Action = std::make_unique<ClangdFrontendAction>();
442  const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0];
443  if (!Action->BeginSourceFile(*Clang, MainInput)) {
444  log("BeginSourceFile() failed when building AST for {0}",
445  MainInput.getFile());
446  return None;
447  }
448  // If we saw an include guard in the preamble section of the main file,
449  // mark the main-file as include-guarded.
450  // This information is part of the HeaderFileInfo but is not loaded from the
451  // preamble as the file's size is part of its identity and may have changed.
452  // (The rest of HeaderFileInfo is not relevant for our purposes).
453  if (Preamble && Preamble->MainIsIncludeGuarded) {
454  const SourceManager &SM = Clang->getSourceManager();
455  const FileEntry *MainFE = SM.getFileEntryForID(SM.getMainFileID());
456  Clang->getPreprocessor().getHeaderSearchInfo().MarkFileIncludeOnce(MainFE);
457  }
458 
459  // Set up ClangTidy. Must happen after BeginSourceFile() so ASTContext exists.
460  // Clang-tidy has some limitations to ensure reasonable performance:
461  // - checks don't see all preprocessor events in the preamble
462  // - matchers run only over the main-file top-level decls (and can't see
463  // ancestors outside this scope).
464  // In practice almost all checks work well without modifications.
465  std::vector<std::unique_ptr<tidy::ClangTidyCheck>> CTChecks;
466  ast_matchers::MatchFinder CTFinder;
467  llvm::Optional<tidy::ClangTidyContext> CTContext;
468  // Must outlive FixIncludes.
469  auto BuildDir = VFS->getCurrentWorkingDirectory();
470  llvm::Optional<IncludeFixer> FixIncludes;
471  llvm::DenseMap<diag::kind, DiagnosticsEngine::Level> OverriddenSeverity;
472  // No need to run clang-tidy or IncludeFixerif we are not going to surface
473  // diagnostics.
474  if (PreserveDiags) {
475  trace::Span Tracer("ClangTidyInit");
476  tidy::ClangTidyCheckFactories CTFactories;
477  for (const auto &E : tidy::ClangTidyModuleRegistry::entries())
478  E.instantiate()->addCheckFactories(CTFactories);
479  CTContext.emplace(std::make_unique<tidy::DefaultOptionsProvider>(
480  tidy::ClangTidyGlobalOptions(), ClangTidyOpts));
481  CTContext->setDiagnosticsEngine(&Clang->getDiagnostics());
482  CTContext->setASTContext(&Clang->getASTContext());
483  CTContext->setCurrentFile(Filename);
484  CTContext->setSelfContainedDiags(true);
485  CTChecks = CTFactories.createChecksForLanguage(&*CTContext);
486  Preprocessor *PP = &Clang->getPreprocessor();
487  for (const auto &Check : CTChecks) {
488  Check->registerPPCallbacks(Clang->getSourceManager(), PP, PP);
489  Check->registerMatchers(&CTFinder);
490  }
491 
492  // Clang only corrects typos for use of undeclared functions in C if that
493  // use is an error. Include fixer relies on typo correction, so pretend
494  // this is an error. (The actual typo correction is nice too).
495  // We restore the original severity in the level adjuster.
496  // FIXME: It would be better to have a real API for this, but what?
497  for (auto ID : {diag::ext_implicit_function_decl_c99,
498  diag::ext_implicit_lib_function_decl,
499  diag::ext_implicit_lib_function_decl_c99,
500  diag::warn_implicit_function_decl}) {
501  OverriddenSeverity.try_emplace(
502  ID, Clang->getDiagnostics().getDiagnosticLevel(ID, SourceLocation()));
503  Clang->getDiagnostics().setSeverity(ID, diag::Severity::Error,
504  SourceLocation());
505  }
506 
507  const Config &Cfg = Config::current();
508  ASTDiags.setLevelAdjuster([&](DiagnosticsEngine::Level DiagLevel,
509  const clang::Diagnostic &Info) {
510  if (Cfg.Diagnostics.SuppressAll ||
511  isBuiltinDiagnosticSuppressed(Info.getID(), Cfg.Diagnostics.Suppress,
512  Clang->getLangOpts()))
513  return DiagnosticsEngine::Ignored;
514 
515  auto It = OverriddenSeverity.find(Info.getID());
516  if (It != OverriddenSeverity.end())
517  DiagLevel = It->second;
518 
519  if (!CTChecks.empty()) {
520  std::string CheckName = CTContext->getCheckName(Info.getID());
521  bool IsClangTidyDiag = !CheckName.empty();
522  if (IsClangTidyDiag) {
523  if (Cfg.Diagnostics.Suppress.contains(CheckName))
524  return DiagnosticsEngine::Ignored;
525  // Check for suppression comment. Skip the check for diagnostics not
526  // in the main file, because we don't want that function to query the
527  // source buffer for preamble files. For the same reason, we ask
528  // shouldSuppressDiagnostic to avoid I/O.
529  // We let suppression comments take precedence over warning-as-error
530  // to match clang-tidy's behaviour.
531  bool IsInsideMainFile =
532  Info.hasSourceManager() &&
533  isInsideMainFile(Info.getLocation(), Info.getSourceManager());
534  SmallVector<tooling::Diagnostic, 1> TidySuppressedErrors;
535  if (IsInsideMainFile && CTContext->shouldSuppressDiagnostic(
536  DiagLevel, Info, TidySuppressedErrors,
537  /*AllowIO=*/false,
538  /*EnableNolintBlocks=*/true)) {
539  // FIXME: should we expose the suppression error (invalid use of
540  // NOLINT comments)?
541  return DiagnosticsEngine::Ignored;
542  }
543 
544  // Check for warning-as-error.
545  if (DiagLevel == DiagnosticsEngine::Warning &&
546  CTContext->treatAsError(CheckName)) {
547  return DiagnosticsEngine::Error;
548  }
549  }
550  }
551  return DiagLevel;
552  });
553 
554  // Add IncludeFixer which can recover diagnostics caused by missing includes
555  // (e.g. incomplete type) and attach include insertion fixes to diagnostics.
556  if (Inputs.Index && !BuildDir.getError()) {
557  auto Style =
558  getFormatStyleForFile(Filename, Inputs.Contents, *Inputs.TFS);
559  auto Inserter = std::make_shared<IncludeInserter>(
560  Filename, Inputs.Contents, Style, BuildDir.get(),
561  &Clang->getPreprocessor().getHeaderSearchInfo());
562  if (Preamble) {
563  for (const auto &Inc : Preamble->Includes.MainFileIncludes)
564  Inserter->addExisting(Inc);
565  }
566  FixIncludes.emplace(Filename, Inserter, *Inputs.Index,
567  /*IndexRequestLimit=*/5);
568  ASTDiags.contributeFixes([&FixIncludes](DiagnosticsEngine::Level DiagLevl,
569  const clang::Diagnostic &Info) {
570  return FixIncludes->fix(DiagLevl, Info);
571  });
572  Clang->setExternalSemaSource(FixIncludes->unresolvedNameRecorder());
573  }
574  }
575 
576  IncludeStructure Includes;
577  // If we are using a preamble, copy existing includes.
578  if (Preamble) {
579  Includes = Preamble->Includes;
580  Includes.MainFileIncludes = Patch->preambleIncludes();
581  // Replay the preamble includes so that clang-tidy checks can see them.
582  ReplayPreamble::attach(Patch->preambleIncludes(), *Clang,
583  Patch->modifiedBounds());
584  }
585  // Important: collectIncludeStructure is registered *after* ReplayPreamble!
586  // Otherwise we would collect the replayed includes again...
587  // (We can't *just* use the replayed includes, they don't have Resolved path).
588  Includes.collect(*Clang);
589  // Copy over the macros in the preamble region of the main file, and combine
590  // with non-preamble macros below.
591  MainFileMacros Macros;
592  if (Preamble)
594  Clang->getPreprocessor().addPPCallbacks(
595  std::make_unique<CollectMainFileMacros>(Clang->getSourceManager(),
596  Macros));
597 
598  std::vector<PragmaMark> Marks;
599  // FIXME: We need to patch the marks for stale preambles.
600  if (Preamble)
601  Marks = Preamble->Marks;
602  Clang->getPreprocessor().addPPCallbacks(
603  collectPragmaMarksCallback(Clang->getSourceManager(), Marks));
604 
605  // Copy over the includes from the preamble, then combine with the
606  // non-preamble includes below.
607  CanonicalIncludes CanonIncludes;
608  if (Preamble)
609  CanonIncludes = Preamble->CanonIncludes;
610  else
611  CanonIncludes.addSystemHeadersMapping(Clang->getLangOpts());
612  std::unique_ptr<CommentHandler> IWYUHandler =
613  collectIWYUHeaderMaps(&CanonIncludes);
614  Clang->getPreprocessor().addCommentHandler(IWYUHandler.get());
615 
616  // Collect tokens of the main file.
617  syntax::TokenCollector CollectTokens(Clang->getPreprocessor());
618 
619  // To remain consistent with preamble builds, these callbacks must be called
620  // exactly here, after preprocessor is initialized and BeginSourceFile() was
621  // called already.
622  for (const auto &L : ASTListeners)
623  L->beforeExecute(*Clang);
624 
625  if (llvm::Error Err = Action->Execute())
626  log("Execute() failed when building AST for {0}: {1}", MainInput.getFile(),
627  toString(std::move(Err)));
628 
629  // We have to consume the tokens before running clang-tidy to avoid collecting
630  // tokens from running the preprocessor inside the checks (only
631  // modernize-use-trailing-return-type does that today).
632  syntax::TokenBuffer Tokens = std::move(CollectTokens).consume();
633  // Makes SelectionTree build much faster.
634  Tokens.indexExpandedTokens();
635  std::vector<Decl *> ParsedDecls = Action->takeTopLevelDecls();
636  // AST traversals should exclude the preamble, to avoid performance cliffs.
637  Clang->getASTContext().setTraversalScope(ParsedDecls);
638  if (!CTChecks.empty()) {
639  // Run the AST-dependent part of the clang-tidy checks.
640  // (The preprocessor part ran already, via PPCallbacks).
641  trace::Span Tracer("ClangTidyMatch");
642  CTFinder.matchAST(Clang->getASTContext());
643  }
644 
645  // XXX: This is messy: clang-tidy checks flush some diagnostics at EOF.
646  // However Action->EndSourceFile() would destroy the ASTContext!
647  // So just inform the preprocessor of EOF, while keeping everything alive.
648  Clang->getPreprocessor().EndSourceFile();
649  // UnitDiagsConsumer is local, we can not store it in CompilerInstance that
650  // has a longer lifetime.
651  Clang->getDiagnostics().setClient(new IgnoreDiagnostics);
652  // CompilerInstance won't run this callback, do it directly.
653  ASTDiags.EndSourceFile();
654 
655  llvm::Optional<std::vector<Diag>> Diags;
656  // FIXME: Also skip generation of diagnostics alltogether to speed up ast
657  // builds when we are patching a stale preamble.
658  if (PreserveDiags) {
659  Diags = CompilerInvocationDiags;
660  // Add diagnostics from the preamble, if any.
661  if (Preamble)
662  Diags->insert(Diags->end(), Preamble->Diags.begin(),
663  Preamble->Diags.end());
664  // Finally, add diagnostics coming from the AST.
665  {
666  std::vector<Diag> D = ASTDiags.take(&*CTContext);
667  Diags->insert(Diags->end(), D.begin(), D.end());
668  }
669  }
670  ParsedAST Result(Filename, Inputs.Version, std::move(Preamble),
671  std::move(Clang), std::move(Action), std::move(Tokens),
672  std::move(Macros), std::move(Marks), std::move(ParsedDecls),
673  std::move(Diags), std::move(Includes),
674  std::move(CanonIncludes));
675  if (Result.Diags) {
676  auto UnusedHeadersDiags =
678  Result.Diags->insert(Result.Diags->end(),
679  make_move_iterator(UnusedHeadersDiags.begin()),
680  make_move_iterator(UnusedHeadersDiags.end()));
681  }
682  return Result;
683 }
684 
685 ParsedAST::ParsedAST(ParsedAST &&Other) = default;
686 
687 ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default;
688 
689 ParsedAST::~ParsedAST() {
690  if (Action) {
691  // We already notified the PP of end-of-file earlier, so detach it first.
692  // We must keep it alive until after EndSourceFile(), Sema relies on this.
693  auto PP = Clang->getPreprocessorPtr(); // Keep PP alive for now.
694  Clang->setPreprocessor(nullptr); // Detach so we don't send EOF again.
695  Action->EndSourceFile(); // Destroy ASTContext and Sema.
696  // Now Sema is gone, it's safe for PP to go out of scope.
697  }
698 }
699 
700 ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); }
701 
702 const ASTContext &ParsedAST::getASTContext() const {
703  return Clang->getASTContext();
704 }
705 
706 Sema &ParsedAST::getSema() { return Clang->getSema(); }
707 
708 Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); }
709 
710 std::shared_ptr<Preprocessor> ParsedAST::getPreprocessorPtr() {
711  return Clang->getPreprocessorPtr();
712 }
713 
714 const Preprocessor &ParsedAST::getPreprocessor() const {
715  return Clang->getPreprocessor();
716 }
717 
718 llvm::ArrayRef<Decl *> ParsedAST::getLocalTopLevelDecls() {
719  return LocalTopLevelDecls;
720 }
721 
722 const MainFileMacros &ParsedAST::getMacros() const { return Macros; }
723 const std::vector<PragmaMark> &ParsedAST::getMarks() const { return Marks; }
724 
725 std::size_t ParsedAST::getUsedBytes() const {
726  auto &AST = getASTContext();
727  // FIXME(ibiryukov): we do not account for the dynamically allocated part of
728  // Message and Fixes inside each diagnostic.
729  std::size_t Total = clangd::getUsedBytes(LocalTopLevelDecls) +
730  (Diags ? clangd::getUsedBytes(*Diags) : 0);
731 
732  // FIXME: the rest of the function is almost a direct copy-paste from
733  // libclang's clang_getCXTUResourceUsage. We could share the implementation.
734 
735  // Sum up various allocators inside the ast context and the preprocessor.
736  Total += AST.getASTAllocatedMemory();
737  Total += AST.getSideTableAllocatedMemory();
738  Total += AST.Idents.getAllocator().getTotalMemory();
739  Total += AST.Selectors.getTotalMemory();
740 
741  Total += AST.getSourceManager().getContentCacheSize();
742  Total += AST.getSourceManager().getDataStructureSizes();
743  Total += AST.getSourceManager().getMemoryBufferSizes().malloc_bytes;
744 
745  if (ExternalASTSource *Ext = AST.getExternalSource())
746  Total += Ext->getMemoryBufferSizes().malloc_bytes;
747 
748  const Preprocessor &PP = getPreprocessor();
749  Total += PP.getTotalMemory();
750  if (PreprocessingRecord *PRec = PP.getPreprocessingRecord())
751  Total += PRec->getTotalMemory();
752  Total += PP.getHeaderSearchInfo().getTotalMemory();
753 
754  return Total;
755 }
756 
757 const IncludeStructure &ParsedAST::getIncludeStructure() const {
758  return Includes;
759 }
760 
761 const CanonicalIncludes &ParsedAST::getCanonicalIncludes() const {
762  return CanonIncludes;
763 }
764 
765 ParsedAST::ParsedAST(PathRef TUPath, llvm::StringRef Version,
766  std::shared_ptr<const PreambleData> Preamble,
767  std::unique_ptr<CompilerInstance> Clang,
768  std::unique_ptr<FrontendAction> Action,
769  syntax::TokenBuffer Tokens, MainFileMacros Macros,
770  std::vector<PragmaMark> Marks,
771  std::vector<Decl *> LocalTopLevelDecls,
772  llvm::Optional<std::vector<Diag>> Diags,
773  IncludeStructure Includes, CanonicalIncludes CanonIncludes)
774  : TUPath(TUPath), Version(Version), Preamble(std::move(Preamble)),
775  Clang(std::move(Clang)), Action(std::move(Action)),
776  Tokens(std::move(Tokens)), Macros(std::move(Macros)),
777  Marks(std::move(Marks)), Diags(std::move(Diags)),
778  LocalTopLevelDecls(std::move(LocalTopLevelDecls)),
779  Includes(std::move(Includes)), CanonIncludes(std::move(CanonIncludes)) {
780  Resolver = std::make_unique<HeuristicResolver>(getASTContext());
781  assert(this->Clang);
782  assert(this->Action);
783 }
784 
785 llvm::Optional<llvm::StringRef> ParsedAST::preambleVersion() const {
786  if (!Preamble)
787  return llvm::None;
788  return llvm::StringRef(Preamble->Version);
789 }
790 
791 } // namespace clangd
792 } // 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:734
dlog
#define dlog(...)
Definition: Logger.h:101
clang::clangd::ParseInputs::Version
std::string Version
Definition: Compiler.h:51
Total
unsigned Total
Definition: FunctionCognitiveComplexityCheck.cpp:145
clang::clangd::IncludeStructure::collect
void collect(const CompilerInstance &CI)
Definition: Headers.cpp:229
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:708
SyntaxOnlyAction
Headers.h
IncludeCleaner.h
clang::tidy::ClangTidyOptions::Checks
llvm::Optional< std::string > Checks
Checks filter.
Definition: ClangTidyOptions.h:70
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:120
clang::clangd::ParseInputs::Contents
std::string Contents
Definition: Compiler.h:49
Macros
llvm::DenseSet< FileID > Macros
Definition: IncludeCleaner.cpp:195
Tracer
std::unique_ptr< trace::EventTracer > Tracer
Definition: TraceTests.cpp:161
DiagnosticConsumer
Checks
static cl::opt< std::string > Checks("checks", cl::desc(R"( Comma-separated list of globs with optional '-' prefix. Globs are processed in order of appearance in the list. Globs without '-' prefix add checks with matching names to the set, globs with the '-' prefix remove checks with matching names from the set of enabled checks. This option's value is appended to the value of the 'Checks' option in .clang-tidy file, if any. )"), cl::init(""), cl::cat(ClangTidyCategory))
clang::clangd::getTidyOptionsForFile
tidy::ClangTidyOptions getTidyOptionsForFile(TidyProviderRef Provider, llvm::StringRef Filename)
Definition: TidyProvider.cpp:283
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:700
clang::clangd::isImplicitTemplateInstantiation
bool isImplicitTemplateInstantiation(const NamedDecl *D)
Indicates if D is a template instantiation implicitly generated by the compiler, e....
Definition: AST.cpp:160
clang::clangd::PreambleData::Macros
MainFileMacros Macros
Definition: Preamble.h:63
Index.h
clang::clangd::issueUnusedIncludesDiagnostics
std::vector< Diag > issueUnusedIncludesDiagnostics(ParsedAST &AST, llvm::StringRef Code)
Definition: IncludeCleaner.cpp:472
CI
std::unique_ptr< CompilerInvocation > CI
Definition: TUScheduler.cpp:549
Kind
BindArgumentKind Kind
Definition: AvoidBindCheck.cpp:59
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:40
Preamble.h
Feature.h
Trace.h
clang::tidy::ClangTidyOptions::ExtraArgsBefore
llvm::Optional< ArgList > ExtraArgsBefore
Add extra compilation arguments to the start of the list.
Definition: ClangTidyOptions.h:126
clang::clangd::Config
Settings that express user/project preferences and control clangd behavior.
Definition: Config.h:44
clang::clangd::PreambleData::Includes
IncludeStructure Includes
Definition: Preamble.h:59
IncludeFixer.h
clang::clangd::IncludeStructure::MainFileIncludes
std::vector< Inclusion > MainFileIncludes
Definition: Headers.h:168
clang::tidy::ClangTidyOptions
Contains options for clang-tidy.
Definition: ClangTidyOptions.h:50
Preamble
const PreambleData & Preamble
Definition: CodeComplete.cpp:1214
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:342
clang::clangd::IncludeStructure
Definition: Headers.h:119
Error
constexpr static llvm::SourceMgr::DiagKind Error
Definition: ConfigCompile.cpp:591
Inputs
ParseInputs Inputs
Definition: TUScheduler.cpp:550
clang::clangd::ParseInputs
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:46
CanonicalIncludes.h
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
Diagnostic
DiagnosticCallback Diagnostic
Definition: ConfigCompile.cpp:100
clang::clangd::DiagBase::Message
std::string Message
Definition: Diagnostics.h:59
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:37
TidyProvider.h
clang::clangd::Diag
A top-level diagnostic that may have Notes and Fixes.
Definition: Diagnostics.h:94
Logger.h
clang::clangd::MainFileMacros
Definition: CollectMacros.h:29
clang::clangd::collectPragmaMarksCallback
std::unique_ptr< PPCallbacks > collectPragmaMarksCallback(const SourceManager &SM, std::vector< PragmaMark > &Out)
Collect all pragma marks from the main file.
Definition: CollectMacros.cpp:56
Patch
const llvm::Optional< PreamblePatch > Patch
Definition: CodeComplete.cpp:1215
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:1393
Diagnostics.h
clang::tidy::configurationAsText
std::string configurationAsText(const ClangTidyOptions &Options)
Serializes configuration to a YAML-encoded string.
Definition: ClangTidyOptions.cpp:443
clang::clangd::PreambleData::Marks
std::vector< PragmaMark > Marks
Definition: Preamble.h:65
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:581
clang::clangd::log
void log(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:67
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:157
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:715
Info
FunctionInfo Info
Definition: FunctionSizeCheck.cpp:121
ID
static char ID
Definition: Logger.cpp:74
clang::tidy::ClangTidyCheckFactories::createChecksForLanguage
std::vector< std::unique_ptr< ClangTidyCheck > > createChecksForLanguage(ClangTidyContext *Context)
Create instances of checks that are enabled for the current Language.
Definition: ClangTidyModule.cpp:35
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:29
ASTConsumer
PPCallbacks
if
if(CLANG_PLUGIN_SUPPORT) export_executable_symbols_for_plugins(clang-tidy) endif() install(PROGRAMS clang-tidy-diff.py DESTINATION "$
Definition: clang-tidy/tool/CMakeLists.txt:59
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:418
clang::clangd::StoreDiags::setDiagCallback
void setDiagCallback(DiagCallback CB)
Invokes a callback every time a diagnostics is completely formed.
Definition: Diagnostics.h:164
clang::clangd::StoreDiags
StoreDiags collects the diagnostics that can later be reported by clangd.
Definition: Diagnostics.h:137
Diags
CapturedDiags Diags
Definition: ConfigCompileTests.cpp:39
LangOpts
const LangOptions * LangOpts
Definition: ExtractFunction.cpp:375
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::PreambleData::CanonIncludes
CanonicalIncludes CanonIncludes
Definition: Preamble.h:69
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:161
clang::clangd::elog
void elog(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:61
clang::clangd::DiagBase
Contains basic information about a diagnostic.
Definition: Diagnostics.h:58
Warning
constexpr static llvm::SourceMgr::DiagKind Warning
Definition: ConfigCompile.cpp:592
clang::clangd::isBuiltinDiagnosticSuppressed
bool isBuiltinDiagnosticSuppressed(unsigned ID, const llvm::StringSet<> &Suppress, const LangOptions &LangOpts)
Determine whether a (non-clang-tidy) diagnostic is suppressed by config.
Definition: Diagnostics.cpp:884
clang::clangd::StoreDiags::take
std::vector< Diag > take(const clang::tidy::ClangTidyContext *Tidy=nullptr)
Definition: Diagnostics.cpp:563
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:785
clang::tidy::ClangTidyOptions::ExtraArgs
llvm::Optional< ArgList > ExtraArgs
Add extra compilation arguments to the end of the list.
Definition: ClangTidyOptions.h:123
Clang
std::unique_ptr< CompilerInstance > Clang
Definition: HeadersTests.cpp:140
clang::clangd::IgnoreDiagnostics
Definition: Compiler.h:31
AST.h
ParsedAST.h
Action
FieldAction Action
Definition: MemberwiseConstructor.cpp:261
clang::clangd::trace::Span
Records an event whose duration is the lifetime of the Span object.
Definition: Trace.h:143