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/tools/clang-cc/Options.cpp b/tools/clang-cc/Options.cpp
index 3c57479..1217a54 100644
--- a/tools/clang-cc/Options.cpp
+++ b/tools/clang-cc/Options.cpp
@@ -14,6 +14,7 @@
 #include "Options.h"
 #include "clang/Basic/LangOptions.h"
 #include "clang/Basic/TargetInfo.h"
+#include "clang/Basic/TargetOptions.h"
 #include "clang/Frontend/AnalysisConsumer.h"
 #include "clang/CodeGen/CodeGenOptions.h"
 #include "clang/Frontend/DependencyOutputOptions.h"
@@ -27,6 +28,7 @@
 #include "llvm/ADT/StringMap.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/RegistryParser.h"
+#include "llvm/System/Host.h"
 #include <stdio.h>
 
 using namespace clang;
@@ -193,13 +195,6 @@
 static llvm::cl::opt<bool>
 OptSize("Os", llvm::cl::desc("Optimize for size"));
 
-static llvm::cl::opt<std::string>
-TargetCPU("mcpu",
-         llvm::cl::desc("Target a specific cpu type (-mcpu=help for details)"));
-
-static llvm::cl::list<std::string>
-TargetFeatures("target-feature", llvm::cl::desc("Target specific attributes"));
-
 }
 
 //===----------------------------------------------------------------------===//
@@ -297,7 +292,6 @@
 
 }
 
-
 //===----------------------------------------------------------------------===//
 // Frontend Options
 //===----------------------------------------------------------------------===//
@@ -455,14 +449,6 @@
 Stats("print-stats",
       llvm::cl::desc("Print performance metrics and statistics"));
 
-static llvm::cl::opt<std::string>
-TargetABI("target-abi",
-          llvm::cl::desc("Target a particular ABI type"));
-
-static llvm::cl::opt<std::string>
-TargetTriple("triple",
-  llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)"));
-
 static llvm::cl::opt<bool>
 TimeReport("ftime-report",
            llvm::cl::desc("Print the amount of time each "
@@ -794,48 +780,36 @@
                                 "addition to normal output"));
 
 }
+//===----------------------------------------------------------------------===//
+// Target Options
+//===----------------------------------------------------------------------===//
+
+namespace targetoptions {
+
+static llvm::cl::opt<std::string>
+TargetABI("target-abi",
+          llvm::cl::desc("Target a particular ABI type"));
+
+static llvm::cl::opt<std::string>
+TargetCPU("mcpu",
+         llvm::cl::desc("Target a specific cpu type (-mcpu=help for details)"));
+
+static llvm::cl::list<std::string>
+TargetFeatures("target-feature", llvm::cl::desc("Target specific attributes"));
+
+static llvm::cl::opt<std::string>
+TargetTriple("triple",
+  llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)"));
+
+}
 
 //===----------------------------------------------------------------------===//
 // Option Object Construction
 //===----------------------------------------------------------------------===//
 
