Add warning capabilities in LLVM.
[oota-llvm.git] / lib / IR / 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/IR/LLVMContext.h"
16 #include "LLVMContextImpl.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Instruction.h"
19 #include "llvm/IR/Metadata.h"
20 #include "llvm/Support/DiagnosticInfo.h"
21 #include "llvm/Support/DiagnosticPrinter.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include <cctype>
25 using namespace llvm;
26
27 static ManagedStatic<LLVMContext> GlobalContext;
28
29 LLVMContext& llvm::getGlobalContext() {
30   return *GlobalContext;
31 }
32
33 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
34   // Create the fixed metadata kinds. This is done in the same order as the
35   // MD_* enum values so that they correspond.
36
37   // Create the 'dbg' metadata kind.
38   unsigned DbgID = getMDKindID("dbg");
39   assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
40
41   // Create the 'tbaa' metadata kind.
42   unsigned TBAAID = getMDKindID("tbaa");
43   assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
44
45   // Create the 'prof' metadata kind.
46   unsigned ProfID = getMDKindID("prof");
47   assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
48
49   // Create the 'fpmath' metadata kind.
50   unsigned FPAccuracyID = getMDKindID("fpmath");
51   assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
52   (void)FPAccuracyID;
53
54   // Create the 'range' metadata kind.
55   unsigned RangeID = getMDKindID("range");
56   assert(RangeID == MD_range && "range kind id drifted");
57   (void)RangeID;
58
59   // Create the 'tbaa.struct' metadata kind.
60   unsigned TBAAStructID = getMDKindID("tbaa.struct");
61   assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
62   (void)TBAAStructID;
63
64   // Create the 'invariant.load' metadata kind.
65   unsigned InvariantLdId = getMDKindID("invariant.load");
66   assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted");
67   (void)InvariantLdId;
68 }
69 LLVMContext::~LLVMContext() { delete pImpl; }
70
71 void LLVMContext::addModule(Module *M) {
72   pImpl->OwnedModules.insert(M);
73 }
74
75 void LLVMContext::removeModule(Module *M) {
76   pImpl->OwnedModules.erase(M);
77 }
78
79 //===----------------------------------------------------------------------===//
80 // Recoverable Backend Errors
81 //===----------------------------------------------------------------------===//
82
83 void LLVMContext::
84 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
85                               void *DiagContext) {
86   pImpl->InlineAsmDiagHandler = DiagHandler;
87   pImpl->InlineAsmDiagContext = DiagContext;
88 }
89
90 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
91 /// setInlineAsmDiagnosticHandler.
92 LLVMContext::InlineAsmDiagHandlerTy
93 LLVMContext::getInlineAsmDiagnosticHandler() const {
94   return pImpl->InlineAsmDiagHandler;
95 }
96
97 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
98 /// setInlineAsmDiagnosticHandler.
99 void *LLVMContext::getInlineAsmDiagnosticContext() const {
100   return pImpl->InlineAsmDiagContext;
101 }
102
103 void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
104                                        void *DiagnosticContext) {
105   pImpl->DiagnosticHandler = DiagnosticHandler;
106   pImpl->DiagnosticContext = DiagnosticContext;
107 }
108
109 LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
110   return pImpl->DiagnosticHandler;
111 }
112
113 void *LLVMContext::getDiagnosticContext() const {
114   return pImpl->DiagnosticContext;
115 }
116
117 void LLVMContext::emitError(const Twine &ErrorStr) {
118   emitError(0U, ErrorStr);
119 }
120
121 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
122   unsigned LocCookie = 0;
123   if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
124     if (SrcLoc->getNumOperands() != 0)
125       if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
126         LocCookie = CI->getZExtValue();
127   }
128   return emitError(LocCookie, ErrorStr);
129 }
130
131 void LLVMContext::diagnose(const DiagnosticInfo &DI) {
132   // If there is a report handler, use it.
133   if (pImpl->DiagnosticHandler != 0) {
134     pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
135     return;
136   }
137   // Otherwise, print the message with a prefix based on the severity.
138   std::string MsgStorage;
139   raw_string_ostream Stream(MsgStorage);
140   DiagnosticPrinterRawOStream DP(Stream);
141   DI.print(DP);
142   Stream.flush();
143   switch (DI.getSeverity()) {
144   case DS_Error:
145     errs() << "error: " << MsgStorage << "\n";
146     exit(1);
147   case DS_Warning:
148     errs() << "warning: " << MsgStorage << "\n";
149     break;
150   case DS_Note:
151     errs() << "note: " << MsgStorage << "\n";
152     break;
153   }
154 }
155
156 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
157   // If there is no error handler installed, just print the error and exit.
158   if (pImpl->InlineAsmDiagHandler == 0) {
159     errs() << "error: " << ErrorStr << "\n";
160     exit(1);
161   }
162
163   // If we do have an error handler, we can report the error and keep going.
164   SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str());
165
166   pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
167 }
168
169 //===----------------------------------------------------------------------===//
170 // Metadata Kind Uniquing
171 //===----------------------------------------------------------------------===//
172
173 #ifndef NDEBUG
174 /// isValidName - Return true if Name is a valid custom metadata handler name.
175 static bool isValidName(StringRef MDName) {
176   if (MDName.empty())
177     return false;
178
179   if (!std::isalpha(static_cast<unsigned char>(MDName[0])))
180     return false;
181
182   for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
183        ++I) {
184     if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' &&
185         *I != '-' && *I != '.')
186       return false;
187   }
188   return true;
189 }
190 #endif
191
192 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
193 unsigned LLVMContext::getMDKindID(StringRef Name) const {
194   assert(isValidName(Name) && "Invalid MDNode name");
195
196   // If this is new, assign it its ID.
197   return
198     pImpl->CustomMDKindNames.GetOrCreateValue(
199       Name, pImpl->CustomMDKindNames.size()).second;
200 }
201
202 /// getHandlerNames - Populate client supplied smallvector using custome
203 /// metadata name and ID.
204 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
205   Names.resize(pImpl->CustomMDKindNames.size());
206   for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
207        E = pImpl->CustomMDKindNames.end(); I != E; ++I)
208     Names[I->second] = I->first();
209 }