Improve support for -g options accepted by Clang:
1. Accept flags -g[0-3], -ggdb[0-3], -gdwarf-[2-4] and collapse them to simple -g (except -g0/-ggdb0).
2. Produce driver error on unsupported formats (-gcoff, -gstabs, -gvms) and options (-gtoggle).
3. Recognize and ignore flags -g[no-]strict-dwarf, -g[no-]record-gcc-switches.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@158906 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td
index ddaf364..b6d352a 100644
--- a/include/clang/Driver/Options.td
+++ b/include/clang/Driver/Options.td
@@ -33,6 +33,7 @@
 def f_Group               : OptionGroup<"<f group>">, Group<CompileOnly_Group>;
 def f_clang_Group         : OptionGroup<"<f (clang-only) group>">, Group<CompileOnly_Group>;
 def g_Group               : OptionGroup<"<g group>">;
+def g_flags_Group         : OptionGroup<"<g flags group>">;
 def i_Group               : OptionGroup<"<i group>">, Group<CompileOnly_Group>;
 def clang_i_Group         : OptionGroup<"<clang i group>">, Group<i_Group>;
 def m_Group               : OptionGroup<"<m group>">, Group<CompileOnly_Group>;
@@ -671,21 +672,34 @@
 def fdata_sections : Flag <"-fdata-sections">, Group<f_Group>, Flags<[CC1Option]>,
   HelpText<"Place each data in its own section (ELF Only)">;
 def f : Joined<"-f">, Group<f_Group>;
-def g0 : Flag<"-g0">, Group<g_Group>;
-def g2 : Flag<"-g2">, Group<g_Group>;
-def g3 : Flag<"-g3">, Group<g_Group>;
-def gdwarf2 : Flag<"-gdwarf-2">, Group<g_Group>;
-def gfull : Flag<"-gfull">, Group<g_Group>;
-def ggdb : Flag<"-ggdb">, Group<g_Group>;
-def gstabs : Flag<"-gstabs">, Group<g_Group>;
-def gstabsplus : Flag<"-gstabs+">, Group<g_Group>;
-def gstabs1 : Flag<"-gstabs1">, Group<g_Group>;
-def gstabs2 : Flag<"-gstabs2">, Group<g_Group>;
-def gused : Flag<"-gused">, Group<g_Group>;
-def g_Flag : Flag<"-g">, Group<g_Group>, HelpText<"Generate source level debug information">,
-    Flags<[CC1Option]>;
+def g_Flag : Flag<"-g">, Group<g_Group>,
+  HelpText<"Generate source level debug information">, Flags<[CC1Option]>;
 def gline_tables_only : Flag<"-gline-tables-only">, Group<g_Group>,
   HelpText<"Emit debug line number tables only">, Flags<[CC1Option]>;
+def g0 : Flag<"-g0">, Group<g_Group>;
+def g1 : Flag<"-g1">, Group<g_Group>;
+def g2 : Flag<"-g2">, Group<g_Group>;
+def g3 : Flag<"-g3">, Group<g_Group>;
+def ggdb : Flag<"-ggdb">, Group<g_Group>;
+def ggdb0 : Flag<"-ggdb0">, Group<g_Group>;
+def ggdb1 : Flag<"-ggdb1">, Group<g_Group>;
+def ggdb2 : Flag<"-ggdb2">, Group<g_Group>;
+def ggdb3 : Flag<"-ggdb3">, Group<g_Group>;
+def gdwarf_2 : Flag<"-gdwarf-2">, Group<g_Group>;
+def gdwarf_3 : Flag<"-gdwarf-3">, Group<g_Group>;
+def gdwarf_4 : Flag<"-gdwarf-4">, Group<g_Group>;
+def gfull : Flag<"-gfull">, Group<g_Group>;
+def gused : Flag<"-gused">, Group<g_Group>;
+def gstabs : Joined<"-gstabs">, Group<g_Group>, Flags<[Unsupported]>;
+def gcoff : Joined<"-gcoff">, Group<g_Group>, Flags<[Unsupported]>;
+def gxcoff : Joined<"-gxcoff">, Group<g_Group>, Flags<[Unsupported]>;
+def gvms : Joined<"-gvms">, Group<g_Group>, Flags<[Unsupported]>;
+def gtoggle : Flag<"-gtoggle">, Group<g_flags_Group>, Flags<[Unsupported]>;
+def grecord_gcc_switches : Flag<"-grecord-gcc-switches">, Group<g_flags_Group>;
+def gno_record_gcc_switches : Flag<"-gno-record-gcc-switches">,
+  Group<g_flags_Group>;
+def gstrict_dwarf : Flag<"-gstrict-dwarf">, Group<g_flags_Group>;
+def gno_strict_dwarf : Flag<"-gno-strict-dwarf">, Group<g_flags_Group>;
 def headerpad__max__install__names : Joined<"-headerpad_max_install_names">;
 def help : Flag<"-help">, Flags<[CC1Option]>,
   HelpText<"Display available options">;
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index e152b93..27a222a 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -1910,21 +1910,19 @@
 
   // Use the last option from "-g" group. "-gline-tables-only" is
   // preserved, all other debug options are substituted with "-g".
