[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang-tools-extra / clangd / ParsedAST.cpp
blobedd0f77b1031ef03e4da50a87948883ff671e74f
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/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>
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
86 namespace clang {
87 namespace clangd {
88 namespace {
90 template <class T> std::size_t getUsedBytes(const std::vector<T> &Vec) {
91 return Vec.capacity() * sizeof(T);
94 class DeclTrackingASTConsumer : public ASTConsumer {
95 public:
96 DeclTrackingASTConsumer(std::vector<Decl *> &TopLevelDecls)
97 : TopLevelDecls(TopLevelDecls) {}
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))
105 if (isImplicitTemplateInstantiation(ND))
106 continue;
108 // ObjCMethodDecl are not actually top-level decls.
109 if (isa<ObjCMethodDecl>(D))
110 continue;
112 TopLevelDecls.push_back(D);
114 return true;
117 private:
118 std::vector<Decl *> &TopLevelDecls;
121 class ClangdFrontendAction : public SyntaxOnlyAction {
122 public:
123 std::vector<Decl *> takeTopLevelDecls() { return std::move(TopLevelDecls); }
125 protected:
126 std::unique_ptr<ASTConsumer>
127 CreateASTConsumer(CompilerInstance &CI, llvm::StringRef InFile) override {
128 return std::make_unique<DeclTrackingASTConsumer>(/*ref*/ TopLevelDecls);
131 private:
132 std::vector<Decl *> TopLevelDecls;
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.
141 class ReplayPreamble : private PPCallbacks {
142 public:
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");
162 private:
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);
173 // In a normal compile, the preamble traverses the following structure:
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 ...
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();
196 void replay() {
197 for (const auto &Inc : Includes) {
198 OptionalFileEntryRef File;
199 if (Inc.Resolved != "")
200 File = expectedToOptional(SM.getFileManager().getFileRef(Inc.Resolved));
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;
208 assert(HashTok != MainFileTokens.end() && HashTok->kind() == tok::hash);
210 auto IncludeTok = std::next(HashTok);
211 assert(IncludeTok != MainFileTokens.end());
213 auto FileTok = std::next(IncludeTok);
214 assert(FileTok != MainFileTokens.end());
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);
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());
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 /*Imported=*/nullptr, Inc.FileKind);
248 if (File)
249 Delegate->FileSkipped(*File, SynthesizedFilenameTok, Inc.FileKind);
253 const std::vector<Inclusion> Includes;
254 PPCallbacks *Delegate;
255 const SourceManager &SM;
256 Preprocessor &PP;
257 std::vector<syntax::Token> MainFileTokens;
260 // Filter for clang diagnostics groups enabled by CTOptions.Checks.
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.
266 // This is used to determine which diagnostics can be enabled by ExtraArgs in
267 // the clang-tidy configuration.
268 class 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;
276 public:
277 TidyDiagnosticGroups(llvm::StringRef Checks) {
278 constexpr llvm::StringLiteral CDPrefix = "clang-diagnostic-";
280 llvm::StringRef Check;
281 while (!Checks.empty()) {
282 std::tie(Check, Checks) = Checks.split(',');
283 if (Check.empty())
284 continue;
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.startswith(Check)) {
292 Default = Enable;
293 Exceptions.clear();
295 continue;
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;
305 if (auto Group = DiagnosticIDs::getGroupForWarningOption(Check))
306 Exceptions.insert(static_cast<unsigned>(*Group));
310 bool operator()(diag::Group GroupID) const {
311 return Exceptions.contains(static_cast<unsigned>(GroupID)) ? !Default
312 : Default;
316 // Find -W<group> and -Wno-<group> options in ExtraArgs and apply them to Diags.
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).
321 void 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;
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;
350 } else {
351 Diags.setSeverity(ID, diag::Severity::Ignored, SourceLocation());
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);
363 std::vector<Diag> getIncludeCleanerDiags(ParsedAST &AST, llvm::StringRef Code) {
364 auto &Cfg = Config::current();
365 if (Cfg.Diagnostics.SuppressAll)
366 return {};
367 bool SuppressMissing =
368 Cfg.Diagnostics.Suppress.contains("missing-includes") ||
369 Cfg.Diagnostics.MissingIncludes == Config::IncludesPolicy::None;
370 bool SuppressUnused =
371 Cfg.Diagnostics.Suppress.contains("unused-includes") ||
372 Cfg.Diagnostics.UnusedIncludes == Config::IncludesPolicy::None;
373 if (SuppressMissing && SuppressUnused)
374 return {};
375 auto Findings = computeIncludeCleanerFindings(AST);
376 if (SuppressMissing)
377 Findings.MissingIncludes.clear();
378 if (SuppressUnused)
379 Findings.UnusedIncludes.clear();
380 return issueIncludeCleanerDiagnostics(AST, Code, Findings,
381 Cfg.Diagnostics.Includes.IgnoreHeader);
384 tidy::ClangTidyCheckFactories
385 filterFastTidyChecks(const tidy::ClangTidyCheckFactories &All,
386 Config::FastCheckPolicy Policy) {
387 if (Policy == Config::FastCheckPolicy::None)
388 return All;
389 bool AllowUnknown = Policy == Config::FastCheckPolicy::Loose;
390 tidy::ClangTidyCheckFactories Fast;
391 for (const auto &Factory : All) {
392 if (isFastTidyCheck(Factory.getKey()).value_or(AllowUnknown))
393 Fast.registerCheckFactory(Factory.first(), Factory.second);
395 return Fast;
398 } // namespace
400 std::optional<ParsedAST>
401 ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
402 std::unique_ptr<clang::CompilerInvocation> CI,
403 llvm::ArrayRef<Diag> CompilerInvocationDiags,
404 std::shared_ptr<const PreambleData> Preamble) {
405 trace::Span Tracer("BuildAST");
406 SPAN_ATTACH(Tracer, "File", Filename);
407 const Config &Cfg = Config::current();
409 auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
410 if (Preamble && Preamble->StatCache)
411 VFS = Preamble->StatCache->getConsumingFS(std::move(VFS));
413 assert(CI);
415 if (CI->getFrontendOpts().Inputs.size() > 0) {
416 auto Lang = CI->getFrontendOpts().Inputs[0].getKind().getLanguage();
417 if (Lang == Language::Asm || Lang == Language::LLVM_IR) {
418 elog("Clangd does not support assembly or IR source files");
419 return std::nullopt;
423 // Command-line parsing sets DisableFree to true by default, but we don't want
424 // to leak memory in clangd.
425 CI->getFrontendOpts().DisableFree = false;
426 const PrecompiledPreamble *PreamblePCH =
427 Preamble ? &Preamble->Preamble : nullptr;
429 // This is on-by-default in windows to allow parsing SDK headers, but it
430 // breaks many features. Disable it for the main-file (not preamble).
431 CI->getLangOpts().DelayedTemplateParsing = false;
433 std::vector<std::unique_ptr<FeatureModule::ASTListener>> ASTListeners;
434 if (Inputs.FeatureModules) {
435 for (auto &M : *Inputs.FeatureModules) {
436 if (auto Listener = M.astListeners())
437 ASTListeners.emplace_back(std::move(Listener));
440 StoreDiags ASTDiags;
441 ASTDiags.setDiagCallback(
442 [&ASTListeners](const clang::Diagnostic &D, clangd::Diag &Diag) {
443 for (const auto &L : ASTListeners)
444 L->sawDiagnostic(D, Diag);
447 std::optional<PreamblePatch> Patch;
448 // We might use an ignoring diagnostic consumer if they are going to be
449 // dropped later on to not pay for extra latency by processing them.
450 DiagnosticConsumer *DiagConsumer = &ASTDiags;
451 IgnoreDiagnostics DropDiags;
452 if (Preamble) {
453 Patch = PreamblePatch::createFullPatch(Filename, Inputs, *Preamble);
454 Patch->apply(*CI);
456 auto Clang = prepareCompilerInstance(
457 std::move(CI), PreamblePCH,
458 llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, Filename), VFS,
459 *DiagConsumer);
460 if (!Clang) {
461 // The last diagnostic contains information about the reason of this
462 // failure.
463 std::vector<Diag> Diags(ASTDiags.take());
464 elog("Failed to prepare a compiler instance: {0}",
465 !Diags.empty() ? static_cast<DiagBase &>(Diags.back()).Message
466 : "unknown error");
467 return std::nullopt;
469 tidy::ClangTidyOptions ClangTidyOpts;
471 trace::Span Tracer("ClangTidyOpts");
472 ClangTidyOpts = getTidyOptionsForFile(Inputs.ClangTidyProvider, Filename);
473 dlog("ClangTidy configuration for file {0}: {1}", Filename,
474 tidy::configurationAsText(ClangTidyOpts));
476 // If clang-tidy is configured to emit clang warnings, we should too.
478 // Such clang-tidy configuration consists of two parts:
479 // - ExtraArgs: ["-Wfoo"] causes clang to produce the warnings
480 // - Checks: "clang-diagnostic-foo" prevents clang-tidy filtering them out
482 // In clang-tidy, diagnostics are emitted if they pass both checks.
483 // When groups contain subgroups, -Wparent includes the child, but
484 // clang-diagnostic-parent does not.
486 // We *don't* want to change the compile command directly. This can have
487 // too many unexpected effects: breaking the command, interactions with
488 // -- and -Werror, etc. Besides, we've already parsed the command.
489 // Instead we parse the -W<group> flags and handle them directly.
491 // Similarly, we don't want to use Checks to filter clang diagnostics after
492 // they are generated, as this spreads clang-tidy emulation everywhere.
493 // Instead, we just use these to filter which extra diagnostics we enable.
494 auto &Diags = Clang->getDiagnostics();
495 TidyDiagnosticGroups TidyGroups(ClangTidyOpts.Checks ? *ClangTidyOpts.Checks
496 : llvm::StringRef());
497 if (ClangTidyOpts.ExtraArgsBefore)
498 applyWarningOptions(*ClangTidyOpts.ExtraArgsBefore, TidyGroups, Diags);
499 if (ClangTidyOpts.ExtraArgs)
500 applyWarningOptions(*ClangTidyOpts.ExtraArgs, TidyGroups, Diags);
503 auto Action = std::make_unique<ClangdFrontendAction>();
504 const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0];
505 if (!Action->BeginSourceFile(*Clang, MainInput)) {
506 log("BeginSourceFile() failed when building AST for {0}",
507 MainInput.getFile());
508 return std::nullopt;
510 // If we saw an include guard in the preamble section of the main file,
511 // mark the main-file as include-guarded.
512 // This information is part of the HeaderFileInfo but is not loaded from the
513 // preamble as the file's size is part of its identity and may have changed.
514 // (The rest of HeaderFileInfo is not relevant for our purposes).
515 if (Preamble && Preamble->MainIsIncludeGuarded) {
516 const SourceManager &SM = Clang->getSourceManager();
517 OptionalFileEntryRef MainFE = SM.getFileEntryRefForID(SM.getMainFileID());
518 Clang->getPreprocessor().getHeaderSearchInfo().MarkFileIncludeOnce(*MainFE);
521 // Set up ClangTidy. Must happen after BeginSourceFile() so ASTContext exists.
522 // Clang-tidy has some limitations to ensure reasonable performance:
523 // - checks don't see all preprocessor events in the preamble
524 // - matchers run only over the main-file top-level decls (and can't see
525 // ancestors outside this scope).
526 // In practice almost all checks work well without modifications.
527 std::vector<std::unique_ptr<tidy::ClangTidyCheck>> CTChecks;
528 ast_matchers::MatchFinder CTFinder;
529 std::optional<tidy::ClangTidyContext> CTContext;
530 // Must outlive FixIncludes.
531 auto BuildDir = VFS->getCurrentWorkingDirectory();
532 std::optional<IncludeFixer> FixIncludes;
533 llvm::DenseMap<diag::kind, DiagnosticsEngine::Level> OverriddenSeverity;
534 // No need to run clang-tidy or IncludeFixerif we are not going to surface
535 // diagnostics.
537 trace::Span Tracer("ClangTidyInit");
538 static const auto *AllCTFactories = [] {
539 auto *CTFactories = new tidy::ClangTidyCheckFactories;
540 for (const auto &E : tidy::ClangTidyModuleRegistry::entries())
541 E.instantiate()->addCheckFactories(*CTFactories);
542 return CTFactories;
543 }();
544 tidy::ClangTidyCheckFactories FastFactories = filterFastTidyChecks(
545 *AllCTFactories, Cfg.Diagnostics.ClangTidy.FastCheckFilter);
546 CTContext.emplace(std::make_unique<tidy::DefaultOptionsProvider>(
547 tidy::ClangTidyGlobalOptions(), ClangTidyOpts));
548 CTContext->setDiagnosticsEngine(&Clang->getDiagnostics());
549 CTContext->setASTContext(&Clang->getASTContext());
550 CTContext->setCurrentFile(Filename);
551 CTContext->setSelfContainedDiags(true);
552 CTChecks = FastFactories.createChecksForLanguage(&*CTContext);
553 Preprocessor *PP = &Clang->getPreprocessor();
554 for (const auto &Check : CTChecks) {
555 Check->registerPPCallbacks(Clang->getSourceManager(), PP, PP);
556 Check->registerMatchers(&CTFinder);
559 // Clang only corrects typos for use of undeclared functions in C if that
560 // use is an error. Include fixer relies on typo correction, so pretend
561 // this is an error. (The actual typo correction is nice too).
562 // We restore the original severity in the level adjuster.
563 // FIXME: It would be better to have a real API for this, but what?
564 for (auto ID : {diag::ext_implicit_function_decl_c99,
565 diag::ext_implicit_lib_function_decl,
566 diag::ext_implicit_lib_function_decl_c99,
567 diag::warn_implicit_function_decl}) {
568 OverriddenSeverity.try_emplace(
569 ID, Clang->getDiagnostics().getDiagnosticLevel(ID, SourceLocation()));
570 Clang->getDiagnostics().setSeverity(ID, diag::Severity::Error,
571 SourceLocation());
574 ASTDiags.setLevelAdjuster([&](DiagnosticsEngine::Level DiagLevel,
575 const clang::Diagnostic &Info) {
576 if (Cfg.Diagnostics.SuppressAll ||
577 isBuiltinDiagnosticSuppressed(Info.getID(), Cfg.Diagnostics.Suppress,
578 Clang->getLangOpts()))
579 return DiagnosticsEngine::Ignored;
581 auto It = OverriddenSeverity.find(Info.getID());
582 if (It != OverriddenSeverity.end())
583 DiagLevel = It->second;
585 if (!CTChecks.empty()) {
586 std::string CheckName = CTContext->getCheckName(Info.getID());
587 bool IsClangTidyDiag = !CheckName.empty();
588 if (IsClangTidyDiag) {
589 if (Cfg.Diagnostics.Suppress.contains(CheckName))
590 return DiagnosticsEngine::Ignored;
591 // Check for suppression comment. Skip the check for diagnostics not
592 // in the main file, because we don't want that function to query the
593 // source buffer for preamble files. For the same reason, we ask
594 // shouldSuppressDiagnostic to avoid I/O.
595 // We let suppression comments take precedence over warning-as-error
596 // to match clang-tidy's behaviour.
597 bool IsInsideMainFile =
598 Info.hasSourceManager() &&
599 isInsideMainFile(Info.getLocation(), Info.getSourceManager());
600 SmallVector<tooling::Diagnostic, 1> TidySuppressedErrors;
601 if (IsInsideMainFile && CTContext->shouldSuppressDiagnostic(
602 DiagLevel, Info, TidySuppressedErrors,
603 /*AllowIO=*/false,
604 /*EnableNolintBlocks=*/true)) {
605 // FIXME: should we expose the suppression error (invalid use of
606 // NOLINT comments)?
607 return DiagnosticsEngine::Ignored;
609 if (!CTContext->getOptions().SystemHeaders.value_or(false) &&
610 Info.hasSourceManager() &&
611 Info.getSourceManager().isInSystemMacro(Info.getLocation()))
612 return DiagnosticsEngine::Ignored;
614 // Check for warning-as-error.
615 if (DiagLevel == DiagnosticsEngine::Warning &&
616 CTContext->treatAsError(CheckName)) {
617 return DiagnosticsEngine::Error;
621 return DiagLevel;
624 // Add IncludeFixer which can recover diagnostics caused by missing includes
625 // (e.g. incomplete type) and attach include insertion fixes to diagnostics.
626 if (Inputs.Index && !BuildDir.getError()) {
627 auto Style =
628 getFormatStyleForFile(Filename, Inputs.Contents, *Inputs.TFS);
629 auto Inserter = std::make_shared<IncludeInserter>(
630 Filename, Inputs.Contents, Style, BuildDir.get(),
631 &Clang->getPreprocessor().getHeaderSearchInfo());
632 ArrayRef<Inclusion> MainFileIncludes;
633 if (Preamble) {
634 MainFileIncludes = Preamble->Includes.MainFileIncludes;
635 for (const auto &Inc : Preamble->Includes.MainFileIncludes)
636 Inserter->addExisting(Inc);
638 // FIXME: Consider piping through ASTSignals to fetch this to handle the
639 // case where a header file contains ObjC decls but no #imports.
640 Symbol::IncludeDirective Directive =
641 Inputs.Opts.ImportInsertions
642 ? preferredIncludeDirective(Filename, Clang->getLangOpts(),
643 MainFileIncludes, {})
644 : Symbol::Include;
645 FixIncludes.emplace(Filename, Inserter, *Inputs.Index,
646 /*IndexRequestLimit=*/5, Directive);
647 ASTDiags.contributeFixes([&FixIncludes](DiagnosticsEngine::Level DiagLevl,
648 const clang::Diagnostic &Info) {
649 return FixIncludes->fix(DiagLevl, Info);
651 Clang->setExternalSemaSource(FixIncludes->unresolvedNameRecorder());
655 IncludeStructure Includes;
656 // If we are using a preamble, copy existing includes.
657 if (Preamble) {
658 Includes = Preamble->Includes;
659 Includes.MainFileIncludes = Patch->preambleIncludes();
660 // Replay the preamble includes so that clang-tidy checks can see them.
661 ReplayPreamble::attach(Patch->preambleIncludes(), *Clang,
662 Patch->modifiedBounds());
664 // Important: collectIncludeStructure is registered *after* ReplayPreamble!
665 // Otherwise we would collect the replayed includes again...
666 // (We can't *just* use the replayed includes, they don't have Resolved path).
667 Includes.collect(*Clang);
668 // Copy over the macros in the preamble region of the main file, and combine
669 // with non-preamble macros below.
670 MainFileMacros Macros;
671 std::vector<PragmaMark> Marks;
672 if (Preamble) {
673 Macros = Patch->mainFileMacros();
674 Marks = Patch->marks();
676 auto &PP = Clang->getPreprocessor();
677 PP.addPPCallbacks(std::make_unique<CollectMainFileMacros>(PP, Macros));
679 PP.addPPCallbacks(
680 collectPragmaMarksCallback(Clang->getSourceManager(), Marks));
682 // FIXME: Attach a comment handler to take care of
683 // keep/export/no_include etc. IWYU pragmas.
685 // Collect tokens of the main file.
686 syntax::TokenCollector CollectTokens(PP);
688 // To remain consistent with preamble builds, these callbacks must be called
689 // exactly here, after preprocessor is initialized and BeginSourceFile() was
690 // called already.
691 for (const auto &L : ASTListeners)
692 L->beforeExecute(*Clang);
694 if (llvm::Error Err = Action->Execute())
695 log("Execute() failed when building AST for {0}: {1}", MainInput.getFile(),
696 toString(std::move(Err)));
698 // We have to consume the tokens before running clang-tidy to avoid collecting
699 // tokens from running the preprocessor inside the checks (only
700 // modernize-use-trailing-return-type does that today).
701 syntax::TokenBuffer Tokens = std::move(CollectTokens).consume();
702 // Makes SelectionTree build much faster.
703 Tokens.indexExpandedTokens();
704 std::vector<Decl *> ParsedDecls = Action->takeTopLevelDecls();
705 // AST traversals should exclude the preamble, to avoid performance cliffs.
706 Clang->getASTContext().setTraversalScope(ParsedDecls);
707 if (!CTChecks.empty()) {
708 // Run the AST-dependent part of the clang-tidy checks.
709 // (The preprocessor part ran already, via PPCallbacks).
710 trace::Span Tracer("ClangTidyMatch");
711 CTFinder.matchAST(Clang->getASTContext());
714 // XXX: This is messy: clang-tidy checks flush some diagnostics at EOF.
715 // However Action->EndSourceFile() would destroy the ASTContext!
716 // So just inform the preprocessor of EOF, while keeping everything alive.
717 PP.EndSourceFile();
718 // UnitDiagsConsumer is local, we can not store it in CompilerInstance that
719 // has a longer lifetime.
720 Clang->getDiagnostics().setClient(new IgnoreDiagnostics);
721 // CompilerInstance won't run this callback, do it directly.
722 ASTDiags.EndSourceFile();
724 std::vector<Diag> Diags = CompilerInvocationDiags;
725 // FIXME: Also skip generation of diagnostics altogether to speed up ast
726 // builds when we are patching a stale preamble.
727 // Add diagnostics from the preamble, if any.
728 if (Preamble)
729 llvm::append_range(Diags, Patch->patchedDiags());
730 // Finally, add diagnostics coming from the AST.
732 std::vector<Diag> D = ASTDiags.take(&*CTContext);
733 Diags.insert(Diags.end(), D.begin(), D.end());
735 ParsedAST Result(Filename, Inputs.Version, std::move(Preamble),
736 std::move(Clang), std::move(Action), std::move(Tokens),
737 std::move(Macros), std::move(Marks), std::move(ParsedDecls),
738 std::move(Diags), std::move(Includes));
739 llvm::move(getIncludeCleanerDiags(Result, Inputs.Contents),
740 std::back_inserter(Result.Diags));
741 return std::move(Result);
744 ParsedAST::ParsedAST(ParsedAST &&Other) = default;
746 ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default;
748 ParsedAST::~ParsedAST() {
749 if (Action) {
750 // We already notified the PP of end-of-file earlier, so detach it first.
751 // We must keep it alive until after EndSourceFile(), Sema relies on this.
752 auto PP = Clang->getPreprocessorPtr(); // Keep PP alive for now.
753 Clang->setPreprocessor(nullptr); // Detach so we don't send EOF again.
754 Action->EndSourceFile(); // Destroy ASTContext and Sema.
755 // Now Sema is gone, it's safe for PP to go out of scope.
759 ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); }
761 const ASTContext &ParsedAST::getASTContext() const {
762 return Clang->getASTContext();
765 Sema &ParsedAST::getSema() { return Clang->getSema(); }
767 Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); }
769 std::shared_ptr<Preprocessor> ParsedAST::getPreprocessorPtr() {
770 return Clang->getPreprocessorPtr();
773 const Preprocessor &ParsedAST::getPreprocessor() const {
774 return Clang->getPreprocessor();
777 llvm::ArrayRef<Decl *> ParsedAST::getLocalTopLevelDecls() {
778 return LocalTopLevelDecls;
781 llvm::ArrayRef<const Decl *> ParsedAST::getLocalTopLevelDecls() const {
782 return LocalTopLevelDecls;
785 const MainFileMacros &ParsedAST::getMacros() const { return Macros; }
786 const std::vector<PragmaMark> &ParsedAST::getMarks() const { return Marks; }
788 std::size_t ParsedAST::getUsedBytes() const {
789 auto &AST = getASTContext();
790 // FIXME(ibiryukov): we do not account for the dynamically allocated part of
791 // Message and Fixes inside each diagnostic.
792 std::size_t Total =
793 clangd::getUsedBytes(LocalTopLevelDecls) + clangd::getUsedBytes(Diags);
795 // FIXME: the rest of the function is almost a direct copy-paste from
796 // libclang's clang_getCXTUResourceUsage. We could share the implementation.
798 // Sum up various allocators inside the ast context and the preprocessor.
799 Total += AST.getASTAllocatedMemory();
800 Total += AST.getSideTableAllocatedMemory();
801 Total += AST.Idents.getAllocator().getTotalMemory();
802 Total += AST.Selectors.getTotalMemory();
804 Total += AST.getSourceManager().getContentCacheSize();
805 Total += AST.getSourceManager().getDataStructureSizes();
806 Total += AST.getSourceManager().getMemoryBufferSizes().malloc_bytes;
808 if (ExternalASTSource *Ext = AST.getExternalSource())
809 Total += Ext->getMemoryBufferSizes().malloc_bytes;
811 const Preprocessor &PP = getPreprocessor();
812 Total += PP.getTotalMemory();
813 if (PreprocessingRecord *PRec = PP.getPreprocessingRecord())
814 Total += PRec->getTotalMemory();
815 Total += PP.getHeaderSearchInfo().getTotalMemory();
817 return Total;
820 const IncludeStructure &ParsedAST::getIncludeStructure() const {
821 return Includes;
824 ParsedAST::ParsedAST(PathRef TUPath, llvm::StringRef Version,
825 std::shared_ptr<const PreambleData> Preamble,
826 std::unique_ptr<CompilerInstance> Clang,
827 std::unique_ptr<FrontendAction> Action,
828 syntax::TokenBuffer Tokens, MainFileMacros Macros,
829 std::vector<PragmaMark> Marks,
830 std::vector<Decl *> LocalTopLevelDecls,
831 std::vector<Diag> Diags, IncludeStructure Includes)
832 : TUPath(TUPath), Version(Version), Preamble(std::move(Preamble)),
833 Clang(std::move(Clang)), Action(std::move(Action)),
834 Tokens(std::move(Tokens)), Macros(std::move(Macros)),
835 Marks(std::move(Marks)), Diags(std::move(Diags)),
836 LocalTopLevelDecls(std::move(LocalTopLevelDecls)),
837 Includes(std::move(Includes)) {
838 Resolver = std::make_unique<HeuristicResolver>(getASTContext());
839 assert(this->Clang);
840 assert(this->Action);
843 std::shared_ptr<const include_cleaner::PragmaIncludes>
844 ParsedAST::getPragmaIncludes() const {
845 if (!Preamble)
846 return nullptr;
847 return Preamble->Pragmas;
850 std::optional<llvm::StringRef> ParsedAST::preambleVersion() const {
851 if (!Preamble)
852 return std::nullopt;
853 return llvm::StringRef(Preamble->Version);
856 llvm::ArrayRef<Diag> ParsedAST::getDiagnostics() const { return Diags; }
857 } // namespace clangd
858 } // namespace clang