| //===--- BackendDiagnosticHandler.cpp - Shared Backend Diagnostics ------===// | |
| // | |
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |
| // See https://llvm.org/LICENSE.txt for license information. | |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |
| // | |
| //===----------------------------------------------------------------------===// | |
| #include "clang/CodeGenUtils/BackendDiagnosticHandler.h" | |
| #include "clang/Basic/CodeGenOptions.h" | |
| #include "clang/Basic/Diagnostic.h" | |
| #include "clang/Basic/DiagnosticFrontend.h" | |
| #include "clang/Basic/FileManager.h" | |
| #include "clang/Basic/SourceManager.h" | |
| #include "llvm/ADT/Hashing.h" | |
| #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" | |
| #include "llvm/Demangle/Demangle.h" | |
| #include "llvm/IR/DiagnosticInfo.h" | |
| #include "llvm/IR/DiagnosticPrinter.h" | |
| #include "llvm/IR/Function.h" | |
| #include "llvm/IR/Module.h" | |
| #include "llvm/Support/raw_ostream.h" | |
| using namespace clang; | |
| using namespace llvm; | |
| namespace { | |
| /// Forwards llvm::DiagnosticHandler callbacks into a BackendDiagnosticConsumer. | |
| class BackendDiagnosticHandlerImpl final : public DiagnosticHandler { | |
| public: | |
| BackendDiagnosticHandlerImpl(const CodeGenOptions &CGOpts, | |
| BackendDiagnosticConsumer *Consumer) | |
| : CodeGenOpts(CGOpts), Consumer(Consumer) {} | |
| bool handleDiagnostics(const DiagnosticInfo &DI) override { | |
| Consumer->handleDiagnostics(DI); | |
| return true; | |
| } | |
| bool isAnalysisRemarkEnabled(StringRef PassName) const override { | |
| return CodeGenOpts.OptimizationRemarkAnalysis.patternMatches(PassName); | |
| } | |
| bool isMissedOptRemarkEnabled(StringRef PassName) const override { | |
| return CodeGenOpts.OptimizationRemarkMissed.patternMatches(PassName); | |
| } | |
| bool isPassedOptRemarkEnabled(StringRef PassName) const override { | |
| return CodeGenOpts.OptimizationRemark.patternMatches(PassName); | |
| } | |
| bool isAnyRemarkEnabled() const override { | |
| return CodeGenOpts.OptimizationRemarkAnalysis.hasValidPattern() || | |
| CodeGenOpts.OptimizationRemarkMissed.hasValidPattern() || | |
| CodeGenOpts.OptimizationRemark.hasValidPattern(); | |
| } | |
| private: | |
| const CodeGenOptions &CodeGenOpts; | |
| BackendDiagnosticConsumer *Consumer; | |
| }; | |
| /// Convert a location in a temporary llvm::SourceMgr buffer to be a valid | |
| /// FullSourceLoc. | |
| FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D, | |
| SourceManager &CSM) { | |
| // Get both the clang and llvm source managers. The location is relative to | |
| // a memory buffer that the LLVM Source Manager is handling, we need to add | |
| // a copy to the Clang source manager. | |
| const llvm::SourceMgr &LSM = *D.getSourceMgr(); | |
| // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr | |
| // already owns its one and clang::SourceManager wants to own its one. | |
| const MemoryBuffer *LBuf = | |
| LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc())); | |
| // Create the copy and transfer ownership to clang::SourceManager. | |
| // TODO: Avoid copying files into memory. | |
| std::unique_ptr<llvm::MemoryBuffer> CBuf = | |
| llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(), | |
| LBuf->getBufferIdentifier()); | |
| // FIXME: Keep a file ID map instead of creating new IDs for each location. | |
| FileID FID = CSM.createFileID(std::move(CBuf)); | |
| // Translate the offset into the file. | |
| unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart(); | |
| SourceLocation NewLoc = | |
| CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset); | |
| return FullSourceLoc(NewLoc, CSM); | |
| } | |
| } // namespace | |
| #define ComputeDiagID(Severity, GroupName, DiagID) \ | |
| do { \ | |
| switch (Severity) { \ | |
| case llvm::DS_Error: \ | |
| DiagID = diag::err_fe_##GroupName; \ | |
| break; \ | |
| case llvm::DS_Warning: \ | |
| DiagID = diag::warn_fe_##GroupName; \ | |
| break; \ | |
| case llvm::DS_Remark: \ | |
| llvm_unreachable("'remark' severity not expected"); \ | |
| break; \ | |
| case llvm::DS_Note: \ | |
| DiagID = diag::note_fe_##GroupName; \ | |
| break; \ | |
| } \ | |
| } while (false) | |
| #define ComputeDiagRemarkID(Severity, GroupName, DiagID) \ | |
| do { \ | |
| switch (Severity) { \ | |
| case llvm::DS_Error: \ | |
| DiagID = diag::err_fe_##GroupName; \ | |
| break; \ | |
| case llvm::DS_Warning: \ | |
| DiagID = diag::warn_fe_##GroupName; \ | |
| break; \ | |
| case llvm::DS_Remark: \ | |
| DiagID = diag::remark_fe_##GroupName; \ | |
| break; \ | |
| case llvm::DS_Note: \ | |
| DiagID = diag::note_fe_##GroupName; \ | |
| break; \ | |
| } \ | |
| } while (false) | |
| std::unique_ptr<llvm::DiagnosticHandler> | |
| BackendDiagnosticConsumer::createDiagnosticHandler() { | |
| return std::make_unique<BackendDiagnosticHandlerImpl>(CodeGenOpts, this); | |
| } | |
| void BackendDiagnosticConsumer::addFunctionSourceLocation(StringRef MangledName, | |
| FullSourceLoc Loc) { | |
| // TODO: use a fast content hash when available. | |
| auto NameHash = llvm::hash_value(MangledName); | |
| ManglingFullSourceLocs.push_back(std::make_pair(NameHash, Loc)); | |
| } | |
| std::optional<FullSourceLoc> | |
| BackendDiagnosticConsumer::getFunctionSourceLocation(const Function &F) const { | |
| auto Hash = llvm::hash_value(F.getName()); | |
| for (const auto &Pair : ManglingFullSourceLocs) { | |
| if (Pair.first == Hash) | |
| return Pair.second; | |
| } | |
| return std::nullopt; | |
| } | |
| const FullSourceLoc BackendDiagnosticConsumer::getBestLocationFromDebugLoc( | |
| const llvm::DiagnosticInfoWithLocationBase &D, bool &BadDebugInfo, | |
| StringRef &Filename, unsigned &Line, unsigned &Column) const { | |
| SourceManager &SourceMgr = *SM; | |
| FileManager &FileMgr = SourceMgr.getFileManager(); | |
| SourceLocation DILoc; | |
| if (D.isLocationAvailable()) { | |
| D.getLocation(Filename, Line, Column); | |
| if (Line > 0) { | |
| auto FE = FileMgr.getOptionalFileRef(Filename); | |
| if (!FE) | |
| FE = FileMgr.getOptionalFileRef(D.getAbsolutePath()); | |
| if (FE) { | |
| // If -gcolumn-info was not used, Column will be 0. This upsets the | |
| // source manager, so pass 1 if Column is not set. | |
| DILoc = SourceMgr.translateFileLineCol(*FE, Line, Column ? Column : 1); | |
| } | |
| } | |
| BadDebugInfo = DILoc.isInvalid(); | |
| } | |
| // If a location isn't available, try to approximate it using the associated | |
| // function definition. We use the definition's right brace to differentiate | |
| // from diagnostics that genuinely relate to the function itself. | |
| FullSourceLoc Loc(DILoc, SourceMgr); | |
| if (Loc.isInvalid()) { | |
| if (auto MaybeLoc = getFunctionSourceLocation(D.getFunction())) | |
| Loc = *MaybeLoc; | |
| } | |
| if (DILoc.isInvalid() && D.isLocationAvailable()) | |
| // If we were not able to translate the file:line:col information | |
| // back to a SourceLocation, at least emit a note stating that | |
| // we could not translate this location. This can happen in the | |
| // case of #line directives. | |
| Diags.Report(Loc, diag::note_fe_backend_invalid_loc) | |
| << Filename << Line << Column; | |
| return Loc; | |
| } | |
| void BackendDiagnosticConsumer::SrcMgrDiagHandler( | |
| const llvm::DiagnosticInfoSrcMgr &DI) { | |
| const llvm::SMDiagnostic &D = DI.getSMDiag(); | |
| unsigned DiagID; | |
| if (DI.isInlineAsmDiag()) | |
| ComputeDiagID(DI.getSeverity(), inline_asm, DiagID); | |
| else | |
| ComputeDiagID(DI.getSeverity(), source_mgr, DiagID); | |
| // This is for the empty consumer that uses the clang diagnostic | |
| // handler for IR input files. | |
| if (!SM) { | |
| D.print(nullptr, llvm::errs()); | |
| Diags.Report(DiagID).AddString("cannot compile inline asm"); | |
| return; | |
| } | |
| // There are a couple of different kinds of errors we could get here. | |
| // First, we re-format the SMDiagnostic in terms of a clang diagnostic. | |
| // Strip "error: " off the start of the message string. | |
| StringRef Message = D.getMessage(); | |
| (void)Message.consume_front("error: "); | |
| // If the SMDiagnostic has an inline asm source location, translate it. | |
| FullSourceLoc Loc; | |
| if (D.getLoc() != SMLoc()) | |
| Loc = ConvertBackendLocation(D, *SM); | |
| // If this problem has clang-level source location information, report the | |
| // issue in the source with a note showing the instantiated | |
| // code. | |
| if (DI.isInlineAsmDiag()) { | |
| SourceLocation LocCookie = | |
| SourceLocation::getFromRawEncoding(DI.getLocCookie()); | |
| if (LocCookie.isValid()) { | |
| Diags.Report(LocCookie, DiagID).AddString(Message); | |
| if (D.getLoc().isValid()) { | |
| DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here); | |
| // Convert the SMDiagnostic ranges into SourceRange and attach them | |
| // to the diagnostic. | |
| for (const std::pair<unsigned, unsigned> &Range : D.getRanges()) { | |
| unsigned Column = D.getColumnNo(); | |
| B << SourceRange(Loc.getLocWithOffset(Range.first - Column), | |
| Loc.getLocWithOffset(Range.second - Column)); | |
| } | |
| } | |
| return; | |
| } | |
| } | |
| // Otherwise, report the backend issue as occurring in the generated .s file. | |
| // If Loc is invalid, we still need to report the issue, it just gets no | |
| // location info. | |
| Diags.Report(Loc, DiagID).AddString(Message); | |
| } | |
| bool BackendDiagnosticConsumer::InlineAsmDiagHandler( | |
| const llvm::DiagnosticInfoInlineAsm &D) { | |
| unsigned DiagID; | |
| ComputeDiagID(D.getSeverity(), inline_asm, DiagID); | |
| std::string Message = D.getMsgStr().str(); | |
| // If this problem has clang-level source location information, report the | |
| // issue as being a problem in the source with a note showing the instantiated | |
| // code. | |
| SourceLocation LocCookie = | |
| SourceLocation::getFromRawEncoding(D.getLocCookie()); | |
| if (LocCookie.isValid()) | |
| Diags.Report(LocCookie, DiagID).AddString(Message); | |
| else { | |
| // Otherwise, report the backend diagnostic as occurring in the generated | |
| // .s file. | |
| // If Loc is invalid, we still need to report the diagnostic, it just gets | |
| // no location info. | |
| FullSourceLoc Loc; | |
| Diags.Report(Loc, DiagID).AddString(Message); | |
| } | |
| // We handled all the possible severities. | |
| return true; | |
| } | |
| bool BackendDiagnosticConsumer::StackSizeDiagHandler( | |
| const llvm::DiagnosticInfoStackSize &D) { | |
| if (D.getSeverity() != llvm::DS_Warning) | |
| // For now, the only support we have for StackSize diagnostic is warning. | |
| // We do not know how to format other severities. | |
| return false; | |
| auto Loc = getFunctionSourceLocation(D.getFunction()); | |
| if (!Loc) | |
| return false; | |
| Diags.Report(*Loc, diag::warn_fe_frame_larger_than) | |
| << D.getStackSize() << D.getStackLimit() | |
| << llvm::demangle(D.getFunction().getName()); | |
| return true; | |
| } | |
| bool BackendDiagnosticConsumer::ResourceLimitDiagHandler( | |
| const llvm::DiagnosticInfoResourceLimit &D) { | |
| auto Loc = getFunctionSourceLocation(D.getFunction()); | |
| if (!Loc) | |
| return false; | |
| unsigned DiagID = diag::err_fe_backend_resource_limit; | |
| ComputeDiagID(D.getSeverity(), backend_resource_limit, DiagID); | |
| Diags.Report(*Loc, DiagID) | |
| << D.getResourceName() << D.getResourceSize() << D.getResourceLimit() | |
| << llvm::demangle(D.getFunction().getName()); | |
| return true; | |
| } | |
| void BackendDiagnosticConsumer::UnsupportedDiagHandler( | |
| const llvm::DiagnosticInfoUnsupported &D) { | |
| // We only support warnings or errors. | |
| assert(D.getSeverity() == llvm::DS_Error || | |
| D.getSeverity() == llvm::DS_Warning); | |
| StringRef Filename; | |
| unsigned Line, Column; | |
| bool BadDebugInfo = false; | |
| FullSourceLoc Loc; | |
| std::string Msg; | |
| raw_string_ostream MsgStream(Msg); | |
| // SM will be null for IR input files, we will construct the diag | |
| // message from llvm::DiagnosticInfoUnsupported. | |
| if (SM != nullptr) { | |
| Loc = getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column); | |
| MsgStream << D.getMessage(); | |
| } else { | |
| DiagnosticPrinterRawOStream DP(MsgStream); | |
| D.print(DP); | |
| } | |
| auto DiagType = D.getSeverity() == llvm::DS_Error | |
| ? diag::err_fe_backend_unsupported | |
| : diag::warn_fe_backend_unsupported; | |
| Diags.Report(Loc, DiagType) << Msg; | |
| if (BadDebugInfo) | |
| // If we were not able to translate the file:line:col information | |
| // back to a SourceLocation, at least emit a note stating that | |
| // we could not translate this location. This can happen in the | |
| // case of #line directives. | |
| Diags.Report(Loc, diag::note_fe_backend_invalid_loc) | |
| << Filename << Line << Column; | |
| } | |
| void BackendDiagnosticConsumer::UnsupportedTargetIntrinsicDiagHandler( | |
| const llvm::DiagnosticInfoUnsupportedTargetIntrinsic &D) { | |
| assert(D.getSeverity() == llvm::DS_Error && | |
| "unsupported target intrinsic diagnostic should be an error"); | |
| StringRef Filename; | |
| unsigned Line, Column; | |
| bool BadDebugInfo = false; | |
| FullSourceLoc Loc; | |
| std::string Msg; | |
| raw_string_ostream MsgStream(Msg); | |
| // SM will be null for IR input files, so construct the diagnostic | |
| // message from llvm::DiagnosticInfoUnsupportedTargetIntrinsic. | |
| if (SM != nullptr) { | |
| Loc = getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column); | |
| MsgStream << D.getMessage(); | |
| } else { | |
| DiagnosticPrinterRawOStream DP(MsgStream); | |
| D.print(DP); | |
| } | |
| Diags.Report(Loc, diag::err_fe_backend_unsupported) << Msg; | |
| if (BadDebugInfo) { | |
| // If we were not able to translate the file:line:col information | |
| // back to a SourceLocation, at least emit a note stating that | |
| // we could not translate this location. This can happen in the | |
| // case of #line directives. | |
| Diags.Report(Loc, diag::note_fe_backend_invalid_loc) | |
| << Filename << Line << Column; | |
| } | |
| } | |
| void BackendDiagnosticConsumer::EmitOptimizationMessage( | |
| const llvm::DiagnosticInfoOptimizationBase &D, unsigned DiagID) { | |
| // We only support warnings and remarks. | |
| assert(D.getSeverity() == llvm::DS_Remark || | |
| D.getSeverity() == llvm::DS_Warning); | |
| StringRef Filename; | |
| unsigned Line, Column; | |
| bool BadDebugInfo = false; | |
| FullSourceLoc Loc; | |
| std::string Msg; | |
| raw_string_ostream MsgStream(Msg); | |
| // SM will be null for IR input files, we will construct the remark | |
| // message from llvm::DiagnosticInfoOptimizationBase. | |
| if (SM != nullptr) { | |
| Loc = getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column); | |
| MsgStream << D.getMsg(); | |
| } else { | |
| DiagnosticPrinterRawOStream DP(MsgStream); | |
| D.print(DP); | |
| } | |
| if (D.getHotness()) | |
| MsgStream << " (hotness: " << *D.getHotness() << ")"; | |
| Diags.Report(Loc, DiagID) << AddFlagValue(D.getPassName()) << Msg; | |
| if (BadDebugInfo) | |
| // If we were not able to translate the file:line:col information | |
| // back to a SourceLocation, at least emit a note stating that | |
| // we could not translate this location. This can happen in the | |
| // case of #line directives. | |
| Diags.Report(Loc, diag::note_fe_backend_invalid_loc) | |
| << Filename << Line << Column; | |
| } | |
| void BackendDiagnosticConsumer::OptimizationRemarkHandler( | |
| const llvm::DiagnosticInfoOptimizationBase &D) { | |
| // Without hotness information, don't show noisy remarks. | |
| if (D.isVerbose() && !D.getHotness()) | |
| return; | |
| if (D.isPassed()) { | |
| // Optimization remarks are active only if the -Rpass flag has a regular | |
| // expression that matches the name of the pass name in \p D. | |
| if (CodeGenOpts.OptimizationRemark.patternMatches(D.getPassName())) | |
| EmitOptimizationMessage(D, diag::remark_fe_backend_optimization_remark); | |
| } else if (D.isMissed()) { | |
| // Missed optimization remarks are active only if the -Rpass-missed | |
| // flag has a regular expression that matches the name of the pass | |
| // name in \p D. | |
| if (CodeGenOpts.OptimizationRemarkMissed.patternMatches(D.getPassName())) | |
| EmitOptimizationMessage( | |
| D, diag::remark_fe_backend_optimization_remark_missed); | |
| } else { | |
| assert(D.isAnalysis() && "Unknown remark type"); | |
| bool ShouldAlwaysPrint = false; | |
| if (auto *ORA = dyn_cast<llvm::OptimizationRemarkAnalysis>(&D)) | |
| ShouldAlwaysPrint = ORA->shouldAlwaysPrint(); | |
| if (ShouldAlwaysPrint || | |
| CodeGenOpts.OptimizationRemarkAnalysis.patternMatches(D.getPassName())) | |
| EmitOptimizationMessage( | |
| D, diag::remark_fe_backend_optimization_remark_analysis); | |
| } | |
| } | |
| void BackendDiagnosticConsumer::OptimizationRemarkHandler( | |
| const llvm::OptimizationRemarkAnalysisFPCommute &D) { | |
| // Optimization analysis remarks are active if the pass name is set to | |
| // llvm::DiagnosticInfo::AlwaysPrint or if the -Rpass-analysis flag has a | |
| // regular expression that matches the name of the pass name in \p D. | |
| if (D.shouldAlwaysPrint() || | |
| CodeGenOpts.OptimizationRemarkAnalysis.patternMatches(D.getPassName())) | |
| EmitOptimizationMessage( | |
| D, diag::remark_fe_backend_optimization_remark_analysis_fpcommute); | |
| } | |
| void BackendDiagnosticConsumer::OptimizationRemarkHandler( | |
| const llvm::OptimizationRemarkAnalysisAliasing &D) { | |
| // Optimization analysis remarks are active if the pass name is set to | |
| // llvm::DiagnosticInfo::AlwaysPrint or if the -Rpass-analysis flag has a | |
| // regular expression that matches the name of the pass name in \p D. | |
| if (D.shouldAlwaysPrint() || | |
| CodeGenOpts.OptimizationRemarkAnalysis.patternMatches(D.getPassName())) | |
| EmitOptimizationMessage( | |
| D, diag::remark_fe_backend_optimization_remark_analysis_aliasing); | |
| } | |
| void BackendDiagnosticConsumer::OptimizationFailureHandler( | |
| const llvm::DiagnosticInfoOptimizationFailure &D) { | |
| EmitOptimizationMessage(D, diag::warn_fe_backend_optimization_failure); | |
| } | |
| void BackendDiagnosticConsumer::DontCallDiagHandler( | |
| const DiagnosticInfoDontCall &D) { | |
| SourceLocation LocCookie = | |
| SourceLocation::getFromRawEncoding(D.getLocCookie()); | |
| // FIXME: we can't yet diagnose indirect calls. When/if we can, we | |
| // should instead assert that LocCookie.isValid(). | |
| if (!LocCookie.isValid()) | |
| return; | |
| Diags.Report(LocCookie, D.getSeverity() == DiagnosticSeverity::DS_Error | |
| ? diag::err_fe_backend_error_attr | |
| : diag::warn_fe_backend_warning_attr) | |
| << llvm::demangle(D.getFunctionName()) << D.getNote(); | |
| if (!CodeGenOpts.ShowInliningChain) | |
| return; | |
| auto EmitNote = [&](SourceLocation Loc, StringRef FuncName, bool IsFirst) { | |
| if (!Loc.isValid()) | |
| Loc = LocCookie; | |
| unsigned DiagID = | |
| IsFirst ? diag::note_fe_backend_in : diag::note_fe_backend_inlined; | |
| Diags.Report(Loc, DiagID) << llvm::demangle(FuncName.str()); | |
| }; | |
| // Try debug info first for accurate source locations. | |
| if (!D.getDebugInlineChain().empty()) { | |
| SourceManager &SrcMgr = *SM; | |
| FileManager &FM = SrcMgr.getFileManager(); | |
| for (const auto &[I, Info] : llvm::enumerate(D.getDebugInlineChain())) { | |
| SourceLocation Loc; | |
| if (Info.Line > 0) | |
| if (auto FE = FM.getOptionalFileRef(Info.Filename)) | |
| Loc = SrcMgr.translateFileLineCol(*FE, Info.Line, | |
| Info.Column ? Info.Column : 1); | |
| EmitNote(Loc, Info.FuncName, I == 0); | |
| } | |
| return; | |
| } | |
| // Fall back to heuristic (srcloc metadata) when debug info is unavailable. | |
| auto InliningDecisions = D.getInliningDecisions(); | |
| if (InliningDecisions.empty()) | |
| return; | |
| for (const auto &[I, Entry] : llvm::enumerate(InliningDecisions)) { | |
| SourceLocation Loc = | |
| I == 0 ? LocCookie : SourceLocation::getFromRawEncoding(Entry.second); | |
| EmitNote(Loc, Entry.first, I == 0); | |
| } | |
| // Suggest enabling debug info (at least -gline-directives-only) for more | |
| // accurate locations. | |
| Diags.Report(LocCookie, diag::note_fe_backend_inlining_debug_info); | |
| } | |
| void BackendDiagnosticConsumer::MisExpectDiagHandler( | |
| const llvm::DiagnosticInfoMisExpect &D) { | |
| StringRef Filename; | |
| unsigned Line, Column; | |
| bool BadDebugInfo = false; | |
| FullSourceLoc Loc = | |
| getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column); | |
| Diags.Report(Loc, diag::warn_profile_data_misexpect) << D.getMsg().str(); | |
| if (BadDebugInfo) | |
| // If we were not able to translate the file:line:col information | |
| // back to a SourceLocation, at least emit a note stating that | |
| // we could not translate this location. This can happen in the | |
| // case of #line directives. | |
| Diags.Report(Loc, diag::note_fe_backend_invalid_loc) | |
| << Filename << Line << Column; | |
| } | |
| void BackendDiagnosticConsumer::handleDiagnostics(const DiagnosticInfo &DI) { | |
| unsigned DiagID = diag::err_fe_inline_asm; | |
| llvm::DiagnosticSeverity Severity = DI.getSeverity(); | |
| // Get the diagnostic ID based. | |
| switch (DI.getKind()) { | |
| case llvm::DK_InlineAsm: | |
| if (InlineAsmDiagHandler(cast<DiagnosticInfoInlineAsm>(DI))) | |
| return; | |
| ComputeDiagID(Severity, inline_asm, DiagID); | |
| break; | |
| case llvm::DK_SrcMgr: | |
| SrcMgrDiagHandler(cast<DiagnosticInfoSrcMgr>(DI)); | |
| return; | |
| case llvm::DK_StackSize: | |
| if (StackSizeDiagHandler(cast<DiagnosticInfoStackSize>(DI))) | |
| return; | |
| ComputeDiagID(Severity, backend_frame_larger_than, DiagID); | |
| break; | |
| case llvm::DK_ResourceLimit: | |
| if (ResourceLimitDiagHandler(cast<DiagnosticInfoResourceLimit>(DI))) | |
| return; | |
| ComputeDiagID(Severity, backend_resource_limit, DiagID); | |
| break; | |
| case DK_Linker: | |
| ComputeDiagID(Severity, linking_module, DiagID); | |
| break; | |
| case llvm::DK_OptimizationRemark: | |
| // Optimization remarks are always handled completely by this | |
| // handler. There is no generic way of emitting them. | |
| OptimizationRemarkHandler(cast<OptimizationRemark>(DI)); | |
| return; | |
| case llvm::DK_OptimizationRemarkMissed: | |
| // Optimization remarks are always handled completely by this | |
| // handler. There is no generic way of emitting them. | |
| OptimizationRemarkHandler(cast<OptimizationRemarkMissed>(DI)); | |
| return; | |
| case llvm::DK_OptimizationRemarkAnalysis: | |
| // Optimization remarks are always handled completely by this | |
| // handler. There is no generic way of emitting them. | |
| OptimizationRemarkHandler(cast<OptimizationRemarkAnalysis>(DI)); | |
| return; | |
| case llvm::DK_OptimizationRemarkAnalysisFPCommute: | |
| // Optimization remarks are always handled completely by this | |
| // handler. There is no generic way of emitting them. | |
| OptimizationRemarkHandler(cast<OptimizationRemarkAnalysisFPCommute>(DI)); | |
| return; | |
| case llvm::DK_OptimizationRemarkAnalysisAliasing: | |
| // Optimization remarks are always handled completely by this | |
| // handler. There is no generic way of emitting them. | |
| OptimizationRemarkHandler(cast<OptimizationRemarkAnalysisAliasing>(DI)); | |
| return; | |
| case llvm::DK_MachineOptimizationRemark: | |
| // Optimization remarks are always handled completely by this | |
| // handler. There is no generic way of emitting them. | |
| OptimizationRemarkHandler(cast<MachineOptimizationRemark>(DI)); | |
| return; | |
| case llvm::DK_MachineOptimizationRemarkMissed: | |
| // Optimization remarks are always handled completely by this | |
| // handler. There is no generic way of emitting them. | |
| OptimizationRemarkHandler(cast<MachineOptimizationRemarkMissed>(DI)); | |
| return; | |
| case llvm::DK_MachineOptimizationRemarkAnalysis: | |
| // Optimization remarks are always handled completely by this | |
| // handler. There is no generic way of emitting them. | |
| OptimizationRemarkHandler(cast<MachineOptimizationRemarkAnalysis>(DI)); | |
| return; | |
| case llvm::DK_OptimizationFailure: | |
| // Optimization failures are always handled completely by this | |
| // handler. | |
| OptimizationFailureHandler(cast<DiagnosticInfoOptimizationFailure>(DI)); | |
| return; | |
| case llvm::DK_Unsupported: | |
| UnsupportedDiagHandler(cast<DiagnosticInfoUnsupported>(DI)); | |
| return; | |
| case llvm::DK_UnsupportedTargetIntrinsic: | |
| UnsupportedTargetIntrinsicDiagHandler( | |
| cast<DiagnosticInfoUnsupportedTargetIntrinsic>(DI)); | |
| return; | |
| case llvm::DK_DontCall: | |
| DontCallDiagHandler(cast<DiagnosticInfoDontCall>(DI)); | |
| return; | |
| case llvm::DK_MisExpect: | |
| MisExpectDiagHandler(cast<DiagnosticInfoMisExpect>(DI)); | |
| return; | |
| default: | |
| // Plugin IDs are not bound to any value as they are set dynamically. | |
| ComputeDiagRemarkID(Severity, backend_plugin, DiagID); | |
| break; | |
| } | |
| std::string MsgStorage; | |
| { | |
| raw_string_ostream Stream(MsgStorage); | |
| DiagnosticPrinterRawOStream DP(Stream); | |
| DI.print(DP); | |
| } | |
| if (DI.getKind() == DK_Linker) { | |
| assert(CurLinkModule && "CurLinkModule must be set for linker diagnostics"); | |
| Diags.Report(DiagID) << CurLinkModule->getModuleIdentifier() << MsgStorage; | |
| return; | |
| } | |
| // Report the backend message using the usual diagnostic mechanism. | |
| FullSourceLoc Loc; | |
| Diags.Report(Loc, DiagID).AddString(MsgStorage); | |
| } | |
| #undef ComputeDiagID | |
| #undef ComputeDiagRemarkID |