[PATCH] C API: return NULL from LLVMGetFirstTarget instead of asserting

Peter Zotov whitequark at whitequark.org
Sat Oct 12 08:48:00 PDT 2013


If no targets are registered, LLVMGetFirstTarget currently fails with an assertion. This patch makes it return NULL instead, similarly to how LLVMGetNextTarget would.

http://llvm-reviews.chandlerc.com/D1908

Files:
  lib/Target/TargetMachineC.cpp

Index: lib/Target/TargetMachineC.cpp
===================================================================
--- lib/Target/TargetMachineC.cpp
+++ lib/Target/TargetMachineC.cpp
@@ -48,21 +48,29 @@
 inline TargetMachine *unwrap(LLVMTargetMachineRef P) {
   return reinterpret_cast<TargetMachine*>(P);
 }
+
 inline Target *unwrap(LLVMTargetRef P) {
   return reinterpret_cast<Target*>(P);
 }
+
 inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
   return
     reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
 }
+
 inline LLVMTargetRef wrap(const Target * P) {
   return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
 }
 
 LLVMTargetRef LLVMGetFirstTarget() {
-   const Target* target = &*TargetRegistry::begin();
-   return wrap(target);
+  if(TargetRegistry::begin() == TargetRegistry::end()) {
+    return NULL;
+  }
+
+  const Target* target = &*TargetRegistry::begin();
+  return wrap(target);
 }
+
 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
   return wrap(unwrap(T)->getNext());
 }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1908.1.patch
Type: text/x-patch
Size: 1033 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131012/2618addd/attachment.bin>


More information about the llvm-commits mailing list