introduce a new recoverable error handling API to LLVMContext
[oota-llvm.git] / lib / VMCore / LLVMContext.cpp
1 //===-- LLVMContext.cpp - Implement LLVMContext -----------------------===//
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 implements LLVMContext, as a wrapper around the opaque
11 //  class LLVMContextImpl.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/LLVMContext.h"
16 #include "llvm/Metadata.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Instruction.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "LLVMContextImpl.h"
22 using namespace llvm;
23
24 static ManagedStatic<LLVMContext> GlobalContext;
25
26 LLVMContext& llvm::getGlobalContext() {
27   return *GlobalContext;
28 }
29
30 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
31   // Create the first metadata kind, which is always 'dbg'.
32   unsigned DbgID = getMDKindID("dbg");
33   assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
34 }
35 LLVMContext::~LLVMContext() { delete pImpl; }
36
37 //===----------------------------------------------------------------------===//
38 // Recoverable Backend Errors
39 //===----------------------------------------------------------------------===//
40
41 void LLVMContext::setInlineAsmDiagnosticHandler(void *DiagHandler, 
42                                                 void *DiagContext) {
43   pImpl->InlineAsmDiagHandler = DiagHandler;
44   pImpl->InlineAsmDiagContext = DiagContext;
45 }
46
47 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
48 /// setInlineAsmDiagnosticHandler.
49 void *LLVMContext::getInlineAsmDiagnosticHandler() const {
50   return pImpl->InlineAsmDiagHandler;
51 }
52
53 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
54 /// setInlineAsmDiagnosticHandler.
55 void *LLVMContext::getInlineAsmDiagnosticContext() const {
56   return pImpl->InlineAsmDiagContext;
57 }
58
59 void LLVMContext::emitError(StringRef ErrorStr) {
60   emitError(0U, ErrorStr);
61 }
62
63 void LLVMContext::emitError(const Instruction *I, StringRef ErrorStr) {
64   unsigned LocCookie = 0;
65   if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
66     if (SrcLoc->getNumOperands() != 0)
67       if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
68         LocCookie = CI->getZExtValue();
69   }
70   return emitError(LocCookie, ErrorStr);
71 }
72
73 void LLVMContext::emitError(unsigned LocCookie, StringRef ErrorStr) {
74   // If there is no error handler installed, just print the error and exit.
75   if (pImpl->InlineAsmDiagHandler == 0) {
76     errs() << "error: " << ErrorStr << "\n";
77     exit(1);
78   }
79   
80   // If we do have an error handler, we can report the error and keep going.
81   SMDiagnostic Diag("", "error: " + ErrorStr.str());
82   
83   ((SourceMgr::DiagHandlerTy)(intptr_t)pImpl->InlineAsmDiagHandler)
84       (Diag, pImpl->InlineAsmDiagContext, LocCookie);
85   
86 }
87
88 //===----------------------------------------------------------------------===//
89 // Metadata Kind Uniquing
90 //===----------------------------------------------------------------------===//
91
92 #ifndef NDEBUG
93 /// isValidName - Return true if Name is a valid custom metadata handler name.
94 static bool isValidName(StringRef MDName) {
95   if (MDName.empty())
96     return false;
97   
98   if (!isalpha(MDName[0]))
99     return false;
100   
101   for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
102        ++I) {
103     if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
104       return false;
105   }
106   return true;
107 }
108 #endif
109
110 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
111 unsigned LLVMContext::getMDKindID(StringRef Name) const {
112   assert(isValidName(Name) && "Invalid MDNode name");
113   
114   unsigned &Entry = pImpl->CustomMDKindNames[Name];
115   
116   // If this is new, assign it its ID.
117   if (Entry == 0) Entry = pImpl->CustomMDKindNames.size();
118   return Entry;
119 }
120
121 /// getHandlerNames - Populate client supplied smallvector using custome
122 /// metadata name and ID.
123 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
124   Names.resize(pImpl->CustomMDKindNames.size()+1);
125   Names[0] = "";
126   for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
127        E = pImpl->CustomMDKindNames.end(); I != E; ++I)
128     // MD Handlers are numbered from 1.
129     Names[I->second] = I->first();
130 }