]> granicus.if.org Git - clang/commitdiff
Driver: Add parsing for TargetOptions to CompilerInvocation::CreateFromArgs.
authorDaniel Dunbar <daniel@zuster.org>
Fri, 20 Nov 2009 01:46:44 +0000 (01:46 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Fri, 20 Nov 2009 01:46:44 +0000 (01:46 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@89435 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Driver/CC1Options.td
lib/Driver/CC1Options.cpp

index ef8d8478282d45eefcac407a86868d634a620f79..2ce2d4c95136d11c85144ddcdedd20f79bcfd56d 100644 (file)
@@ -18,9 +18,9 @@ include "OptParser.td"
 
 def target_abi : Separate<"-target-abi">,
   HelpText<"Target a particular ABI type">;
-def target_cpu : Separate<"-mcpu">,
+def mcpu : Separate<"-mcpu">,
   HelpText<"Target a specific cpu type (-mcpu=help for details)">;
-def target_features : Separate<"-target-feature">,
+def target_feature : Separate<"-target-feature">,
   HelpText<"Target specific attributes">;
-def target_triple : Separate<"-triple">,
+def triple : Separate<"-triple">,
   HelpText<"Specify target triple (e.g. i686-apple-darwin9)">;
index 45ec1ab6a0fc59d9c16b80c95b3a2f74f64aeff6..c9bb5d08f55a88b91cedfee070febdd6352f82c0 100644 (file)
@@ -8,10 +8,15 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Driver/CC1Options.h"
+#include "clang/Driver/ArgList.h"
+#include "clang/Driver/Arg.h"
 #include "clang/Driver/OptTable.h"
 #include "clang/Driver/Option.h"
 #include "clang/Frontend/CompilerInvocation.h"
+#include "llvm/ADT/OwningPtr.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/System/Host.h"
 
 using namespace clang::driver;
 using namespace clang::driver::options;
@@ -43,6 +48,55 @@ OptTable *clang::driver::createCC1OptTable() {
 
 using namespace clang;
 
+static llvm::StringRef getLastArgValue(ArgList &Args, cc1options::ID ID,
+                                       llvm::StringRef Default = "") {
+  if (Arg *A = Args.getLastArg(ID))
+    return A->getValue(Args);
+  return Default;
+}
+
+static std::vector<std::string>
+getAllArgValues(ArgList &Args, cc1options::ID ID) {
+  llvm::SmallVector<const char *, 16> Values;
+  Args.AddAllArgValues(Values, ID);
+  return std::vector<std::string>(Values.begin(), Values.end());
+}
+
+static void ParseTargetArgs(TargetOptions &Opts, ArgList &Args) {
+  Opts.ABI = getLastArgValue(Args, cc1options::OPT_target_abi);
+  Opts.CPU = getLastArgValue(Args, cc1options::OPT_mcpu);
+  Opts.Triple = getLastArgValue(Args, cc1options::OPT_triple);
+  Opts.Features = getAllArgValues(Args, cc1options::OPT_target_feature);
+
+  // Use the host triple if unspecified.
+  if (Opts.Triple.empty())
+    Opts.Triple = llvm::sys::getHostTriple();
+}
+
 void CompilerInvocation::CreateFromArgs(CompilerInvocation &Res,
                            const llvm::SmallVectorImpl<llvm::StringRef> &Args) {
+  // This is gratuitous, but until we switch the driver to using StringRe we
+  // need to get C strings.
+  llvm::SmallVector<std::string, 16> StringArgs(Args.begin(), Args.end());
+  llvm::SmallVector<const char *, 16> CStringArgs;
+  for (unsigned i = 0, e = Args.size(); i != e; ++i)
+    CStringArgs.push_back(StringArgs[i].c_str());
+
+  // Parse the arguments.
+  llvm::OwningPtr<OptTable> Opts(createCC1OptTable());
+  unsigned MissingArgIndex, MissingArgCount;
+  llvm::OwningPtr<InputArgList> InputArgs(
+    Opts->ParseArgs(CStringArgs.begin(), CStringArgs.end(),
+                    MissingArgIndex, MissingArgCount));
+
+  // Check for missing argument error.
+  if (MissingArgCount) {
+    // FIXME: Use proper diagnostics!
+    llvm::errs() << "error: argument to '"
+                 << InputArgs->getArgString(MissingArgIndex)
+                 << "' is missing (expected " << MissingArgCount
+                 << " value )\n";
+  }
+
+  ParseTargetArgs(Res.getTargetOpts(), *InputArgs);
 }