On 12/3/20 2:12 PM, Richard Biener wrote:
Can we somehow preserve this by making the helper produce separate
strings for the 'GNU C17 11.0 ...' part and the options passed part?  So the
-fverbose-asm and -Q consumer can continue to nicely print the option part?

Yep, good idea. I've just done that and I have:

1) -Q output now:

GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
options passed: -dumpbase-ext .c -mtune=generic -march=x86-64 -g -O2

and

GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
options passed: ./xgcc -B. -Q -v /home/marxin/Programming/testcases/a.c -c -O2 
-fverbose-asm -frecord-gcc-switches-format=driver -g -S

2) For -fverbose-asm:

# GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
# options passed: -dumpbase-ext .c -mtune=generic -march=x86-64 -g -O2

and

# GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
# options passed: -dumpbase-ext .c -mtune=generic -march=x86-64 -g -O2 
-frecord-gcc-switches-file=/tmp/ccm3kL7d.cmdline

3) DWARF producer:

    <c>   DW_AT_producer    : (indirect string, offset: 0x97): GNU C17 11.0.0 
20201204 (experimental) -dumpbase-ext .c -mtune=generic -march=x86-64 -g -O2

and

    <c>   DW_AT_producer    : (indirect string, offset: 0x27): GNU C17 11.0.0 
20201204 (experimental) ./xgcc -B. -Q -v /home/marxin/Programming/testcases/a.c -c 
-O2 -fverbose-asm -frecord-gcc-switches-format=driver -g

Thoughts?
Thanks,
Martin
>From 23de1dbbd81f662e9eb4a67385b0a2d0773569be Mon Sep 17 00:00:00 2001
From: Martin Liska <mli...@suse.cz>
Date: Mon, 23 Nov 2020 13:40:04 +0100
Subject: [PATCH 1/2] Refactor -frecord-gcc-switches.

gcc/ChangeLog:

	* doc/tm.texi: Change argument of the record_gcc_switches
	hook and remove SWITCH_TYPE_* enum values.
	* dwarf2out.c (gen_producer_string): Move to opts.c and remove
	handling of the dwarf_record_gcc_switches option.
	(dwarf2out_early_finish): Use moved gen_producer_string
	function.
	* opts.c (gen_producer_string): New.
	* opts.h (gen_producer_string): New.
	* target.def: Change type of record_gcc_switches.
	* target.h (enum print_switch_type): Remove.
	(elf_record_gcc_switches): Change first argument.
	* toplev.c (MAX_LINE): Remove.
	(print_to_asm_out_file):  Likewise.
	(print_to_stderr): Likewise.
	(print_single_switch): Likewise.
	(print_switch_values): Likewise.
	(init_asm_output): Use new gen_producer_string function.
	(process_options): Likewise.
	* varasm.c (elf_record_gcc_switches): Just save the string argument
	to the ELF container.
---
 gcc/doc/tm.texi |  38 +----------
 gcc/dwarf2out.c | 118 +++-----------------------------
 gcc/opts.c      | 118 ++++++++++++++++++++++++++++++++
 gcc/opts.h      |   6 ++
 gcc/target.def  |  38 +----------
 gcc/target.h    |  14 +---
 gcc/toplev.c    | 176 +++++-------------------------------------------
 gcc/varasm.c    |  48 +++----------
 8 files changed, 165 insertions(+), 391 deletions(-)

diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi
index f5077655716..d9b855c13ac 100644
--- a/gcc/doc/tm.texi
+++ b/gcc/doc/tm.texi
@@ -8090,43 +8090,9 @@ need to override this if your target has special flags that might be
 set via @code{__attribute__}.
 @end deftypefn
 
-@deftypefn {Target Hook} int TARGET_ASM_RECORD_GCC_SWITCHES (print_switch_type @var{type}, const char *@var{text})
+@deftypefn {Target Hook} void TARGET_ASM_RECORD_GCC_SWITCHES (const char *@var{})
 Provides the target with the ability to record the gcc command line
-switches that have been passed to the compiler, and options that are
-enabled.  The @var{type} argument specifies what is being recorded.
-It can take the following values:
-
-@table @gcctabopt
-@item SWITCH_TYPE_PASSED
-@var{text} is a command line switch that has been set by the user.
-
-@item SWITCH_TYPE_ENABLED
-@var{text} is an option which has been enabled.  This might be as a
-direct result of a command line switch, or because it is enabled by
-default or because it has been enabled as a side effect of a different
-command line switch.  For example, the @option{-O2} switch enables
-various different individual optimization passes.
-
-@item SWITCH_TYPE_DESCRIPTIVE
-@var{text} is either NULL or some descriptive text which should be
-ignored.  If @var{text} is NULL then it is being used to warn the
-target hook that either recording is starting or ending.  The first
-time @var{type} is SWITCH_TYPE_DESCRIPTIVE and @var{text} is NULL, the
-warning is for start up and the second time the warning is for
-wind down.  This feature is to allow the target hook to make any
-necessary preparations before it starts to record switches and to
-perform any necessary tidying up after it has finished recording
-switches.
-
-@item SWITCH_TYPE_LINE_START
-This option can be ignored by this target hook.
-
-@item  SWITCH_TYPE_LINE_END
-This option can be ignored by this target hook.
-@end table
-
-The hook's return value must be zero.  Other return values may be
-supported in the future.
+switches provided as argument.
 
 By default this hook is set to NULL, but an example implementation is
 provided for ELF based targets.  Called @var{elf_record_gcc_switches},
diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c
index c23a3ca3f6d..30e7ca68dbc 100644
--- a/gcc/dwarf2out.c
+++ b/gcc/dwarf2out.c
@@ -24438,115 +24438,6 @@ gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
 
 static char *producer_string;
 
-/* Return a heap allocated producer string including command line options
-   if -grecord-gcc-switches.  */
-
-static char *
-gen_producer_string (void)
-{
-  size_t j;
-  auto_vec<const char *> switches;
-  const char *language_string = lang_hooks.name;
-  char *producer, *tail;
-  const char *p;
-  size_t len = dwarf_record_gcc_switches ? 0 : 3;
-  size_t plen = strlen (language_string) + 1 + strlen (version_string);
-
-  for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
-    switch (save_decoded_options[j].opt_index)
-      {
-      case OPT_o:
-      case OPT_d:
-      case OPT_dumpbase:
-      case OPT_dumpbase_ext:
-      case OPT_dumpdir:
-      case OPT_quiet:
-      case OPT_version:
-      case OPT_v:
-      case OPT_w:
-      case OPT_L:
-      case OPT_D:
-      case OPT_I:
-      case OPT_U:
-      case OPT_SPECIAL_unknown:
-      case OPT_SPECIAL_ignore:
-      case OPT_SPECIAL_warn_removed:
-      case OPT_SPECIAL_program_name:
-      case OPT_SPECIAL_input_file:
-      case OPT_grecord_gcc_switches:
-      case OPT__output_pch_:
-      case OPT_fdiagnostics_show_location_:
-      case OPT_fdiagnostics_show_option:
-      case OPT_fdiagnostics_show_caret:
-      case OPT_fdiagnostics_show_labels:
-      case OPT_fdiagnostics_show_line_numbers:
-      case OPT_fdiagnostics_color_:
-      case OPT_fdiagnostics_format_:
-      case OPT_fverbose_asm:
-      case OPT____:
-      case OPT__sysroot_:
-      case OPT_nostdinc:
-      case OPT_nostdinc__:
-      case OPT_fpreprocessed:
-      case OPT_fltrans_output_list_:
-      case OPT_fresolution_:
-      case OPT_fdebug_prefix_map_:
-      case OPT_fmacro_prefix_map_:
-      case OPT_ffile_prefix_map_:
-      case OPT_fcompare_debug:
-      case OPT_fchecking:
-      case OPT_fchecking_:
-	/* Ignore these.  */
-	continue;
-      case OPT_flto_:
-	{
-	  const char *lto_canonical = "-flto";
-	  switches.safe_push (lto_canonical);
-	  len += strlen (lto_canonical) + 1;
-	  break;
-	}
-      default:
-        if (cl_options[save_decoded_options[j].opt_index].flags
-	    & CL_NO_DWARF_RECORD)
-	  continue;
-        gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
-			     == '-');
-        switch (save_decoded_options[j].canonical_option[0][1])
-	  {
-	  case 'M':
-	  case 'i':
-	  case 'W':
-	    continue;
-	  case 'f':
-	    if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
-			 "dump", 4) == 0)
-	      continue;
-	    break;
-	  default:
-	    break;
-	  }
-	switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
-	len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
-	break;
-      }
-
-  producer = XNEWVEC (char, plen + 1 + len + 1);
-  tail = producer;
-  sprintf (tail, "%s %s", language_string, version_string);
-  tail += plen;
-
-  FOR_EACH_VEC_ELT (switches, j, p)
-    {
-      len = strlen (p);
-      *tail = ' ';
-      memcpy (tail + 1, p, len);
-      tail += len + 1;
-    }
-
-  *tail = '\0';
-  return producer;
-}
-
 /* Given a C and/or C++ language/version string return the "highest".
    C++ is assumed to be "higher" than C in this case.  Used for merging
    LTO translation unit languages.  */
@@ -32157,7 +32048,14 @@ dwarf2out_early_finish (const char *filename)
      header compilation, so always fill it with empty string initially
      and overwrite only here.  */
   dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
-  producer_string = gen_producer_string ();
+
+  if (dwarf_record_gcc_switches)
+    producer_string = gen_producer_string (lang_hooks.name,
+					   save_decoded_options,
+					   save_decoded_options_count);
+  else
+    producer_string = concat (lang_hooks.name, " ", version_string, NULL);
+
   producer->dw_attr_val.v.val_str->refcount--;
   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
 
diff --git a/gcc/opts.c b/gcc/opts.c
index cc1d0cc04f6..efaa4348dde 100644
--- a/gcc/opts.c
+++ b/gcc/opts.c
@@ -32,6 +32,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "spellcheck.h"
 #include "opt-suggestions.h"
 #include "diagnostic-color.h"
+#include "version.h"
 #include "selftest.h"
 
 static void set_Wstrict_aliasing (struct gcc_options *opts, int onoff);
@@ -3266,6 +3267,123 @@ get_option_url (diagnostic_context *, int option_index)
     return NULL;
 }
 
