[C++11] More 'nullptr' conversion or in some cases just using a boolean check instead...
[oota-llvm.git] / lib / IR / DiagnosticInfo.cpp
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/ADT/Twine.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/DebugInfo.h"
18 #include "llvm/IR/DiagnosticInfo.h"
19 #include "llvm/IR/DiagnosticPrinter.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Instruction.h"
22 #include "llvm/IR/Metadata.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/Support/Atomic.h"
25 #include <string>
26
27 using namespace llvm;
28
29 int llvm::getNextAvailablePluginDiagnosticKind() {
30   static sys::cas_flag PluginKindID = DK_FirstPluginKind;
31   return (int)sys::AtomicIncrement(&PluginKindID);
32 }
33
34 DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(const Instruction &I,
35                                                  const Twine &MsgStr,
36                                                  DiagnosticSeverity Severity)
37     : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
38       Instr(&I) {
39   if (const MDNode *SrcLoc = I.getMetadata("srcloc")) {
40     if (SrcLoc->getNumOperands() != 0)
41       if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
42         LocCookie = CI->getZExtValue();
43   }
44 }
45
46 void DiagnosticInfoInlineAsm::print(DiagnosticPrinter &DP) const {
47   DP << getMsgStr();
48   if (getLocCookie())
49     DP << " at line " << getLocCookie();
50 }
51
52 void DiagnosticInfoStackSize::print(DiagnosticPrinter &DP) const {
53   DP << "stack size limit exceeded (" << getStackSize() << ") in "
54      << getFunction();
55 }
56
57 void DiagnosticInfoDebugMetadataVersion::print(DiagnosticPrinter &DP) const {
58   DP << "ignoring debug info with an invalid version (" << getMetadataVersion()
59      << ") in " << getModule();
60 }
61
62 void DiagnosticInfoSampleProfile::print(DiagnosticPrinter &DP) const {
63   if (getFileName() && getLineNum() > 0)
64     DP << getFileName() << ":" << getLineNum() << ": ";
65   else if (getFileName())
66     DP << getFileName() << ": ";
67   DP << getMsg();
68 }
69
70 bool DiagnosticInfoOptimizationRemark::isLocationAvailable() const {
71   return getFunction().getParent()->getNamedMetadata("llvm.dbg.cu") != nullptr;
72 }
73
74 void DiagnosticInfoOptimizationRemark::getLocation(StringRef *Filename,
75                                                    unsigned *Line,
76                                                    unsigned *Column) const {
77   DILocation DIL(getDebugLoc().getAsMDNode(getFunction().getContext()));
78   *Filename = DIL.getFilename();
79   *Line = DIL.getLineNumber();
80   *Column = DIL.getColumnNumber();
81 }
82
83 const StringRef DiagnosticInfoOptimizationRemark::getLocationStr() const {
84   StringRef Filename("<unknown>");
85   unsigned Line = 0;
86   unsigned Column = 0;
87   if (isLocationAvailable())
88     getLocation(&Filename, &Line, &Column);
89   return Twine(Filename + ":" + Twine(Line) + ":" + Twine(Column)).str();
90 }
91
92 void DiagnosticInfoOptimizationRemark::print(DiagnosticPrinter &DP) const {
93   DP << getLocationStr() << ": " << getMsg();
94 }