LLVM  3.7.0
DiagnosticInfo.cpp
Go to the documentation of this file.
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 "LLVMContextImpl.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DebugInfo.h"
19 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/Instruction.h"
23 #include "llvm/IR/Metadata.h"
24 #include "llvm/IR/Module.h"
26 #include "llvm/Support/Regex.h"
27 #include <atomic>
28 #include <string>
29 
30 using namespace llvm;
31 
32 namespace {
33 
34 /// \brief Regular expression corresponding to the value given in one of the
35 /// -pass-remarks* command line flags. Passes whose name matches this regexp
36 /// will emit a diagnostic when calling the associated diagnostic function
37 /// (emitOptimizationRemark, emitOptimizationRemarkMissed or
38 /// emitOptimizationRemarkAnalysis).
39 struct PassRemarksOpt {
40  std::shared_ptr<Regex> Pattern;
41 
42  void operator=(const std::string &Val) {
43  // Create a regexp object to match pass names for emitOptimizationRemark.
44  if (!Val.empty()) {
45  Pattern = std::make_shared<Regex>(Val);
46  std::string RegexError;
47  if (!Pattern->isValid(RegexError))
48  report_fatal_error("Invalid regular expression '" + Val +
49  "' in -pass-remarks: " + RegexError,
50  false);
51  }
52  };
53 };
54 
55 static PassRemarksOpt PassRemarksOptLoc;
56 static PassRemarksOpt PassRemarksMissedOptLoc;
57 static PassRemarksOpt PassRemarksAnalysisOptLoc;
58 
59 // -pass-remarks
60 // Command line flag to enable emitOptimizationRemark()
62 PassRemarks("pass-remarks", cl::value_desc("pattern"),
63  cl::desc("Enable optimization remarks from passes whose name match "
64  "the given regular expression"),
65  cl::Hidden, cl::location(PassRemarksOptLoc), cl::ValueRequired,
67 
68 // -pass-remarks-missed
69 // Command line flag to enable emitOptimizationRemarkMissed()
71  "pass-remarks-missed", cl::value_desc("pattern"),
72  cl::desc("Enable missed optimization remarks from passes whose name match "
73  "the given regular expression"),
74  cl::Hidden, cl::location(PassRemarksMissedOptLoc), cl::ValueRequired,
76 
77 // -pass-remarks-analysis
78 // Command line flag to enable emitOptimizationRemarkAnalysis()
80 PassRemarksAnalysis(
81  "pass-remarks-analysis", cl::value_desc("pattern"),
82  cl::desc(
83  "Enable optimization analysis remarks from passes whose name match "
84  "the given regular expression"),
85  cl::Hidden, cl::location(PassRemarksAnalysisOptLoc), cl::ValueRequired,
87 }
88 
90  static std::atomic<int> PluginKindID(DK_FirstPluginKind);
91  return ++PluginKindID;
92 }
93 
95  const Twine &MsgStr,
96  DiagnosticSeverity Severity)
97  : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
98  Instr(&I) {
99  if (const MDNode *SrcLoc = I.getMetadata("srcloc")) {
100  if (SrcLoc->getNumOperands() != 0)
101  if (const auto *CI =
102  mdconst::dyn_extract<ConstantInt>(SrcLoc->getOperand(0)))
103  LocCookie = CI->getZExtValue();
104  }
105 }
106 
108  DP << getMsgStr();
109  if (getLocCookie())
110  DP << " at line " << getLocCookie();
111 }
112 
114  DP << "stack size limit exceeded (" << getStackSize() << ") in "
115  << getFunction();
116 }
117 
119  DP << "ignoring debug info with an invalid version (" << getMetadataVersion()
120  << ") in " << getModule();
121 }
122 
124  if (getFileName() && getLineNum() > 0)
125  DP << getFileName() << ":" << getLineNum() << ": ";
126  else if (getFileName())
127  DP << getFileName() << ": ";
128  DP << getMsg();
129 }
130 
132  return getDebugLoc();
133 }
134 
136  unsigned *Line,
137  unsigned *Column) const {
138  DILocation *L = getDebugLoc();
139  assert(L != nullptr && "debug location is invalid");
140  *Filename = L->getFilename();
141  *Line = L->getLine();
142  *Column = L->getColumn();
143 }
144 
146  StringRef Filename("<unknown>");
147  unsigned Line = 0;
148  unsigned Column = 0;
149  if (isLocationAvailable())
150  getLocation(&Filename, &Line, &Column);
151  return (Filename + ":" + Twine(Line) + ":" + Twine(Column)).str();
152 }
153 
155  DP << getLocationStr() << ": " << getMsg();
156 }
157 
159  return PassRemarksOptLoc.Pattern &&
160  PassRemarksOptLoc.Pattern->match(getPassName());
161 }
162 
164  return PassRemarksMissedOptLoc.Pattern &&
165  PassRemarksMissedOptLoc.Pattern->match(getPassName());
166 }
167 
169  return PassRemarksAnalysisOptLoc.Pattern &&
170  PassRemarksAnalysisOptLoc.Pattern->match(getPassName());
171 }
172 
174  DP << Diagnostic;
175 }
176 
177 void llvm::emitOptimizationRemark(LLVMContext &Ctx, const char *PassName,
178  const Function &Fn, const DebugLoc &DLoc,
179  const Twine &Msg) {
180  Ctx.diagnose(DiagnosticInfoOptimizationRemark(PassName, Fn, DLoc, Msg));
181 }
182 
183 void llvm::emitOptimizationRemarkMissed(LLVMContext &Ctx, const char *PassName,
184  const Function &Fn,
185  const DebugLoc &DLoc,
186  const Twine &Msg) {
187  Ctx.diagnose(DiagnosticInfoOptimizationRemarkMissed(PassName, Fn, DLoc, Msg));
188 }
189 
191  const char *PassName,
192  const Function &Fn,
193  const DebugLoc &DLoc,
194  const Twine &Msg) {
195  Ctx.diagnose(
196  DiagnosticInfoOptimizationRemarkAnalysis(PassName, Fn, DLoc, Msg));
197 }
198 
200  // Only print warnings.
201  return getSeverity() == DS_Warning;
202 }
203 
205  const DebugLoc &DLoc, const Twine &Msg) {
207  Fn, DLoc, Twine("loop not vectorized: " + Msg)));
208 }
209 
211  const DebugLoc &DLoc, const Twine &Msg) {
213  Fn, DLoc, Twine("loop not interleaved: " + Msg)));
214 }
void getLocation(StringRef *Filename, unsigned *Line, unsigned *Column) const
Return location information for this diagnostic in three parts: the source file name, line number and column.
unsigned getStackSize() const
DiagnosticInfoInlineAsm(const Twine &MsgStr, DiagnosticSeverity Severity=DS_Error)
MsgStr is the message to be reported to the frontend.
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
void print(DiagnosticPrinter &DP) const override
This file contains the declarations for metadata subclasses.
Diagnostic information for applied optimization remarks.
A debug info location.
Definition: DebugLoc.h:34
Metadata node.
Definition: Metadata.h:740
void emitOptimizationRemark(LLVMContext &Ctx, const char *PassName, const Function &Fn, const DebugLoc &DLoc, const Twine &Msg)
Emit an optimization-applied message.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
void emitOptimizationRemarkAnalysis(LLVMContext &Ctx, const char *PassName, const Function &Fn, const DebugLoc &DLoc, const Twine &Msg)
Emit an optimization analysis remark message.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
Diagnostic information for optimization failures.
Interface for custom diagnostic printing.
const char * getFileName() const
const DebugLoc & getDebugLoc() const
void emitOptimizationRemarkMissed(LLVMContext &Ctx, const char *PassName, const Function &Fn, const DebugLoc &DLoc, const Twine &Msg)
Emit an optimization-missed message.
void print(DiagnosticPrinter &DP) const override
DiagnosticSeverity getSeverity() const
Debug location.
This is the base abstract class for diagnostic reporting in the backend.
bool isLocationAvailable() const
Return true if location information is available for this diagnostic.
const Twine & getMsg() const
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
Diagnostic information for missed-optimization remarks.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const std::string getLocationStr() const
Return a string with the location information for this diagnostic in the format "file:line:col".
void emitLoopVectorizeWarning(LLVMContext &Ctx, const Function &Fn, const DebugLoc &DLoc, const Twine &Msg)
Emit a warning when loop vectorization is specified but fails.
void emitLoopInterleaveWarning(LLVMContext &Ctx, const Function &Fn, const DebugLoc &DLoc, const Twine &Msg)
Emit a warning when loop interleaving is specified but fails.
unsigned getLocCookie() const
Diagnostic information for optimization analysis remarks.
Module.h This file contains the declarations for the Module class.
MDNode * getMetadata(unsigned KindID) const
getMetadata - Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:167
int getNextAvailablePluginDiagnosticKind()
Get the next available kind ID for a plugin diagnostic.
const Function & getFunction() const
void print(DiagnosticPrinter &DP) const override
#define I(x, y, z)
Definition: MD5.cpp:54
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
const Twine & getMsgStr() const
void print(DiagnosticPrinter &DP) const override
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
void print(DiagnosticPrinter &DP) const override
void print(DiagnosticPrinter &DP) const override
Print using the given DP a user-friendly message.
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:340