-void clang::InitializeCodeGenOptions(CodeGenOptions &Opts,
-                                     const TargetInfo &Target) {
+void clang::InitializeCodeGenOptions(CodeGenOptions &Opts) {
   using namespace codegenoptions;
 
-  // Compute the target features, we need the target to handle this because
-  // features may have dependencies on one another.
-  llvm::StringMap<bool> Features;
-  Target.getDefaultFeatures(TargetCPU, Features);
-
-  // Apply the user specified deltas.
-  for (llvm::cl::list<std::string>::iterator it = TargetFeatures.begin(),
-         ie = TargetFeatures.end(); it != ie; ++it) {
-    const char *Name = it->c_str();
-
-    // FIXME: Don't handle errors like this.
-    if (Name[0] != '-' && Name[0] != '+') {
-      fprintf(stderr, "error: clang-cc: invalid target feature string: %s\n",
-              Name);
-      exit(1);
-    }
-
-    // Apply the feature via the target.
-    if (!Target.setFeatureEnabled(Features, Name + 1, (Name[0] == '+'))) {
-      fprintf(stderr, "error: clang-cc: invalid target feature name: %s\n",
-              Name + 1);
-      exit(1);
-    }
-  }
-
-  // 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.
-  for (llvm::StringMap<bool>::const_iterator it = Features.begin(),
-         ie = Features.end(); it != ie; ++it)
-    Opts.Features.push_back(std::string(it->second ? "+" : "-") + it->first());
-
   // -Os implies -O2
   Opts.OptimizationLevel = OptSize ? 2 : OptLevel;
 
@@ -843,7 +817,6 @@
   Opts.Inlining = (Opts.OptimizationLevel > 1) ? CodeGenOptions::NormalInlining
     : CodeGenOptions::OnlyAlwaysInlining;
 
-  Opts.CPU = TargetCPU;
   Opts.DebugInfo = GenerateDebugInfo;
   Opts.DisableLLVMOpts = DisableLLVMOptimizations;
   Opts.DisableRedZone = DisableRedZone;
@@ -908,8 +881,6 @@
   Opts.ShowMacrosInCodeCompletion = CodeCompletionWantsMacros;
   Opts.ShowStats = Stats;
   Opts.ShowTimers = TimeReport;
-  Opts.TargetABI = TargetABI;
-  Opts.TargetTriple = TargetTriple;
   Opts.ViewClassInheritance = InheritanceViewCls;
 
   // '-' is the default input if none is given.
@@ -1160,14 +1131,9 @@
   Options.setVisibilityMode(SymbolVisibility);
   Options.OverflowChecking = OverflowChecking;
 
-
   // Allow the target to set the default the language options as it sees fit.
   Target.getDefaultLangOptions(Options);
 
-  // Pass the map of target features to the target for validation and
-  // processing.
-  Target.HandleTargetFeatures(CodeGenOpts.Features);
-
   if (LangStd == lang_unspecified) {
     // Based on the base language, pick one.
     switch (IK) {
@@ -1363,3 +1329,16 @@
   Opts.ShowComments = EnableCommentOutput;
   Opts.ShowMacroComments = EnableMacroCommentOutput;
 }
+
+void clang::InitializeTargetOptions(TargetOptions &Opts) {
+  using namespace targetoptions;
+
+  Opts.ABI = TargetABI;
+  Opts.CPU = TargetCPU;
+  Opts.Triple = TargetTriple;
+  Opts.Features = TargetFeatures;
+
+  // Use the host triple if unspecified.
+  if (Opts.Triple.empty())
+    Opts.Triple = llvm::sys::getHostTriple();
+}
diff --git a/tools/clang-cc/Options.h b/tools/clang-cc/Options.h
index b3fe36f..461c46e 100644
--- a/tools/clang-cc/Options.h
+++ b/tools/clang-cc/Options.h
@@ -25,6 +25,7 @@
 class PreprocessorOptions;
 class PreprocessorOutputOptions;
 class TargetInfo;
+class TargetOptions;
 
 void InitializeAnalyzerOptions(AnalyzerOptions &Opts);
 
@@ -34,8 +35,7 @@
 
 void InitializeFrontendOptions(FrontendOptions &Opts);
 
-void InitializeCodeGenOptions(CodeGenOptions &Opts,
-                              const TargetInfo &Target);
+void InitializeCodeGenOptions(CodeGenOptions &Opts);
 
 void InitializeHeaderSearchOptions(HeaderSearchOptions &Opts,
                                    llvm::StringRef BuiltinIncludePath,
@@ -50,6 +50,8 @@
 
 void InitializePreprocessorOutputOptions(PreprocessorOutputOptions &Opts);
 
+void InitializeTargetOptions(TargetOptions &Opts);
+
 } // end namespace clang
 
 #endif
diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp
index 3df2edc..19c0590 100644
--- a/tools/clang-cc/clang-cc.cpp
+++ b/tools/clang-cc/clang-cc.cpp
@@ -156,37 +156,21 @@
 static TargetInfo *
 ConstructCompilerInvocation(CompilerInvocation &Opts, Diagnostic &Diags,
                             const char *Argv0, bool &IsAST) {
-  // Initialize frontend options.
-  InitializeFrontendOptions(Opts.getFrontendOpts());
-
-  // FIXME: The target information in frontend options should be split out into
-  // TargetOptions, and the target options in codegen options should move there
-  // as well. Then we could properly initialize in layering order.
-
-  // Initialize base triple.  If a -triple option has been specified, use
-  // that triple.  Otherwise, default to the host triple.
-  llvm::Triple Triple(Opts.getFrontendOpts().TargetTriple);
-  if (Triple.getTriple().empty())
-    Triple = llvm::Triple(llvm::sys::getHostTriple());
+  // Initialize target options.
+  InitializeTargetOptions(Opts.getTargetOpts());
 
   // Get information about the target being compiled for.
-  TargetInfo *Target = TargetInfo::CreateTargetInfo(Triple.getTriple());
-  if (!Target) {
-    Diags.Report(diag::err_fe_unknown_triple) << Triple.getTriple().c_str();
+  llvm::OwningPtr<TargetInfo> Target(
+    TargetInfo::CreateTargetInfo(Diags, Opts.getTargetOpts()));
+  if (!Target)
     return 0;
-  }
-
-  // Set the target ABI if specified.
-  if (!Opts.getFrontendOpts().TargetABI.empty() &&
-      !Target->setABI(Opts.getFrontendOpts().TargetABI)) {
-    Diags.Report(diag::err_fe_unknown_target_abi)
-      << Opts.getFrontendOpts().TargetABI;
-    return 0;
-  }
 
   // Initialize backend options, which may also be used to key some language
   // options.
-  InitializeCodeGenOptions(Opts.getCodeGenOpts(), *Target);
+  InitializeCodeGenOptions(Opts.getCodeGenOpts());
+
+  // Initialize frontend options.
+  InitializeFrontendOptions(Opts.getFrontendOpts());
 
   // Determine the input language, we currently require all files to match.
   FrontendOptions::InputKind IK = Opts.getFrontendOpts().Inputs[0].first;
@@ -231,7 +215,7 @@
     Opts.getCodeGenOpts().NoCommon = 1;
   Opts.getCodeGenOpts().TimePasses = Opts.getFrontendOpts().ShowTimers;
 
-  return Target;
+  return Target.take();
 }
 
 int main(int argc, char **argv) {