+/* Return a heap allocated producer with command line options.  */
+
+char *gen_command_line_string (cl_decoded_option *options,
+			       unsigned int options_count)
+{
+  auto_vec<const char *> switches;
+  char *options_string, *tail;
+  const char *p;
+  size_t len = 0;
+
+  for (unsigned i = 0; i < options_count; i++)
+    switch (options[i].opt_index)
+      {
+      case OPT_o:
+      case OPT_d:
+      case OPT_dumpbase:
+      case OPT_dumpdir:
+      case OPT_quiet:
+      case OPT_version:
+      case OPT_v:
+      case OPT_w:
+      case OPT_L:
+      case OPT_D:
+      case OPT_I:
+      case OPT_U:
+      case OPT_SPECIAL_unknown:
+      case OPT_SPECIAL_ignore:
+      case OPT_SPECIAL_warn_removed:
+      case OPT_SPECIAL_program_name:
+      case OPT_SPECIAL_input_file:
+      case OPT_grecord_gcc_switches:
+      case OPT_frecord_gcc_switches:
+      case OPT__output_pch_:
+      case OPT_fdiagnostics_show_location_:
+      case OPT_fdiagnostics_show_option:
+      case OPT_fdiagnostics_show_caret:
+      case OPT_fdiagnostics_show_labels:
+      case OPT_fdiagnostics_show_line_numbers:
+      case OPT_fdiagnostics_color_:
+      case OPT_fdiagnostics_format_:
+      case OPT_fverbose_asm:
+      case OPT____:
+      case OPT__sysroot_:
+      case OPT_nostdinc:
+      case OPT_nostdinc__:
+      case OPT_fpreprocessed:
+      case OPT_fltrans_output_list_:
+      case OPT_fresolution_:
+      case OPT_fdebug_prefix_map_:
+      case OPT_fmacro_prefix_map_:
+      case OPT_ffile_prefix_map_:
+      case OPT_fcompare_debug:
+      case OPT_fchecking:
+      case OPT_fchecking_:
+	/* Ignore these.  */
+	continue;
+      case OPT_flto_:
+	{
+	  const char *lto_canonical = "-flto";
+	  switches.safe_push (lto_canonical);
+	  len += strlen (lto_canonical) + 1;
+	  break;
+	}
+      default:
+	if (cl_options[options[i].opt_index].flags
+	    & CL_NO_DWARF_RECORD)
+	  continue;
+	gcc_checking_assert (options[i].canonical_option[0][0] == '-');
+	switch (options[i].canonical_option[0][1])
+	  {
+	  case 'M':
+	  case 'i':
+	  case 'W':
+	    continue;
+	  case 'f':
+	    if (strncmp (options[i].canonical_option[0] + 2,
+			 "dump", 4) == 0)
+	      continue;
+	    break;
+	  default:
+	    break;
+	  }
+	switches.safe_push (options[i].orig_option_with_args_text);
+	len += strlen (options[i].orig_option_with_args_text) + 1;
+	break;
+      }
+
+  options_string = XNEWVEC (char, len + 1);
+  tail = options_string;
+
+  unsigned i;
+  FOR_EACH_VEC_ELT (switches, i, p)
+    {
+      len = strlen (p);
+      memcpy (tail, p, len);
+      tail += len;
+      if (i != switches.length () - 1)
+	{
+	  *tail = ' ';
+	  ++tail;
+	}
+    }
+
+  *tail = '\0';
+  return options_string;
+}
+
+/* Return a heap allocated producer string including command line options.  */
+
+char *
+gen_producer_string (const char *language_string, cl_decoded_option *options,
+		     unsigned int options_count)
+{
+  return concat (language_string, " ", version_string, " ",
+		 gen_command_line_string (options, options_count), NULL);
+}
+
 #if CHECKING_P
 
 namespace selftest {
diff --git a/gcc/opts.h b/gcc/opts.h
index d62bfcfdf6a..b36c0d18759 100644
--- a/gcc/opts.h
+++ b/gcc/opts.h
@@ -482,6 +482,12 @@ extern void parse_options_from_collect_gcc_options (const char *, obstack *,
 
 extern void prepend_xassembler_to_collect_as_options (const char *, obstack *);
 
+extern char *gen_command_line_string (cl_decoded_option *options,
+				      unsigned int options_count);
+extern char *gen_producer_string (const char *language_string,
+				  cl_decoded_option *options,
+				  unsigned int options_count);
+
 /* Set OPTION in OPTS to VALUE if the option is not set in OPTS_SET.  */
 
 #define SET_OPTION_IF_UNSET(OPTS, OPTS_SET, OPTION, VALUE) \
diff --git a/gcc/target.def b/gcc/target.def
index a0ea853b75f..acdc694d44c 100644
--- a/gcc/target.def
+++ b/gcc/target.def
@@ -757,41 +757,7 @@ directive to annotate @var{symbol} as used.  The Darwin target uses the\n\
 DEFHOOK
 (record_gcc_switches,
  "Provides the target with the ability to record the gcc command line\n\
-switches that have been passed to the compiler, and options that are\n\
-enabled.  The @var{type} argument specifies what is being recorded.\n\
-It can take the following values:\n\
-\n\
-@table @gcctabopt\n\
-@item SWITCH_TYPE_PASSED\n\
-@var{text} is a command line switch that has been set by the user.\n\
-\n\
-@item SWITCH_TYPE_ENABLED\n\
-@var{text} is an option which has been enabled.  This might be as a\n\
-direct result of a command line switch, or because it is enabled by\n\
-default or because it has been enabled as a side effect of a different\n\
-command line switch.  For example, the @option{-O2} switch enables\n\
-various different individual optimization passes.\n\
-\n\
-@item SWITCH_TYPE_DESCRIPTIVE\n\
-@var{text} is either NULL or some descriptive text which should be\n\
-ignored.  If @var{text} is NULL then it is being used to warn the\n\
-target hook that either recording is starting or ending.  The first\n\
-time @var{type} is SWITCH_TYPE_DESCRIPTIVE and @var{text} is NULL, the\n\
-warning is for start up and the second time the warning is for\n\
-wind down.  This feature is to allow the target hook to make any\n\
-necessary preparations before it starts to record switches and to\n\
-perform any necessary tidying up after it has finished recording\n\
-switches.\n\
-\n\
-@item SWITCH_TYPE_LINE_START\n\
-This option can be ignored by this target hook.\n\
-\n\
-@item  SWITCH_TYPE_LINE_END\n\
-This option can be ignored by this target hook.\n\
-@end table\n\
-\n\
-The hook's return value must be zero.  Other return values may be\n\
-supported in the future.\n\
+switches provided as argument.\n\
 \n\
 By default this hook is set to NULL, but an example implementation is\n\
 provided for ELF based targets.  Called @var{elf_record_gcc_switches},\n\
@@ -799,7 +765,7 @@ it records the switches as ASCII text inside a new, string mergeable\n\
 section in the assembler output file.  The name of the new section is\n\
 provided by the @code{TARGET_ASM_RECORD_GCC_SWITCHES_SECTION} target\n\
 hook.",
- int, (print_switch_type type, const char *text),
+ void, (const char *),
  NULL)
 
 /* The name of the section that the example ELF implementation of
diff --git a/gcc/target.h b/gcc/target.h
index 440cd25f297..960188023f1 100644
--- a/gcc/target.h
+++ b/gcc/target.h
@@ -68,16 +68,6 @@ union cumulative_args_t { void *p; };
 
 #endif /* !CHECKING_P */
 
-/* Types used by the record_gcc_switches() target function.  */
-enum print_switch_type
-{
-  SWITCH_TYPE_PASSED,		/* A switch passed on the command line.  */
-  SWITCH_TYPE_ENABLED,		/* An option that is currently enabled.  */
-  SWITCH_TYPE_DESCRIPTIVE,	/* Descriptive text, not a switch or option.  */
-  SWITCH_TYPE_LINE_START,	/* Please emit any necessary text at the start of a line.  */
-  SWITCH_TYPE_LINE_END		/* Please emit a line terminator.  */
-};
-
 /* Types of memory operation understood by the "by_pieces" infrastructure.
    Used by the TARGET_USE_BY_PIECES_INFRASTRUCTURE_P target hook and
    internally by the functions in expr.c.  */
@@ -96,10 +86,8 @@ extern unsigned HOST_WIDE_INT by_pieces_ninsns (unsigned HOST_WIDE_INT,
 						unsigned int,
 						by_pieces_operation);
 
-typedef int (* print_switch_fn_type) (print_switch_type, const char *);
-
 /* An example implementation for ELF targets.  Defined in varasm.c  */
-extern int elf_record_gcc_switches (print_switch_type type, const char *);
+extern void elf_record_gcc_switches (const char *);
 
 /* Some places still assume that all pointer or address modes are the
    standard Pmode and ptr_mode.  These optimizations become invalid if
diff --git a/gcc/toplev.c b/gcc/toplev.c
index cb4ae77fccb..93a41943b8c 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -116,9 +116,6 @@ static void compile_file (void);
 /* True if we don't need a backend (e.g. preprocessing only).  */
 static bool no_backend;
 
-/* Length of line when printing switch values.  */
-#define MAX_LINE 75
-
 /* Decoded options, and number of such options.  */
 struct cl_decoded_option *save_decoded_options;
 unsigned int save_decoded_options_count;
@@ -687,148 +684,7 @@ print_version (FILE *file, const char *indent, bool show_global_state)
     }
 }
 
