LCOV - code coverage report
Current view: top level - lib/IR - DiagnosticInfo.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 205 213 96.2 %
Date: 2018-07-13 00:08:38 Functions: 45 48 93.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- llvm/Support/DiagnosticInfo.cpp - Diagnostic Definitions -*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file defines the different classes involved in low level diagnostics.
      11             : //
      12             : // Diagnostics reporting is still done as part of the LLVMContext.
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #include "llvm/IR/DiagnosticInfo.h"
      16             : #include "LLVMContextImpl.h"
      17             : #include "llvm/ADT/StringExtras.h"
      18             : #include "llvm/ADT/Twine.h"
      19             : #include "llvm/ADT/iterator_range.h"
      20             : #include "llvm/IR/BasicBlock.h"
      21             : #include "llvm/IR/Constants.h"
      22             : #include "llvm/IR/DebugInfoMetadata.h"
      23             : #include "llvm/IR/DerivedTypes.h"
      24             : #include "llvm/IR/DiagnosticPrinter.h"
      25             : #include "llvm/IR/Function.h"
      26             : #include "llvm/IR/GlobalValue.h"
      27             : #include "llvm/IR/Instruction.h"
      28             : #include "llvm/IR/LLVMContext.h"
      29             : #include "llvm/IR/Metadata.h"
      30             : #include "llvm/IR/Module.h"
      31             : #include "llvm/IR/Type.h"
      32             : #include "llvm/IR/Value.h"
      33             : #include "llvm/Support/Casting.h"
      34             : #include "llvm/Support/CommandLine.h"
      35             : #include "llvm/Support/ErrorHandling.h"
      36             : #include "llvm/Support/Regex.h"
      37             : #include "llvm/Support/raw_ostream.h"
      38             : #include "llvm/Support/ScopedPrinter.h"
      39             : #include <atomic>
      40             : #include <cassert>
      41             : #include <memory>
      42             : #include <string>
      43             : 
      44             : using namespace llvm;
      45             : 
      46       46112 : int llvm::getNextAvailablePluginDiagnosticKind() {
      47             :   static std::atomic<int> PluginKindID(DK_FirstPluginKind);
      48       46112 :   return ++PluginKindID;
      49             : }
      50             : 
      51             : const char *OptimizationRemarkAnalysis::AlwaysPrint = "";
      52             : 
      53          82 : DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(const Instruction &I,
      54             :                                                  const Twine &MsgStr,
      55          82 :                                                  DiagnosticSeverity Severity)
      56          82 :     : DiagnosticInfo(DK_InlineAsm, Severity), MsgStr(MsgStr), Instr(&I) {
      57          12 :   if (const MDNode *SrcLoc = I.getMetadata("srcloc")) {
      58          12 :     if (SrcLoc->getNumOperands() != 0)
      59             :       if (const auto *CI =
      60             :               mdconst::dyn_extract<ConstantInt>(SrcLoc->getOperand(0)))
      61          12 :         LocCookie = CI->getZExtValue();
      62             :   }
      63          82 : }
      64             : 
      65         261 : void DiagnosticInfoInlineAsm::print(DiagnosticPrinter &DP) const {
      66         261 :   DP << getMsgStr();
      67         261 :   if (getLocCookie())
      68           8 :     DP << " at line " << getLocCookie();
      69         261 : }
      70             : 
      71          14 : void DiagnosticInfoResourceLimit::print(DiagnosticPrinter &DP) const {
      72          14 :   DP << getResourceName() << " limit";
      73             : 
      74          14 :   if (getResourceLimit() != 0)
      75           5 :     DP << " of " << getResourceLimit();
      76             : 
      77          14 :   DP << " exceeded (" <<  getResourceSize() << ") in " << getFunction();
      78          14 : }
      79             : 
      80          49 : void DiagnosticInfoDebugMetadataVersion::print(DiagnosticPrinter &DP) const {
      81          49 :   DP << "ignoring debug info with an invalid version (" << getMetadataVersion()
      82          49 :      << ") in " << getModule();
      83          49 : }
      84             : 
      85          46 : void DiagnosticInfoIgnoringInvalidDebugMetadata::print(
      86             :     DiagnosticPrinter &DP) const {
      87          92 :   DP << "ignoring invalid debug info in " << getModule().getModuleIdentifier();
      88          46 : }
      89             : 
      90          26 : void DiagnosticInfoSampleProfile::print(DiagnosticPrinter &DP) const {
      91          26 :   if (!FileName.empty()) {
      92          20 :     DP << getFileName();
      93          20 :     if (LineNum > 0)
      94          16 :       DP << ":" << getLineNum();
      95          20 :     DP << ": ";
      96             :   }
      97          26 :   DP << getMsg();
      98          26 : }
      99             : 
     100           8 : void DiagnosticInfoPGOProfile::print(DiagnosticPrinter &DP) const {
     101           8 :   if (getFileName())
     102           8 :     DP << getFileName() << ": ";
     103           8 :   DP << getMsg();
     104           8 : }
     105             : 
     106      998410 : DiagnosticLocation::DiagnosticLocation(const DebugLoc &DL) {
     107      499205 :   if (!DL)
     108             :     return;
     109       91182 :   Filename = DL->getFilename();
     110       91182 :   Line = DL->getLine();
     111      182364 :   Column = DL->getColumn();
     112             : }
     113             : 
     114      460622 : DiagnosticLocation::DiagnosticLocation(const DISubprogram *SP) {
     115      230311 :   if (!SP)
     116             :     return;
     117       37112 :   Filename = SP->getFilename();
     118       37112 :   Line = SP->getScopeLine();
     119       37112 :   Column = 0;
     120             : }
     121             : 
     122         403 : void DiagnosticInfoWithLocationBase::getLocation(StringRef *Filename,
     123             :                                                  unsigned *Line,
     124             :                                                  unsigned *Column) const {
     125         403 :   *Filename = Loc.getFilename();
     126         403 :   *Line = Loc.getLine();
     127         403 :   *Column = Loc.getColumn();
     128         403 : }
     129             : 
     130        1273 : const std::string DiagnosticInfoWithLocationBase::getLocationStr() const {
     131             :   StringRef Filename("<unknown>");
     132        1273 :   unsigned Line = 0;
     133        1273 :   unsigned Column = 0;
     134        1273 :   if (isLocationAvailable())
     135         375 :     getLocation(&Filename, &Line, &Column);
     136        3819 :   return (Filename + ":" + Twine(Line) + ":" + Twine(Column)).str();
     137             : }
     138             : 
     139         700 : DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, const Value *V)
     140         700 :     : Key(Key) {
     141             :   if (auto *F = dyn_cast<Function>(V)) {
     142         560 :     if (DISubprogram *SP = F->getSubprogram())
     143         174 :       Loc = SP;
     144             :   }
     145             :   else if (auto *I = dyn_cast<Instruction>(V))
     146         130 :     Loc = I->getDebugLoc();
     147             : 
     148             :   // Only include names that correspond to user variables.  FIXME: We should use
     149             :   // debug info if available to get the name of the user variable.
     150         700 :   if (isa<llvm::Argument>(V) || isa<GlobalValue>(V))
     151        1692 :     Val = GlobalValue::dropLLVMManglingEscape(V->getName());
     152         136 :   else if (isa<Constant>(V)) {
     153           4 :     raw_string_ostream OS(Val);
     154           4 :     V->printAsOperand(OS, /*PrintType=*/false);
     155             :   } else if (auto *I = dyn_cast<Instruction>(V))
     156             :     Val = I->getOpcodeName();
     157         700 : }
     158             : 
     159        8499 : DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, const Type *T)
     160        8499 :     : Key(Key) {
     161        8499 :   raw_string_ostream OS(Val);
     162             :   OS << *T;
     163        8499 : }
     164             : 
     165          33 : DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, StringRef S)
     166          33 :     : Key(Key), Val(S.str()) {}
     167             : 
     168      295302 : DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, int N)
     169      590604 :     : Key(Key), Val(itostr(N)) {}
     170             : 
     171           0 : DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, float N)
     172           0 :     : Key(Key), Val(llvm::to_string(N)) {}
     173             : 
     174          11 : DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, long N)
     175          22 :     : Key(Key), Val(itostr(N)) {}
     176             : 
     177           0 : DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, long long N)
     178           0 :     : Key(Key), Val(itostr(N)) {}
     179             : 
     180       74050 : DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, unsigned N)
     181      148100 :     : Key(Key), Val(utostr(N)) {}
     182             : 
     183         356 : DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key,
     184         356 :                                                    unsigned long N)
     185         712 :     : Key(Key), Val(utostr(N)) {}
     186             : 
     187           0 : DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key,
     188           0 :                                                    unsigned long long N)
     189           0 :     : Key(Key), Val(utostr(N)) {}
     190             : 
     191          83 : DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, DebugLoc Loc)
     192         166 :     : Key(Key), Loc(Loc) {
     193          83 :   if (Loc) {
     194          76 :     Val = (Loc->getFilename() + ":" + Twine(Loc.getLine()) + ":" +
     195          76 :            Twine(Loc.getCol())).str();
     196             :   } else {
     197             :     Val = "<UNKNOWN LOCATION>";
     198             :   }
     199          83 : }
     200             : 
     201        1113 : void DiagnosticInfoOptimizationBase::print(DiagnosticPrinter &DP) const {
     202        3339 :   DP << getLocationStr() << ": " << getMsg();
     203        1113 :   if (Hotness)
     204          46 :     DP << " (hotness: " << *Hotness << ")";
     205        1113 : }
     206             : 
     207      381727 : OptimizationRemark::OptimizationRemark(const char *PassName,
     208             :                                        StringRef RemarkName,
     209             :                                        const DiagnosticLocation &Loc,
     210             :                                        const Value *CodeRegion)
     211             :     : DiagnosticInfoIROptimization(
     212             :           DK_OptimizationRemark, DS_Remark, PassName, RemarkName,
     213      763454 :           *cast<BasicBlock>(CodeRegion)->getParent(), Loc, CodeRegion) {}
     214             : 
     215       74046 : OptimizationRemark::OptimizationRemark(const char *PassName,
     216             :                                        StringRef RemarkName,
     217       74046 :                                        const Instruction *Inst)
     218             :     : DiagnosticInfoIROptimization(DK_OptimizationRemark, DS_Remark, PassName,
     219       74046 :                                    RemarkName, *Inst->getParent()->getParent(),
     220      222138 :                                    Inst->getDebugLoc(), Inst->getParent()) {}
     221             : 
     222             : // Helper to allow for an assert before attempting to return an invalid
     223             : // reference.
     224             : static const BasicBlock &getFirstFunctionBlock(const Function *Func) {
     225             :   assert(!Func->empty() && "Function does not have a body");
     226             :   return Func->front();
     227             : }
     228             : 
     229          16 : OptimizationRemark::OptimizationRemark(const char *PassName,
     230             :                                        StringRef RemarkName,
     231          16 :                                        const Function *Func)
     232             :     : DiagnosticInfoIROptimization(DK_OptimizationRemark, DS_Remark, PassName,
     233          16 :                                    RemarkName, *Func, Func->getSubprogram(),
     234          32 :                                    &getFirstFunctionBlock(Func)) {}
     235             : 
     236      383880 : bool OptimizationRemark::isEnabled() const {
     237      383880 :   const Function &Fn = getFunction();
     238      383880 :   LLVMContext &Ctx = Fn.getContext();
     239      767760 :   return Ctx.getDiagHandlerPtr()->isPassedOptRemarkEnabled(getPassName());
     240             : }
     241             : 
     242       37507 : OptimizationRemarkMissed::OptimizationRemarkMissed(
     243             :     const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc,
     244             :     const Value *CodeRegion)
     245             :     : DiagnosticInfoIROptimization(
     246             :           DK_OptimizationRemarkMissed, DS_Remark, PassName, RemarkName,
     247       75014 :           *cast<BasicBlock>(CodeRegion)->getParent(), Loc, CodeRegion) {}
     248             : 
     249         215 : OptimizationRemarkMissed::OptimizationRemarkMissed(const char *PassName,
     250             :                                                    StringRef RemarkName,
     251         215 :                                                    const Instruction *Inst)
     252             :     : DiagnosticInfoIROptimization(DK_OptimizationRemarkMissed, DS_Remark,
     253             :                                    PassName, RemarkName,
     254         215 :                                    *Inst->getParent()->getParent(),
     255         645 :                                    Inst->getDebugLoc(), Inst->getParent()) {}
     256             : 
     257       45311 : bool OptimizationRemarkMissed::isEnabled() const {
     258       45311 :   const Function &Fn = getFunction();
     259       45311 :   LLVMContext &Ctx = Fn.getContext();
     260       90622 :   return Ctx.getDiagHandlerPtr()->isMissedOptRemarkEnabled(getPassName());
     261             : }
     262             : 
     263       10911 : OptimizationRemarkAnalysis::OptimizationRemarkAnalysis(
     264             :     const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc,
     265             :     const Value *CodeRegion)
     266             :     : DiagnosticInfoIROptimization(
     267             :           DK_OptimizationRemarkAnalysis, DS_Remark, PassName, RemarkName,
     268       21822 :           *cast<BasicBlock>(CodeRegion)->getParent(), Loc, CodeRegion) {}
     269             : 
     270        2823 : OptimizationRemarkAnalysis::OptimizationRemarkAnalysis(const char *PassName,
     271             :                                                        StringRef RemarkName,
     272        2823 :                                                        const Instruction *Inst)
     273             :     : DiagnosticInfoIROptimization(DK_OptimizationRemarkAnalysis, DS_Remark,
     274             :                                    PassName, RemarkName,
     275        2823 :                                    *Inst->getParent()->getParent(),
     276        8469 :                                    Inst->getDebugLoc(), Inst->getParent()) {}
     277             : 
     278           9 : OptimizationRemarkAnalysis::OptimizationRemarkAnalysis(
     279             :     enum DiagnosticKind Kind, const char *PassName, StringRef RemarkName,
     280             :     const DiagnosticLocation &Loc, const Value *CodeRegion)
     281             :     : DiagnosticInfoIROptimization(Kind, DS_Remark, PassName, RemarkName,
     282           9 :                                    *cast<BasicBlock>(CodeRegion)->getParent(),
     283           9 :                                    Loc, CodeRegion) {}
     284             : 
     285        9123 : bool OptimizationRemarkAnalysis::isEnabled() const {
     286        9123 :   const Function &Fn = getFunction();
     287        9123 :   LLVMContext &Ctx = Fn.getContext();
     288       26972 :   return Ctx.getDiagHandlerPtr()->isAnalysisRemarkEnabled(getPassName()) ||
     289       17849 :          shouldAlwaysPrint();
     290             : }
     291             : 
     292         118 : void DiagnosticInfoMIRParser::print(DiagnosticPrinter &DP) const {
     293         118 :   DP << Diagnostic;
     294         118 : }
     295             : 
     296          13 : DiagnosticInfoOptimizationFailure::DiagnosticInfoOptimizationFailure(
     297             :     const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc,
     298             :     const Value *CodeRegion)
     299             :     : DiagnosticInfoIROptimization(
     300             :           DK_OptimizationFailure, DS_Warning, PassName, RemarkName,
     301          26 :           *cast<BasicBlock>(CodeRegion)->getParent(), Loc, CodeRegion) {}
     302             : 
     303          17 : bool DiagnosticInfoOptimizationFailure::isEnabled() const {
     304             :   // Only print warnings.
     305          17 :   return getSeverity() == DS_Warning;
     306             : }
     307             : 
     308         160 : void DiagnosticInfoUnsupported::print(DiagnosticPrinter &DP) const {
     309             :   std::string Str;
     310         160 :   raw_string_ostream OS(Str);
     311             : 
     312         480 :   OS << getLocationStr() << ": in function " << getFunction().getName() << ' '
     313         480 :      << *getFunction().getFunctionType() << ": " << Msg << '\n';
     314             :   OS.flush();
     315         160 :   DP << Str;
     316         160 : }
     317             : 
     318         101 : void DiagnosticInfoISelFallback::print(DiagnosticPrinter &DP) const {
     319         101 :   DP << "Instruction selection used fallback path for " << getFunction();
     320         101 : }
     321             : 
     322      440894 : void DiagnosticInfoOptimizationBase::insert(StringRef S) {
     323      440894 :   Args.emplace_back(S);
     324      440894 : }
     325             : 
     326      379063 : void DiagnosticInfoOptimizationBase::insert(Argument A) {
     327      379063 :   Args.push_back(std::move(A));
     328      379063 : }
     329             : 
     330          40 : void DiagnosticInfoOptimizationBase::insert(setIsVerbose V) {
     331          40 :   IsVerbose = true;
     332          40 : }
     333             : 
     334          23 : void DiagnosticInfoOptimizationBase::insert(setExtraArgs EA) {
     335          23 :   FirstExtraArgIndex = Args.size();
     336          23 : }
     337             : 
     338        1193 : std::string DiagnosticInfoOptimizationBase::getMsg() const {
     339             :   std::string Str;
     340        1193 :   raw_string_ostream OS(Str);
     341        2758 :   for (const DiagnosticInfoOptimizationBase::Argument &Arg :
     342        1193 :        make_range(Args.begin(), FirstExtraArgIndex == -1
     343             :                                     ? Args.end()
     344        5153 :                                     : Args.begin() + FirstExtraArgIndex))
     345             :     OS << Arg.Val;
     346        1193 :   return OS.str();
     347             : }
     348             : 
     349             : namespace llvm {
     350             : namespace yaml {
     351             : 
     352         393 : void MappingTraits<DiagnosticInfoOptimizationBase *>::mapping(
     353             :     IO &io, DiagnosticInfoOptimizationBase *&OptDiag) {
     354             :   assert(io.outputting() && "input not yet implemented");
     355             : 
     356         786 :   if (io.mapTag("!Passed",
     357         393 :                 (OptDiag->getKind() == DK_OptimizationRemark ||
     358         393 :                  OptDiag->getKind() == DK_MachineOptimizationRemark)))
     359             :     ;
     360         578 :   else if (io.mapTag(
     361             :                "!Missed",
     362         289 :                (OptDiag->getKind() == DK_OptimizationRemarkMissed ||
     363         289 :                 OptDiag->getKind() == DK_MachineOptimizationRemarkMissed)))
     364             :     ;
     365         346 :   else if (io.mapTag(
     366             :                "!Analysis",
     367         173 :                (OptDiag->getKind() == DK_OptimizationRemarkAnalysis ||
     368         173 :                 OptDiag->getKind() == DK_MachineOptimizationRemarkAnalysis)))
     369             :     ;
     370           6 :   else if (io.mapTag("!AnalysisFPCommute",
     371           3 :                      OptDiag->getKind() ==
     372           3 :                          DK_OptimizationRemarkAnalysisFPCommute))
     373             :     ;
     374           6 :   else if (io.mapTag("!AnalysisAliasing",
     375           3 :                      OptDiag->getKind() ==
     376           3 :                          DK_OptimizationRemarkAnalysisAliasing))
     377             :     ;
     378           4 :   else if (io.mapTag("!Failure", OptDiag->getKind() == DK_OptimizationFailure))
     379             :     ;
     380             :   else
     381           0 :     llvm_unreachable("Unknown remark type");
     382             : 
     383             :   // These are read-only for now.
     384         393 :   DiagnosticLocation DL = OptDiag->getLocation();
     385             :   StringRef FN =
     386         393 :       GlobalValue::dropLLVMManglingEscape(OptDiag->getFunction().getName());
     387             : 
     388         393 :   StringRef PassName(OptDiag->PassName);
     389             :   io.mapRequired("Pass", PassName);
     390         393 :   io.mapRequired("Name", OptDiag->RemarkName);
     391         786 :   if (!io.outputting() || DL.isValid())
     392             :     io.mapOptional("DebugLoc", DL);
     393             :   io.mapRequired("Function", FN);
     394         393 :   io.mapOptional("Hotness", OptDiag->Hotness);
     395         393 :   io.mapOptional("Args", OptDiag->Args);
     396         393 : }
     397             : 
     398             : template <> struct MappingTraits<DiagnosticLocation> {
     399         176 :   static void mapping(IO &io, DiagnosticLocation &DL) {
     400             :     assert(io.outputting() && "input not yet implemented");
     401             : 
     402         176 :     StringRef File = DL.getFilename();
     403         176 :     unsigned Line = DL.getLine();
     404         176 :     unsigned Col = DL.getColumn();
     405             : 
     406             :     io.mapRequired("File", File);
     407             :     io.mapRequired("Line", Line);
     408             :     io.mapRequired("Column", Col);
     409         176 :   }
     410             : 
     411             :   static const bool flow = true;
     412             : };
     413             : 
     414             : // Implement this as a mapping for now to get proper quotation for the value.
     415             : template <> struct MappingTraits<DiagnosticInfoOptimizationBase::Argument> {
     416        1250 :   static void mapping(IO &io, DiagnosticInfoOptimizationBase::Argument &A) {
     417             :     assert(io.outputting() && "input not yet implemented");
     418        1250 :     io.mapRequired(A.Key.data(), A.Val);
     419        1250 :     if (A.Loc.isValid())
     420          54 :       io.mapOptional("DebugLoc", A.Loc);
     421        1250 :   }
     422             : };
     423             : 
     424             : } // end namespace yaml
     425             : } // end namespace llvm
     426             : 
     427             : LLVM_YAML_IS_SEQUENCE_VECTOR(DiagnosticInfoOptimizationBase::Argument)

Generated by: LCOV version 1.13