Add registered target list to --version output.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75889 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp
index 34200f7..ed1ed2d 100644
--- a/lib/Support/CommandLine.cpp
+++ b/lib/Support/CommandLine.cpp
@@ -23,6 +23,7 @@
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/Streams.h"
+#include "llvm/Target/TargetRegistry.h"
 #include "llvm/System/Path.h"
 #include <algorithm>
 #include <functional>
@@ -823,8 +824,8 @@
 // Print out the option for the alias.
 void alias::printOptionInfo(size_t GlobalWidth) const {
   size_t L = std::strlen(ArgStr);
-  cout << "  -" << ArgStr << std::string(GlobalWidth-L-6, ' ') << " - "
-       << HelpStr << "\n";
+  cerr << "  -" << ArgStr << std::string(GlobalWidth-L-6, ' ') << " - "
+         << HelpStr << "\n";
 }
 
 
@@ -1140,6 +1141,23 @@
 #endif
         cout << ".\n";
         cout << "  Built " << __DATE__ << "(" << __TIME__ << ").\n";
+        cout << "\n";
+        cout << "  Registered Targets:\n";
+
+        size_t Width = 0;
+        for (TargetRegistry::iterator it = TargetRegistry::begin(), 
+               ie = TargetRegistry::end(); it != ie; ++it)
+          Width = std::max(Width, ::strlen(it->getName()));
+
+        unsigned NumTargets = 0;
+        for (TargetRegistry::iterator it = TargetRegistry::begin(), 
+               ie = TargetRegistry::end(); it != ie; ++it, ++NumTargets) {
+          cout << "    " << it->getName()
+               << std::string(Width - ::strlen(it->getName()), ' ') << " - "
+               << it->getShortDescription() << "\n";
+        }
+        if (!NumTargets)
+          cout << "    (none)\n";
   }
   void operator=(bool OptionWasSpecified) {
     if (OptionWasSpecified) {
diff --git a/lib/Support/TargetRegistry.cpp b/lib/Support/TargetRegistry.cpp
index 77cf2dd..bf631fe 100644
--- a/lib/Support/TargetRegistry.cpp
+++ b/lib/Support/TargetRegistry.cpp
@@ -14,20 +14,23 @@
 // Clients are responsible for avoid race conditions in registration.
 static Target *FirstTarget = 0;
 
+TargetRegistry::iterator TargetRegistry::begin() {
+  return iterator(FirstTarget);
+}
+
 const Target *
 TargetRegistry::getClosestStaticTargetForTriple(const std::string &TT,
                                                 std::string &Error) {
-  Target *Best = 0, *EquallyBest = 0;
+  const Target *Best = 0, *EquallyBest = 0;
   unsigned BestQuality = 0;
-  // FIXME: Use iterator.
-  for (Target *i = FirstTarget; i; i = i->Next) {
-    if (unsigned Qual = i->TripleMatchQualityFn(TT)) {
+  for (iterator it = begin(), ie = end(); it != ie; ++it) {
+    if (unsigned Qual = it->TripleMatchQualityFn(TT)) {
       if (!Best || Qual > BestQuality) {
-        Best = i;
+        Best = &*it;
         EquallyBest = 0;
         BestQuality = Qual;
       } else if (Qual == BestQuality)
-        EquallyBest = i;
+        EquallyBest = &*it;
     }
   }
 
@@ -50,17 +53,16 @@
 const Target *
 TargetRegistry::getClosestStaticTargetForModule(const Module &M,
                                                 std::string &Error) {
-  Target *Best = 0, *EquallyBest = 0;
+  const Target *Best = 0, *EquallyBest = 0;
   unsigned BestQuality = 0;
-  // FIXME: Use iterator.
-  for (Target *i = FirstTarget; i; i = i->Next) {
-    if (unsigned Qual = i->ModuleMatchQualityFn(M)) {
+  for (iterator it = begin(), ie = end(); it != ie; ++it) {
+    if (unsigned Qual = it->ModuleMatchQualityFn(M)) {
       if (!Best || Qual > BestQuality) {
-        Best = i;
+        Best = &*it;
         EquallyBest = 0;
         BestQuality = Qual;
       } else if (Qual == BestQuality)
-        EquallyBest = i;
+        EquallyBest = &*it;
     }
   }
 
@@ -82,17 +84,16 @@
 
 const Target *
 TargetRegistry::getClosestTargetForJIT(std::string &Error) {
-  Target *Best = 0, *EquallyBest = 0;
+  const Target *Best = 0, *EquallyBest = 0;
   unsigned BestQuality = 0;
-  // FIXME: Use iterator.
-  for (Target *i = FirstTarget; i; i = i->Next) {
-    if (unsigned Qual = i->JITMatchQualityFn()) {
+  for (iterator it = begin(), ie = end(); it != ie; ++it) {
+    if (unsigned Qual = it->JITMatchQualityFn()) {
       if (!Best || Qual > BestQuality) {
-        Best = i;
+        Best = &*it;
         EquallyBest = 0;
         BestQuality = Qual;
       } else if (Qual == BestQuality)
-        EquallyBest = i;
+        EquallyBest = &*it;
     }
   }