1 //===- llvm/Support/DiagnosticInfo.cpp - Diagnostic Definitions -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the different classes involved in low level diagnostics.
12 // Diagnostics reporting is still done as part of the LLVMContext.
13 //===----------------------------------------------------------------------===//
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"
29 int llvm::getNextAvailablePluginDiagnosticKind() {
30 static sys::cas_flag PluginKindID = DK_FirstPluginKind;
31 return (int)sys::AtomicIncrement(&PluginKindID);
34 DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(const Instruction &I,
36 DiagnosticSeverity Severity)
37 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
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();
46 void DiagnosticInfoInlineAsm::print(DiagnosticPrinter &DP) const {
49 DP << " at line " << getLocCookie();
52 void DiagnosticInfoStackSize::print(DiagnosticPrinter &DP) const {
53 DP << "stack size limit exceeded (" << getStackSize() << ") in "
57 void DiagnosticInfoDebugMetadataVersion::print(DiagnosticPrinter &DP) const {
58 DP << "ignoring debug info with an invalid version (" << getMetadataVersion()
59 << ") in " << getModule();
62 void DiagnosticInfoSampleProfile::print(DiagnosticPrinter &DP) const {
63 if (getFileName() && getLineNum() > 0)
64 DP << getFileName() << ":" << getLineNum() << ": ";
65 else if (getFileName())
66 DP << getFileName() << ": ";
70 bool DiagnosticInfoOptimizationRemark::isLocationAvailable() const {
71 return getFunction().getParent()->getNamedMetadata("llvm.dbg.cu") != nullptr;
74 void DiagnosticInfoOptimizationRemark::getLocation(StringRef *Filename,
76 unsigned *Column) const {
77 DILocation DIL(getDebugLoc().getAsMDNode(getFunction().getContext()));
78 *Filename = DIL.getFilename();
79 *Line = DIL.getLineNumber();
80 *Column = DIL.getColumnNumber();
83 const std::string DiagnosticInfoOptimizationRemark::getLocationStr() const {
84 StringRef Filename("<unknown>");
87 if (isLocationAvailable())
88 getLocation(&Filename, &Line, &Column);
89 return Twine(Filename + ":" + Twine(Line) + ":" + Twine(Column)).str();
92 void DiagnosticInfoOptimizationRemark::print(DiagnosticPrinter &DP) const {
93 DP << getLocationStr() << ": " << getMsg();