clang-tools 20.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/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) {
91 return Vec.capacity() * sizeof(T);
92}
93
94class DeclTrackingASTConsumer : public ASTConsumer {
95public:
96 DeclTrackingASTConsumer(std::vector<Decl *> &TopLevelDecls)
97 : TopLevelDecls(TopLevelDecls) {}
98
99 bool HandleTopLevelDecl(DeclGroupRef DG) override {
100 for (Decl *D : DG) {
101 auto &SM = D->getASTContext().getSourceManager();
102 if (!isInsideMainFile(D->getLocation(), SM))
103 continue;
104 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
106 continue;
107
108 // ObjCMethodDecl are not actually top-level decls.
109 if (isa<ObjCMethodDecl>(D))
110 continue;
111
112 TopLevelDecls.push_back(D);
113 }
114 return true;
115 }
116
117private:
118 std::vector<Decl *> &TopLevelDecls;
119};
120
121class ClangdFrontendAction : public SyntaxOnlyAction {
122public:
123 std::vector<Decl *> takeTopLevelDecls() { return std::move(TopLevelDecls); }
124
125protected:
126 std::unique_ptr<ASTConsumer>
127 CreateASTConsumer(CompilerInstance &CI, llvm::StringRef InFile) override {
128 return std::make_unique<DeclTrackingASTConsumer>(/*ref*/ TopLevelDecls);
129 }
130
131private:
132 std::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.
146 static void attach(std::vector<Inclusion> Includes, CompilerInstance &Clang,
147 const PreambleBounds &PB) {
148 auto &PP = Clang.getPreprocessor();
149 auto *ExistingCallbacks = PP.getPPCallbacks();
150 // No need to replay events if nobody is listening.
151 if (!ExistingCallbacks)
152 return;
153 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(new ReplayPreamble(
154 std::move(Includes), ExistingCallbacks, Clang.getSourceManager(), PP,
155 Clang.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.
158 assert(PP.getPPCallbacks() != ExistingCallbacks &&
159 "Expected chaining implementation");
160 }
161
162private:
163 ReplayPreamble(std::vector<Inclusion> Includes, PPCallbacks *Delegate,
164 const SourceManager &SM, Preprocessor &PP,
165 const 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.
169 MainFileTokens = syntax::tokenize(
170 syntax::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.
188 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
189 SrcMgr::CharacteristicKind Kind, FileID PrevFID) override {
190 // It'd be nice if there was a better way to identify built-in headers...
191 if (Reason == FileChangeReason::ExitFile &&
192 SM.getBufferOrFake(PrevFID).getBufferIdentifier() == "<built-in>")
193 replay();
194 }
195
196 void replay() {
197 for (const auto &Inc : Includes) {
198 OptionalFileEntryRef File;
199 if (Inc.Resolved != "")
200 File = expectedToOptional(SM.getFileManager().getFileRef(Inc.Resolved));
201
202 // Re-lex the #include directive to find its interesting parts.
203 auto HashLoc = SM.getComposedLoc(SM.getMainFileID(), Inc.HashOffset);
204 auto HashTok = llvm::partition_point(MainFileTokens,
205 [&HashLoc](const syntax::Token &T) {
206 return T.location() < HashLoc;
207 });
208 assert(HashTok != MainFileTokens.end() && HashTok->kind() == tok::hash);
209
210 auto IncludeTok = std::next(HashTok);
211 assert(IncludeTok != MainFileTokens.end());
212
213 auto FileTok = std::next(IncludeTok);
214 assert(FileTok != MainFileTokens.end());
215
216 // Create a fake import/include token, none of the callers seem to care
217 // about clang::Token::Flags.
218 Token SynthesizedIncludeTok;
219 SynthesizedIncludeTok.startToken();
220 SynthesizedIncludeTok.setLocation(IncludeTok->location());
221 SynthesizedIncludeTok.setLength(IncludeTok->length());
222 SynthesizedIncludeTok.setKind(tok::raw_identifier);
223 SynthesizedIncludeTok.setRawIdentifierData(IncludeTok->text(SM).data());
224 PP.LookUpIdentifierInfo(SynthesizedIncludeTok);
225
226 // Same here, create a fake one for Filename, including angles or quotes.
227 Token SynthesizedFilenameTok;
228 SynthesizedFilenameTok.startToken();
229 SynthesizedFilenameTok.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.
234 SynthesizedFilenameTok.setLength(Inc.Written.length());
235 SynthesizedFilenameTok.setKind(tok::header_name);
236 SynthesizedFilenameTok.setLiteralData(Inc.Written.data());
237
238 llvm::StringRef WrittenFilename =
239 llvm::StringRef(Inc.Written).drop_front().drop_back();
240 Delegate->InclusionDirective(
241 HashTok->location(), SynthesizedIncludeTok, WrittenFilename,
242 Inc.Written.front() == '<',
243 syntax::FileRange(SM, SynthesizedFilenameTok.getLocation(),
244 SynthesizedFilenameTok.getEndLoc())
245 .toCharRange(SM),
246 File, "SearchPath", "RelPath",
247 /*SuggestedModule=*/nullptr, /*ModuleImported=*/false, Inc.FileKind);
248 if (File)
249 Delegate->FileSkipped(*File, SynthesizedFilenameTok, Inc.FileKind);
250 }
251 }
252
253 const std::vector<Inclusion> Includes;
254 PPCallbacks *Delegate;
255 const SourceManager &SM;
256 Preprocessor &PP;
257 std::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-*.
271 bool 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.
274 llvm::DenseSet<unsigned> Exceptions;
275
276public:
277 TidyDiagnosticGroups(llvm::StringRef Checks) {
278 constexpr llvm::StringLiteral CDPrefix = "clang-diagnostic-";
279
280 llvm::StringRef Check;
281 while (!Checks.empty()) {
282 std::tie(Check, Checks) = Checks.split(',');
283 if (Check.empty())
284 continue;
285
286 bool Enable = !Check.consume_front("-");
287 bool Glob = Check.consume_back("*");
288 if (Glob) {
289 // Is this clang-diagnostic-*, or *, or so?
290 // (We ignore all other types of globs).
291 if (CDPrefix.starts_with(Check)) {
292 Default = Enable;
293 Exceptions.clear();
294 }
295 continue;
296 }
297
298 // In "*,clang-diagnostic-foo", the latter is a no-op.
299 if (Default == Enable)
300 continue;
301 // The only non-glob entries we care about are clang-diagnostic-foo.
302 if (!Check.consume_front(CDPrefix))
303 continue;
304
305 if (auto Group = DiagnosticIDs::getGroupForWarningOption(Check))
306 Exceptions.insert(static_cast<unsigned>(*Group));
307 }
308 }
309
310 bool operator()(diag::Group GroupID) const {
311 return 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,
322 llvm::function_ref<bool(diag::Group)> EnabledGroups,
323 DiagnosticsEngine &Diags) {
324 for (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.
327 llvm::SmallVector<diag::kind> Members;
328 if (!Group.consume_front("-W") || Group.empty())
329 continue;
330 bool Enable = !Group.consume_front("no-");
331 if (Diags.getDiagnosticIDs()->getDiagnosticsInGroup(
332 diag::Flavor::WarningOrError, Group, Members))
333 continue;
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.
338 bool NeedsWerrorExclusion = false;
339 for (diag::kind ID : Members) {
340 if (Enable) {
341 if (Diags.getDiagnosticLevel(ID, SourceLocation()) <
342 DiagnosticsEngine::Warning) {
343 auto Group = DiagnosticIDs::getGroupForDiag(ID);
344 if (!Group || !EnabledGroups(*Group))
345 continue;
346 Diags.setSeverity(ID, diag::Severity::Warning, SourceLocation());
347 if (Diags.getWarningsAsErrors())
348 NeedsWerrorExclusion = true;
349 }
350 } else {
351 Diags.setSeverity(ID, diag::Severity::Ignored, SourceLocation());
352 }
353 }
354 if (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.
358 Diags.setDiagnosticGroupWarningAsError(Group, false);
359 }
360 }
361}
362
363std::vector<Diag> getIncludeCleanerDiags(ParsedAST &AST, llvm::StringRef Code,
364 const ThreadsafeFS &TFS) {
365 auto &Cfg = Config::current();
366 if (Cfg.Diagnostics.SuppressAll)
367 return {};
368 bool SuppressMissing =
369 Cfg.Diagnostics.Suppress.contains("missing-includes") ||
371 bool SuppressUnused =
372 Cfg.Diagnostics.Suppress.contains("unused-includes") ||
374 if (SuppressMissing && SuppressUnused)
375 return {};
376 auto Findings = computeIncludeCleanerFindings(
378 if (SuppressMissing)
379 Findings.MissingIncludes.clear();
380 if (SuppressUnused)
381 Findings.UnusedIncludes.clear();
382 return issueIncludeCleanerDiagnostics(AST, Code, Findings, TFS,
384}
385
386tidy::ClangTidyCheckFactories
387filterFastTidyChecks(const tidy::ClangTidyCheckFactories &All,
389 if (Policy == Config::FastCheckPolicy::None)
390 return All;
391 bool AllowUnknown = Policy == Config::FastCheckPolicy::Loose;
392 tidy::ClangTidyCheckFactories Fast;
393 for (const auto &Factory : All) {
394 if (isFastTidyCheck(Factory.getKey()).value_or(AllowUnknown))
395 Fast.registerCheckFactory(Factory.first(), Factory.second);
396 }
397 return Fast;
398}
399
400} // namespace
401
402std::optional<ParsedAST>
403ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
404 std::unique_ptr<clang::CompilerInvocation> CI,
405 llvm::ArrayRef<Diag> CompilerInvocationDiags,
406 std::shared_ptr<const PreambleData> Preamble) {
407 trace::Span Tracer("BuildAST");
408 SPAN_ATTACH(Tracer, "File", Filename);
409 const Config &Cfg = Config::current();
410
411 auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
412 if (Preamble && Preamble->StatCache)
413 VFS = Preamble->StatCache->getConsumingFS(std::move(VFS));
414
415 assert(CI);
416
417 if (CI->getFrontendOpts().Inputs.size() > 0) {
418 auto Lang = CI->getFrontendOpts().Inputs[0].getKind().getLanguage();
419 if (Lang == Language::Asm || Lang == Language::LLVM_IR) {
420 elog("Clangd does not support assembly or IR source files");
421 return 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.
427 CI->getFrontendOpts().DisableFree = false;
428 const PrecompiledPreamble *PreamblePCH =
429 Preamble ? &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).
433 CI->getLangOpts().DelayedTemplateParsing = false;
434
435 std::vector<std::unique_ptr<FeatureModule::ASTListener>> ASTListeners;
436 if (Inputs.FeatureModules) {
437 for (auto &M : *Inputs.FeatureModules) {
438 if (auto Listener = M.astListeners())
439 ASTListeners.emplace_back(std::move(Listener));
440 }
441 }
442 StoreDiags ASTDiags;
443 ASTDiags.setDiagCallback(
444 [&ASTListeners](const clang::Diagnostic &D, clangd::Diag &Diag) {
445 for (const auto &L : ASTListeners)
446 L->sawDiagnostic(D, Diag);
447 });
448
449 // Adjust header search options to load the built module files recorded
450 // in RequiredModules.
451 if (Preamble && Preamble->RequiredModules)
452 Preamble->RequiredModules->adjustHeaderSearchOptions(
453 CI->getHeaderSearchOpts());
454
455 std::optional<PreamblePatch> Patch;
456 // We might use an ignoring diagnostic consumer if they are going to be
457 // dropped later on to not pay for extra latency by processing them.
458 DiagnosticConsumer *DiagConsumer = &ASTDiags;
459 IgnoreDiagnostics DropDiags;
460 if (Preamble) {
462 Patch->apply(*CI);
463 }
465 std::move(CI), PreamblePCH,
466 llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, Filename), VFS,
467 *DiagConsumer);
468
469 if (!Clang) {
470 // The last diagnostic contains information about the reason of this
471 // failure.
472 std::vector<Diag> Diags(ASTDiags.take());
473 elog("Failed to prepare a compiler instance: {0}",
474 !Diags.empty() ? static_cast<DiagBase &>(Diags.back()).Message
475 : "unknown error");
476 return std::nullopt;
477 }
478 tidy::ClangTidyOptions ClangTidyOpts;
479 {
480 trace::Span Tracer("ClangTidyOpts");
481 ClangTidyOpts = getTidyOptionsForFile(Inputs.ClangTidyProvider, Filename);
482 dlog("ClangTidy configuration for file {0}: {1}", Filename,
483 tidy::configurationAsText(ClangTidyOpts));
484
485 // If clang-tidy is configured to emit clang warnings, we should too.
486 //
487 // Such clang-tidy configuration consists of two parts:
488 // - ExtraArgs: ["-Wfoo"] causes clang to produce the warnings
489 // - Checks: "clang-diagnostic-foo" prevents clang-tidy filtering them out
490 //
491 // In clang-tidy, diagnostics are emitted if they pass both checks.
492 // When groups contain subgroups, -Wparent includes the child, but
493 // clang-diagnostic-parent does not.
494 //
495 // We *don't* want to change the compile command directly. This can have
496 // too many unexpected effects: breaking the command, interactions with
497 // -- and -Werror, etc. Besides, we've already parsed the command.
498 // Instead we parse the -W<group> flags and handle them directly.
499 //
500 // Similarly, we don't want to use Checks to filter clang diagnostics after
501 // they are generated, as this spreads clang-tidy emulation everywhere.
502 // Instead, we just use these to filter which extra diagnostics we enable.
503 auto &Diags = Clang->getDiagnostics();
504 TidyDiagnosticGroups TidyGroups(ClangTidyOpts.Checks ? *ClangTidyOpts.Checks
505 : llvm::StringRef());
506 if (ClangTidyOpts.ExtraArgsBefore)
507 applyWarningOptions(*ClangTidyOpts.ExtraArgsBefore, TidyGroups, Diags);
508 if (ClangTidyOpts.ExtraArgs)
509 applyWarningOptions(*ClangTidyOpts.ExtraArgs, TidyGroups, Diags);
510 }
511
512 auto Action = std::make_unique<ClangdFrontendAction>();
513 const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0];
514 if (!Action->BeginSourceFile(*Clang, MainInput)) {
515 elog("BeginSourceFile() failed when building AST for {0}",
516 MainInput.getFile());
517 return std::nullopt;
518 }
519 // If we saw an include guard in the preamble section of the main file,
520 // mark the main-file as include-guarded.
521 // This information is part of the HeaderFileInfo but is not loaded from the
522 // preamble as the file's size is part of its identity and may have changed.
523 // (The rest of HeaderFileInfo is not relevant for our purposes).
524 if (Preamble && Preamble->MainIsIncludeGuarded) {
525 const SourceManager &SM = Clang->getSourceManager();
526 OptionalFileEntryRef MainFE = SM.getFileEntryRefForID(SM.getMainFileID());
527 Clang->getPreprocessor().getHeaderSearchInfo().MarkFileIncludeOnce(*MainFE);
528 }
529
530 // Set up ClangTidy. Must happen after BeginSourceFile() so ASTContext exists.
531 // Clang-tidy has some limitations to ensure reasonable performance:
532 // - checks don't see all preprocessor events in the preamble
533 // - matchers run only over the main-file top-level decls (and can't see
534 // ancestors outside this scope).
535 // In practice almost all checks work well without modifications.
536 std::vector<std::unique_ptr<tidy::ClangTidyCheck>> CTChecks;
537 ast_matchers::MatchFinder CTFinder;
538 std::optional<tidy::ClangTidyContext> CTContext;
539 // Must outlive FixIncludes.
540 auto BuildDir = VFS->getCurrentWorkingDirectory();
541 std::optional<IncludeFixer> FixIncludes;
542 llvm::DenseMap<diag::kind, DiagnosticsEngine::Level> OverriddenSeverity;
543 // No need to run clang-tidy or IncludeFixerif we are not going to surface
544 // diagnostics.
545 {
546 trace::Span Tracer("ClangTidyInit");
547 static const auto *AllCTFactories = [] {
548 auto *CTFactories = new tidy::ClangTidyCheckFactories;
549 for (const auto &E : tidy::ClangTidyModuleRegistry::entries())
550 E.instantiate()->addCheckFactories(*CTFactories);
551 return CTFactories;
552 }();
553 tidy::ClangTidyCheckFactories FastFactories = filterFastTidyChecks(
554 *AllCTFactories, Cfg.Diagnostics.ClangTidy.FastCheckFilter);
555 CTContext.emplace(std::make_unique<tidy::DefaultOptionsProvider>(
556 tidy::ClangTidyGlobalOptions(), ClangTidyOpts));
557 CTContext->setDiagnosticsEngine(&Clang->getDiagnostics());
558 CTContext->setASTContext(&Clang->getASTContext());
559 CTContext->setCurrentFile(Filename);
560 CTContext->setSelfContainedDiags(true);
561 CTChecks = FastFactories.createChecksForLanguage(&*CTContext);
562 Preprocessor *PP = &Clang->getPreprocessor();
563 for (const auto &Check : CTChecks) {
564 Check->registerPPCallbacks(Clang->getSourceManager(), PP, PP);
565 Check->registerMatchers(&CTFinder);
566 }
567
568 // Clang only corrects typos for use of undeclared functions in C if that
569 // use is an error. Include fixer relies on typo correction, so pretend
570 // this is an error. (The actual typo correction is nice too).
571 // We restore the original severity in the level adjuster.
572 // FIXME: It would be better to have a real API for this, but what?
573 for (auto ID : {diag::ext_implicit_function_decl_c99,
574 diag::ext_implicit_lib_function_decl,
575 diag::ext_implicit_lib_function_decl_c99,
576 diag::warn_implicit_function_decl}) {
577 OverriddenSeverity.try_emplace(
578 ID, Clang->getDiagnostics().getDiagnosticLevel(ID, SourceLocation()));
579 Clang->getDiagnostics().setSeverity(ID, diag::Severity::Error,
580 SourceLocation());
581 }
582
583 ASTDiags.setLevelAdjuster([&](DiagnosticsEngine::Level DiagLevel,
584 const clang::Diagnostic &Info) {
585 if (Cfg.Diagnostics.SuppressAll ||
586 isBuiltinDiagnosticSuppressed(Info.getID(), Cfg.Diagnostics.Suppress,
587 Clang->getLangOpts()))
588 return DiagnosticsEngine::Ignored;
589
590 auto It = OverriddenSeverity.find(Info.getID());
591 if (It != OverriddenSeverity.end())
592 DiagLevel = It->second;
593
594 if (!CTChecks.empty()) {
595 std::string CheckName = CTContext->getCheckName(Info.getID());
596 bool IsClangTidyDiag = !CheckName.empty();
597 if (IsClangTidyDiag) {
598 if (Cfg.Diagnostics.Suppress.contains(CheckName))
599 return DiagnosticsEngine::Ignored;
600 // Check for suppression comment. Skip the check for diagnostics not
601 // in the main file, because we don't want that function to query the
602 // source buffer for preamble files. For the same reason, we ask
603 // shouldSuppressDiagnostic to avoid I/O.
604 // We let suppression comments take precedence over warning-as-error
605 // to match clang-tidy's behaviour.
606 bool IsInsideMainFile =
607 Info.hasSourceManager() &&
608 isInsideMainFile(Info.getLocation(), Info.getSourceManager());
609 SmallVector<tooling::Diagnostic, 1> TidySuppressedErrors;
610 if (IsInsideMainFile && CTContext->shouldSuppressDiagnostic(
611 DiagLevel, Info, TidySuppressedErrors,
612 /*AllowIO=*/false,
613 /*EnableNolintBlocks=*/true)) {
614 // FIXME: should we expose the suppression error (invalid use of
615 // NOLINT comments)?
616 return DiagnosticsEngine::Ignored;
617 }
618 if (!CTContext->getOptions().SystemHeaders.value_or(false) &&
619 Info.hasSourceManager() &&
620 Info.getSourceManager().isInSystemMacro(Info.getLocation()))
621 return DiagnosticsEngine::Ignored;
622
623 // Check for warning-as-error.
624 if (DiagLevel == DiagnosticsEngine::Warning &&
625 CTContext->treatAsError(CheckName)) {
626 return DiagnosticsEngine::Error;
627 }
628 }
629 }
630 return DiagLevel;
631 });
632
633 // Add IncludeFixer which can recover diagnostics caused by missing includes
634 // (e.g. incomplete type) and attach include insertion fixes to diagnostics.
635 if (Inputs.Index && !BuildDir.getError()) {
636 auto Style =
637 getFormatStyleForFile(Filename, Inputs.Contents, *Inputs.TFS, false);
638 auto Inserter = std::make_shared<IncludeInserter>(
639 Filename, Inputs.Contents, Style, BuildDir.get(),
640 &Clang->getPreprocessor().getHeaderSearchInfo());
641 ArrayRef<Inclusion> MainFileIncludes;
642 if (Preamble) {
643 MainFileIncludes = Preamble->Includes.MainFileIncludes;
644 for (const auto &Inc : Preamble->Includes.MainFileIncludes)
645 Inserter->addExisting(Inc);
646 }
647 // FIXME: Consider piping through ASTSignals to fetch this to handle the
648 // case where a header file contains ObjC decls but no #imports.
650 Inputs.Opts.ImportInsertions
651 ? preferredIncludeDirective(Filename, Clang->getLangOpts(),
652 MainFileIncludes, {})
654 FixIncludes.emplace(Filename, Inserter, *Inputs.Index,
655 /*IndexRequestLimit=*/5, Directive);
656 ASTDiags.contributeFixes([&FixIncludes](DiagnosticsEngine::Level DiagLevl,
657 const clang::Diagnostic &Info) {
658 return FixIncludes->fix(DiagLevl, Info);
659 });
660 Clang->setExternalSemaSource(FixIncludes->unresolvedNameRecorder());
661 }
662 }
663
664 IncludeStructure Includes;
665 include_cleaner::PragmaIncludes PI;
666 // If we are using a preamble, copy existing includes.
667 if (Preamble) {
668 Includes = Preamble->Includes;
669 Includes.MainFileIncludes = Patch->preambleIncludes();
670 // Replay the preamble includes so that clang-tidy checks can see them.
671 ReplayPreamble::attach(Patch->preambleIncludes(), *Clang,
672 Patch->modifiedBounds());
673 PI = *Preamble->Pragmas;
674 }
675 // Important: collectIncludeStructure is registered *after* ReplayPreamble!
676 // Otherwise we would collect the replayed includes again...
677 // (We can't *just* use the replayed includes, they don't have Resolved path).
678 Includes.collect(*Clang);
679 // Same for pragma-includes, we're already inheriting preamble includes, so we
680 // should only receive callbacks for non-preamble mainfile includes.
681 PI.record(*Clang);
682 // Copy over the macros in the preamble region of the main file, and combine
683 // with non-preamble macros below.
684 MainFileMacros Macros;
685 std::vector<PragmaMark> Marks;
686 if (Preamble) {
687 Macros = Patch->mainFileMacros();
688 Marks = Patch->marks();
689 }
690 auto &PP = Clang->getPreprocessor();
691 PP.addPPCallbacks(std::make_unique<CollectMainFileMacros>(PP, Macros));
692
693 PP.addPPCallbacks(
694 collectPragmaMarksCallback(Clang->getSourceManager(), Marks));
695
696 // FIXME: Attach a comment handler to take care of
697 // keep/export/no_include etc. IWYU pragmas.
698
699 // Collect tokens of the main file.
700 syntax::TokenCollector CollectTokens(PP);
701
702 // To remain consistent with preamble builds, these callbacks must be called
703 // exactly here, after preprocessor is initialized and BeginSourceFile() was
704 // called already.
705 for (const auto &L : ASTListeners)
706 L->beforeExecute(*Clang);
707
708 if (llvm::Error Err = Action->Execute())
709 log("Execute() failed when building AST for {0}: {1}", MainInput.getFile(),
710 toString(std::move(Err)));
711
712 // We have to consume the tokens before running clang-tidy to avoid collecting
713 // tokens from running the preprocessor inside the checks (only
714 // modernize-use-trailing-return-type does that today).
715 syntax::TokenBuffer Tokens = std::move(CollectTokens).consume();
716 // Makes SelectionTree build much faster.
717 Tokens.indexExpandedTokens();
718 std::vector<Decl *> ParsedDecls = Action->takeTopLevelDecls();
719 // AST traversals should exclude the preamble, to avoid performance cliffs.
720 Clang->getASTContext().setTraversalScope(ParsedDecls);
721 if (!CTChecks.empty()) {
722 // Run the AST-dependent part of the clang-tidy checks.
723 // (The preprocessor part ran already, via PPCallbacks).
724 trace::Span Tracer("ClangTidyMatch");
725 CTFinder.matchAST(Clang->getASTContext());
726 }
727
728 // XXX: This is messy: clang-tidy checks flush some diagnostics at EOF.
729 // However Action->EndSourceFile() would destroy the ASTContext!
730 // So just inform the preprocessor of EOF, while keeping everything alive.
731 PP.EndSourceFile();
732 // UnitDiagsConsumer is local, we can not store it in CompilerInstance that
733 // has a longer lifetime.
734 Clang->getDiagnostics().setClient(new IgnoreDiagnostics);
735 // CompilerInstance won't run this callback, do it directly.
736 ASTDiags.EndSourceFile();
737
738 std::vector<Diag> Diags = CompilerInvocationDiags;
739 // FIXME: Also skip generation of diagnostics altogether to speed up ast
740 // builds when we are patching a stale preamble.
741 // Add diagnostics from the preamble, if any.
742 if (Preamble)
743 llvm::append_range(Diags, Patch->patchedDiags());
744 // Finally, add diagnostics coming from the AST.
745 {
746 std::vector<Diag> D = ASTDiags.take(&*CTContext);
747 Diags.insert(Diags.end(), D.begin(), D.end());
748 }
749 ParsedAST Result(Filename, Inputs.Version, std::move(Preamble),
750 std::move(Clang), std::move(Action), std::move(Tokens),
751 std::move(Macros), std::move(Marks), std::move(ParsedDecls),
752 std::move(Diags), std::move(Includes), std::move(PI));
753 llvm::move(getIncludeCleanerDiags(Result, Inputs.Contents, *Inputs.TFS),
754 std::back_inserter(Result.Diags));
755 return std::move(Result);
756}
757
758ParsedAST::ParsedAST(ParsedAST &&Other) = default;
759
760ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default;
761
762ParsedAST::~ParsedAST() {
763 if (Action) {
764 // We already notified the PP of end-of-file earlier, so detach it first.
765 // We must keep it alive until after EndSourceFile(), Sema relies on this.
766 auto PP = Clang->getPreprocessorPtr(); // Keep PP alive for now.
767 Clang->setPreprocessor(nullptr); // Detach so we don't send EOF again.
768 Action->EndSourceFile(); // Destroy ASTContext and Sema.
769 // Now Sema is gone, it's safe for PP to go out of scope.
770 }
771}
772
773ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); }
774
775const ASTContext &ParsedAST::getASTContext() const {
776 return Clang->getASTContext();
777}
778
779Sema &ParsedAST::getSema() { return Clang->getSema(); }
780
781Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); }
782
783std::shared_ptr<Preprocessor> ParsedAST::getPreprocessorPtr() {
784 return Clang->getPreprocessorPtr();
785}
786
787const Preprocessor &ParsedAST::getPreprocessor() const {
788 return Clang->getPreprocessor();
789}
790
791llvm::ArrayRef<Decl *> ParsedAST::getLocalTopLevelDecls() {
792 return LocalTopLevelDecls;
793}
794
795llvm::ArrayRef<const Decl *> ParsedAST::getLocalTopLevelDecls() const {
796 return LocalTopLevelDecls;
797}
798
799const MainFileMacros &ParsedAST::getMacros() const { return Macros; }
800const std::vector<PragmaMark> &ParsedAST::getMarks() const { return Marks; }
801
802std::size_t ParsedAST::getUsedBytes() const {
803 auto &AST = getASTContext();
804 // FIXME(ibiryukov): we do not account for the dynamically allocated part of
805 // Message and Fixes inside each diagnostic.
806 std::size_t Total =
807 clangd::getUsedBytes(LocalTopLevelDecls) + clangd::getUsedBytes(Diags);
808
809 // FIXME: the rest of the function is almost a direct copy-paste from
810 // libclang's clang_getCXTUResourceUsage. We could share the implementation.
811
812 // Sum up various allocators inside the ast context and the preprocessor.
813 Total += AST.getASTAllocatedMemory();
814 Total += AST.getSideTableAllocatedMemory();
815 Total += AST.Idents.getAllocator().getTotalMemory();
816 Total += AST.Selectors.getTotalMemory();
817
818 Total += AST.getSourceManager().getContentCacheSize();
819 Total += AST.getSourceManager().getDataStructureSizes();
820 Total += AST.getSourceManager().getMemoryBufferSizes().malloc_bytes;
821
822 if (ExternalASTSource *Ext = AST.getExternalSource())
823 Total += Ext->getMemoryBufferSizes().malloc_bytes;
824
825 const Preprocessor &PP = getPreprocessor();
826 Total += PP.getTotalMemory();
827 if (PreprocessingRecord *PRec = PP.getPreprocessingRecord())
828 Total += PRec->getTotalMemory();
829 Total += PP.getHeaderSearchInfo().getTotalMemory();
830
831 return Total;
832}
833
834const IncludeStructure &ParsedAST::getIncludeStructure() const {
835 return Includes;
836}
837
838ParsedAST::ParsedAST(PathRef TUPath, llvm::StringRef Version,
839 std::shared_ptr<const PreambleData> Preamble,
840 std::unique_ptr<CompilerInstance> Clang,
841 std::unique_ptr<FrontendAction> Action,
842 syntax::TokenBuffer Tokens, MainFileMacros Macros,
843 std::vector<PragmaMark> Marks,
844 std::vector<Decl *> LocalTopLevelDecls,
845 std::vector<Diag> Diags, IncludeStructure Includes,
846 include_cleaner::PragmaIncludes PI)
847 : TUPath(TUPath), Version(Version), Preamble(std::move(Preamble)),
848 Clang(std::move(Clang)), Action(std::move(Action)),
849 Tokens(std::move(Tokens)), Macros(std::move(Macros)),
850 Marks(std::move(Marks)), Diags(std::move(Diags)),
851 LocalTopLevelDecls(std::move(LocalTopLevelDecls)),
852 Includes(std::move(Includes)), PI(std::move(PI)),
853 Resolver(std::make_unique<HeuristicResolver>(getASTContext())) {
854 assert(this->Clang);
855 assert(this->Action);
856}
857
858const include_cleaner::PragmaIncludes &ParsedAST::getPragmaIncludes() const {
859 return PI;
860}
861
862std::optional<llvm::StringRef> ParsedAST::preambleVersion() const {
863 if (!Preamble)
864 return std::nullopt;
865 return llvm::StringRef(Preamble->Version);
866}
867
868llvm::ArrayRef<Diag> ParsedAST::getDiagnostics() const { return Diags; }
869} // namespace clangd
870} // namespace clang
const Expr * E
const FunctionDecl * Decl
BindArgumentKind Kind
StringRef BuildDir
Definition: ClangTidy.cpp:305
const std::optional< PreamblePatch > Patch
const PreambleData & Preamble
std::string Code
std::unique_ptr< CompilerInstance > Clang
Include Cleaner is clangd functionality for providing diagnostics for misuse of transitive headers an...
std::string Filename
Filename as a string.
SourceLocation Loc
#define dlog(...)
Definition: Logger.h:101
const MacroDirective * Directive
FieldAction Action
DiagnosticConsumer DiagConsumer
const google::protobuf::Message & M
Definition: Server.cpp:309
std::string Lang
std::unique_ptr< CompilerInvocation > CI
#define SPAN_ATTACH(S, Name, Expr)
Attach a key-value pair to a Span event.
Definition: Trace.h:164
std::vector< Inclusion > MainFileIncludes
Definition: Headers.h:174
void collect(const CompilerInstance &CI)
Definition: Headers.cpp:178
std::optional< llvm::StringRef > preambleVersion() const
Returns the version of the ParseInputs used to build Preamble part of this AST.
Definition: ParsedAST.cpp:862
const include_cleaner::PragmaIncludes & getPragmaIncludes() const
Returns the PramaIncludes for preamble + main file includes.
Definition: ParsedAST.cpp:858
static std::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:403
llvm::ArrayRef< Diag > getDiagnostics() const
Definition: ParsedAST.cpp:868
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:920
StoreDiags collects the diagnostics that can later be reported by clangd.
Definition: Diagnostics.h:138
void contributeFixes(DiagFixer Fixer)
If set, possibly adds fixes for diagnostics using Fixer.
Definition: Diagnostics.h:158
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:162
std::vector< Diag > take(const clang::tidy::ClangTidyContext *Tidy=nullptr)
void setDiagCallback(DiagCallback CB)
Invokes a callback every time a diagnostics is completely formed.
Definition: Diagnostics.h:165
Records an event whose duration is the lifetime of the Span object.
Definition: Trace.h:143
A collection of ClangTidyCheckFactory instances.
std::vector< std::unique_ptr< ClangTidyCheck > > createChecksForLanguage(ClangTidyContext *Context) const
Create instances of checks that are enabled for the current Language.
@ Info
An information message.
bool isInsideMainFile(SourceLocation Loc, const SourceManager &SM)
Returns true iff Loc is inside the main file.
Definition: SourceCode.cpp:423
static const char * toString(OffsetEncoding OE)
Definition: Protocol.cpp:1553
bool isBuiltinDiagnosticSuppressed(unsigned ID, const llvm::StringSet<> &Suppress, const LangOptions &LangOpts)
Determine whether a (non-clang-tidy) diagnostic is suppressed by config.
std::unique_ptr< PPCallbacks > collectPragmaMarksCallback(const SourceManager &SM, std::vector< PragmaMark > &Out)
Collect all pragma marks from the main file.
std::optional< bool > isFastTidyCheck(llvm::StringRef Check)
Returns if Check is known-fast, known-slow, or its speed is unknown.
Symbol::IncludeDirective preferredIncludeDirective(llvm::StringRef FileName, const LangOptions &LangOpts, ArrayRef< Inclusion > MainFileIncludes, ArrayRef< const Decl * > TopLevelDecls)
Infer the include directive to use for the given FileName.
Definition: AST.cpp:363
std::vector< Diag > issueIncludeCleanerDiagnostics(ParsedAST &AST, llvm::StringRef Code, const IncludeCleanerFindings &Findings, const ThreadsafeFS &TFS, HeaderFilter IgnoreHeaders)
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:129
void log(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:67
IncludeCleanerFindings computeIncludeCleanerFindings(ParsedAST &AST, bool AnalyzeAngledIncludes)
tidy::ClangTidyOptions getTidyOptionsForFile(TidyProviderRef Provider, llvm::StringRef Filename)
bool isImplicitTemplateInstantiation(const NamedDecl *D)
Indicates if D is a template instantiation implicitly generated by the compiler, e....
Definition: AST.cpp:154
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:29
void elog(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:61
format::FormatStyle getFormatStyleForFile(llvm::StringRef File, llvm::StringRef Content, const ThreadsafeFS &TFS, bool FormatFile)
Choose the clang-format style we should apply to a certain file.
Definition: SourceCode.cpp:583
std::string configurationAsText(const ClangTidyOptions &Options)
Serializes configuration to a YAML-encoded string.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Settings that express user/project preferences and control clangd behavior.
Definition: Config.h:44
struct clang::clangd::Config::@4::@13 Includes
static const Config & current()
Returns the Config of the current Context, or an empty configuration.
Definition: Config.cpp:17
llvm::StringSet Suppress
Definition: Config.h:100
std::vector< std::function< bool(llvm::StringRef)> > IgnoreHeader
IncludeCleaner will not diagnose usages of these headers matched by these regexes.
Definition: Config.h:116
IncludesPolicy UnusedIncludes
Definition: Config.h:110
bool AnalyzeAngledIncludes
Definition: Config.h:117
IncludesPolicy MissingIncludes
Definition: Config.h:111
struct clang::clangd::Config::@4 Diagnostics
Controls warnings and errors when parsing code.
Contains basic information about a diagnostic.
Definition: Diagnostics.h:58
A top-level diagnostic that may have Notes and Fixes.
Definition: Diagnostics.h:98
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:49
const ThreadsafeFS * TFS
Definition: Compiler.h:51
@ Include
#include "header.h"
Definition: Symbol.h:93
Contains options for clang-tidy.
std::optional< std::string > Checks
Checks filter.
std::optional< ArgList > ExtraArgsBefore
Add extra compilation arguments to the start of the list.
std::optional< ArgList > ExtraArgs
Add extra compilation arguments to the end of the list.