[AMDGPU][AsmParser][NFC] Translate parsed MIMG instructions to MCInsts automatically.
[llvm-project.git] / clang-tools-extra / clangd / ParsedAST.cpp
blob02c8597c79613db3538d1e48a6b270d56b933e73
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 //===----------------------------------------------------------------------===//
9 #include "ParsedAST.h"
10 #include "../clang-tidy/ClangTidyCheck.h"
11 #include "../clang-tidy/ClangTidyDiagnosticConsumer.h"
12 #include "../clang-tidy/ClangTidyModule.h"
13 #include "../clang-tidy/ClangTidyModuleRegistry.h"
14 #include "../clang-tidy/ClangTidyOptions.h"
15 #include "AST.h"
16 #include "CollectMacros.h"
17 #include "Compiler.h"
18 #include "Config.h"
19 #include "Diagnostics.h"
20 #include "Feature.h"
21 #include "FeatureModule.h"
22 #include "Headers.h"
23 #include "HeuristicResolver.h"
24 #include "IncludeCleaner.h"
25 #include "IncludeFixer.h"
26 #include "Preamble.h"
27 #include "SourceCode.h"
28 #include "TidyProvider.h"
29 #include "clang-include-cleaner/Record.h"
30 #include "index/CanonicalIncludes.h"
31 #include "index/Symbol.h"
32 #include "support/Logger.h"
33 #include "support/Path.h"
34 #include "support/Trace.h"
35 #include "clang/AST/ASTContext.h"
36 #include "clang/AST/Decl.h"
37 #include "clang/AST/DeclGroup.h"
38 #include "clang/AST/ExternalASTSource.h"
39 #include "clang/ASTMatchers/ASTMatchFinder.h"
40 #include "clang/Basic/Diagnostic.h"
41 #include "clang/Basic/DiagnosticIDs.h"
42 #include "clang/Basic/DiagnosticSema.h"
43 #include "clang/Basic/FileEntry.h"
44 #include "clang/Basic/LLVM.h"
45 #include "clang/Basic/LangOptions.h"
46 #include "clang/Basic/SourceLocation.h"
47 #include "clang/Basic/SourceManager.h"
48 #include "clang/Basic/TokenKinds.h"
49 #include "clang/Frontend/CompilerInstance.h"
50 #include "clang/Frontend/CompilerInvocation.h"
51 #include "clang/Frontend/FrontendActions.h"
52 #include "clang/Frontend/FrontendOptions.h"
53 #include "clang/Frontend/PrecompiledPreamble.h"
54 #include "clang/Lex/Lexer.h"
55 #include "clang/Lex/PPCallbacks.h"
56 #include "clang/Lex/Preprocessor.h"
57 #include "clang/Serialization/ASTWriter.h"
58 #include "clang/Tooling/CompilationDatabase.h"
59 #include "clang/Tooling/Core/Diagnostic.h"
60 #include "clang/Tooling/Syntax/Tokens.h"
61 #include "llvm/ADT/ArrayRef.h"
62 #include "llvm/ADT/DenseMap.h"
63 #include "llvm/ADT/DenseSet.h"
64 #include "llvm/ADT/STLExtras.h"
65 #include "llvm/ADT/STLFunctionalExtras.h"
66 #include "llvm/ADT/SmallVector.h"
67 #include "llvm/ADT/StringRef.h"
68 #include "llvm/Support/Error.h"
69 #include "llvm/Support/MemoryBuffer.h"
70 #include <cassert>
71 #include <cstddef>
72 #include <iterator>
73 #include <memory>
74 #include <optional>
75 #include <string>
76 #include <tuple>
77 #include <utility>
78 #include <vector>
80 // Force the linker to link in Clang-tidy modules.
81 // clangd doesn't support the static analyzer.
82 #if CLANGD_TIDY_CHECKS
83 #define CLANG_TIDY_DISABLE_STATIC_ANALYZER_CHECKS
84 #include "../clang-tidy/ClangTidyForceLinker.h"
85 #endif
87 namespace clang {
88 namespace clangd {
89 namespace {
91 template <class T> std::size_t getUsedBytes(const std::vector<T> &Vec) {
92 return Vec.capacity() * sizeof(T);
95 class DeclTrackingASTConsumer : public ASTConsumer {
96 public:
97 DeclTrackingASTConsumer(std::vector<Decl *> &TopLevelDecls)
98 : TopLevelDecls(TopLevelDecls) {}
100 bool HandleTopLevelDecl(DeclGroupRef DG) override {
101 for (Decl *D : DG) {
102 auto &SM = D->getASTContext().getSourceManager();
103 if (!isInsideMainFile(D->getLocation(), SM))
104 continue;
105 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
106 if (isImplicitTemplateInstantiation(ND))
107 continue;
109 // ObjCMethodDecl are not actually top-level decls.
110 if (isa<ObjCMethodDecl>(D))
111 continue;
113 TopLevelDecls.push_back(D);
115 return true;
118 private:
119 std::vector<Decl *> &TopLevelDecls;
122 class ClangdFrontendAction : public SyntaxOnlyAction {
123 public:
124 std::vector<Decl *> takeTopLevelDecls() { return std::move(TopLevelDecls); }
126 protected:
127 std::unique_ptr<ASTConsumer>
128 CreateASTConsumer(CompilerInstance &CI, llvm::StringRef InFile) override {
129 return std::make_unique<DeclTrackingASTConsumer>(/*ref*/ TopLevelDecls);
132 private:
133 std::vector<Decl *> TopLevelDecls;
136 // When using a preamble, only preprocessor events outside its bounds are seen.
137 // This is almost what we want: replaying transitive preprocessing wastes time.
138 // However this confuses clang-tidy checks: they don't see any #includes!
139 // So we replay the *non-transitive* #includes that appear in the main-file.
140 // It would be nice to replay other events (macro definitions, ifdefs etc) but
141 // this addresses the most common cases fairly cheaply.
142 class ReplayPreamble : private PPCallbacks {
143 public:
144 // Attach preprocessor hooks such that preamble events will be injected at
145 // the appropriate time.
146 // Events will be delivered to the *currently registered* PP callbacks.
147 static void attach(std::vector<Inclusion> Includes, CompilerInstance &Clang,
148 const PreambleBounds &PB) {
149 auto &PP = Clang.getPreprocessor();
150 auto *ExistingCallbacks = PP.getPPCallbacks();
151 // No need to replay events if nobody is listening.
152 if (!ExistingCallbacks)
153 return;
154 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(new ReplayPreamble(
155 std::move(Includes), ExistingCallbacks, Clang.getSourceManager(), PP,
156 Clang.getLangOpts(), PB)));
157 // We're relying on the fact that addPPCallbacks keeps the old PPCallbacks
158 // around, creating a chaining wrapper. Guard against other implementations.
159 assert(PP.getPPCallbacks() != ExistingCallbacks &&
160 "Expected chaining implementation");
163 private:
164 ReplayPreamble(std::vector<Inclusion> Includes, PPCallbacks *Delegate,
165 const SourceManager &SM, Preprocessor &PP,
166 const LangOptions &LangOpts, const PreambleBounds &PB)
167 : Includes(std::move(Includes)), Delegate(Delegate), SM(SM), PP(PP) {
168 // Only tokenize the preamble section of the main file, as we are not
169 // interested in the rest of the tokens.
170 MainFileTokens = syntax::tokenize(
171 syntax::FileRange(SM.getMainFileID(), 0, PB.Size), SM, LangOpts);
174 // In a normal compile, the preamble traverses the following structure:
176 // mainfile.cpp
177 // <built-in>
178 // ... macro definitions like __cplusplus ...
179 // <command-line>
180 // ... macro definitions for args like -Dfoo=bar ...
181 // "header1.h"
182 // ... header file contents ...
183 // "header2.h"
184 // ... header file contents ...
185 // ... main file contents ...
187 // When using a preamble, the "header1" and "header2" subtrees get skipped.
188 // We insert them right after the built-in header, which still appears.
189 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
190 SrcMgr::CharacteristicKind Kind, FileID PrevFID) override {
191 // It'd be nice if there was a better way to identify built-in headers...
192 if (Reason == FileChangeReason::ExitFile &&
193 SM.getBufferOrFake(PrevFID).getBufferIdentifier() == "<built-in>")
194 replay();
197 void replay() {
198 for (const auto &Inc : Includes) {
199 OptionalFileEntryRef File;
200 if (Inc.Resolved != "")
201 File = expectedToOptional(SM.getFileManager().getFileRef(Inc.Resolved));
203 // Re-lex the #include directive to find its interesting parts.
204 auto HashLoc = SM.getComposedLoc(SM.getMainFileID(), Inc.HashOffset);
205 auto HashTok = llvm::partition_point(MainFileTokens,
206 [&HashLoc](const syntax::Token &T) {
207 return T.location() < HashLoc;
209 assert(HashTok != MainFileTokens.end() && HashTok->kind() == tok::hash);
211 auto IncludeTok = std::next(HashTok);
212 assert(IncludeTok != MainFileTokens.end());
214 auto FileTok = std::next(IncludeTok);
215 assert(FileTok != MainFileTokens.end());
217 // Create a fake import/include token, none of the callers seem to care
218 // about clang::Token::Flags.
219 Token SynthesizedIncludeTok;
220 SynthesizedIncludeTok.startToken();
221 SynthesizedIncludeTok.setLocation(IncludeTok->location());
222 SynthesizedIncludeTok.setLength(IncludeTok->length());
223 SynthesizedIncludeTok.setKind(tok::raw_identifier);
224 SynthesizedIncludeTok.setRawIdentifierData(IncludeTok->text(SM).data());
225 PP.LookUpIdentifierInfo(SynthesizedIncludeTok);
227 // Same here, create a fake one for Filename, including angles or quotes.
228 Token SynthesizedFilenameTok;
229 SynthesizedFilenameTok.startToken();
230 SynthesizedFilenameTok.setLocation(FileTok->location());
231 // Note that we can't make use of FileTok->length/text in here as in the
232 // case of angled includes this will contain tok::less instead of
233 // filename. Whereas Inc.Written contains the full header name including
234 // quotes/angles.
235 SynthesizedFilenameTok.setLength(Inc.Written.length());
236 SynthesizedFilenameTok.setKind(tok::header_name);
237 SynthesizedFilenameTok.setLiteralData(Inc.Written.data());
239 llvm::StringRef WrittenFilename =
240 llvm::StringRef(Inc.Written).drop_front().drop_back();
241 Delegate->InclusionDirective(
242 HashTok->location(), SynthesizedIncludeTok, WrittenFilename,
243 Inc.Written.front() == '<',
244 syntax::FileRange(SM, SynthesizedFilenameTok.getLocation(),
245 SynthesizedFilenameTok.getEndLoc())
246 .toCharRange(SM),
247 File, "SearchPath", "RelPath",
248 /*Imported=*/nullptr, Inc.FileKind);
249 if (File)
250 Delegate->FileSkipped(*File, SynthesizedFilenameTok, Inc.FileKind);
254 const std::vector<Inclusion> Includes;
255 PPCallbacks *Delegate;
256 const SourceManager &SM;
257 Preprocessor &PP;
258 std::vector<syntax::Token> MainFileTokens;
261 // Filter for clang diagnostics groups enabled by CTOptions.Checks.
263 // These are check names like clang-diagnostics-unused.
264 // Note that unlike -Wunused, clang-diagnostics-unused does not imply
265 // subcategories like clang-diagnostics-unused-function.
267 // This is used to determine which diagnostics can be enabled by ExtraArgs in
268 // the clang-tidy configuration.
269 class TidyDiagnosticGroups {
270 // Whether all diagnostic groups are enabled by default.
271 // True if we've seen clang-diagnostic-*.
272 bool Default = false;
273 // Set of diag::Group whose enablement != Default.
274 // If Default is false, this is foo where we've seen clang-diagnostic-foo.
275 llvm::DenseSet<unsigned> Exceptions;
277 public:
278 TidyDiagnosticGroups(llvm::StringRef Checks) {
279 constexpr llvm::StringLiteral CDPrefix = "clang-diagnostic-";
281 llvm::StringRef Check;
282 while (!Checks.empty()) {
283 std::tie(Check, Checks) = Checks.split(',');
284 if (Check.empty())
285 continue;
287 bool Enable = !Check.consume_front("-");
288 bool Glob = Check.consume_back("*");
289 if (Glob) {
290 // Is this clang-diagnostic-*, or *, or so?
291 // (We ignore all other types of globs).
292 if (CDPrefix.startswith(Check)) {
293 Default = Enable;
294 Exceptions.clear();
296 continue;
299 // In "*,clang-diagnostic-foo", the latter is a no-op.
300 if (Default == Enable)
301 continue;
302 // The only non-glob entries we care about are clang-diagnostic-foo.
303 if (!Check.consume_front(CDPrefix))
304 continue;
306 if (auto Group = DiagnosticIDs::getGroupForWarningOption(Check))
307 Exceptions.insert(static_cast<unsigned>(*Group));
311 bool operator()(diag::Group GroupID) const {
312 return Exceptions.contains(static_cast<unsigned>(GroupID)) ? !Default
313 : Default;
317 // Find -W<group> and -Wno-<group> options in ExtraArgs and apply them to Diags.
319 // This is used to handle ExtraArgs in clang-tidy configuration.
320 // We don't use clang's standard handling of this as we want slightly different
321 // behavior (e.g. we want to exclude these from -Wno-error).
322 void applyWarningOptions(llvm::ArrayRef<std::string> ExtraArgs,
323 llvm::function_ref<bool(diag::Group)> EnabledGroups,
324 DiagnosticsEngine &Diags) {
325 for (llvm::StringRef Group : ExtraArgs) {
326 // Only handle args that are of the form -W[no-]<group>.
327 // Other flags are possible but rare and deliberately out of scope.
328 llvm::SmallVector<diag::kind> Members;
329 if (!Group.consume_front("-W") || Group.empty())
330 continue;
331 bool Enable = !Group.consume_front("no-");
332 if (Diags.getDiagnosticIDs()->getDiagnosticsInGroup(
333 diag::Flavor::WarningOrError, Group, Members))
334 continue;
336 // Upgrade (or downgrade) the severity of each diagnostic in the group.
337 // If -Werror is on, newly added warnings will be treated as errors.
338 // We don't want this, so keep track of them to fix afterwards.
339 bool NeedsWerrorExclusion = false;
340 for (diag::kind ID : Members) {
341 if (Enable) {
342 if (Diags.getDiagnosticLevel(ID, SourceLocation()) <
343 DiagnosticsEngine::Warning) {
344 auto Group = DiagnosticIDs::getGroupForDiag(ID);
345 if (!Group || !EnabledGroups(*Group))
346 continue;
347 Diags.setSeverity(ID, diag::Severity::Warning, SourceLocation());
348 if (Diags.getWarningsAsErrors())
349 NeedsWerrorExclusion = true;
351 } else {
352 Diags.setSeverity(ID, diag::Severity::Ignored, SourceLocation());
355 if (NeedsWerrorExclusion) {
356 // FIXME: there's no API to suppress -Werror for single diagnostics.
357 // In some cases with sub-groups, we may end up erroneously
358 // downgrading diagnostics that were -Werror in the compile command.
359 Diags.setDiagnosticGroupWarningAsError(Group, false);
364 std::vector<Diag> getIncludeCleanerDiags(ParsedAST &AST, llvm::StringRef Code) {
365 auto &Cfg = Config::current();
366 if (Cfg.Diagnostics.SuppressAll)
367 return {};
368 bool SuppressMissing =
369 Cfg.Diagnostics.Suppress.contains("missing-includes") ||
370 Cfg.Diagnostics.MissingIncludes == Config::IncludesPolicy::None;
371 bool SuppressUnused =
372 Cfg.Diagnostics.Suppress.contains("unused-includes") ||
373 Cfg.Diagnostics.UnusedIncludes == Config::IncludesPolicy::None;
374 if (SuppressMissing && SuppressUnused)
375 return {};
376 auto Findings = computeIncludeCleanerFindings(AST);
377 if (SuppressMissing)
378 Findings.MissingIncludes.clear();
379 if (SuppressUnused)
380 Findings.UnusedIncludes.clear();
381 return issueIncludeCleanerDiagnostics(AST, Code, Findings,
382 Cfg.Diagnostics.Includes.IgnoreHeader);
385 } // namespace
387 std::optional<ParsedAST>
388 ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
389 std::unique_ptr<clang::CompilerInvocation> CI,
390 llvm::ArrayRef<Diag> CompilerInvocationDiags,
391 std::shared_ptr<const PreambleData> Preamble) {
392 trace::Span Tracer("BuildAST");
393 SPAN_ATTACH(Tracer, "File", Filename);
395 auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
396 if (Preamble && Preamble->StatCache)
397 VFS = Preamble->StatCache->getConsumingFS(std::move(VFS));
399 assert(CI);
400 // Command-line parsing sets DisableFree to true by default, but we don't want
401 // to leak memory in clangd.
402 CI->getFrontendOpts().DisableFree = false;
403 const PrecompiledPreamble *PreamblePCH =
404 Preamble ? &Preamble->Preamble : nullptr;
406 // This is on-by-default in windows to allow parsing SDK headers, but it
407 // breaks many features. Disable it for the main-file (not preamble).
408 CI->getLangOpts()->DelayedTemplateParsing = false;
410 std::vector<std::unique_ptr<FeatureModule::ASTListener>> ASTListeners;
411 if (Inputs.FeatureModules) {
412 for (auto &M : *Inputs.FeatureModules) {
413 if (auto Listener = M.astListeners())
414 ASTListeners.emplace_back(std::move(Listener));
417 StoreDiags ASTDiags;
418 ASTDiags.setDiagCallback(
419 [&ASTListeners](const clang::Diagnostic &D, clangd::Diag &Diag) {
420 llvm::for_each(ASTListeners,
421 [&](const auto &L) { L->sawDiagnostic(D, Diag); });
424 std::optional<PreamblePatch> Patch;
425 // We might use an ignoring diagnostic consumer if they are going to be
426 // dropped later on to not pay for extra latency by processing them.
427 DiagnosticConsumer *DiagConsumer = &ASTDiags;
428 IgnoreDiagnostics DropDiags;
429 if (Preamble) {
430 Patch = PreamblePatch::createFullPatch(Filename, Inputs, *Preamble);
431 Patch->apply(*CI);
433 auto Clang = prepareCompilerInstance(
434 std::move(CI), PreamblePCH,
435 llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, Filename), VFS,
436 *DiagConsumer);
437 if (!Clang) {
438 // The last diagnostic contains information about the reason of this
439 // failure.
440 std::vector<Diag> Diags(ASTDiags.take());
441 elog("Failed to prepare a compiler instance: {0}",
442 !Diags.empty() ? static_cast<DiagBase &>(Diags.back()).Message
443 : "unknown error");
444 return std::nullopt;
446 tidy::ClangTidyOptions ClangTidyOpts;
448 trace::Span Tracer("ClangTidyOpts");
449 ClangTidyOpts = getTidyOptionsForFile(Inputs.ClangTidyProvider, Filename);
450 dlog("ClangTidy configuration for file {0}: {1}", Filename,
451 tidy::configurationAsText(ClangTidyOpts));
453 // If clang-tidy is configured to emit clang warnings, we should too.
455 // Such clang-tidy configuration consists of two parts:
456 // - ExtraArgs: ["-Wfoo"] causes clang to produce the warnings
457 // - Checks: "clang-diagnostic-foo" prevents clang-tidy filtering them out
459 // In clang-tidy, diagnostics are emitted if they pass both checks.
460 // When groups contain subgroups, -Wparent includes the child, but
461 // clang-diagnostic-parent does not.
463 // We *don't* want to change the compile command directly. This can have
464 // too many unexpected effects: breaking the command, interactions with
465 // -- and -Werror, etc. Besides, we've already parsed the command.
466 // Instead we parse the -W<group> flags and handle them directly.
468 // Similarly, we don't want to use Checks to filter clang diagnostics after
469 // they are generated, as this spreads clang-tidy emulation everywhere.
470 // Instead, we just use these to filter which extra diagnostics we enable.
471 auto &Diags = Clang->getDiagnostics();
472 TidyDiagnosticGroups TidyGroups(ClangTidyOpts.Checks ? *ClangTidyOpts.Checks
473 : llvm::StringRef());
474 if (ClangTidyOpts.ExtraArgsBefore)
475 applyWarningOptions(*ClangTidyOpts.ExtraArgsBefore, TidyGroups, Diags);
476 if (ClangTidyOpts.ExtraArgs)
477 applyWarningOptions(*ClangTidyOpts.ExtraArgs, TidyGroups, Diags);
480 auto Action = std::make_unique<ClangdFrontendAction>();
481 const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0];
482 if (!Action->BeginSourceFile(*Clang, MainInput)) {
483 log("BeginSourceFile() failed when building AST for {0}",
484 MainInput.getFile());
485 return std::nullopt;
487 // If we saw an include guard in the preamble section of the main file,
488 // mark the main-file as include-guarded.
489 // This information is part of the HeaderFileInfo but is not loaded from the
490 // preamble as the file's size is part of its identity and may have changed.
491 // (The rest of HeaderFileInfo is not relevant for our purposes).
492 if (Preamble && Preamble->MainIsIncludeGuarded) {
493 const SourceManager &SM = Clang->getSourceManager();
494 const FileEntry *MainFE = SM.getFileEntryForID(SM.getMainFileID());
495 Clang->getPreprocessor().getHeaderSearchInfo().MarkFileIncludeOnce(MainFE);
498 // Set up ClangTidy. Must happen after BeginSourceFile() so ASTContext exists.
499 // Clang-tidy has some limitations to ensure reasonable performance:
500 // - checks don't see all preprocessor events in the preamble
501 // - matchers run only over the main-file top-level decls (and can't see
502 // ancestors outside this scope).
503 // In practice almost all checks work well without modifications.
504 std::vector<std::unique_ptr<tidy::ClangTidyCheck>> CTChecks;
505 ast_matchers::MatchFinder CTFinder;
506 std::optional<tidy::ClangTidyContext> CTContext;
507 // Must outlive FixIncludes.
508 auto BuildDir = VFS->getCurrentWorkingDirectory();
509 std::optional<IncludeFixer> FixIncludes;
510 llvm::DenseMap<diag::kind, DiagnosticsEngine::Level> OverriddenSeverity;
511 // No need to run clang-tidy or IncludeFixerif we are not going to surface
512 // diagnostics.
514 trace::Span Tracer("ClangTidyInit");
515 static const auto *CTFactories = [] {
516 auto *CTFactories = new tidy::ClangTidyCheckFactories;
517 for (const auto &E : tidy::ClangTidyModuleRegistry::entries())
518 E.instantiate()->addCheckFactories(*CTFactories);
519 return CTFactories;
520 }();
521 CTContext.emplace(std::make_unique<tidy::DefaultOptionsProvider>(
522 tidy::ClangTidyGlobalOptions(), ClangTidyOpts));
523 CTContext->setDiagnosticsEngine(&Clang->getDiagnostics());
524 CTContext->setASTContext(&Clang->getASTContext());
525 CTContext->setCurrentFile(Filename);
526 CTContext->setSelfContainedDiags(true);
527 CTChecks = CTFactories->createChecksForLanguage(&*CTContext);
528 Preprocessor *PP = &Clang->getPreprocessor();
529 for (const auto &Check : CTChecks) {
530 Check->registerPPCallbacks(Clang->getSourceManager(), PP, PP);
531 Check->registerMatchers(&CTFinder);
534 // Clang only corrects typos for use of undeclared functions in C if that
535 // use is an error. Include fixer relies on typo correction, so pretend
536 // this is an error. (The actual typo correction is nice too).
537 // We restore the original severity in the level adjuster.
538 // FIXME: It would be better to have a real API for this, but what?
539 for (auto ID : {diag::ext_implicit_function_decl_c99,
540 diag::ext_implicit_lib_function_decl,
541 diag::ext_implicit_lib_function_decl_c99,
542 diag::warn_implicit_function_decl}) {
543 OverriddenSeverity.try_emplace(
544 ID, Clang->getDiagnostics().getDiagnosticLevel(ID, SourceLocation()));
545 Clang->getDiagnostics().setSeverity(ID, diag::Severity::Error,
546 SourceLocation());
549 const Config &Cfg = Config::current();
550 ASTDiags.setLevelAdjuster([&](DiagnosticsEngine::Level DiagLevel,
551 const clang::Diagnostic &Info) {
552 if (Cfg.Diagnostics.SuppressAll ||
553 isBuiltinDiagnosticSuppressed(Info.getID(), Cfg.Diagnostics.Suppress,
554 Clang->getLangOpts()))
555 return DiagnosticsEngine::Ignored;
557 auto It = OverriddenSeverity.find(Info.getID());
558 if (It != OverriddenSeverity.end())
559 DiagLevel = It->second;
561 if (!CTChecks.empty()) {
562 std::string CheckName = CTContext->getCheckName(Info.getID());
563 bool IsClangTidyDiag = !CheckName.empty();
564 if (IsClangTidyDiag) {
565 if (Cfg.Diagnostics.Suppress.contains(CheckName))
566 return DiagnosticsEngine::Ignored;
567 // Check for suppression comment. Skip the check for diagnostics not
568 // in the main file, because we don't want that function to query the
569 // source buffer for preamble files. For the same reason, we ask
570 // shouldSuppressDiagnostic to avoid I/O.
571 // We let suppression comments take precedence over warning-as-error
572 // to match clang-tidy's behaviour.
573 bool IsInsideMainFile =
574 Info.hasSourceManager() &&
575 isInsideMainFile(Info.getLocation(), Info.getSourceManager());
576 SmallVector<tooling::Diagnostic, 1> TidySuppressedErrors;
577 if (IsInsideMainFile && CTContext->shouldSuppressDiagnostic(
578 DiagLevel, Info, TidySuppressedErrors,
579 /*AllowIO=*/false,
580 /*EnableNolintBlocks=*/true)) {
581 // FIXME: should we expose the suppression error (invalid use of
582 // NOLINT comments)?
583 return DiagnosticsEngine::Ignored;
585 if (!CTContext->getOptions().SystemHeaders.value_or(false) &&
586 Info.hasSourceManager() &&
587 Info.getSourceManager().isInSystemMacro(Info.getLocation()))
588 return DiagnosticsEngine::Ignored;
590 // Check for warning-as-error.
591 if (DiagLevel == DiagnosticsEngine::Warning &&
592 CTContext->treatAsError(CheckName)) {
593 return DiagnosticsEngine::Error;
597 return DiagLevel;
600 // Add IncludeFixer which can recover diagnostics caused by missing includes
601 // (e.g. incomplete type) and attach include insertion fixes to diagnostics.
602 if (Inputs.Index && !BuildDir.getError()) {
603 auto Style =
604 getFormatStyleForFile(Filename, Inputs.Contents, *Inputs.TFS);
605 auto Inserter = std::make_shared<IncludeInserter>(
606 Filename, Inputs.Contents, Style, BuildDir.get(),
607 &Clang->getPreprocessor().getHeaderSearchInfo());
608 ArrayRef<Inclusion> MainFileIncludes;
609 if (Preamble) {
610 MainFileIncludes = Preamble->Includes.MainFileIncludes;
611 for (const auto &Inc : Preamble->Includes.MainFileIncludes)
612 Inserter->addExisting(Inc);
614 // FIXME: Consider piping through ASTSignals to fetch this to handle the
615 // case where a header file contains ObjC decls but no #imports.
616 Symbol::IncludeDirective Directive =
617 Inputs.Opts.ImportInsertions
618 ? preferredIncludeDirective(Filename, Clang->getLangOpts(),
619 MainFileIncludes, {})
620 : Symbol::Include;
621 FixIncludes.emplace(Filename, Inserter, *Inputs.Index,
622 /*IndexRequestLimit=*/5, Directive);
623 ASTDiags.contributeFixes([&FixIncludes](DiagnosticsEngine::Level DiagLevl,
624 const clang::Diagnostic &Info) {
625 return FixIncludes->fix(DiagLevl, Info);
627 Clang->setExternalSemaSource(FixIncludes->unresolvedNameRecorder());
631 IncludeStructure Includes;
632 // If we are using a preamble, copy existing includes.
633 if (Preamble) {
634 Includes = Preamble->Includes;
635 Includes.MainFileIncludes = Patch->preambleIncludes();
636 // Replay the preamble includes so that clang-tidy checks can see them.
637 ReplayPreamble::attach(Patch->preambleIncludes(), *Clang,
638 Patch->modifiedBounds());
640 // Important: collectIncludeStructure is registered *after* ReplayPreamble!
641 // Otherwise we would collect the replayed includes again...
642 // (We can't *just* use the replayed includes, they don't have Resolved path).
643 Includes.collect(*Clang);
644 // Copy over the macros in the preamble region of the main file, and combine
645 // with non-preamble macros below.
646 MainFileMacros Macros;
647 std::vector<PragmaMark> Marks;
648 if (Preamble) {
649 Macros = Patch->mainFileMacros();
650 Marks = Patch->marks();
652 auto &PP = Clang->getPreprocessor();
653 PP.addPPCallbacks(std::make_unique<CollectMainFileMacros>(PP, Macros));
655 PP.addPPCallbacks(
656 collectPragmaMarksCallback(Clang->getSourceManager(), Marks));
658 // Copy over the includes from the preamble, then combine with the
659 // non-preamble includes below.
660 CanonicalIncludes CanonIncludes;
661 if (Preamble)
662 CanonIncludes = *Preamble->CanonIncludes;
663 else
664 CanonIncludes.addSystemHeadersMapping(Clang->getLangOpts());
665 std::unique_ptr<CommentHandler> IWYUHandler =
666 collectIWYUHeaderMaps(&CanonIncludes);
667 PP.addCommentHandler(IWYUHandler.get());
669 // Collect tokens of the main file.
670 syntax::TokenCollector CollectTokens(PP);
672 // To remain consistent with preamble builds, these callbacks must be called
673 // exactly here, after preprocessor is initialized and BeginSourceFile() was
674 // called already.
675 for (const auto &L : ASTListeners)
676 L->beforeExecute(*Clang);
678 if (llvm::Error Err = Action->Execute())
679 log("Execute() failed when building AST for {0}: {1}", MainInput.getFile(),
680 toString(std::move(Err)));
682 // We have to consume the tokens before running clang-tidy to avoid collecting
683 // tokens from running the preprocessor inside the checks (only
684 // modernize-use-trailing-return-type does that today).
685 syntax::TokenBuffer Tokens = std::move(CollectTokens).consume();
686 // Makes SelectionTree build much faster.
687 Tokens.indexExpandedTokens();
688 std::vector<Decl *> ParsedDecls = Action->takeTopLevelDecls();
689 // AST traversals should exclude the preamble, to avoid performance cliffs.
690 Clang->getASTContext().setTraversalScope(ParsedDecls);
691 if (!CTChecks.empty()) {
692 // Run the AST-dependent part of the clang-tidy checks.
693 // (The preprocessor part ran already, via PPCallbacks).
694 trace::Span Tracer("ClangTidyMatch");
695 CTFinder.matchAST(Clang->getASTContext());
698 // XXX: This is messy: clang-tidy checks flush some diagnostics at EOF.
699 // However Action->EndSourceFile() would destroy the ASTContext!
700 // So just inform the preprocessor of EOF, while keeping everything alive.
701 PP.EndSourceFile();
702 // UnitDiagsConsumer is local, we can not store it in CompilerInstance that
703 // has a longer lifetime.
704 Clang->getDiagnostics().setClient(new IgnoreDiagnostics);
705 // CompilerInstance won't run this callback, do it directly.
706 ASTDiags.EndSourceFile();
708 std::vector<Diag> Diags = CompilerInvocationDiags;
709 // FIXME: Also skip generation of diagnostics altogether to speed up ast
710 // builds when we are patching a stale preamble.
711 // Add diagnostics from the preamble, if any.
712 if (Preamble)
713 llvm::append_range(Diags, Patch->patchedDiags());
714 // Finally, add diagnostics coming from the AST.
716 std::vector<Diag> D = ASTDiags.take(&*CTContext);
717 Diags.insert(Diags.end(), D.begin(), D.end());
719 ParsedAST Result(Filename, Inputs.Version, std::move(Preamble),
720 std::move(Clang), std::move(Action), std::move(Tokens),
721 std::move(Macros), std::move(Marks), std::move(ParsedDecls),
722 std::move(Diags), std::move(Includes),
723 std::move(CanonIncludes));
724 llvm::move(getIncludeCleanerDiags(Result, Inputs.Contents),
725 std::back_inserter(Result.Diags));
726 return std::move(Result);
729 ParsedAST::ParsedAST(ParsedAST &&Other) = default;
731 ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default;
733 ParsedAST::~ParsedAST() {
734 if (Action) {
735 // We already notified the PP of end-of-file earlier, so detach it first.
736 // We must keep it alive until after EndSourceFile(), Sema relies on this.
737 auto PP = Clang->getPreprocessorPtr(); // Keep PP alive for now.
738 Clang->setPreprocessor(nullptr); // Detach so we don't send EOF again.
739 Action->EndSourceFile(); // Destroy ASTContext and Sema.
740 // Now Sema is gone, it's safe for PP to go out of scope.
744 ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); }
746 const ASTContext &ParsedAST::getASTContext() const {
747 return Clang->getASTContext();
750 Sema &ParsedAST::getSema() { return Clang->getSema(); }
752 Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); }
754 std::shared_ptr<Preprocessor> ParsedAST::getPreprocessorPtr() {
755 return Clang->getPreprocessorPtr();
758 const Preprocessor &ParsedAST::getPreprocessor() const {
759 return Clang->getPreprocessor();
762 llvm::ArrayRef<Decl *> ParsedAST::getLocalTopLevelDecls() {
763 return LocalTopLevelDecls;
766 llvm::ArrayRef<const Decl *> ParsedAST::getLocalTopLevelDecls() const {
767 return LocalTopLevelDecls;
770 const MainFileMacros &ParsedAST::getMacros() const { return Macros; }
771 const std::vector<PragmaMark> &ParsedAST::getMarks() const { return Marks; }
773 std::size_t ParsedAST::getUsedBytes() const {
774 auto &AST = getASTContext();
775 // FIXME(ibiryukov): we do not account for the dynamically allocated part of
776 // Message and Fixes inside each diagnostic.
777 std::size_t Total =
778 clangd::getUsedBytes(LocalTopLevelDecls) + clangd::getUsedBytes(Diags);
780 // FIXME: the rest of the function is almost a direct copy-paste from
781 // libclang's clang_getCXTUResourceUsage. We could share the implementation.
783 // Sum up various allocators inside the ast context and the preprocessor.
784 Total += AST.getASTAllocatedMemory();
785 Total += AST.getSideTableAllocatedMemory();
786 Total += AST.Idents.getAllocator().getTotalMemory();
787 Total += AST.Selectors.getTotalMemory();
789 Total += AST.getSourceManager().getContentCacheSize();
790 Total += AST.getSourceManager().getDataStructureSizes();
791 Total += AST.getSourceManager().getMemoryBufferSizes().malloc_bytes;
793 if (ExternalASTSource *Ext = AST.getExternalSource())
794 Total += Ext->getMemoryBufferSizes().malloc_bytes;
796 const Preprocessor &PP = getPreprocessor();
797 Total += PP.getTotalMemory();
798 if (PreprocessingRecord *PRec = PP.getPreprocessingRecord())
799 Total += PRec->getTotalMemory();
800 Total += PP.getHeaderSearchInfo().getTotalMemory();
802 return Total;
805 const IncludeStructure &ParsedAST::getIncludeStructure() const {
806 return Includes;
809 const CanonicalIncludes &ParsedAST::getCanonicalIncludes() const {
810 return CanonIncludes;
813 ParsedAST::ParsedAST(PathRef TUPath, llvm::StringRef Version,
814 std::shared_ptr<const PreambleData> Preamble,
815 std::unique_ptr<CompilerInstance> Clang,
816 std::unique_ptr<FrontendAction> Action,
817 syntax::TokenBuffer Tokens, MainFileMacros Macros,
818 std::vector<PragmaMark> Marks,
819 std::vector<Decl *> LocalTopLevelDecls,
820 std::vector<Diag> Diags, IncludeStructure Includes,
821 CanonicalIncludes CanonIncludes)
822 : TUPath(TUPath), Version(Version), Preamble(std::move(Preamble)),
823 Clang(std::move(Clang)), Action(std::move(Action)),
824 Tokens(std::move(Tokens)), Macros(std::move(Macros)),
825 Marks(std::move(Marks)), Diags(std::move(Diags)),
826 LocalTopLevelDecls(std::move(LocalTopLevelDecls)),
827 Includes(std::move(Includes)), CanonIncludes(std::move(CanonIncludes)) {
828 Resolver = std::make_unique<HeuristicResolver>(getASTContext());
829 assert(this->Clang);
830 assert(this->Action);
833 const include_cleaner::PragmaIncludes *ParsedAST::getPragmaIncludes() const {
834 if (!Preamble)
835 return nullptr;
836 return &Preamble->Pragmas;
839 std::optional<llvm::StringRef> ParsedAST::preambleVersion() const {
840 if (!Preamble)
841 return std::nullopt;
842 return llvm::StringRef(Preamble->Version);
845 llvm::ArrayRef<Diag> ParsedAST::getDiagnostics() const { return Diags; }
846 } // namespace clangd
847 } // namespace clang