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