-static int
-print_to_asm_out_file (print_switch_type type, const char * text)
-{
-  bool prepend_sep = true;
-
-  switch (type)
-    {
-    case SWITCH_TYPE_LINE_END:
-      putc ('\n', asm_out_file);
-      return 1;
-
-    case SWITCH_TYPE_LINE_START:
-      fputs (ASM_COMMENT_START, asm_out_file);
-      return strlen (ASM_COMMENT_START);
-
-    case SWITCH_TYPE_DESCRIPTIVE:
-      if (ASM_COMMENT_START[0] == 0)
-	prepend_sep = false;
-      /* FALLTHRU */
-    case SWITCH_TYPE_PASSED:
-    case SWITCH_TYPE_ENABLED:
-      if (prepend_sep)
-	fputc (' ', asm_out_file);
-      fputs (text, asm_out_file);
-      /* No need to return the length here as
-	 print_single_switch has already done it.  */
-      return 0;
-
-    default:
-      return -1;
-    }
-}
-
-static int
-print_to_stderr (print_switch_type type, const char * text)
-{
-  switch (type)
-    {
-    case SWITCH_TYPE_LINE_END:
-      putc ('\n', stderr);
-      return 1;
-
-    case SWITCH_TYPE_LINE_START:
-      return 0;
-
-    case SWITCH_TYPE_PASSED:
-    case SWITCH_TYPE_ENABLED:
-      fputc (' ', stderr);
-      /* FALLTHRU */
-
-    case SWITCH_TYPE_DESCRIPTIVE:
-      fputs (text, stderr);
-      /* No need to return the length here as
-	 print_single_switch has already done it.  */
-      return 0;
-
-    default:
-      return -1;
-    }
-}
-
-/* Print an option value and return the adjusted position in the line.
-   ??? print_fn doesn't handle errors, eg disk full; presumably other
-   code will catch a disk full though.  */
-
-static int
-print_single_switch (print_switch_fn_type print_fn,
-		     int pos,
-		     print_switch_type type,
-		     const char * text)
-{
-  /* The ultrix fprintf returns 0 on success, so compute the result
-     we want here since we need it for the following test.  The +1
-     is for the separator character that will probably be emitted.  */
-  int len = strlen (text) + 1;
-
-  if (pos != 0
-      && pos + len > MAX_LINE)
-    {
-      print_fn (SWITCH_TYPE_LINE_END, NULL);
-      pos = 0;
-    }
-
-  if (pos == 0)
-    pos += print_fn (SWITCH_TYPE_LINE_START, NULL);
-
-  print_fn (type, text);
-  return pos + len;
-}
-
-/* Print active target switches using PRINT_FN.
-   POS is the current cursor position and MAX is the size of a "line".
-   Each line begins with INDENT and ends with TERM.
-   Each switch is separated from the next by SEP.  */
 