-  // FIXME: We should eventually do the following:
-  //   1) collapse gdb and dwarf variations to -g (as we do now);
-  //   2) support things like -gtoggle;
-  //   3) ignore flag options like -gstrict-dwarf or -grecord-gcc-switches;
-  //   4) produce a driver error on unsupported formats
-  //      (-gstabs, -gcoff, -gvms etc.)
   Args.ClaimAllArgs(options::OPT_g_Group);
   if (Arg *A = Args.getLastArg(options::OPT_g_Group)) {
     if (A->getOption().matches(options::OPT_gline_tables_only)) {
       CmdArgs.push_back("-gline-tables-only");
-    } else if (!A->getOption().matches(options::OPT_g0)) {
+    } else if (!A->getOption().matches(options::OPT_g0) &&
+               !A->getOption().matches(options::OPT_ggdb0)) {
       CmdArgs.push_back("-g");
     }
   }
 
+  // We ignore flags -gstrict-dwarf and -grecord-gcc-switches for now.
+  Args.ClaimAllArgs(options::OPT_g_flags_Group);
+
   Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
   Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
 
diff --git a/test/Driver/debug-options.c b/test/Driver/debug-options.c
index 76fcb7b..bdeb6ca 100644
--- a/test/Driver/debug-options.c
+++ b/test/Driver/debug-options.c
@@ -2,49 +2,43 @@
 // rdar://10383444
 
 // RUN: %clang -### -c -g %s 2>&1 | FileCheck -check-prefix=G %s
-// RUN: %clang -### -c -g2 %s 2>&1 | FileCheck -check-prefix=G2 %s
-// RUN: %clang -### -c -g3 %s 2>&1 | FileCheck -check-prefix=G3 %s
-// RUN: %clang -### -c -ganything %s 2>&1 | FileCheck -check-prefix=GANY %s
-// RUN: %clang -### -c -ggdb %s 2>&1 | FileCheck -check-prefix=GGDB %s
-// RUN: %clang -### -c -gfoo %s 2>&1 | FileCheck -check-prefix=GFOO %s
-// RUN: %clang -### -c -g -g0 %s 2>&1 | FileCheck -check-prefix=GG0 %s
+// RUN: %clang -### -c -g2 %s 2>&1 | FileCheck -check-prefix=G %s
+// RUN: %clang -### -c -g3 %s 2>&1 | FileCheck -check-prefix=G %s
+// RUN: %clang -### -c -ggdb %s 2>&1 | FileCheck -check-prefix=G %s
+// RUN: %clang -### -c -ggdb1 %s 2>&1 | FileCheck -check-prefix=G %s
+// RUN: %clang -### -c -ggdb3 %s 2>&1 | FileCheck -check-prefix=G %s
+// RUN: %clang -### -c -gdwarf-2 %s 2>&1 | FileCheck -check-prefix=G %s
+//
+// RUN: %clang -### -c -gfoo %s 2>&1 | FileCheck -check-prefix=G_NO %s
+// RUN: %clang -### -c -g -g0 %s 2>&1 | FileCheck -check-prefix=G_NO %s
+// RUN: %clang -### -c -ggdb0 %s 2>&1 | FileCheck -check-prefix=G_NO %s
+//
 // RUN: %clang -### -c -gline-tables-only %s 2>&1 \
