[cfe-commits] r89435 - in /cfe/trunk: include/clang/Driver/CC1Options.td lib/Driver/CC1Options.cpp

Daniel Dunbar daniel at zuster.org
Thu Nov 19 17:46:45 PST 2009


Author: ddunbar
Date: Thu Nov 19 19:46:44 2009
New Revision: 89435

URL: http://llvm.org/viewvc/llvm-project?rev=89435&view=rev
Log:
Driver: Add parsing for TargetOptions to CompilerInvocation::CreateFromArgs.

Modified:
    cfe/trunk/include/clang/Driver/CC1Options.td
    cfe/trunk/lib/Driver/CC1Options.cpp

Modified: cfe/trunk/include/clang/Driver/CC1Options.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/CC1Options.td?rev=89435&r1=89434&r2=89435&view=diff

==============================================================================
--- cfe/trunk/include/clang/Driver/CC1Options.td (original)
+++ cfe/trunk/include/clang/Driver/CC1Options.td Thu Nov 19 19:46:44 2009
@@ -18,9 +18,9 @@
 
 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)">;

Modified: cfe/trunk/lib/Driver/CC1Options.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/CC1Options.cpp?rev=89435&r1=89434&r2=89435&view=diff

==============================================================================
--- cfe/trunk/lib/Driver/CC1Options.cpp (original)
+++ cfe/trunk/lib/Driver/CC1Options.cpp Thu Nov 19 19:46:44 2009
@@ -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 @@
 
 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);
 }





More information about the cfe-commits mailing list