]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
diagnostics: introduce struct column_options
authorDavid Malcolm <dmalcolm@redhat.com>
Fri, 8 Aug 2025 20:55:43 +0000 (16:55 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Fri, 8 Aug 2025 20:55:43 +0000 (16:55 -0400)
Consolidate 3 fields in diagnostics::context and
diagnostics::column_policy into a new struct
diagnostics::column_options.

No functional change intended; reduces the number of public
fields in diagnostics::context.

gcc/c-family/ChangeLog:
* c-indentation.cc (should_warn_for_misleading_indentation):
Update for moving diagnostics::context::m_tabstop into
diagnostics::column_options.
* c-opts.cc (c_common_post_options): Likewise.

gcc/ChangeLog:
* diagnostics/column-options.h: New file, adding struct
diagnostics::column_options, taken from fields in
diagnostics::context and diagnostics::column_policy.
* diagnostics/context.cc (context::initialize): Update for moving
fields of diagnostics::context into diagnostics::column_options.
(column_policy::column_policy): Likewise.
(column_policy::converted_column): Move implementation to...
(column_options::convert_column): ...this new function.
(context::report_diagnostic): Update for moving fields of
diagnostics::context into diagnostics::column_options.
(assert_location_text): Likewise.
* diagnostics/context.h: Include "diagnostics/column-options.h".
(class column_policy): Replace fields m_column_unit,
m_column_origin, and m_tabstop with m_column_options.
(context::get_column_options): New accessors.
(context::m_column_unit): Move to struct column_options and
replace with m_column_options.
(context::m_column_origin): Likewise.
(context::m_tabstop): Likewise.
* diagnostics/sarif-sink.cc (sarif_builder::sarif_builder): Update
for moving fields of diagnostics::context into
diagnostics::column_options.
* diagnostics/source-printing.cc: Likewise.
* opts.cc (common_handle_option): Likewise.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/c-family/c-indentation.cc
gcc/c-family/c-opts.cc
gcc/diagnostics/column-options.h [new file with mode: 0644]
gcc/diagnostics/context.cc
gcc/diagnostics/context.h
gcc/diagnostics/sarif-sink.cc
gcc/diagnostics/source-printing.cc
gcc/opts.cc

index bb214fc259b7b60308fb389cae2c1a36e007c950..d378464be1504c6e1c9197d9b721cbd5a703faa9 100644 (file)
@@ -330,7 +330,7 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo,
   if (guard_loc == body_loc || body_loc == next_stmt_loc)
     return false;
 
-  const unsigned int tab_width = global_dc->m_tabstop;
+  const unsigned int tab_width = global_dc->get_column_options ().m_tabstop;
 
   /* They must be in the same file.  */
   if (next_stmt_exploc.file != body_exploc.file)
index 3fb12b984f235727cec78097920ef70c4b383152..0ec30e852158b0d0f40066c2741f9543dc2364d3 100644 (file)
@@ -1200,7 +1200,7 @@ c_common_post_options (const char **pfilename)
     flag_char8_t = (cxx_dialect >= cxx20) || flag_isoc23;
   cpp_opts->unsigned_utf8char = flag_char8_t ? 1 : cpp_opts->unsigned_char;
 
-  cpp_opts->cpp_tabstop = global_dc->m_tabstop;
+  cpp_opts->cpp_tabstop = global_dc->get_column_options ().m_tabstop;
 
   if (flag_extern_tls_init)
     {
diff --git a/gcc/diagnostics/column-options.h b/gcc/diagnostics/column-options.h
new file mode 100644 (file)
index 0000000..86296e9
--- /dev/null
@@ -0,0 +1,44 @@
+/* Options relating to the meaning of column numbers.
+   Copyright (C) 2000-2025 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#ifndef GCC_DIAGNOSTICS_COLUMN_OPTIONS_H
+#define GCC_DIAGNOSTICS_COLUMN_OPTIONS_H
+
+namespace diagnostics {
+
+/* A bundle of options relating to the meaning of column numbers.  */
+
+struct column_options
+{
+  int convert_column (file_cache &fc,
+                     expanded_location s) const;
+
+  /* What units to use when outputting the column number.  */
+  enum diagnostics_column_unit m_column_unit;
+
+  /* The origin for the column number (1-based or 0-based typically).  */
+  int m_column_origin;
+
+  /* The size of the tabstop for tab expansion.  */
+  int m_tabstop;
+};
+
+} // namespace diagnostics
+
+#endif /* ! GCC_DIAGNOSTICS_COLUMN_OPTIONS_H */
index a1441ca5e73ac641beaae94fcca81b976ed31ee2..b05e078794284e36fbbc846190fbfa07de062bef 100644 (file)
@@ -176,12 +176,18 @@ context::initialize (int n_opts)
   m_client_aux_data = nullptr;
   m_lock = 0;
   m_inhibit_notes_p = false;
+
   m_source_printing.colorize_source_p = false;
   m_source_printing.show_labels_p = false;
   m_source_printing.show_line_numbers_p = false;
   m_source_printing.min_margin_width = 0;
   m_source_printing.show_ruler_p = false;
   m_source_printing.show_event_links_p = false;
+
+  m_column_options.m_column_unit = DIAGNOSTICS_COLUMN_UNIT_DISPLAY;
+  m_column_options.m_column_origin = 1;
+  m_column_options.m_tabstop = 8;
+
   m_report_bug = false;
   m_extra_output_kind = EXTRA_DIAGNOSTIC_OUTPUT_none;
   if (const char *var = getenv ("GCC_EXTRA_DIAGNOSTIC_OUTPUT"))
@@ -192,9 +198,6 @@ context::initialize (int n_opts)
        m_extra_output_kind = EXTRA_DIAGNOSTIC_OUTPUT_fixits_v2;
       /* Silently ignore unrecognized values.  */
     }
