Add TargetOptions and use it when constructing targets.
 - This ended up being hard to factor, sorry for the large diff.

 - Some post-commit cleanup to come.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@88833 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Basic/Targets.cpp b/lib/Basic/Targets.cpp
index d1888df..f00eb8f 100644
--- a/lib/Basic/Targets.cpp
+++ b/lib/Basic/Targets.cpp
@@ -12,11 +12,14 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "clang/Basic/Builtins.h"
-#include "clang/Basic/TargetBuiltins.h"
 #include "clang/Basic/TargetInfo.h"
+#include "clang/Basic/Builtins.h"
+#include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/LangOptions.h"
+#include "clang/Basic/TargetBuiltins.h"
+#include "clang/Basic/TargetOptions.h"
 #include "llvm/ADT/APFloat.h"
+#include "llvm/ADT/OwningPtr.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
@@ -1836,9 +1839,7 @@
 // Driver code
 //===----------------------------------------------------------------------===//
 
-/// CreateTargetInfo - Return the target info object for the specified target
-/// triple.
-TargetInfo* TargetInfo::CreateTargetInfo(const std::string &T) {
+static TargetInfo *AllocateTarget(const std::string &T) {
   llvm::Triple Triple(T);
   llvm::Triple::OSType os = Triple.getOS();
 
@@ -1942,3 +1943,53 @@
     }
   }
 }
+
+/// CreateTargetInfo - Return the target info object for the specified target
+/// triple.
+TargetInfo *TargetInfo::CreateTargetInfo(Diagnostic &Diags,
+                                         const TargetOptions &Opts) {
+  llvm::Triple Triple(Opts.Triple);
+
+  // Construct the target
+  llvm::OwningPtr<TargetInfo> Target(AllocateTarget(Triple.str()));
+  if (!Target) {
+    Diags.Report(diag::err_target_unknown_triple) << Triple.str();
+    return 0;
+  }
+
+  // Set the target ABI if specified.
+  if (!Opts.ABI.empty() && !Target->setABI(Opts.ABI)) {
+    Diags.Report(diag::err_target_unknown_abi) << Opts.ABI;
+    return 0;
+  }
+
+  // Compute the default target features, we need the target to handle this
+  // because features may have dependencies on one another.
+  llvm::StringMap<bool> Features;
+  Target->getDefaultFeatures(Opts.CPU, Features);
+
+  // Apply the user specified deltas.
+  for (std::vector<std::string>::const_iterator it = Opts.Features.begin(),
+         ie = Opts.Features.end(); it != ie; ++it) {
+    const char *Name = it->c_str();
+
+    // Apply the feature via the target.
+    if ((Name[0] != '-' && Name[0] != '+') ||
+        !Target->setFeatureEnabled(Features, Name + 1, (Name[0] == '+'))) {
+      Diags.Report(diag::err_target_invalid_feature) << Name;
+      return 0;
+    }
+  }
+
+  // Add the features to the compile options.
+  //
+  // FIXME: If we are completely confident that we have the right set, we only
+  // need to pass the minuses.
+  std::vector<std::string> StrFeatures;
+  for (llvm::StringMap<bool>::const_iterator it = Features.begin(),
+         ie = Features.end(); it != ie; ++it)
+    StrFeatures.push_back(std::string(it->second ? "+" : "-") + it->first());
+  Target->HandleTargetFeatures(StrFeatures);
+
+  return Target.take();
+}
diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp
index 82786aa..e3cd6dd 100644
--- a/lib/Frontend/ASTUnit.cpp
+++ b/lib/Frontend/ASTUnit.cpp
@@ -18,6 +18,7 @@
 #include "clang/AST/StmtVisitor.h"
 #include "clang/Lex/HeaderSearch.h"
 #include "clang/Lex/Preprocessor.h"
+#include "clang/Basic/TargetOptions.h"
 #include "clang/Basic/TargetInfo.h"
 #include "clang/Basic/Diagnostic.h"
 #include "llvm/Support/Compiler.h"
@@ -132,7 +133,14 @@
   // PCH loaded successfully. Now create the preprocessor.
 
   // Get information about the target being compiled for.
-  AST->Target.reset(TargetInfo::CreateTargetInfo(TargetTriple));
+  //
+  // FIXME: This is broken, we should store the TargetOptions in the PCH.
+  TargetOptions TargetOpts;
+  TargetOpts.ABI = "";
+  TargetOpts.CPU = "";
+  TargetOpts.Features.clear();
+  TargetOpts.Triple = TargetTriple;
+  AST->Target.reset(TargetInfo::CreateTargetInfo(AST->Diags, TargetOpts));
   AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(),
                                  AST->getSourceManager(), HeaderInfo));
   Preprocessor &PP = *AST->PP.get();
