clang 23.0.0git
CodeGenAction.cpp
Go to the documentation of this file.
1//===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===//
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
10#include "BackendConsumer.h"
11#include "CGCall.h"
12#include "CodeGenModule.h"
13#include "CoverageMappingGen.h"
14#include "MacroPPCallbacks.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclGroup.h"
31#include "llvm/ADT/Hashing.h"
32#include "llvm/ADT/ScopeExit.h"
33#include "llvm/Bitcode/BitcodeReader.h"
34#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
35#include "llvm/Demangle/Demangle.h"
36#include "llvm/IR/DebugInfo.h"
37#include "llvm/IR/DiagnosticInfo.h"
38#include "llvm/IR/DiagnosticPrinter.h"
39#include "llvm/IR/GlobalValue.h"
40#include "llvm/IR/LLVMContext.h"
41#include "llvm/IR/LLVMRemarkStreamer.h"
42#include "llvm/IR/Module.h"
43#include "llvm/IR/PassTimingInfo.h"
44#include "llvm/IR/Verifier.h"
45#include "llvm/IRReader/IRReader.h"
46#include "llvm/LTO/LTOBackend.h"
47#include "llvm/Linker/Linker.h"
48#include "llvm/Pass.h"
49#include "llvm/Support/MemoryBuffer.h"
50#include "llvm/Support/SourceMgr.h"
51#include "llvm/Support/TimeProfiler.h"
52#include "llvm/Support/Timer.h"
53#include "llvm/Support/ToolOutputFile.h"
54#include "llvm/Transforms/IPO/Internalize.h"
55#include "llvm/Transforms/Utils/Cloning.h"
56
57#include <optional>
58using namespace clang;
59using namespace llvm;
60
61#define DEBUG_TYPE "codegenaction"
62
63namespace clang {
64class BackendConsumer;
66public:
68 : CodeGenOpts(CGOpts), BackendCon(BCon) {}
69
70 bool handleDiagnostics(const DiagnosticInfo &DI) override;
71
72 bool isAnalysisRemarkEnabled(StringRef PassName) const override {
73 return CodeGenOpts.OptimizationRemarkAnalysis.patternMatches(PassName);
74 }
75 bool isMissedOptRemarkEnabled(StringRef PassName) const override {
76 return CodeGenOpts.OptimizationRemarkMissed.patternMatches(PassName);
77 }
78 bool isPassedOptRemarkEnabled(StringRef PassName) const override {
79 return CodeGenOpts.OptimizationRemark.patternMatches(PassName);
80 }
81
82 bool isAnyRemarkEnabled() const override {
83 return CodeGenOpts.OptimizationRemarkAnalysis.hasValidPattern() ||
84 CodeGenOpts.OptimizationRemarkMissed.hasValidPattern() ||
85 CodeGenOpts.OptimizationRemark.hasValidPattern();
86 }
87
88private:
89 const CodeGenOptions &CodeGenOpts;
90 BackendConsumer *BackendCon;
91};
92
94 const CodeGenOptions &CodeGenOpts) {
95 handleAllErrors(
96 std::move(E),
97 [&](const LLVMRemarkSetupFileError &E) {
98 Diags.Report(diag::err_cannot_open_file)
99 << CodeGenOpts.OptRecordFile << E.message();
100 },
101 [&](const LLVMRemarkSetupPatternError &E) {
102 Diags.Report(diag::err_drv_optimization_remark_pattern)
103 << E.message() << CodeGenOpts.OptRecordPasses;
104 },
105 [&](const LLVMRemarkSetupFormatError &E) {
106 Diags.Report(diag::err_drv_optimization_remark_format)
107 << CodeGenOpts.OptRecordFormat;
108 });
109}
110
113 LLVMContext &C,
114 SmallVector<LinkModule, 4> LinkModules,
115 StringRef InFile,
116 std::unique_ptr<raw_pwrite_stream> OS,
117 CoverageSourceInfo *CoverageInfo,
118 llvm::Module *CurLinkModule)
119 : CI(CI), Diags(CI.getDiagnostics()), CodeGenOpts(CI.getCodeGenOpts()),
120 TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),
121 AsmOutStream(std::move(OS)), FS(VFS), Action(Action),
122 Gen(CreateLLVMCodeGen(CI, InFile, C, CoverageInfo)),
123 LinkModules(std::move(LinkModules)), CurLinkModule(CurLinkModule) {
124 TimerIsEnabled = CodeGenOpts.TimePasses;
125 llvm::TimePassesIsEnabled = CodeGenOpts.TimePasses;
126 llvm::TimePassesPerRun = CodeGenOpts.TimePassesPerRun;
127 if (CodeGenOpts.TimePasses)
128 LLVMIRGeneration.init("irgen", "LLVM IR generation", CI.getTimerGroup());
129}
130
131llvm::Module* BackendConsumer::getModule() const {
132 return Gen->GetModule();
133}
134
135std::unique_ptr<llvm::Module> BackendConsumer::takeModule() {
136 return std::unique_ptr<llvm::Module>(Gen->ReleaseModule());
137}
138
140 return Gen.get();
141}
142
146
148 assert(!Context && "initialized multiple times");
149
150 Context = &Ctx;
151
152 if (TimerIsEnabled)
153 LLVMIRGeneration.startTimer();
154
155 Gen->Initialize(Ctx);
156
157 if (TimerIsEnabled)
158 LLVMIRGeneration.stopTimer();
159}
160
162 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
163 Context->getSourceManager(),
164 "LLVM IR generation of declaration");
165
166 // Recurse.
167 if (TimerIsEnabled && !LLVMIRGenerationRefCount++)
168 CI.getFrontendTimer().yieldTo(LLVMIRGeneration);
169
170 Gen->HandleTopLevelDecl(D);
171
172 if (TimerIsEnabled && !--LLVMIRGenerationRefCount)
173 LLVMIRGeneration.yieldTo(CI.getFrontendTimer());
174
175 return true;
176}
177
180 Context->getSourceManager(),
181 "LLVM IR generation of inline function");
182 if (TimerIsEnabled)
183 CI.getFrontendTimer().yieldTo(LLVMIRGeneration);
184
185 Gen->HandleInlineFunctionDefinition(D);
186
187 if (TimerIsEnabled)
188 LLVMIRGeneration.yieldTo(CI.getFrontendTimer());
189}
190
194
195// Links each entry in LinkModules into our module. Returns true on error.
196bool BackendConsumer::LinkInModules(llvm::Module *M) {
197 for (auto &LM : LinkModules) {
198 assert(LM.Module && "LinkModule does not actually have a module");
199
200 if (LM.PropagateAttrs)
201 for (Function &F : *LM.Module) {
202 // Skip intrinsics. Keep consistent with how intrinsics are created
203 // in LLVM IR.
204 if (F.isIntrinsic())
205 continue;
207 F, CodeGenOpts, LangOpts, TargetOpts, LM.Internalize);
208 }
209
210 CurLinkModule = LM.Module.get();
211 bool Err;
212
213 if (LM.Internalize) {
214 Err = Linker::linkModules(
215 *M, std::move(LM.Module), LM.LinkFlags,
216 [](llvm::Module &M, const llvm::StringSet<> &GVS) {
217 internalizeModule(M, [&GVS](const llvm::GlobalValue &GV) {
218 return !GV.hasName() || (GVS.count(GV.getName()) == 0);
219 });
220 });
221 } else
222 Err = Linker::linkModules(*M, std::move(LM.Module), LM.LinkFlags);
223
224 if (Err)
225 return true;
226 }
227
228 LinkModules.clear();
229 return false; // success
230}
231
233 {
234 llvm::TimeTraceScope TimeScope("Frontend");
235 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
236 if (TimerIsEnabled && !LLVMIRGenerationRefCount++)
237 CI.getFrontendTimer().yieldTo(LLVMIRGeneration);
238
239 Gen->HandleTranslationUnit(C);
240
241 if (TimerIsEnabled && !--LLVMIRGenerationRefCount)
242 LLVMIRGeneration.yieldTo(CI.getFrontendTimer());
243 }
244
245 // Silently ignore if we weren't initialized for some reason.
246 if (!getModule())
247 return;
248
249 LLVMContext &Ctx = getModule()->getContext();
250 std::unique_ptr<DiagnosticHandler> OldDiagnosticHandler =
251 Ctx.getDiagnosticHandler();
252 llvm::scope_exit RestoreDiagnosticHandler(
253 [&]() { Ctx.setDiagnosticHandler(std::move(OldDiagnosticHandler)); });
254 Ctx.setDiagnosticHandler(std::make_unique<ClangDiagnosticHandler>(
255 CodeGenOpts, this));
256
257 Ctx.setDefaultTargetCPU(TargetOpts.CPU);
258 Ctx.setDefaultTargetFeatures(llvm::join(TargetOpts.Features, ","));
259
260 Expected<LLVMRemarkFileHandle> OptRecordFileOrErr =
261 setupLLVMOptimizationRemarks(
262 Ctx, CodeGenOpts.OptRecordFile, CodeGenOpts.OptRecordPasses,
263 CodeGenOpts.OptRecordFormat, CodeGenOpts.DiagnosticsWithHotness,
264 CodeGenOpts.DiagnosticsHotnessThreshold);
265
266 if (Error E = OptRecordFileOrErr.takeError()) {
267 reportOptRecordError(std::move(E), Diags, CodeGenOpts);
268 return;
269 }
270
271 LLVMRemarkFileHandle OptRecordFile = std::move(*OptRecordFileOrErr);
272
273 if (OptRecordFile && CodeGenOpts.getProfileUse() !=
274 llvm::driver::ProfileInstrKind::ProfileNone)
275 Ctx.setDiagnosticsHotnessRequested(true);
276
277 if (CodeGenOpts.MisExpect) {
278 Ctx.setMisExpectWarningRequested(true);
279 }
280
281 if (CodeGenOpts.DiagnosticsMisExpectTolerance) {
282 Ctx.setDiagnosticsMisExpectTolerance(
283 CodeGenOpts.DiagnosticsMisExpectTolerance);
284 }
285
286 // Link each LinkModule into our module.
287 if (!CodeGenOpts.LinkBitcodePostopt && LinkInModules(getModule()))
288 return;
289
290 for (auto &F : getModule()->functions()) {
291 if (const Decl *FD = Gen->GetDeclForMangledName(F.getName())) {
292 auto Loc = FD->getASTContext().getFullLoc(FD->getLocation());
293 // TODO: use a fast content hash when available.
294 auto NameHash = llvm::hash_value(F.getName());
295 ManglingFullSourceLocs.push_back(std::make_pair(NameHash, Loc));
296 }
297 }
298
299 if (CodeGenOpts.ClearASTBeforeBackend) {
300 LLVM_DEBUG(llvm::dbgs() << "Clearing AST...\n");
301 // Access to the AST is no longer available after this.
302 // Other things that the ASTContext manages are still available, e.g.
303 // the SourceManager. It'd be nice if we could separate out all the
304 // things in ASTContext used after this point and null out the
305 // ASTContext, but too many various parts of the ASTContext are still
306 // used in various parts.
307 C.cleanup();
308 C.getAllocator().Reset();
309 }
310
311 EmbedBitcode(getModule(), CodeGenOpts, llvm::MemoryBufferRef());
312
313 emitBackendOutput(CI, CI.getCodeGenOpts(),
314 C.getTargetInfo().getDataLayoutString(), getModule(),
315 Action, FS, std::move(AsmOutStream), this);
316
317 if (OptRecordFile)
318 OptRecordFile->keep();
319}
320
323 Context->getSourceManager(),
324 "LLVM IR generation of declaration");
325 Gen->HandleTagDeclDefinition(D);
326}
327
329 Gen->HandleTagDeclRequiredDefinition(D);
330}
331
333 Gen->CompleteTentativeDefinition(D);
334}
335
337 Gen->CompleteExternalDeclaration(D);
338}
339
341 Gen->AssignInheritanceModel(RD);
342}
343
345 Gen->HandleVTable(RD);
346}
347
348void BackendConsumer::anchor() { }
349
350} // namespace clang
351
352bool ClangDiagnosticHandler::handleDiagnostics(const DiagnosticInfo &DI) {
353 BackendCon->DiagnosticHandlerImpl(DI);
354 return true;
355}
356
357/// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr
358/// buffer to be a valid FullSourceLoc.
359static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D,
360 SourceManager &CSM) {
361 // Get both the clang and llvm source managers. The location is relative to
362 // a memory buffer that the LLVM Source Manager is handling, we need to add
363 // a copy to the Clang source manager.
364 const llvm::SourceMgr &LSM = *D.getSourceMgr();
365
366 // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr
367 // already owns its one and clang::SourceManager wants to own its one.
368 const MemoryBuffer *LBuf =
369 LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
370
371 // Create the copy and transfer ownership to clang::SourceManager.
372 // TODO: Avoid copying files into memory.
373 std::unique_ptr<llvm::MemoryBuffer> CBuf =
374 llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(),
375 LBuf->getBufferIdentifier());
376 // FIXME: Keep a file ID map instead of creating new IDs for each location.
377 FileID FID = CSM.createFileID(std::move(CBuf));
378
379 // Translate the offset into the file.
380 unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
381 SourceLocation NewLoc =
382 CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset);
383 return FullSourceLoc(NewLoc, CSM);
384}
385
386#define ComputeDiagID(Severity, GroupName, DiagID) \
387 do { \
388 switch (Severity) { \
389 case llvm::DS_Error: \
390 DiagID = diag::err_fe_##GroupName; \
391 break; \
392 case llvm::DS_Warning: \
393 DiagID = diag::warn_fe_##GroupName; \
394 break; \
395 case llvm::DS_Remark: \
396 llvm_unreachable("'remark' severity not expected"); \
397 break; \
398 case llvm::DS_Note: \
399 DiagID = diag::note_fe_##GroupName; \
400 break; \
401 } \
402 } while (false)
403
404#define ComputeDiagRemarkID(Severity, GroupName, DiagID) \
405 do { \
406 switch (Severity) { \
407 case llvm::DS_Error: \
408 DiagID = diag::err_fe_##GroupName; \
409 break; \
410 case llvm::DS_Warning: \
411 DiagID = diag::warn_fe_##GroupName; \
412 break; \
413 case llvm::DS_Remark: \
414 DiagID = diag::remark_fe_##GroupName; \
415 break; \
416 case llvm::DS_Note: \
417 DiagID = diag::note_fe_##GroupName; \
418 break; \
419 } \
420 } while (false)
421
422void BackendConsumer::SrcMgrDiagHandler(const llvm::DiagnosticInfoSrcMgr &DI) {
423 const llvm::SMDiagnostic &D = DI.getSMDiag();
424
425 unsigned DiagID;
426 if (DI.isInlineAsmDiag())
427 ComputeDiagID(DI.getSeverity(), inline_asm, DiagID);
428 else
429 ComputeDiagID(DI.getSeverity(), source_mgr, DiagID);
430
431 // This is for the empty BackendConsumer that uses the clang diagnostic
432 // handler for IR input files.
433 if (!Context) {
434 D.print(nullptr, llvm::errs());
435 Diags.Report(DiagID).AddString("cannot compile inline asm");
436 return;
437 }
438
439 // There are a couple of different kinds of errors we could get here.
440 // First, we re-format the SMDiagnostic in terms of a clang diagnostic.
441
442 // Strip "error: " off the start of the message string.
443 StringRef Message = D.getMessage();
444 (void)Message.consume_front("error: ");
445
446 // If the SMDiagnostic has an inline asm source location, translate it.
447 FullSourceLoc Loc;
448 if (D.getLoc() != SMLoc())
449 Loc = ConvertBackendLocation(D, Context->getSourceManager());
450
451 // If this problem has clang-level source location information, report the
452 // issue in the source with a note showing the instantiated
453 // code.
454 if (DI.isInlineAsmDiag()) {
455 SourceLocation LocCookie =
456 SourceLocation::getFromRawEncoding(DI.getLocCookie());
457 if (LocCookie.isValid()) {
458 Diags.Report(LocCookie, DiagID).AddString(Message);
459
460 if (D.getLoc().isValid()) {
461 DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here);
462 // Convert the SMDiagnostic ranges into SourceRange and attach them
463 // to the diagnostic.
464 for (const std::pair<unsigned, unsigned> &Range : D.getRanges()) {
465 unsigned Column = D.getColumnNo();
466 B << SourceRange(Loc.getLocWithOffset(Range.first - Column),
467 Loc.getLocWithOffset(Range.second - Column));
468 }
469 }
470 return;
471 }
472 }
473
474 // Otherwise, report the backend issue as occurring in the generated .s file.
475 // If Loc is invalid, we still need to report the issue, it just gets no
476 // location info.
477 Diags.Report(Loc, DiagID).AddString(Message);
478}
479
480bool
481BackendConsumer::InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D) {
482 unsigned DiagID;
483 ComputeDiagID(D.getSeverity(), inline_asm, DiagID);
484 std::string Message = D.getMsgStr().str();
485
486 // If this problem has clang-level source location information, report the
487 // issue as being a problem in the source with a note showing the instantiated
488 // code.
489 SourceLocation LocCookie =
490 SourceLocation::getFromRawEncoding(D.getLocCookie());
491 if (LocCookie.isValid())
492 Diags.Report(LocCookie, DiagID).AddString(Message);
493 else {
494 // Otherwise, report the backend diagnostic as occurring in the generated
495 // .s file.
496 // If Loc is invalid, we still need to report the diagnostic, it just gets
497 // no location info.
498 FullSourceLoc Loc;
499 Diags.Report(Loc, DiagID).AddString(Message);
500 }
501 // We handled all the possible severities.
502 return true;
503}
504
505bool
506BackendConsumer::StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D) {
507 if (D.getSeverity() != llvm::DS_Warning)
508 // For now, the only support we have for StackSize diagnostic is warning.
509 // We do not know how to format other severities.
510 return false;
511
512 auto Loc = getFunctionSourceLocation(D.getFunction());
513 if (!Loc)
514 return false;
515
516 Diags.Report(*Loc, diag::warn_fe_frame_larger_than)
517 << D.getStackSize() << D.getStackLimit()
518 << llvm::demangle(D.getFunction().getName());
519 return true;
520}
521
523 const llvm::DiagnosticInfoResourceLimit &D) {
524 auto Loc = getFunctionSourceLocation(D.getFunction());
525 if (!Loc)
526 return false;
527 unsigned DiagID = diag::err_fe_backend_resource_limit;
528 ComputeDiagID(D.getSeverity(), backend_resource_limit, DiagID);
529
530 Diags.Report(*Loc, DiagID)
531 << D.getResourceName() << D.getResourceSize() << D.getResourceLimit()
532 << llvm::demangle(D.getFunction().getName());
533 return true;
534}
535
537 const llvm::DiagnosticInfoWithLocationBase &D, bool &BadDebugInfo,
538 StringRef &Filename, unsigned &Line, unsigned &Column) const {
539 SourceManager &SourceMgr = Context->getSourceManager();
540 FileManager &FileMgr = SourceMgr.getFileManager();
541 SourceLocation DILoc;
542
543 if (D.isLocationAvailable()) {
544 D.getLocation(Filename, Line, Column);
545 if (Line > 0) {
546 auto FE = FileMgr.getOptionalFileRef(Filename);
547 if (!FE)
548 FE = FileMgr.getOptionalFileRef(D.getAbsolutePath());
549 if (FE) {
550 // If -gcolumn-info was not used, Column will be 0. This upsets the
551 // source manager, so pass 1 if Column is not set.
552 DILoc = SourceMgr.translateFileLineCol(*FE, Line, Column ? Column : 1);
553 }
554 }
555 BadDebugInfo = DILoc.isInvalid();
556 }
557
558 // If a location isn't available, try to approximate it using the associated
559 // function definition. We use the definition's right brace to differentiate
560 // from diagnostics that genuinely relate to the function itself.
561 FullSourceLoc Loc(DILoc, SourceMgr);
562 if (Loc.isInvalid()) {
563 if (auto MaybeLoc = getFunctionSourceLocation(D.getFunction()))
564 Loc = *MaybeLoc;
565 }
566
567 if (DILoc.isInvalid() && D.isLocationAvailable())
568 // If we were not able to translate the file:line:col information
569 // back to a SourceLocation, at least emit a note stating that
570 // we could not translate this location. This can happen in the
571 // case of #line directives.
572 Diags.Report(Loc, diag::note_fe_backend_invalid_loc)
573 << Filename << Line << Column;
574
575 return Loc;
576}
577
578std::optional<FullSourceLoc>
580 auto Hash = llvm::hash_value(F.getName());
581 for (const auto &Pair : ManglingFullSourceLocs) {
582 if (Pair.first == Hash)
583 return Pair.second;
584 }
585 return std::nullopt;
586}
587
589 const llvm::DiagnosticInfoUnsupported &D) {
590 // We only support warnings or errors.
591 assert(D.getSeverity() == llvm::DS_Error ||
592 D.getSeverity() == llvm::DS_Warning);
593
594 StringRef Filename;
595 unsigned Line, Column;
596 bool BadDebugInfo = false;
597 FullSourceLoc Loc;
598 std::string Msg;
599 raw_string_ostream MsgStream(Msg);
600
601 // Context will be nullptr for IR input files, we will construct the diag
602 // message from llvm::DiagnosticInfoUnsupported.
603 if (Context != nullptr) {
604 Loc = getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column);
605 MsgStream << D.getMessage();
606 } else {
607 DiagnosticPrinterRawOStream DP(MsgStream);
608 D.print(DP);
609 }
610
611 auto DiagType = D.getSeverity() == llvm::DS_Error
612 ? diag::err_fe_backend_unsupported
613 : diag::warn_fe_backend_unsupported;
614 Diags.Report(Loc, DiagType) << Msg;
615
616 if (BadDebugInfo)
617 // If we were not able to translate the file:line:col information
618 // back to a SourceLocation, at least emit a note stating that
619 // we could not translate this location. This can happen in the
620 // case of #line directives.
621 Diags.Report(Loc, diag::note_fe_backend_invalid_loc)
622 << Filename << Line << Column;
623}
624
626 const llvm::DiagnosticInfoOptimizationBase &D, unsigned DiagID) {
627 // We only support warnings and remarks.
628 assert(D.getSeverity() == llvm::DS_Remark ||
629 D.getSeverity() == llvm::DS_Warning);
630
631 StringRef Filename;
632 unsigned Line, Column;
633 bool BadDebugInfo = false;
634 FullSourceLoc Loc;
635 std::string Msg;
636 raw_string_ostream MsgStream(Msg);
637
638 // Context will be nullptr for IR input files, we will construct the remark
639 // message from llvm::DiagnosticInfoOptimizationBase.
640 if (Context != nullptr) {
641 Loc = getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column);
642 MsgStream << D.getMsg();
643 } else {
644 DiagnosticPrinterRawOStream DP(MsgStream);
645 D.print(DP);
646 }
647
648 if (D.getHotness())
649 MsgStream << " (hotness: " << *D.getHotness() << ")";
650
651 Diags.Report(Loc, DiagID) << AddFlagValue(D.getPassName()) << Msg;
652
653 if (BadDebugInfo)
654 // If we were not able to translate the file:line:col information
655 // back to a SourceLocation, at least emit a note stating that
656 // we could not translate this location. This can happen in the
657 // case of #line directives.
658 Diags.Report(Loc, diag::note_fe_backend_invalid_loc)
659 << Filename << Line << Column;
660}
661
663 const llvm::DiagnosticInfoOptimizationBase &D) {
664 // Without hotness information, don't show noisy remarks.
665 if (D.isVerbose() && !D.getHotness())
666 return;
667
668 if (D.isPassed()) {
669 // Optimization remarks are active only if the -Rpass flag has a regular
670 // expression that matches the name of the pass name in \p D.
671 if (CodeGenOpts.OptimizationRemark.patternMatches(D.getPassName()))
672 EmitOptimizationMessage(D, diag::remark_fe_backend_optimization_remark);
673 } else if (D.isMissed()) {
674 // Missed optimization remarks are active only if the -Rpass-missed
675 // flag has a regular expression that matches the name of the pass
676 // name in \p D.
677 if (CodeGenOpts.OptimizationRemarkMissed.patternMatches(D.getPassName()))
679 D, diag::remark_fe_backend_optimization_remark_missed);
680 } else {
681 assert(D.isAnalysis() && "Unknown remark type");
682
683 bool ShouldAlwaysPrint = false;
684 if (auto *ORA = dyn_cast<llvm::OptimizationRemarkAnalysis>(&D))
685 ShouldAlwaysPrint = ORA->shouldAlwaysPrint();
686
687 if (ShouldAlwaysPrint ||
688 CodeGenOpts.OptimizationRemarkAnalysis.patternMatches(D.getPassName()))
690 D, diag::remark_fe_backend_optimization_remark_analysis);
691 }
692}
693
695 const llvm::OptimizationRemarkAnalysisFPCommute &D) {
696 // Optimization analysis remarks are active if the pass name is set to
697 // llvm::DiagnosticInfo::AlwasyPrint or if the -Rpass-analysis flag has a
698 // regular expression that matches the name of the pass name in \p D.
699
700 if (D.shouldAlwaysPrint() ||
701 CodeGenOpts.OptimizationRemarkAnalysis.patternMatches(D.getPassName()))
703 D, diag::remark_fe_backend_optimization_remark_analysis_fpcommute);
704}
705
707 const llvm::OptimizationRemarkAnalysisAliasing &D) {
708 // Optimization analysis remarks are active if the pass name is set to
709 // llvm::DiagnosticInfo::AlwasyPrint or if the -Rpass-analysis flag has a
710 // regular expression that matches the name of the pass name in \p D.
711
712 if (D.shouldAlwaysPrint() ||
713 CodeGenOpts.OptimizationRemarkAnalysis.patternMatches(D.getPassName()))
715 D, diag::remark_fe_backend_optimization_remark_analysis_aliasing);
716}
717
719 const llvm::DiagnosticInfoOptimizationFailure &D) {
720 EmitOptimizationMessage(D, diag::warn_fe_backend_optimization_failure);
721}
722
723void BackendConsumer::DontCallDiagHandler(const DiagnosticInfoDontCall &D) {
724 SourceLocation LocCookie =
725 SourceLocation::getFromRawEncoding(D.getLocCookie());
726
727 // FIXME: we can't yet diagnose indirect calls. When/if we can, we
728 // should instead assert that LocCookie.isValid().
729 if (!LocCookie.isValid())
730 return;
731
732 Diags.Report(LocCookie, D.getSeverity() == DiagnosticSeverity::DS_Error
733 ? diag::err_fe_backend_error_attr
734 : diag::warn_fe_backend_warning_attr)
735 << llvm::demangle(D.getFunctionName()) << D.getNote();
736
737 if (!CodeGenOpts.ShowInliningChain)
738 return;
739
740 auto EmitNote = [&](SourceLocation Loc, StringRef FuncName, bool IsFirst) {
741 if (!Loc.isValid())
742 Loc = LocCookie;
743 unsigned DiagID =
744 IsFirst ? diag::note_fe_backend_in : diag::note_fe_backend_inlined;
745 Diags.Report(Loc, DiagID) << llvm::demangle(FuncName.str());
746 };
747
748 // Try debug info first for accurate source locations.
749 if (!D.getDebugInlineChain().empty()) {
750 SourceManager &SM = Context->getSourceManager();
751 FileManager &FM = SM.getFileManager();
752 for (const auto &[I, Info] : llvm::enumerate(D.getDebugInlineChain())) {
753 SourceLocation Loc;
754 if (Info.Line > 0)
755 if (auto FE = FM.getOptionalFileRef(Info.Filename))
756 Loc = SM.translateFileLineCol(*FE, Info.Line,
757 Info.Column ? Info.Column : 1);
758 EmitNote(Loc, Info.FuncName, I == 0);
759 }
760 return;
761 }
762
763 // Fall back to heuristic (srcloc metadata) when debug info is unavailable.
764 auto InliningDecisions = D.getInliningDecisions();
765 if (InliningDecisions.empty())
766 return;
767
768 for (const auto &[I, Entry] : llvm::enumerate(InliningDecisions)) {
769 SourceLocation Loc =
770 I == 0 ? LocCookie : SourceLocation::getFromRawEncoding(Entry.second);
771 EmitNote(Loc, Entry.first, I == 0);
772 }
773
774 // Suggest enabling debug info (at least -gline-directives-only) for more
775 // accurate locations.
776 Diags.Report(LocCookie, diag::note_fe_backend_inlining_debug_info);
777}
778
780 const llvm::DiagnosticInfoMisExpect &D) {
781 StringRef Filename;
782 unsigned Line, Column;
783 bool BadDebugInfo = false;
784 FullSourceLoc Loc =
785 getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column);
786
787 Diags.Report(Loc, diag::warn_profile_data_misexpect) << D.getMsg().str();
788
789 if (BadDebugInfo)
790 // If we were not able to translate the file:line:col information
791 // back to a SourceLocation, at least emit a note stating that
792 // we could not translate this location. This can happen in the
793 // case of #line directives.
794 Diags.Report(Loc, diag::note_fe_backend_invalid_loc)
795 << Filename << Line << Column;
796}
797
798/// This function is invoked when the backend needs
799/// to report something to the user.
800void BackendConsumer::DiagnosticHandlerImpl(const DiagnosticInfo &DI) {
801 unsigned DiagID = diag::err_fe_inline_asm;
802 llvm::DiagnosticSeverity Severity = DI.getSeverity();
803 // Get the diagnostic ID based.
804 switch (DI.getKind()) {
805 case llvm::DK_InlineAsm:
807 return;
808 ComputeDiagID(Severity, inline_asm, DiagID);
809 break;
810 case llvm::DK_SrcMgr:
812 return;
813 case llvm::DK_StackSize:
815 return;
816 ComputeDiagID(Severity, backend_frame_larger_than, DiagID);
817 break;
818 case llvm::DK_ResourceLimit:
820 return;
821 ComputeDiagID(Severity, backend_resource_limit, DiagID);
822 break;
823 case DK_Linker:
824 ComputeDiagID(Severity, linking_module, DiagID);
825 break;
826 case llvm::DK_OptimizationRemark:
827 // Optimization remarks are always handled completely by this
828 // handler. There is no generic way of emitting them.
830 return;
831 case llvm::DK_OptimizationRemarkMissed:
832 // Optimization remarks are always handled completely by this
833 // handler. There is no generic way of emitting them.
835 return;
836 case llvm::DK_OptimizationRemarkAnalysis:
837 // Optimization remarks are always handled completely by this
838 // handler. There is no generic way of emitting them.
840 return;
841 case llvm::DK_OptimizationRemarkAnalysisFPCommute:
842 // Optimization remarks are always handled completely by this
843 // handler. There is no generic way of emitting them.
845 return;
846 case llvm::DK_OptimizationRemarkAnalysisAliasing:
847 // Optimization remarks are always handled completely by this
848 // handler. There is no generic way of emitting them.
850 return;
851 case llvm::DK_MachineOptimizationRemark:
852 // Optimization remarks are always handled completely by this
853 // handler. There is no generic way of emitting them.
855 return;
856 case llvm::DK_MachineOptimizationRemarkMissed:
857 // Optimization remarks are always handled completely by this
858 // handler. There is no generic way of emitting them.
860 return;
861 case llvm::DK_MachineOptimizationRemarkAnalysis:
862 // Optimization remarks are always handled completely by this
863 // handler. There is no generic way of emitting them.
865 return;
866 case llvm::DK_OptimizationFailure:
867 // Optimization failures are always handled completely by this
868 // handler.
870 return;
871 case llvm::DK_Unsupported:
873 return;
874 case llvm::DK_DontCall:
876 return;
877 case llvm::DK_MisExpect:
879 return;
880 default:
881 // Plugin IDs are not bound to any value as they are set dynamically.
882 ComputeDiagRemarkID(Severity, backend_plugin, DiagID);
883 break;
884 }
885 std::string MsgStorage;
886 {
887 raw_string_ostream Stream(MsgStorage);
888 DiagnosticPrinterRawOStream DP(Stream);
889 DI.print(DP);
890 }
891
892 if (DI.getKind() == DK_Linker) {
893 assert(CurLinkModule && "CurLinkModule must be set for linker diagnostics");
894 Diags.Report(DiagID) << CurLinkModule->getModuleIdentifier() << MsgStorage;
895 return;
896 }
897
898 // Report the backend message using the usual diagnostic mechanism.
899 FullSourceLoc Loc;
900 Diags.Report(Loc, DiagID).AddString(MsgStorage);
901}
902#undef ComputeDiagID
903
904CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
905 : Act(_Act), VMContext(_VMContext ? _VMContext : new LLVMContext),
906 OwnsVMContext(!_VMContext) {}
907
909 TheModule.reset();
910 if (OwnsVMContext)
911 delete VMContext;
912}
913
914bool CodeGenAction::loadLinkModules(CompilerInstance &CI) {
915 if (!LinkModules.empty())
916 return false;
917
920 auto BCBuf = CI.getFileManager().getBufferForFile(F.Filename);
921 if (!BCBuf) {
922 CI.getDiagnostics().Report(diag::err_cannot_open_file)
923 << F.Filename << BCBuf.getError().message();
924 LinkModules.clear();
925 return true;
926 }
927
929 getOwningLazyBitcodeModule(std::move(*BCBuf), *VMContext);
930 if (!ModuleOrErr) {
931 handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
932 CI.getDiagnostics().Report(diag::err_cannot_open_file)
933 << F.Filename << EIB.message();
934 });
935 LinkModules.clear();
936 return true;
937 }
938 LinkModules.push_back({std::move(ModuleOrErr.get()), F.PropagateAttrs,
939 F.Internalize, F.LinkFlags});
940 }
941 return false;
942}
943
944bool CodeGenAction::hasIRSupport() const { return true; }
945
948
949 // If the consumer creation failed, do nothing.
950 if (!getCompilerInstance().hasASTConsumer())
951 return;
952
953 // Steal the module from the consumer.
954 TheModule = BEConsumer->takeModule();
955}
956
957std::unique_ptr<llvm::Module> CodeGenAction::takeModule() {
958 return std::move(TheModule);
959}
960
961llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
962 OwnsVMContext = false;
963 return VMContext;
964}
965
967 return BEConsumer->getCodeGenerator();
968}
969
975
976static std::unique_ptr<raw_pwrite_stream>
977GetOutputStream(CompilerInstance &CI, StringRef InFile, BackendAction Action) {
978 switch (Action) {
980 return CI.createDefaultOutputFile(false, InFile, "s");
981 case Backend_EmitLL:
982 return CI.createDefaultOutputFile(false, InFile, "ll");
983 case Backend_EmitBC:
984 return CI.createDefaultOutputFile(true, InFile, "bc");
986 return nullptr;
988 return CI.createNullOutputFile();
989 case Backend_EmitObj:
990 return CI.createDefaultOutputFile(true, InFile, "o");
991 }
992
993 llvm_unreachable("Invalid action!");
994}
995
996std::unique_ptr<ASTConsumer>
998 BackendAction BA = static_cast<BackendAction>(Act);
999 std::unique_ptr<raw_pwrite_stream> OS = CI.takeOutputStream();
1000 if (!OS)
1001 OS = GetOutputStream(CI, InFile, BA);
1002
1003 if (BA != Backend_EmitNothing && !OS)
1004 return nullptr;
1005
1006 // Load bitcode modules to link with, if we need to.
1007 if (loadLinkModules(CI))
1008 return nullptr;
1009
1010 CoverageSourceInfo *CoverageInfo = nullptr;
1011 // Add the preprocessor callback only when the coverage mapping is generated.
1012 if (CI.getCodeGenOpts().CoverageMapping)
1014 CI.getPreprocessor());
1015
1016 std::unique_ptr<BackendConsumer> Result(new BackendConsumer(
1017 CI, BA, CI.getVirtualFileSystemPtr(), *VMContext, std::move(LinkModules),
1018 InFile, std::move(OS), CoverageInfo));
1019 BEConsumer = Result.get();
1020
1021 // Enable generating macro debug info only when debug info is not disabled and
1022 // also macro debug info is enabled.
1023 if (CI.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo &&
1024 CI.getCodeGenOpts().MacroDebugInfo) {
1025 std::unique_ptr<PPCallbacks> Callbacks =
1026 std::make_unique<MacroPPCallbacks>(BEConsumer->getCodeGenerator(),
1027 CI.getPreprocessor());
1028 CI.getPreprocessor().addPPCallbacks(std::move(Callbacks));
1029 }
1030
1031 if (CI.getFrontendOpts().GenReducedBMI &&
1032 !CI.getFrontendOpts().ModuleOutputPath.empty()) {
1033 std::vector<std::unique_ptr<ASTConsumer>> Consumers(2);
1034 Consumers[0] = std::make_unique<ReducedBMIGenerator>(
1037 Consumers[1] = std::move(Result);
1038 return std::make_unique<MultiplexConsumer>(std::move(Consumers));
1039 }
1040
1041 return std::move(Result);
1042}
1043
1044std::unique_ptr<llvm::Module>
1045CodeGenAction::loadModule(MemoryBufferRef MBRef) {
1048
1049 auto DiagErrors = [&](Error E) -> std::unique_ptr<llvm::Module> {
1050 unsigned DiagID =
1052 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1053 CI.getDiagnostics().Report(DiagID) << EIB.message();
1054 });
1055 return {};
1056 };
1057
1058 // For ThinLTO backend invocations, ensure that the context
1059 // merges types based on ODR identifiers. We also need to read
1060 // the correct module out of a multi-module bitcode file.
1061 if (!CI.getCodeGenOpts().ThinLTOIndexFile.empty()) {
1062 VMContext->enableDebugTypeODRUniquing();
1063
1064 Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(MBRef);
1065 if (!BMsOrErr)
1066 return DiagErrors(BMsOrErr.takeError());
1067 BitcodeModule *Bm = llvm::lto::findThinLTOModule(*BMsOrErr);
1068 // We have nothing to do if the file contains no ThinLTO module. This is
1069 // possible if ThinLTO compilation was not able to split module. Content of
1070 // the file was already processed by indexing and will be passed to the
1071 // linker using merged object file.
1072 if (!Bm) {
1073 auto M = std::make_unique<llvm::Module>("empty", *VMContext);
1074 M->setTargetTriple(Triple(CI.getTargetOpts().Triple));
1075 return M;
1076 }
1077 Expected<std::unique_ptr<llvm::Module>> MOrErr =
1078 Bm->parseModule(*VMContext);
1079 if (!MOrErr)
1080 return DiagErrors(MOrErr.takeError());
1081 return std::move(*MOrErr);
1082 }
1083
1084 // Load bitcode modules to link with, if we need to.
1085 if (loadLinkModules(CI))
1086 return nullptr;
1087
1088 // Handle textual IR and bitcode file with one single module.
1089 llvm::SMDiagnostic Err;
1090 if (std::unique_ptr<llvm::Module> M = parseIR(MBRef, Err, *VMContext)) {
1091 // For LLVM IR files, always verify the input and report the error in a way
1092 // that does not ask people to report an issue for it.
1093 std::string VerifierErr;
1094 raw_string_ostream VerifierErrStream(VerifierErr);
1095 if (llvm::verifyModule(*M, &VerifierErrStream)) {
1096 CI.getDiagnostics().Report(diag::err_invalid_llvm_ir) << VerifierErr;
1097 return {};
1098 }
1099 return M;
1100 }
1101
1102 // If MBRef is a bitcode with multiple modules (e.g., -fsplit-lto-unit
1103 // output), place the extra modules (actually only one, a regular LTO module)
1104 // into LinkModules as if we are using -mlink-bitcode-file.
1105 Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(MBRef);
1106 if (BMsOrErr && BMsOrErr->size()) {
1107 std::unique_ptr<llvm::Module> FirstM;
1108 for (auto &BM : *BMsOrErr) {
1109 Expected<std::unique_ptr<llvm::Module>> MOrErr =
1110 BM.parseModule(*VMContext);
1111 if (!MOrErr)
1112 return DiagErrors(MOrErr.takeError());
1113 if (FirstM)
1114 LinkModules.push_back({std::move(*MOrErr), /*PropagateAttrs=*/false,
1115 /*Internalize=*/false, /*LinkFlags=*/{}});
1116 else
1117 FirstM = std::move(*MOrErr);
1118 }
1119 if (FirstM)
1120 return FirstM;
1121 }
1122 // If BMsOrErr fails, consume the error and use the error message from
1123 // parseIR.
1124 consumeError(BMsOrErr.takeError());
1125
1126 // Translate from the diagnostic info to the SourceManager location if
1127 // available.
1128 // TODO: Unify this with ConvertBackendLocation()
1129 SourceLocation Loc;
1130 if (Err.getLineNo() > 0) {
1131 assert(Err.getColumnNo() >= 0);
1132 Loc = SM.translateFileLineCol(SM.getFileEntryForID(SM.getMainFileID()),
1133 Err.getLineNo(), Err.getColumnNo() + 1);
1134 }
1135
1136 // Strip off a leading diagnostic code if there is one.
1137 StringRef Msg = Err.getMessage();
1138 Msg.consume_front("error: ");
1139
1140 unsigned DiagID =
1142
1143 CI.getDiagnostics().Report(Loc, DiagID) << Msg;
1144 return {};
1145}
1146
1148 if (getCurrentFileKind().getLanguage() != Language::LLVM_IR) {
1150 return;
1151 }
1152
1153 // If this is an IR file, we have to treat it specially.
1154 BackendAction BA = static_cast<BackendAction>(Act);
1156 auto &CodeGenOpts = CI.getCodeGenOpts();
1157 auto &Diagnostics = CI.getDiagnostics();
1158 std::unique_ptr<raw_pwrite_stream> OS =
1160 if (BA != Backend_EmitNothing && !OS)
1161 return;
1162
1164 FileID FID = SM.getMainFileID();
1165 std::optional<MemoryBufferRef> MainFile = SM.getBufferOrNone(FID);
1166 if (!MainFile)
1167 return;
1168
1169 TheModule = loadModule(*MainFile);
1170 if (!TheModule)
1171 return;
1172
1173 const TargetOptions &TargetOpts = CI.getTargetOpts();
1174 if (TheModule->getTargetTriple().str() != TargetOpts.Triple) {
1175 Diagnostics.Report(SourceLocation(), diag::warn_fe_override_module)
1176 << TargetOpts.Triple;
1177 TheModule->setTargetTriple(Triple(TargetOpts.Triple));
1178 }
1179
1180 EmbedObject(TheModule.get(), CodeGenOpts, CI.getVirtualFileSystem(),
1181 Diagnostics);
1182 EmbedBitcode(TheModule.get(), CodeGenOpts, *MainFile);
1183
1184 LLVMContext &Ctx = TheModule->getContext();
1185
1186 // Restore any diagnostic handler previously set before returning from this
1187 // function.
1188 struct RAII {
1189 LLVMContext &Ctx;
1190 std::unique_ptr<DiagnosticHandler> PrevHandler = Ctx.getDiagnosticHandler();
1191 ~RAII() { Ctx.setDiagnosticHandler(std::move(PrevHandler)); }
1192 } _{Ctx};
1193
1194 // Set clang diagnostic handler. To do this we need to create a fake
1195 // BackendConsumer.
1196 BackendConsumer Result(CI, BA, CI.getVirtualFileSystemPtr(), *VMContext,
1197 std::move(LinkModules), "", nullptr, nullptr,
1198 TheModule.get());
1199
1200 // Link in each pending link module.
1201 if (!CodeGenOpts.LinkBitcodePostopt && Result.LinkInModules(&*TheModule))
1202 return;
1203
1204 // PR44896: Force DiscardValueNames as false. DiscardValueNames cannot be
1205 // true here because the valued names are needed for reading textual IR.
1206 Ctx.setDiscardValueNames(false);
1207 Ctx.setDiagnosticHandler(
1208 std::make_unique<ClangDiagnosticHandler>(CodeGenOpts, &Result));
1209
1210 Ctx.setDefaultTargetCPU(TargetOpts.CPU);
1211 Ctx.setDefaultTargetFeatures(llvm::join(TargetOpts.Features, ","));
1212
1213 Expected<LLVMRemarkFileHandle> OptRecordFileOrErr =
1214 setupLLVMOptimizationRemarks(
1215 Ctx, CodeGenOpts.OptRecordFile, CodeGenOpts.OptRecordPasses,
1216 CodeGenOpts.OptRecordFormat, CodeGenOpts.DiagnosticsWithHotness,
1217 CodeGenOpts.DiagnosticsHotnessThreshold);
1218
1219 if (Error E = OptRecordFileOrErr.takeError()) {
1220 reportOptRecordError(std::move(E), Diagnostics, CodeGenOpts);
1221 return;
1222 }
1223 LLVMRemarkFileHandle OptRecordFile = std::move(*OptRecordFileOrErr);
1224
1226 CI.getTarget().getDataLayoutString(), TheModule.get(), BA,
1228 std::move(OS));
1229 if (OptRecordFile)
1230 OptRecordFile->keep();
1231}
1232
1233//
1234
1235void EmitAssemblyAction::anchor() { }
1236EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
1237 : CodeGenAction(Backend_EmitAssembly, _VMContext) {}
1238
1239void EmitBCAction::anchor() { }
1240EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
1241 : CodeGenAction(Backend_EmitBC, _VMContext) {}
1242
1243void EmitLLVMAction::anchor() { }
1244EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
1245 : CodeGenAction(Backend_EmitLL, _VMContext) {}
1246
1247void EmitLLVMOnlyAction::anchor() { }
1248EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
1249 : CodeGenAction(Backend_EmitNothing, _VMContext) {}
1250
1251void EmitCodeGenOnlyAction::anchor() { }
1253 : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
1254
1255void EmitObjAction::anchor() { }
1256EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
1257 : CodeGenAction(Backend_EmitObj, _VMContext) {}
Defines the clang::ASTContext interface.
#define ComputeDiagID(Severity, GroupName, DiagID)
static std::unique_ptr< raw_pwrite_stream > GetOutputStream(CompilerInstance &CI, StringRef InFile, BackendAction Action)
#define ComputeDiagRemarkID(Severity, GroupName, DiagID)
static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D, SourceManager &CSM)
ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr buffer to be a valid FullS...
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::FileManager interface and associated types.
unsigned IsFirst
Indicates that this is the first token of the file.
#define SM(sm)
Defines the clang::Preprocessor interface.
Defines the SourceManager interface.
virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *D)
HandleCXXStaticMemberVarInstantiation - Tell the consumer that this.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
llvm::Module * getModule() const
void CompleteExternalDeclaration(DeclaratorDecl *D) override
CompleteExternalDeclaration - Callback invoked at the end of a translation unit to notify the consume...
void OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationBase &D)
bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D)
Specialized handler for StackSize diagnostic.
void HandleVTable(CXXRecordDecl *RD) override
Callback involved at the end of a translation unit to notify the consumer that a vtable for the given...
void HandleTagDeclDefinition(TagDecl *D) override
HandleTagDeclDefinition - This callback is invoked each time a TagDecl (e.g.
bool HandleTopLevelDecl(DeclGroupRef D) override
HandleTopLevelDecl - Handle the specified top-level declaration.
void Initialize(ASTContext &Ctx) override
Initialize - This is called to initialize the consumer, providing the ASTContext.
void HandleInlineFunctionDefinition(FunctionDecl *D) override
This callback is invoked each time an inline (method or friend) function definition in a class is com...
void OptimizationFailureHandler(const llvm::DiagnosticInfoOptimizationFailure &D)
void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI)
This function is invoked when the backend needs to report something to the user.
void HandleTagDeclRequiredDefinition(const TagDecl *D) override
This callback is invoked the first time each TagDecl is required to be complete.
void HandleInterestingDecl(DeclGroupRef D) override
HandleInterestingDecl - Handle the specified interesting declaration.
void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override
HandleCXXStaticMemberVarInstantiation - Tell the consumer that this.
std::optional< FullSourceLoc > getFunctionSourceLocation(const llvm::Function &F) const
bool ResourceLimitDiagHandler(const llvm::DiagnosticInfoResourceLimit &D)
Specialized handler for ResourceLimit diagnostic.
std::unique_ptr< llvm::Module > takeModule()
void AssignInheritanceModel(CXXRecordDecl *RD) override
Callback invoked when an MSInheritanceAttr has been attached to a CXXRecordDecl.
void HandleTranslationUnit(ASTContext &C) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
void CompleteTentativeDefinition(VarDecl *D) override
CompleteTentativeDefinition - Callback invoked at the end of a translation unit to notify the consume...
void UnsupportedDiagHandler(const llvm::DiagnosticInfoUnsupported &D)
Specialized handler for unsupported backend feature diagnostic.
bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D)
Specialized handler for InlineAsm diagnostic.
bool LinkInModules(llvm::Module *M)
const FullSourceLoc getBestLocationFromDebugLoc(const llvm::DiagnosticInfoWithLocationBase &D, bool &BadDebugInfo, StringRef &Filename, unsigned &Line, unsigned &Column) const
Get the best possible source location to represent a diagnostic that may have associated debug info.
void EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase &D, unsigned DiagID)
Specialized handlers for optimization remarks.
void DontCallDiagHandler(const llvm::DiagnosticInfoDontCall &D)
void MisExpectDiagHandler(const llvm::DiagnosticInfoMisExpect &D)
Specialized handler for misexpect warnings.
CodeGenerator * getCodeGenerator()
void SrcMgrDiagHandler(const llvm::DiagnosticInfoSrcMgr &D)
Specialized handler for diagnostics reported using SMDiagnostic.
BackendConsumer(CompilerInstance &CI, BackendAction Action, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, llvm::LLVMContext &C, SmallVector< LinkModule, 4 > LinkModules, StringRef InFile, std::unique_ptr< raw_pwrite_stream > OS, CoverageSourceInfo *CoverageInfo, llvm::Module *CurLinkModule=nullptr)
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
bool isMissedOptRemarkEnabled(StringRef PassName) const override
bool handleDiagnostics(const DiagnosticInfo &DI) override
ClangDiagnosticHandler(const CodeGenOptions &CGOpts, BackendConsumer *BCon)
bool isPassedOptRemarkEnabled(StringRef PassName) const override
bool isAnyRemarkEnabled() const override
bool isAnalysisRemarkEnabled(StringRef PassName) const override
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
CodeGenerator * getCodeGenerator() const
friend class BackendConsumer
void EndSourceFileAction() override
Callback at the end of processing a single input.
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
CodeGenAction(unsigned _Act, llvm::LLVMContext *_VMContext=nullptr)
Create a new code generation action.
llvm::LLVMContext * takeLLVMContext()
Take the LLVM context used by this action.
BackendConsumer * BEConsumer
bool hasIRSupport() const override
Does this action support use with IR files?
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
std::unique_ptr< llvm::Module > takeModule()
Take the generated LLVM module, for use after the action has been run.
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
std::string OptRecordFile
The name of the file to which the backend should save YAML optimization records.
std::vector< BitcodeFileToLink > LinkBitcodeFiles
The files specified here are linked in to the module before optimizations.
std::string OptRecordPasses
The regex that filters the passes that should be saved to the optimization records.
std::string ThinLTOIndexFile
Name of the function summary index file to use for ThinLTO function importing.
std::string OptRecordFormat
The format used for serializing remarks (default: YAML)
static CoverageSourceInfo * setUpCoverageCallbacks(Preprocessor &PP)
The primary public interface to the Clang code generator.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
std::unique_ptr< raw_pwrite_stream > createDefaultOutputFile(bool Binary=true, StringRef BaseInput="", StringRef Extension="", bool RemoveFileOnSignal=true, bool CreateMissingDirectories=false, bool ForceUseTemporary=false)
Create the default output file (from the invocation's options) and add it to the list of tracked outp...
FileManager & getFileManager() const
Return the current file manager to the caller.
ModuleCache & getModuleCache() const
Preprocessor & getPreprocessor() const
Return the current preprocessor.
IntrusiveRefCntPtr< llvm::vfs::FileSystem > getVirtualFileSystemPtr() const
TargetOptions & getTargetOpts()
std::unique_ptr< llvm::raw_pwrite_stream > takeOutputStream()
FrontendOptions & getFrontendOpts()
TargetInfo & getTarget() const
llvm::vfs::FileSystem & getVirtualFileSystem() const
CodeGenOptions & getCodeGenOpts()
SourceManager & getSourceManager() const
Return the current source manager.
std::unique_ptr< raw_pwrite_stream > createNullOutputFile()
Stores additional source code information like skipped ranges which is required by the coverage mappi...
iterator begin()
Definition DeclGroup.h:95
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
Represents a ValueDecl that came out of a declarator.
Definition Decl.h:780
A little helper class used to produce diagnostics.
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:233
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
unsigned getCustomDiagID(Level L, const char(&FormatString)[N])
Return an ID for a diagnostic with the specified format string and level.
Definition Diagnostic.h:914
EmitAssemblyAction(llvm::LLVMContext *_VMContext=nullptr)
EmitBCAction(llvm::LLVMContext *_VMContext=nullptr)
EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext=nullptr)
EmitLLVMAction(llvm::LLVMContext *_VMContext=nullptr)
EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext=nullptr)
EmitObjAction(llvm::LLVMContext *_VMContext=nullptr)
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Implements support for file system lookup, file system caching, and directory search management.
Definition FileManager.h:53
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(FileEntryRef Entry, bool isVolatile=false, bool RequiresNullTerminator=true, std::optional< int64_t > MaybeLimit=std::nullopt, bool IsText=true)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > getVirtualFileSystemPtr() const
OptionalFileEntryRef getOptionalFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true, bool IsText=true)
Get a FileEntryRef if it exists, without doing anything on error.
InputKind getCurrentFileKind() const
virtual void EndSourceFileAction()
Callback at the end of processing a single input.
CompilerInstance & getCompilerInstance() const
virtual bool BeginSourceFileAction(CompilerInstance &CI)
Callback at the start of processing a single input.
StringRef getCurrentFileOrBufferName() const
unsigned GenReducedBMI
Whether to generate reduced BMI for C++20 named modules.
std::string ModuleOutputPath
Output Path for module output file.
A SourceLocation and its associated SourceManager.
Represents a function declaration or definition.
Definition Decl.h:2015
@ CMK_ModuleInterface
Compiling a C++ modules interface unit.
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
PrettyStackTraceDecl - If a crash occurs, indicate that it happened when doing something to a specifi...
Definition DeclBase.h:1313
Encodes a location in the source.
static SourceLocation getFromRawEncoding(UIntTy Encoding)
Turn a raw encoding of a SourceLocation object into a real SourceLocation.
bool isValid() const
Return true if this is a valid SourceLocation object.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
This class handles loading and caching of source files into memory.
FileID createFileID(FileEntryRef SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, int LoadedID=0, SourceLocation::UIntTy LoadedOffset=0)
Create a new FileID that represents the specified file being #included from the specified IncludePosi...
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file.
A trivial tuple used to represent a source range.
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3732
const char * getDataLayoutString() const
Options for controlling the target.
std::string Triple
The name of the target triple to compile for.
std::vector< std::string > Features
The list of target specific features to enable or disable – this should be a list of strings starting...
std::string CPU
If given, the name of the target CPU to generate code for.
Represents a variable declaration or definition.
Definition Decl.h:926
Defines the clang::TargetInfo interface.
void mergeDefaultFunctionDefinitionAttributes(llvm::Function &F, const CodeGenOptions &CodeGenOpts, const LangOptions &LangOpts, const TargetOptions &TargetOpts, bool WillInternalize)
Adds attributes to F according to our CodeGenOpts and LangOpts, as though we had emitted it ourselves...
Definition CGCall.cpp:2239
The JSON file list parser is used to communicate input to InstallAPI.
static void reportOptRecordError(Error E, DiagnosticsEngine &Diags, const CodeGenOptions &CodeGenOpts)
@ Result
The result type of a method or function.
Definition TypeBase.h:905
void emitBackendOutput(CompilerInstance &CI, CodeGenOptions &CGOpts, StringRef TDesc, llvm::Module *M, BackendAction Action, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, std::unique_ptr< raw_pwrite_stream > OS, BackendConsumer *BC=nullptr)
void EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts, llvm::vfs::FileSystem &VFS, DiagnosticsEngine &Diags)
void EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts, llvm::MemoryBufferRef Buf)
BackendAction
Definition BackendUtil.h:33
@ Backend_EmitAssembly
Emit native assembly files.
Definition BackendUtil.h:34
@ Backend_EmitLL
Emit human-readable LLVM assembly.
Definition BackendUtil.h:36
@ Backend_EmitBC
Emit LLVM bitcode files.
Definition BackendUtil.h:35
@ Backend_EmitObj
Emit native object files.
Definition BackendUtil.h:39
@ Backend_EmitMCNull
Run CodeGen, but don't emit anything.
Definition BackendUtil.h:38
@ Backend_EmitNothing
Don't emit anything (benchmarking mode)
Definition BackendUtil.h:37
std::unique_ptr< CodeGenerator > CreateLLVMCodeGen(const CompilerInstance &CI, llvm::StringRef ModuleName, llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo=nullptr)
CreateLLVMCodeGen - Create a CodeGenerator instance.
U cast(CodeGen::Address addr)
Definition Address.h:327
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
hash_code hash_value(const clang::dependencies::ModuleID &ID)