-  m_column_unit = DIAGNOSTICS_COLUMN_UNIT_DISPLAY;
-  m_column_origin = 1;
-  m_tabstop = 8;
   m_escape_format = DIAGNOSTICS_ESCAPE_FORMAT_UNICODE;
   m_fixits_change_set = nullptr;
   m_diagnostic_groups.m_group_nesting_depth = 0;
@@ -671,11 +674,22 @@ convert_column_unit (file_cache &fc,
     }
 }
 
+/* Given an expanded_location, convert the column (which is in 1-based bytes)
+   to the requested units and origin.  Return -1 if the column is
+   invalid (<= 0).  */
+int
+column_options::convert_column (file_cache &fc,
+                               expanded_location s) const
+{
+  int one_based_col = convert_column_unit (fc, m_column_unit, m_tabstop, s);
+  if (one_based_col <= 0)
+    return -1;
+  return one_based_col + (m_column_origin - 1);
+}
+
 column_policy::column_policy (const context &dc)
 : m_file_cache (dc.get_file_cache ()),
-  m_column_unit (dc.m_column_unit),
-  m_column_origin (dc.m_column_origin),
-  m_tabstop (dc.m_tabstop)
+  m_column_options (dc.get_column_options ())
 {
 }
 
@@ -685,11 +699,7 @@ column_policy::column_policy (const context &dc)
 int
 column_policy::converted_column (expanded_location s) const
 {
-  int one_based_col = convert_column_unit (m_file_cache,
-                                          m_column_unit, m_tabstop, s);
-  if (one_based_col <= 0)
-    return -1;
-  return one_based_col + (m_column_origin - 1);
+  return m_column_options.convert_column (m_file_cache, s);
 }
 
 /* Return a string describing a location e.g. "foo.c:42:10".  */
@@ -1385,6 +1395,8 @@ context::report_diagnostic (diagnostic_info *diagnostic)
       sink_->on_report_diagnostic (*diagnostic, orig_diag_kind);
     }
 
+  const int tabstop = get_column_options ().m_tabstop;
+
   switch (m_extra_output_kind)
     {
     default:
@@ -1393,14 +1405,14 @@ context::report_diagnostic (diagnostic_info *diagnostic)
       print_parseable_fixits (get_file_cache (),
                              m_reference_printer, diagnostic->m_richloc,
                              DIAGNOSTICS_COLUMN_UNIT_BYTE,
-                             m_tabstop);
+                             tabstop);
       pp_flush (m_reference_printer);
       break;
     case EXTRA_DIAGNOSTIC_OUTPUT_fixits_v2:
       print_parseable_fixits (get_file_cache (),
                              m_reference_printer, diagnostic->m_richloc,
                              DIAGNOSTICS_COLUMN_UNIT_DISPLAY,
-                             m_tabstop);
+                             tabstop);
       pp_flush (m_reference_printer);
       break;
     }
@@ -2015,8 +2027,8 @@ assert_location_text (const char *expected_loc_text,
                        = DIAGNOSTICS_COLUMN_UNIT_BYTE)
 {
   diagnostics::selftest::test_context dc;
-  dc.m_column_unit = column_unit;
-  dc.m_column_origin = origin;
+  dc.get_column_options ().m_column_unit = column_unit;
+  dc.get_column_options ().m_column_origin = origin;
 
   expanded_location xloc;
   xloc.file = filename;
index b6ec85cc26ee4c5677b0ee43bbf5e1a8072c6b98..961a87299e8ab9fe73964ea09dcdfede63b31846 100644 (file)
@@ -26,6 +26,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "diagnostics/option-id-manager.h"
 #include "diagnostics/context-options.h"
 #include "diagnostics/source-printing-options.h"
+#include "diagnostics/column-options.h"
 #include "diagnostics/counters.h"
 
 namespace diagnostics {
@@ -99,13 +100,11 @@ public:
                                bool show_column,
                                bool colorize) const;
 
-  int get_tabstop () const { return m_tabstop; }
+  int get_tabstop () const { return m_column_options.m_tabstop; }
 
 private:
   file_cache &m_file_cache;
-  enum diagnostics_column_unit m_column_unit;
-  int m_column_origin;
-  int m_tabstop;
+  column_options m_column_options;
 };
 
 /* A bundle of state for printing locations within diagnostics
@@ -592,6 +591,9 @@ public:
     return m_source_printing;
   }
 
+  column_options &get_column_options () { return m_column_options; }
+  const column_options &get_column_options () const { return m_column_options; }
+
   void set_caret_max_width (int value);
 
 private:
@@ -739,6 +741,7 @@ private:
   bool m_inhibit_notes_p;
 
   source_printing_options m_source_printing;
+  column_options m_column_options;
 
   /* True if -freport-bug option is used.  */
   bool m_report_bug;