-// RUN:             | FileCheck -check-prefix=GLTO %s
+// RUN:             | FileCheck -check-prefix=GLTO_ONLY %s
 // RUN: %clang -### -c -gline-tables-only -g %s 2>&1 \
-// RUN:             | FileCheck -check-prefix=GLTO2 %s
+// RUN:             | FileCheck -check-prefix=G_ONLY %s
 // RUN: %clang -### -c -gline-tables-only -g0 %s 2>&1 \
-// RUN:             | FileCheck -check-prefix=GLTO3 %s
+// RUN:             | FileCheck -check-prefix=GLTO_NO %s
+//
+// RUN: %clang -c -grecord-gcc-switches -gno-record-gcc-switches \
+// RUN:           -gstrict-dwarf -gno-strict-dwarf %s 2>&1 \
+// RUN:        | not grep "argument unused during compilation"
 //
 // G: "-cc1"
 // G: "-g"
 //
-// G2: "-cc1"
-// G2: "-g"
+// G_NO: "-cc1"
+// G_NO-NOT: "-g"
 //
-// G3: "-cc1"
-// G3: "-g"
+// GLTO_ONLY: "-cc1"
+// GLTO_ONLY-NOT: "-g"
+// GLTO_ONLY: "-gline-tables-only"
+// GLTO_ONLY-NOT: "-g"
 //
-// GANY: "-cc1"
-// GANY-NOT: "-g"
+// G_ONLY: "-cc1"
+// G_ONLY-NOT: "-gline-tables-only"
+// G_ONLY: "-g"
+// G_ONLY-NOT: "-gline-tables-only"
 //
-// GGDB: "-cc1"
-// GGDB: "-g"
-//
-// GFOO: "-cc1"
-// GFOO-NOT: "-g"
-//
-// GG0: "-cc1"
-// GG0-NOT: "-g"
-//
-// GLTO: "-cc1"
-// GLTO-NOT: "-g"
-// GLTO: "-gline-tables-only"
-// GLTO-NOT: "-g"
-//
-// GLTO2: "-cc1"
-// GLTO2-NOT: "-gline-tables-only"
-// GLTO2: "-g"
-// GLTO2-NOT: "-gline-tables-only"
-//
-// GLTO3: "-cc1"
-// GLTO3-NOT: "-gline-tables-only"
+// GLTO_NO: "-cc1"
+// GLTO_NO-NOT: "-gline-tables-only"
diff --git a/test/Driver/debug-unsupported.c b/test/Driver/debug-unsupported.c
new file mode 100644
index 0000000..2c3de0b
--- /dev/null
+++ b/test/Driver/debug-unsupported.c
@@ -0,0 +1,13 @@
+// RUN: %clang -c -gstabs %s 2>&1 | FileCheck %s
+// RUN: %clang -c -gstabs+ %s 2>&1 | FileCheck %s
+// RUN: %clang -c -gcoff %s 2>&1 | FileCheck %s
+// RUN: %clang -c -gxcoff %s 2>&1 | FileCheck %s
+// RUN: %clang -c -gxcoff+ %s 2>&1 | FileCheck %s
+// RUN: %clang -c -gvms %s 2>&1 | FileCheck %s
+// RUN: %clang -c -gstabs1 %s 2>&1 | FileCheck %s
+// RUN: %clang -c -gcoff2 %s 2>&1 | FileCheck %s
+// RUN: %clang -c -gxcoff3 %s 2>&1 | FileCheck %s
+// RUN: %clang -c -gvms0 %s 2>&1 | FileCheck %s
+// RUN: %clang -c -gtoggle %s 2>&1 | FileCheck %s
+//
+// CHECK: clang: error: unsupported option