-static void
-print_switch_values (print_switch_fn_type print_fn)
-{
-  int pos = 0;
-  size_t j;
-
-  /* Print the options as passed.  */
-  pos = print_single_switch (print_fn, pos,
-			     SWITCH_TYPE_DESCRIPTIVE, _("options passed: "));
-
-  for (j = 1; j < save_decoded_options_count; j++)
-    {
-      switch (save_decoded_options[j].opt_index)
-	{
-	case OPT_o:
-	case OPT_d:
-	case OPT_dumpbase:
-	case OPT_dumpbase_ext:
-	case OPT_dumpdir:
-	case OPT_quiet:
-	case OPT_version:
-	  /* Ignore these.  */
-	  continue;
-	}
-
-      pos = print_single_switch (print_fn, pos, SWITCH_TYPE_PASSED,
-				 save_decoded_options[j].orig_option_with_args_text);
-    }
-
-  if (pos > 0)
-    print_fn (SWITCH_TYPE_LINE_END, NULL);
-
-  /* Print the -f and -m options that have been enabled.
-     We don't handle language specific options but printing argv
-     should suffice.  */
-  pos = print_single_switch (print_fn, 0,
-			     SWITCH_TYPE_DESCRIPTIVE, _("options enabled: "));
-
-  unsigned lang_mask = lang_hooks.option_lang_mask ();
-  for (j = 0; j < cl_options_count; j++)
-    if (cl_options[j].cl_report
-	&& option_enabled (j, lang_mask, &global_options) > 0)
-      pos = print_single_switch (print_fn, pos,
-				 SWITCH_TYPE_ENABLED, cl_options[j].opt_text);
-
-  print_fn (SWITCH_TYPE_LINE_END, NULL);
-}
 
 /* Open assembly code output file.  Do this even if -fsyntax-only is
    on, because then the driver will have provided the name of a
@@ -875,14 +731,11 @@ init_asm_output (const char *name)
 	{
 	  if (targetm.asm_out.record_gcc_switches)
 	    {
-	      /* Let the target know that we are about to start recording.  */
-	      targetm.asm_out.record_gcc_switches (SWITCH_TYPE_DESCRIPTIVE,
-						   NULL);
-	      /* Now record the switches.  */
-	      print_switch_values (targetm.asm_out.record_gcc_switches);
-	      /* Let the target know that the recording is over.  */
-	      targetm.asm_out.record_gcc_switches (SWITCH_TYPE_DESCRIPTIVE,
-						   NULL);
+	      const char *str
+		= gen_producer_string (lang_hooks.name,
+				       save_decoded_options,
+				       save_decoded_options_count);
+	      targetm.asm_out.record_gcc_switches (str);
 	    }
 	  else
 	    inform (UNKNOWN_LOCATION,
@@ -892,11 +745,13 @@ init_asm_output (const char *name)
 
       if (flag_verbose_asm)
 	{
-	  /* Print the list of switches in effect
-	     into the assembler file as comments.  */
 	  print_version (asm_out_file, ASM_COMMENT_START, true);
-	  print_switch_values (print_to_asm_out_file);
-	  putc ('\n', asm_out_file);
+	  fputs (ASM_COMMENT_START, asm_out_file);
+	  fputs (" options passed: ", asm_out_file);
+	  fputs (gen_command_line_string (save_decoded_options,
+					  save_decoded_options_count),
+		 asm_out_file);
+	  fputc ('\n', asm_out_file);
 	}
     }
 }
@@ -1526,8 +1381,13 @@ process_options (void)
   if (version_flag)
     {
       print_version (stderr, "", true);
-      if (! quiet_flag)
-	print_switch_values (print_to_stderr);
+      if (!quiet_flag)
+	{
+	  fputs ("options passed: ", stderr);
+	  fputs (gen_command_line_string (save_decoded_options,
+					  save_decoded_options_count), stderr);
+	  fputc ('\n', stderr);
+	}
     }
 
   if (flag_syntax_only)
diff --git a/gcc/varasm.c b/gcc/varasm.c
index 961d2d6fe3b..0fac3688828 100644
--- a/gcc/varasm.c
+++ b/gcc/varasm.c
@@ -43,6 +43,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "fold-const.h"
 #include "stor-layout.h"
 #include "varasm.h"
+#include "version.h"
 #include "flags.h"
 #include "stmt.h"
 #include "expr.h"
@@ -58,6 +59,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "rtl-iter.h"
 #include "file-prefix-map.h" /* remap_debug_filename()  */
 #include "alloc-pool.h"
+#include "toplev.h"
+#include "opts.h"
 
 #ifdef XCOFF_DEBUGGING_INFO
 #include "xcoffout.h"		/* Needed for external data declarations.  */
@@ -8053,45 +8056,14 @@ output_object_blocks (void)
    we want to emit NUL strings terminators into the object file we have to use
    ASM_OUTPUT_SKIP.  */
 
