[PATCH] clang-cl: Add the /c, /W0 and /W1 options

Hans Wennborg hans at chromium.org
Mon Jul 29 18:15:11 PDT 2013


Hi rnk,

The purpose of this patch is not so much to add a bunch of options, but to show the plan for handling clang-cl options, which is basically to translate them to regular clang options.

I was hoping that I wouldn't have to touch Options.td at all for this patch, but I realized I have to add the CLOption flag to driver_mode so that we can claim it when in cl mode, and we need -### for the test.

http://llvm-reviews.chandlerc.com/D1232

Files:
  include/clang/Driver/CLCompatOptions.td
  include/clang/Driver/Options.td
  lib/Driver/ToolChains.h
  lib/Driver/WindowsToolChain.cpp
  test/Driver/cl-options.c

Index: include/clang/Driver/CLCompatOptions.td
===================================================================
--- include/clang/Driver/CLCompatOptions.td
+++ include/clang/Driver/CLCompatOptions.td
@@ -12,10 +12,14 @@
 //===----------------------------------------------------------------------===//
 
 def cl_Group : OptionGroup<"<clang-cl options>">,
-    HelpText<"CL.EXE COMPATIBILITY OPTIONS">;
+  HelpText<"CL.EXE COMPATIBILITY OPTIONS">;
 
 class CLFlag<string name> : Option<["/", "-"], name, KIND_FLAG>,
-    Group<cl_Group>, Flags<[CLOption]>;
+  Group<cl_Group>, Flags<[CLOption, DriverOption]>;
 
 def _QUESTION : CLFlag<"?">, Alias<help>, HelpText<"Display available options">;
-def cl_help : CLFlag<"help">, Alias<help>, HelpText<"Display available options">;
+def _SLASH_c : CLFlag<"c">, HelpText<"Compile only">, Alias<c>;
+def _SLASH_help : CLFlag<"help">, Alias<help>,
+  HelpText<"Display available options">;
+def _SLASH_W0 : CLFlag<"W0">, HelpText<"Disable all warnings">;
+def _SLASH_W1 : CLFlag<"W1">, HelpText<"Enable -Wall">, Alias<Wall>;
Index: include/clang/Driver/Options.td
===================================================================
--- include/clang/Driver/Options.td
+++ include/clang/Driver/Options.td
@@ -98,6 +98,7 @@
 // substitutions:
 //   _ => __
 //   - => _
+//   / => _SLASH
 //   # => _HASH
 //   ? => _QUESTION
 //   , => _COMMA
@@ -116,7 +117,8 @@
 
 class InternalDriverOpt : Group<internal_driver_Group>,
   Flags<[DriverOption, HelpHidden]>;
-def driver_mode : Joined<["--"], "driver-mode=">, InternalDriverOpt,
+def driver_mode : Joined<["--"], "driver-mode=">, Group<internal_driver_Group>,
+  Flags<[CLOption, DriverOption, HelpHidden]>,
   HelpText<"Set the driver mode to either 'gcc', 'g++', 'cpp', or 'cl'">;
 def ccc_gcc_name : Separate<["-"], "ccc-gcc-name">, InternalDriverOpt,
   HelpText<"Name for native GCC compiler">,
@@ -171,7 +173,7 @@
 
 // Standard Options
 
-def _HASH_HASH_HASH : Flag<["-"], "###">, Flags<[DriverOption]>,
+def _HASH_HASH_HASH : Flag<["-"], "###">, Flags<[DriverOption, CLOption]>,
     HelpText<"Print the commands to run for this compilation">;
 // The '--' option is here for the sake of compatibility with gcc, but is 
 // being ignored by the driver.
Index: lib/Driver/ToolChains.h
===================================================================
--- lib/Driver/ToolChains.h
+++ lib/Driver/ToolChains.h
@@ -626,6 +626,10 @@
   AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
                                llvm::opt::ArgStringList &CC1Args) const;
 
+  virtual llvm::opt::DerivedArgList *TranslateArgs(
+      const llvm::opt::DerivedArgList &Args,
+      const char *BoundArch) const;
+
 protected:
   virtual Tool *buildLinker() const;
   virtual Tool *buildAssembler() const;
Index: lib/Driver/WindowsToolChain.cpp
===================================================================
--- lib/Driver/WindowsToolChain.cpp
+++ lib/Driver/WindowsToolChain.cpp
@@ -339,3 +339,51 @@
                                            ArgStringList &CC1Args) const {
   // FIXME: There should probably be logic here to find libc++ on Windows.
 }
+
+static bool TranslateWarningOption(DerivedArgList *DAL, const OptTable &Opts,
+                                   Arg *A, unsigned OptID) {
+  const char *Alias;
+
+  switch (OptID) {
+    case options::OPT__SLASH_W0:
+      Alias = "no-everything";
+      break;
+    case options::OPT__SLASH_W1:
+      Alias = "all";
+      break;
+    default:
+      return false;
+  }
+
+  DAL->AddJoinedArg(A, Opts.getOption(options::OPT_W_Joined), Alias);
+  return true;
+}
+
+DerivedArgList *Windows::TranslateArgs(const DerivedArgList &Args,
+                                       const char *BoundArch) const {
+  DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
+  const OptTable &Opts = getDriver().getOpts();
+
+  // Only options in cl_Group would need translation.
+  for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); it != ie;
+      ++it) {
+
+    Arg *A = *it;
+    Option Opt = A->getOption();
+    unsigned OptID = Opt.getID();
+
+    if (!Opt.matches(options::OPT_cl_Group)) {
+      // This option does not need translation; pass it through.
+      DAL->append(A);
+      continue;
+    } 
+
+    if (TranslateWarningOption(DAL, Opts, A, OptID))
+      continue;
+
+    // Option could not be translated; leave it unclaimed and let the driver
+    // warn about it.
+  }
+ 
+  return DAL;
+}
Index: test/Driver/cl-options.c
===================================================================
--- /dev/null
+++ test/Driver/cl-options.c
@@ -0,0 +1,10 @@
+// Don't attempt slash switches on msys bash.
+// REQUIRES: shell-preserves-root
+
+// RUN: %clang_cl /c /W0 %s -### 2>&1 | FileCheck -check-prefix=W0 %s
+// W0-DAG: -c
+// W0-DAG: -Wno-everything
+
+// RUN: %clang_cl /c /W1 %s -### 2>&1 | FileCheck -check-prefix=W1 %s
+// W1-DAG: -c
+// W1-DAG: -Wall
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1232.1.patch
Type: text/x-patch
Size: 4955 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130729/a875b436/attachment.bin>


More information about the cfe-commits mailing list