llvm-c: Return NULL from LLVMGetFirstTarget instead of asserting
authorAnders Waldenborg <anders@0x63.nu>
Thu, 17 Oct 2013 10:25:24 +0000 (10:25 +0000)
committerAnders Waldenborg <anders@0x63.nu>
Thu, 17 Oct 2013 10:25:24 +0000 (10:25 +0000)
If no targets are registered, LLVMGetFirstTarget currently fails with
an assertion. This patch makes it return NULL instead, similarly to
how LLVMGetNextTarget would.

Patch by Peter Zotov

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@192878 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/TargetMachineC.cpp

index 741912200da14fef70d4d421f5809e99beb3de88..9fccfcd9e22517c265b6f7e791d99223ed4ecb2b 100644 (file)
@@ -60,8 +60,12 @@ inline LLVMTargetRef wrap(const 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());