blob: 45be100ae0534621c263620816d5f1958bf7ca5f [file] [log] [blame]
Quentin Colombetb4c44d22013-12-17 17:47:22 +00001//===- 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
Diego Novillo7f8af8b2014-05-22 14:19:46 +000015#include "LLVMContextImpl.h"
Quentin Colombetb4c44d22013-12-17 17:47:22 +000016#include "llvm/ADT/Twine.h"
17#include "llvm/IR/Constants.h"
Diego Novilloa9298b22014-04-08 16:42:34 +000018#include "llvm/IR/DebugInfo.h"
Quentin Colombetb4c44d22013-12-17 17:47:22 +000019#include "llvm/IR/DiagnosticInfo.h"
20#include "llvm/IR/DiagnosticPrinter.h"
21#include "llvm/IR/Function.h"
22#include "llvm/IR/Instruction.h"
23#include "llvm/IR/Metadata.h"
Diego Novilloa9298b22014-04-08 16:42:34 +000024#include "llvm/IR/Module.h"
Benjamin Kramer17388a62014-03-03 18:02:34 +000025#include "llvm/Support/Atomic.h"
Diego Novillo0b761a42014-05-22 17:19:01 +000026#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/Regex.h"
Quentin Colombetb4c44d22013-12-17 17:47:22 +000028#include <string>
29
30using namespace llvm;
31
Diego Novillo0b761a42014-05-22 17:19:01 +000032namespace {
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).
39struct 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
55static PassRemarksOpt PassRemarksOptLoc;
56static PassRemarksOpt PassRemarksMissedOptLoc;
57static PassRemarksOpt PassRemarksAnalysisOptLoc;
58
59// -pass-remarks
60// Command line flag to enable emitOptimizationRemark()
61static cl::opt<PassRemarksOpt, true, cl::parser<std::string>>
62PassRemarks("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,
66 cl::ZeroOrMore);
67
68// -pass-remarks-missed
69// Command line flag to enable emitOptimizationRemarkMissed()
70static cl::opt<PassRemarksOpt, true, cl::parser<std::string>> PassRemarksMissed(
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,
75 cl::ZeroOrMore);
76
77// -pass-remarks-analysis
78// Command line flag to enable emitOptimizationRemarkAnalysis()
79static cl::opt<PassRemarksOpt, true, cl::parser<std::string>>
80PassRemarksAnalysis(
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,
86 cl::ZeroOrMore);
87}
88
Tobias Grosser84db1e72013-12-18 10:12:06 +000089int llvm::getNextAvailablePluginDiagnosticKind() {
Benjamin Kramer17388a62014-03-03 18:02:34 +000090 static sys::cas_flag PluginKindID = DK_FirstPluginKind;
91 return (int)sys::AtomicIncrement(&PluginKindID);
Quentin Colombetb4c44d22013-12-17 17:47:22 +000092}
93
94DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(const Instruction &I,
95 const Twine &MsgStr,
96 DiagnosticSeverity Severity)
97 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
98 Instr(&I) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +000099 if (const MDNode *SrcLoc = I.getMetadata("srcloc")) {
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000100 if (SrcLoc->getNumOperands() != 0)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000101 if (const auto *CI =
102 mdconst::dyn_extract<ConstantInt>(SrcLoc->getOperand(0)))
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000103 LocCookie = CI->getZExtValue();
104 }
105}
106
107void DiagnosticInfoInlineAsm::print(DiagnosticPrinter &DP) const {
108 DP << getMsgStr();
109 if (getLocCookie())
110 DP << " at line " << getLocCookie();
111}
112
113void DiagnosticInfoStackSize::print(DiagnosticPrinter &DP) const {
114 DP << "stack size limit exceeded (" << getStackSize() << ") in "
115 << getFunction();
116}
Manman Ren2ebfb422014-01-16 01:51:12 +0000117
118void DiagnosticInfoDebugMetadataVersion::print(DiagnosticPrinter &DP) const {
Manman Ren37628082014-02-04 23:49:02 +0000119 DP << "ignoring debug info with an invalid version (" << getMetadataVersion()
120 << ") in " << getModule();
Manman Ren2ebfb422014-01-16 01:51:12 +0000121}
Diego Novilloa32aa322014-03-14 21:58:59 +0000122
123void DiagnosticInfoSampleProfile::print(DiagnosticPrinter &DP) const {
124 if (getFileName() && getLineNum() > 0)
125 DP << getFileName() << ":" << getLineNum() << ": ";
126 else if (getFileName())
127 DP << getFileName() << ": ";
128 DP << getMsg();
129}
Diego Novilloa9298b22014-04-08 16:42:34 +0000130
Tyler Nowicki641d8a02014-07-16 00:36:00 +0000131bool DiagnosticInfoOptimizationBase::isLocationAvailable() const {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000132 return getDebugLoc();
Diego Novilloa9298b22014-04-08 16:42:34 +0000133}
134
Tyler Nowicki641d8a02014-07-16 00:36:00 +0000135void DiagnosticInfoOptimizationBase::getLocation(StringRef *Filename,
136 unsigned *Line,
137 unsigned *Column) const {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000138 DILocation *L = getDebugLoc();
Davide Italianoe197d4f2015-05-04 18:08:35 +0000139 assert(L != nullptr && "debug location is invalid");
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000140 *Filename = L->getFilename();
141 *Line = L->getLine();
142 *Column = L->getColumn();
Diego Novilloa9298b22014-04-08 16:42:34 +0000143}
144
Tyler Nowicki641d8a02014-07-16 00:36:00 +0000145const std::string DiagnosticInfoOptimizationBase::getLocationStr() const {
Diego Novilloa9298b22014-04-08 16:42:34 +0000146 StringRef Filename("<unknown>");
147 unsigned Line = 0;
148 unsigned Column = 0;
149 if (isLocationAvailable())
150 getLocation(&Filename, &Line, &Column);
Yaron Keren075759a2015-03-30 15:42:36 +0000151 return (Filename + ":" + Twine(Line) + ":" + Twine(Column)).str();
Diego Novilloa9298b22014-04-08 16:42:34 +0000152}
153
Tyler Nowicki641d8a02014-07-16 00:36:00 +0000154void DiagnosticInfoOptimizationBase::print(DiagnosticPrinter &DP) const {
Diego Novilloa9298b22014-04-08 16:42:34 +0000155 DP << getLocationStr() << ": " << getMsg();
156}
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000157
Diego Novillo0b761a42014-05-22 17:19:01 +0000158bool DiagnosticInfoOptimizationRemark::isEnabled() const {
159 return PassRemarksOptLoc.Pattern &&
160 PassRemarksOptLoc.Pattern->match(getPassName());
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000161}
162
Diego Novillo0b761a42014-05-22 17:19:01 +0000163bool DiagnosticInfoOptimizationRemarkMissed::isEnabled() const {
164 return PassRemarksMissedOptLoc.Pattern &&
165 PassRemarksMissedOptLoc.Pattern->match(getPassName());
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000166}
167
Diego Novillo0b761a42014-05-22 17:19:01 +0000168bool DiagnosticInfoOptimizationRemarkAnalysis::isEnabled() const {
169 return PassRemarksAnalysisOptLoc.Pattern &&
170 PassRemarksAnalysisOptLoc.Pattern->match(getPassName());
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000171}
172
173void llvm::emitOptimizationRemark(LLVMContext &Ctx, const char *PassName,
174 const Function &Fn, const DebugLoc &DLoc,
175 const Twine &Msg) {
176 Ctx.diagnose(DiagnosticInfoOptimizationRemark(PassName, Fn, DLoc, Msg));
177}
178
179void llvm::emitOptimizationRemarkMissed(LLVMContext &Ctx, const char *PassName,
180 const Function &Fn,
181 const DebugLoc &DLoc,
182 const Twine &Msg) {
183 Ctx.diagnose(DiagnosticInfoOptimizationRemarkMissed(PassName, Fn, DLoc, Msg));
184}
185
186void llvm::emitOptimizationRemarkAnalysis(LLVMContext &Ctx,
187 const char *PassName,
188 const Function &Fn,
189 const DebugLoc &DLoc,
190 const Twine &Msg) {
191 Ctx.diagnose(
192 DiagnosticInfoOptimizationRemarkAnalysis(PassName, Fn, DLoc, Msg));
193}
Tyler Nowicki641d8a02014-07-16 00:36:00 +0000194
Tyler Nowicki55454c62014-07-18 19:36:04 +0000195bool DiagnosticInfoOptimizationFailure::isEnabled() const {
Tyler Nowicki641d8a02014-07-16 00:36:00 +0000196 // Only print warnings.
197 return getSeverity() == DS_Warning;
198}
199
200void llvm::emitLoopVectorizeWarning(LLVMContext &Ctx, const Function &Fn,
201 const DebugLoc &DLoc, const Twine &Msg) {
Tyler Nowicki55454c62014-07-18 19:36:04 +0000202 Ctx.diagnose(DiagnosticInfoOptimizationFailure(
Tyler Nowicki641d8a02014-07-16 00:36:00 +0000203 Fn, DLoc, Twine("loop not vectorized: " + Msg)));
204}
205
206void llvm::emitLoopInterleaveWarning(LLVMContext &Ctx, const Function &Fn,
207 const DebugLoc &DLoc, const Twine &Msg) {
Tyler Nowicki55454c62014-07-18 19:36:04 +0000208 Ctx.diagnose(DiagnosticInfoOptimizationFailure(
Tyler Nowicki641d8a02014-07-16 00:36:00 +0000209 Fn, DLoc, Twine("loop not interleaved: " + Msg)));
210}