-int
-elf_record_gcc_switches (print_switch_type type, const char * name)
+void
+elf_record_gcc_switches (const char *options)
 {
-  switch (type)
-    {
-    case SWITCH_TYPE_PASSED:
-      ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
-      ASM_OUTPUT_SKIP (asm_out_file, HOST_WIDE_INT_1U);
-      break;
-
-    case SWITCH_TYPE_DESCRIPTIVE:
-      if (name == NULL)
-	{
-	  /* Distinguish between invocations where name is NULL.  */
-	  static bool started = false;
-
-	  if (!started)
-	    {
-	      section * sec;
-
-	      sec = get_section (targetm.asm_out.record_gcc_switches_section,
-				 SECTION_DEBUG
-				 | SECTION_MERGE
-				 | SECTION_STRINGS
-				 | (SECTION_ENTSIZE & 1),
-				 NULL);
-	      switch_to_section (sec);
-	      started = true;
-	    }
-	}
-
-    default:
-      break;
-    }
-
-  /* The return value is currently ignored by the caller, but must be 0.
-     For -fverbose-asm the return value would be the number of characters
-     emitted into the assembler file.  */
-  return 0;
+  section *sec = get_section (targetm.asm_out.record_gcc_switches_section,
+			      SECTION_DEBUG | SECTION_MERGE
+			      | SECTION_STRINGS | (SECTION_ENTSIZE & 1), NULL);
+  switch_to_section (sec);
+  ASM_OUTPUT_ASCII (asm_out_file, options, strlen (options) + 1);
 }
 
 /* Emit text to declare externally defined symbols. It is needed to
-- 
2.29.2

>From 397e1a28c3e5c0aa9f44a8e19115cc7c50d8b388 Mon Sep 17 00:00:00 2001
From: Martin Liska <mli...@suse.cz>
Date: Fri, 4 Dec 2020 08:52:33 +0100
Subject: [PATCH 2/2] Change semantics of -frecord-gcc-switches and add
 -frecord-gcc-switches-format.

gcc/ChangeLog:

	* common.opt: Document new options -frecord-gcc-switches-format
	and -frecord-gcc-switches-file.
	* doc/invoke.texi: Document the new options.
	* dwarf2out.c (dwarf2out_early_finish): Respect
	flag_record_gcc_switches_format.
	* flag-types.h (enum record_gcc_switches_format): New.
	* gcc.c (save_driver_cmdline): Save command line to a temporary
	file.
	(driver::main): Call set_commandline.
	(driver::set_commandline): New.
	* gcc.h (set_commandline): New.
	* opts.c (get_driver_command_line): New.
	* opts.h (get_driver_command_line): New.
	* toplev.c (init_asm_output): Use new function get_producer_string.
	(process_options): Respect flag_record_gcc_switches_format
	option.

Co-Authored-By: Egeyar Bagcioglu  <egeyar.bagcio...@oracle.com>
---
 gcc/common.opt      | 19 ++++++++++++++++++-
 gcc/doc/invoke.texi | 23 ++++++++++++++++++++++-
 gcc/dwarf2out.c     | 17 ++++++++++++-----
 gcc/flag-types.h    |  7 +++++++
 gcc/gcc.c           | 37 ++++++++++++++++++++++++++++++++++++-
 gcc/gcc.h           |  1 +
 gcc/opts.c          | 31 +++++++++++++++++++++++++++++++
 gcc/opts.h          |  2 ++
 gcc/toplev.c        | 27 ++++++++++++++++++++-------
 9 files changed, 149 insertions(+), 15 deletions(-)

diff --git a/gcc/common.opt b/gcc/common.opt
index 582e2aa8774..eb112fcdf90 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -2322,9 +2322,26 @@ Common Joined RejectNegative Var(common_deferred_options) Defer
 ; records information in the assembler output file as comments, so
 ; they never reach the object file.
 frecord-gcc-switches
-Common Report Var(flag_record_gcc_switches)
+Common Driver Report Var(flag_record_gcc_switches)
 Record gcc command line switches in the object file.
 
+Enum
+Name(record_gcc_switches_format) Type(enum record_gcc_switches_format)
+
+EnumValue
+Enum(record_gcc_switches_format) String(processed) Value(RECORD_GCC_SWITCHES_PROCESSED)
+
+EnumValue
+Enum(record_gcc_switches_format) String(driver) Value(RECORD_GCC_SWITCHES_DRIVER)
+
+frecord-gcc-switches-format=
+Common Report Joined RejectNegative Var(flag_record_gcc_switches_format) Enum(record_gcc_switches_format) Init(RECORD_GCC_SWITCHES_PROCESSED)
+-frecord-gcc-switches-format=[processed|driver]	Format of recorded gcc command line switches.
+
+frecord-gcc-switches-file=
+Common Report Joined RejectNegative Var(flag_record_gcc_switches_file) Undocumented
+Path to file that contains driver command line options.
+
 freg-struct-return
 Common Report Var(flag_pcc_struct_return,0) Optimization
 Return small aggregates in registers.
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 615eae9a1c5..76f938da5da 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -643,7 +643,7 @@ Objective-C and Objective-C++ Dialects}.
 -finhibit-size-directive  -fcommon  -fno-ident @gol
 -fpcc-struct-return  -fpic  -fPIC  -fpie  -fPIE  -fno-plt @gol
 -fno-jump-tables -fno-bit-tests @gol
--frecord-gcc-switches @gol
+-frecord-gcc-switches -frecord-gcc-switches-format @gol
 -freg-struct-return  -fshort-enums  -fshort-wchar @gol
 -fverbose-asm  -fpack-struct[=@var{n}]  @gol
 -fleading-underscore  -ftls-model=@var{model} @gol
@@ -16206,6 +16206,27 @@ comments, so it never reaches the object file.
 See also @option{-grecord-gcc-switches} for another
 way of storing compiler options into the object file.
 
+@item -frecord-gcc-switches-format=@var{format}
+@opindex frecord-gcc-switches-format
+This switch controls the output format of options that record
+the command line.  The affected options are @option{-frecord-gcc-switches},
+@option{-grecord-gcc-switches} and @option{-fverbose-asm}.
+
+The @var{format} argument should be one of the following:
+
+@table @samp
+
+@item processed
+
+Options are printed after processing by the compiler driver.
+It is the default option value.
+
+@item driver
+
+Options are printed as provided to the compiler driver.
+
+@end table
+
 @item -fpic
 @opindex fpic
 @cindex global offset table
diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c
index 30e7ca68dbc..184f3363008 100644
--- a/gcc/dwarf2out.c
+++ b/gcc/dwarf2out.c
@@ -32048,13 +32048,20 @@ dwarf2out_early_finish (const char *filename)
      header compilation, so always fill it with empty string initially
      and overwrite only here.  */
   dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