diff --git a/lib/Frontend/Backend.cpp b/lib/Frontend/Backend.cpp
index 23d59cf..bc56029 100644
--- a/lib/Frontend/Backend.cpp
+++ b/lib/Frontend/Backend.cpp
@@ -8,12 +8,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Frontend/ASTConsumers.h"
-#include "clang/CodeGen/ModuleBuilder.h"
-#include "clang/CodeGen/CodeGenOptions.h"
-#include "clang/AST/ASTContext.h"
 #include "clang/AST/ASTConsumer.h"
+#include "clang/AST/ASTContext.h"
 #include "clang/AST/DeclGroup.h"
 #include "clang/Basic/TargetInfo.h"
+#include "clang/Basic/TargetOptions.h"
+#include "clang/CodeGen/CodeGenOptions.h"
+#include "clang/CodeGen/ModuleBuilder.h"
 #include "llvm/Module.h"
 #include "llvm/ModuleProvider.h"
 #include "llvm/PassManager.h"
@@ -41,6 +42,7 @@
   class VISIBILITY_HIDDEN BackendConsumer : public ASTConsumer {
     BackendAction Action;
     CodeGenOptions CodeGenOpts;
+    TargetOptions TargetOpts;
     llvm::raw_ostream *AsmOutStream;
     llvm::formatted_raw_ostream FormattedOutStream;
     ASTContext *Context;
@@ -76,10 +78,11 @@
   public:
     BackendConsumer(BackendAction action, Diagnostic &Diags,
                     const LangOptions &langopts, const CodeGenOptions &compopts,
-                    const std::string &infile, llvm::raw_ostream* OS,
-                    LLVMContext& C) :
+                    const TargetOptions &targetopts, const std::string &infile,
+                    llvm::raw_ostream* OS, LLVMContext& C) :
       Action(action),
       CodeGenOpts(compopts),
+      TargetOpts(targetopts),
       AsmOutStream(OS),
       LLVMIRGeneration("LLVM IR Generation Time"),
       CodeGenerationTime("Code Generation Time"),
@@ -213,12 +216,12 @@
     }
 
     std::string FeaturesStr;
-    if (CodeGenOpts.CPU.size() || CodeGenOpts.Features.size()) {
+    if (TargetOpts.CPU.size() || TargetOpts.Features.size()) {
       SubtargetFeatures Features;
-      Features.setCPU(CodeGenOpts.CPU);
+      Features.setCPU(TargetOpts.CPU);
       for (std::vector<std::string>::iterator
-             it = CodeGenOpts.Features.begin(),
-             ie = CodeGenOpts.Features.end(); it != ie; ++it)
+             it = TargetOpts.Features.begin(),
+             ie = TargetOpts.Features.end(); it != ie; ++it)
         Features.AddFeature(*it);
       FeaturesStr = Features.getString();
     }
@@ -371,9 +374,10 @@
                                           Diagnostic &Diags,
                                           const LangOptions &LangOpts,
                                           const CodeGenOptions &CodeGenOpts,
+                                          const TargetOptions &TargetOpts,
                                           const std::string& InFile,
                                           llvm::raw_ostream* OS,
                                           LLVMContext& C) {
   return new BackendConsumer(Action, Diags, LangOpts, CodeGenOpts,
-                             InFile, OS, C);
+                             TargetOpts, InFile, OS, C);
 }
diff --git a/lib/Frontend/FrontendActions.cpp b/lib/Frontend/FrontendActions.cpp
index 3222471..7a7537b 100644
--- a/lib/Frontend/FrontendActions.cpp
+++ b/lib/Frontend/FrontendActions.cpp
@@ -170,8 +170,8 @@
     OS.reset(CI.createDefaultOutputFile(true, InFile, "bc"));
 
   return CreateBackendConsumer(BA, CI.getDiagnostics(), CI.getLangOpts(),
-                               CI.getCodeGenOpts(), InFile, OS.take(),
-                               CI.getLLVMContext());
+                               CI.getCodeGenOpts(), CI.getTargetOpts(), InFile,
+                               OS.take(), CI.getLLVMContext());
 }
 
 EmitAssemblyAction::EmitAssemblyAction()