/*===-- Error handling ----------------------------------------------------===*/
-static char *LLVMCreateMessage(StringRef Message) {
- assert(Message.find('\0') == Message.npos);
- return strndup(Message.data(), Message.size());
-}
-
char *LLVMCreateMessage(const char *Message) {
return strdup(Message);
}
}
char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
- string_ostream Msg;
- DiagnosticPrinterRawOStream DP(Msg);
+ std::string MsgStorage;
+ raw_string_ostream Stream(MsgStorage);
+ DiagnosticPrinterRawOStream DP(Stream);
+
unwrap(DI)->print(DP);
- return LLVMCreateMessage(Msg.str());
+ Stream.flush();
+
+ return LLVMCreateMessage(MsgStorage.c_str());
}
LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI){
}
char *LLVMPrintModuleToString(LLVMModuleRef M) {
- string_ostream os;
+ std::string buf;
+ raw_string_ostream os(buf);
+
unwrap(M)->print(os, nullptr);
- return LLVMCreateMessage(os.str());
+ os.flush();
+
+ return strdup(buf.c_str());
}
/*--.. Operations on inline assembler ......................................--*/
}
char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
- string_ostream os;
+ std::string buf;
+ raw_string_ostream os(buf);
if (unwrap(Ty))
unwrap(Ty)->print(os);
else
os << "Printing <null> Type";
- return strndup(os.str().data(), os.str().size());
+ os.flush();
+
+ return strdup(buf.c_str());
}
/*--.. Operations on integer types .........................................--*/
}
char* LLVMPrintValueToString(LLVMValueRef Val) {
- string_ostream os;
+ std::string buf;
+ raw_string_ostream os(buf);
if (unwrap(Val))
unwrap(Val)->print(os);
else
os << "Printing <null> Value";
- return strndup(os.str().data(), os.str().size());
+ os.flush();
+
+ return strdup(buf.c_str());
}
void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
DontNullTerminate);
}
+
+LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef c, unsigned idx) {
+ return wrap(static_cast<ConstantDataSequential*>(unwrap(c))->getElementAsConstant(idx));
+}
+
+LLVMBool LLVMIsConstantString(LLVMValueRef c) {
+ return static_cast<ConstantDataSequential*>(unwrap(c))->isString();
+}
+
+const char *LLVMGetAsString(LLVMValueRef c, size_t* Length) {
+ StringRef str = static_cast<ConstantDataSequential*>(unwrap(c))->getAsString();
+ *Length = str.size();
+ return str.data();
+}
+
LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
LLVMValueRef *ConstantVals, unsigned Length) {
ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
}
+
LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
LLVMBool Packed) {
return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
LLVMMemoryBufferRef *OutMemBuf,
char **OutMessage) {
- std::unique_ptr<MemoryBuffer> MB;
- std::error_code ec;
- if (!(ec = MemoryBuffer::getFile(Path, MB))) {
- *OutMemBuf = wrap(MB.release());
- return 0;
+ ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
+ if (std::error_code EC = MBOrErr.getError()) {
+ *OutMessage = strdup(EC.message().c_str());
+ return 1;
}
-
- *OutMessage = strdup(ec.message().c_str());
- return 1;
+ *OutMemBuf = wrap(MBOrErr.get().release());
+ return 0;
}
LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
char **OutMessage) {
- std::unique_ptr<MemoryBuffer> MB;
- std::error_code ec;
- if (!(ec = MemoryBuffer::getSTDIN(MB))) {
- *OutMemBuf = wrap(MB.release());
- return 0;
+ ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
+ if (std::error_code EC = MBOrErr.getError()) {
+ *OutMessage = strdup(EC.message().c_str());
+ return 1;
}
-
- *OutMessage = strdup(ec.message().c_str());
- return 1;
+ *OutMemBuf = wrap(MBOrErr.get().release());
+ return 0;
}
LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
/*===-- Threading ------------------------------------------------------===*/
LLVMBool LLVMStartMultithreaded() {
- return llvm_start_multithreaded();
+ return LLVMIsMultithreaded();
}
void LLVMStopMultithreaded() {
- llvm_stop_multithreaded();
}
LLVMBool LLVMIsMultithreaded() {