+  producer_string = concat (lang_hooks.name, " ", version_string, NULL);
 
   if (dwarf_record_gcc_switches)
-    producer_string = gen_producer_string (lang_hooks.name,
-					   save_decoded_options,
-					   save_decoded_options_count);
-  else
-    producer_string = concat (lang_hooks.name, " ", version_string, NULL);
+    {
+      char *producer_string_old = producer_string;
+      if (flag_record_gcc_switches_format == RECORD_GCC_SWITCHES_DRIVER)
+	producer_string = concat (producer_string, " ",
+				  get_driver_command_line (), NULL);
+      else
+	producer_string = gen_producer_string (lang_hooks.name,
+					       save_decoded_options,
+					       save_decoded_options_count);
+      free (producer_string_old);
+    }
 
   producer->dw_attr_val.v.val_str->refcount--;
   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
diff --git a/gcc/flag-types.h b/gcc/flag-types.h
index 9342bd87be3..4ded267f197 100644
--- a/gcc/flag-types.h
+++ b/gcc/flag-types.h
@@ -427,6 +427,13 @@ enum openacc_kernels
   OPENACC_KERNELS_PARLOOPS
 };
 
+/* Record GCC options type.  */
+enum record_gcc_switches_format
+{
+  RECORD_GCC_SWITCHES_PROCESSED = 0,
+  RECORD_GCC_SWITCHES_DRIVER
+};
+
 #endif
 
 #endif /* ! GCC_FLAG_TYPES_H */
diff --git a/gcc/gcc.c b/gcc/gcc.c
index 1d32375f648..7ef888c8b18 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -235,6 +235,12 @@ static int verbose_only_flag;
 
 static int print_subprocess_help;
 
+/* argc and argv provided to the driver.  Necessary to keep for when
+   -frecord-gcc-switches-format=driver is given.  */
+
+static unsigned int driver_gcc_argc;
+static const char **driver_gcc_argv;
+
 /* Linker suffix passed to -fuse-ld=... */
 static const char *use_ld;
 
@@ -433,6 +439,7 @@ static const char *greater_than_spec_func (int, const char **);
 static const char *debug_level_greater_than_spec_func (int, const char **);
 static const char *dwarf_version_greater_than_spec_func (int, const char **);
 static const char *find_fortran_preinclude_file (int, const char **);
+static const char *save_driver_cmdline (int, const char **);
 static char *convert_white_space (char *);
 static char *quote_spec (char *);
 static char *quote_spec_arg (char *);
@@ -1276,7 +1283,9 @@ static const char *cc1_options =
  %{coverage:-fprofile-arcs -ftest-coverage}\
  %{fprofile-arcs|fprofile-generate*|coverage:\
    %{!fprofile-update=single:\
-     %{pthread:-fprofile-update=prefer-atomic}}}";
+     %{pthread:-fprofile-update=prefer-atomic}}}\
+ %{frecord-gcc-switches-format=driver:-frecord-gcc-switches-file=%:save-driver-cmdline(%g.cmdline)\
+   %<-frecord-gcc-switches-format=driver}";
 
 static const char *asm_options =
 "%{-target-help:%:print-asm-header()} "
@@ -1768,6 +1777,7 @@ static const struct spec_function static_spec_functions[] =
   { "debug-level-gt",		debug_level_greater_than_spec_func },
   { "dwarf-version-gt",		dwarf_version_greater_than_spec_func },
   { "fortran-preinclude-file",	find_fortran_preinclude_file},
+  { "save-driver-cmdline",	save_driver_cmdline},
 #ifdef EXTRA_SPEC_FUNCTIONS
   EXTRA_SPEC_FUNCTIONS
 #endif
@@ -8012,6 +8022,7 @@ driver::main (int argc, char **argv)
 
   set_progname (argv[0]);
   expand_at_files (&argc, &argv);
+  set_commandline (argc, const_cast <const char **> (argv));
   decode_argv (argc, const_cast <const char **> (argv));
   global_initializations ();
   build_multilib_strings ();
@@ -8055,6 +8066,15 @@ driver::set_progname (const char *argv0) const
   xmalloc_set_program_name (progname);
 }
 
+/* Keep the command line for -frecord-gcc-switches-format=driver.  */
+
+void
+driver::set_commandline (int argc, const char **argv) const
+{
+  driver_gcc_argc = argc;
+  driver_gcc_argv = argv;
+}
+
 /* Expand any @ files within the command-line args,
    setting at_file_supplied if any were expanded.  */
 
@@ -10807,6 +10827,21 @@ find_fortran_preinclude_file (int argc, const char **argv)
   return result;
 }
 
+/* Save driver options to a temporary file.  */
+
+static const char *
+save_driver_cmdline (int argc ATTRIBUTE_UNUSED,
+		     const char **argv)
+{
+  FILE *f = fopen (argv[0], "w");
+  for (unsigned int i = 0; i < driver_gcc_argc; i++)
+    fprintf (f, i == 0 ? "%s" : " %s", driver_gcc_argv[i]);
+  fclose (f);
+
+  return argv[0];
+}
+
+
 /* If any character in ORIG fits QUOTE_P (_, P), reallocate the string
    so as to precede every one of them with a backslash.  Return the
    original string or the reallocated one.  */
diff --git a/gcc/gcc.h b/gcc/gcc.h
index 70d8f08f059..cde77fbaad7 100644
--- a/gcc/gcc.h
+++ b/gcc/gcc.h
@@ -37,6 +37,7 @@ class driver
 
  private:
   void set_progname (const char *argv0) const;
+  void set_commandline (int argc, const char **argv) const;
   void expand_at_files (int *argc, char ***argv) const;
   void decode_argv (int argc, const char **argv);
   void global_initializations ();
diff --git a/gcc/opts.c b/gcc/opts.c
index efaa4348dde..a760f8d6d21 100644
--- a/gcc/opts.c
+++ b/gcc/opts.c
@@ -3299,6 +3299,7 @@ char *gen_command_line_string (cl_decoded_option *options,
       case OPT_SPECIAL_input_file:
       case OPT_grecord_gcc_switches:
       case OPT_frecord_gcc_switches:
+      case OPT_frecord_gcc_switches_format_:
       case OPT__output_pch_:
       case OPT_fdiagnostics_show_location_:
       case OPT_fdiagnostics_show_option:
@@ -3384,6 +3385,36 @@ gen_producer_string (const char *language_string, cl_decoded_option *options,
 		 gen_command_line_string (options, options_count), NULL);
 }
 
+/* Return value of env variable GCC_DRIVER_COMMAND_LINE if exists.
+   Otherwise return empty string.  */
+
+const char *
+get_driver_command_line ()
+{
+  static char *cmdline = NULL;
+
+  if (cmdline != NULL)
+    return cmdline;
+
+  FILE *f = fopen (flag_record_gcc_switches_file, "r");
+  if (f == NULL)
+    fatal_error (UNKNOWN_LOCATION, "cannot open %s: %m", flag_record_gcc_switches_file);
+
+  fseek(f, 0, SEEK_END);
+  unsigned int size = ftell (f);
+  fseek(f, 0, SEEK_SET);
+
+  cmdline = XNEWVEC (char, size + 1);
+
+  unsigned int read;
+  char *ptr = cmdline;
+  while ((read = fread (ptr, 4096, 1, f)))
+    ptr += read;
+
+  cmdline[size] = '\0';
+  return cmdline;
+}
+
 #if CHECKING_P
 
 namespace selftest {
diff --git a/gcc/opts.h b/gcc/opts.h
index b36c0d18759..a3f1823071b 100644
--- a/gcc/opts.h
+++ b/gcc/opts.h
@@ -488,6 +488,8 @@ extern char *gen_producer_string (const char *language_string,
 				  cl_decoded_option *options,
 				  unsigned int options_count);
 
+extern const char *get_driver_command_line ();
+
 /* Set OPTION in OPTS to VALUE if the option is not set in OPTS_SET.  */
 
 #define SET_OPTION_IF_UNSET(OPTS, OPTS_SET, OPTION, VALUE) \
diff --git a/gcc/toplev.c b/gcc/toplev.c
index 93a41943b8c..6891addba75 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -731,11 +731,21 @@ init_asm_output (const char *name)
 	{
 	  if (targetm.asm_out.record_gcc_switches)
 	    {
-	      const char *str
-		= gen_producer_string (lang_hooks.name,
-				       save_decoded_options,
-				       save_decoded_options_count);
-	      targetm.asm_out.record_gcc_switches (str);
+	      if (flag_record_gcc_switches_format == RECORD_GCC_SWITCHES_DRIVER)
+		{
+		  const char *str = concat (version_string, " ",
+					    get_driver_command_line (), NULL);
+		  targetm.asm_out.record_gcc_switches (str);
+		  free (CONST_CAST (char *, str));
+		}
+	      else
+		{
+		  const char *str
+		    = gen_producer_string (lang_hooks.name,
+					   save_decoded_options,
+					   save_decoded_options_count);
+		  targetm.asm_out.record_gcc_switches (str);
+		}
 	    }
 	  else
 	    inform (UNKNOWN_LOCATION,
@@ -1384,8 +1394,11 @@ process_options (void)
       if (!quiet_flag)
 	{
 	  fputs ("options passed: ", stderr);
-	  fputs (gen_command_line_string (save_decoded_options,
-					  save_decoded_options_count), stderr);
+	  if (flag_record_gcc_switches_format == RECORD_GCC_SWITCHES_DRIVER)
+	    fputs (get_driver_command_line (), stderr);
+	  else
+	    fputs (gen_command_line_string (save_decoded_options,
+					    save_decoded_options_count), stderr);
 	  fputc ('\n', stderr);
 	}
     }
-- 
2.29.2

Reply via email to