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