@@ -23,18 +23,28 @@ using namespace llvm;
23
23
using namespace llvm ::sys;
24
24
using namespace llvm ::object;
25
25
26
- const char *LLVMRustError ;
26
+ static char *LastError ;
27
27
28
28
extern " C" LLVMMemoryBufferRef
29
29
LLVMRustCreateMemoryBufferWithContentsOfFile (const char *Path) {
30
30
LLVMMemoryBufferRef MemBuf = NULL ;
31
- LLVMCreateMemoryBufferWithContentsOfFile (Path, &MemBuf,
32
- const_cast <char **>(&LLVMRustError));
31
+ char *err = NULL ;
32
+ LLVMCreateMemoryBufferWithContentsOfFile (Path, &MemBuf, &err);
33
+ if (err != NULL ) {
34
+ LLVMRustSetLastError (err);
35
+ }
33
36
return MemBuf;
34
37
}
35
38
36
- extern " C" const char *LLVMRustGetLastError (void ) {
37
- return LLVMRustError;
39
+ extern " C" char *LLVMRustGetLastError (void ) {
40
+ char *ret = LastError;
41
+ LastError = NULL ;
42
+ return ret;
43
+ }
44
+
45
+ void LLVMRustSetLastError (const char *err) {
46
+ free ((void *) LastError);
47
+ LastError = strdup (err);
38
48
}
39
49
40
50
extern " C" void
@@ -609,14 +619,14 @@ LLVMRustLinkInExternalBitcode(LLVMModuleRef dst, char *bc, size_t len) {
609
619
MemoryBuffer* buf = MemoryBuffer::getMemBufferCopy (StringRef (bc, len));
610
620
ErrorOr<Module *> Src = llvm::getLazyBitcodeModule (buf, Dst->getContext ());
611
621
if (!Src) {
612
- LLVMRustError = Src.getError ().message ().c_str ();
622
+ LLVMRustSetLastError ( Src.getError ().message ().c_str () );
613
623
delete buf;
614
624
return false ;
615
625
}
616
626
617
627
std::string Err;
618
628
if (Linker::LinkModules (Dst, *Src, Linker::DestroySource, &Err)) {
619
- LLVMRustError = Err.c_str ();
629
+ LLVMRustSetLastError ( Err.c_str () );
620
630
return false ;
621
631
}
622
632
return true ;
@@ -629,13 +639,13 @@ LLVMRustLinkInExternalBitcode(LLVMModuleRef dst, char *bc, size_t len) {
629
639
std::string Err;
630
640
Module *Src = llvm::getLazyBitcodeModule (buf, Dst->getContext (), &Err);
631
641
if (!Src) {
632
- LLVMRustError = Err.c_str ();
642
+ LLVMRustSetLastError ( Err.c_str () );
633
643
delete buf;
634
644
return false ;
635
645
}
636
646
637
647
if (Linker::LinkModules (Dst, Src, Linker::DestroySource, &Err)) {
638
- LLVMRustError = Err.c_str ();
648
+ LLVMRustSetLastError ( Err.c_str () );
639
649
return false ;
640
650
}
641
651
return true ;
@@ -648,12 +658,12 @@ LLVMRustOpenArchive(char *path) {
648
658
std::unique_ptr<MemoryBuffer> buf;
649
659
error_code err = MemoryBuffer::getFile (path, buf);
650
660
if (err) {
651
- LLVMRustError = err.message ().c_str ();
661
+ LLVMRustSetLastError ( err.message ().c_str () );
652
662
return NULL ;
653
663
}
654
664
Archive *ret = new Archive (buf.release (), err);
655
665
if (err) {
656
- LLVMRustError = err.message ().c_str ();
666
+ LLVMRustSetLastError ( err.message ().c_str () );
657
667
return NULL ;
658
668
}
659
669
return ret;
@@ -664,12 +674,12 @@ LLVMRustOpenArchive(char *path) {
664
674
OwningPtr<MemoryBuffer> buf;
665
675
error_code err = MemoryBuffer::getFile (path, buf);
666
676
if (err) {
667
- LLVMRustError = err.message ().c_str ();
677
+ LLVMRustSetLastError ( err.message ().c_str () );
668
678
return NULL ;
669
679
}
670
680
Archive *ret = new Archive (buf.take (), err);
671
681
if (err) {
672
- LLVMRustError = err.message ().c_str ();
682
+ LLVMRustSetLastError ( err.message ().c_str () );
673
683
return NULL ;
674
684
}
675
685
return ret;
0 commit comments