@@ -748,17 +751,6 @@ private:
      -fdiagnostics-parseable-fixits and GCC_EXTRA_DIAGNOSTIC_OUTPUT.  */
   enum diagnostics_extra_output_kind m_extra_output_kind;
 
-public:
-  /* What units to use when outputting the column number.  */
-  enum diagnostics_column_unit m_column_unit;
-
-  /* The origin for the column number (1-based or 0-based typically).  */
-  int m_column_origin;
-
-  /* The size of the tabstop for tab expansion.  */
-  int m_tabstop;
-
-private:
   /* How should non-ASCII/non-printable bytes be escaped when
      a diagnostic suggests escaping the source code on output.  */
   enum diagnostics_escape_format m_escape_format;
index 4738ae9c3007d0289087e1f9151bda63493768c0..4b27acb13ad2f25c9fd89b5568f301674cf9cb16 100644 (file)
@@ -1674,7 +1674,7 @@ sarif_builder::sarif_builder (diagnostics::context &dc,
     (std::make_unique<sarif_array_of_unique<sarif_logical_location>> ()),
   m_run_graphs
     (std::make_unique<sarif_array_of_unique<sarif_graph>> ()),
-  m_tabstop (dc.m_tabstop),
+  m_tabstop (dc.get_column_options ().m_tabstop),
   m_serialization_format (std::move (serialization_format)),
   m_sarif_gen_opts (sarif_gen_opts),
   m_next_result_idx (0),
index 94b1c2dcf9cb48ac5b802c3be07f2a8e6ac5e24c..aeda9ad54e5379e8dadc80cd8517fbbea2685311 100644 (file)
@@ -4412,7 +4412,7 @@ test_layout_x_offset_display_tab (const line_table_case &case_)
   for (int tabstop = 1; tabstop != num_tabstops; ++tabstop)
     {
       test_context dc;
-      dc.m_tabstop = tabstop;
+      dc.get_column_options ().m_tabstop = tabstop;
       diagnostics::source_print_policy policy (dc);
       layout test_layout (policy, richloc, nullptr);
       colorizer col (*dc.get_reference_printer (),
@@ -4436,7 +4436,7 @@ test_layout_x_offset_display_tab (const line_table_case &case_)
   for (int tabstop = 1; tabstop != num_tabstops; ++tabstop)
     {
       test_context dc;
-      dc.m_tabstop = tabstop;
+      dc.get_column_options ().m_tabstop = tabstop;
       static const int small_width = 24;
       auto &source_printing_opts = dc.get_source_printing_options ();
       source_printing_opts.max_width = small_width - 4;
@@ -6833,7 +6833,7 @@ test_tab_expansion (const line_table_case &case_)
      everything too.  */
   {
     test_context dc;
-    dc.m_tabstop = tabstop;
+    dc.get_column_options ().m_tabstop = tabstop;
     rich_location richloc (line_table,
                           linemap_position_for_column (line_table,
                                                        first_non_ws_byte_col));
@@ -6846,7 +6846,7 @@ test_tab_expansion (const line_table_case &case_)
      as well.  */
   {
     test_context dc;
-    dc.m_tabstop = tabstop;
+    dc.get_column_options ().m_tabstop = tabstop;
     rich_location richloc (line_table,
                           linemap_position_for_column (line_table,
                                                        right_quote_byte_col));
index b6d25bfd1702bdbe51c5b5d0b50f4a3a3d19a141..1468b0906f281b7de120feec26f4fc9e2d431dd5 100644 (file)
@@ -3016,11 +3016,12 @@ common_handle_option (struct gcc_options *opts,
       break;
 
     case OPT_fdiagnostics_column_unit_:
-      dc->m_column_unit = (enum diagnostics_column_unit)value;
+      dc->get_column_options ().m_column_unit
+       = (enum diagnostics_column_unit)value;
       break;
 
     case OPT_fdiagnostics_column_origin_:
-      dc->m_column_origin = value;
+      dc->get_column_options ().m_column_origin = value;
       break;
 
     case OPT_fdiagnostics_escape_format_:
@@ -3408,7 +3409,7 @@ common_handle_option (struct gcc_options *opts,
     case OPT_ftabstop_:
       /* It is documented that we silently ignore silly values.  */
       if (value >= 1 && value <= 100)
-       dc->m_tabstop = value;
+       dc->get_column_options ().m_tabstop = value;
       break;
 
     case OPT_freport_bug: