diff options
author | David Malcolm <dmalcolm@redhat.com> | 2025-08-08 16:55:43 -0400 |
---|---|---|
committer | David Malcolm <dmalcolm@redhat.com> | 2025-08-08 16:55:43 -0400 |
commit | 5edb251951a3a250b54a22fbd24c4f65f19f76fb (patch) | |
tree | 9095b84c41f1ebd60b278ab7123dd8cc93ef676b | |
parent | 435e09e05c3750cad153c3c6e83134803c22c7f6 (diff) | |
download | gcc-5edb251951a3a250b54a22fbd24c4f65f19f76fb.zip gcc-5edb251951a3a250b54a22fbd24c4f65f19f76fb.tar.gz gcc-5edb251951a3a250b54a22fbd24c4f65f19f76fb.tar.bz2 |
diagnostics: introduce struct column_options
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>
-rw-r--r-- | gcc/c-family/c-indentation.cc | 2 | ||||
-rw-r--r-- | gcc/c-family/c-opts.cc | 2 | ||||
-rw-r--r-- | gcc/diagnostics/column-options.h | 44 | ||||
-rw-r--r-- | gcc/diagnostics/context.cc | 42 | ||||
-rw-r--r-- | gcc/diagnostics/context.h | 22 | ||||
-rw-r--r-- | gcc/diagnostics/sarif-sink.cc | 2 | ||||
-rw-r--r-- | gcc/diagnostics/source-printing.cc | 8 | ||||
-rw-r--r-- | gcc/opts.cc | 7 |
8 files changed, 89 insertions, 40 deletions
diff --git a/gcc/c-family/c-indentation.cc b/gcc/c-family/c-indentation.cc index bb214fc..d378464 100644 --- a/gcc/c-family/c-indentation.cc +++ b/gcc/c-family/c-indentation.cc @@ -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) diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc index 3fb12b9..0ec30e8 100644 --- a/gcc/c-family/c-opts.cc +++ b/gcc/c-family/c-opts.cc @@ -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 index 0000000..86296e9 --- /dev/null +++ b/gcc/diagnostics/column-options.h @@ -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 */ diff --git a/gcc/diagnostics/context.cc b/gcc/diagnostics/context.cc index a1441ca..b05e078 100644 --- a/gcc/diagnostics/context.cc +++ b/gcc/diagnostics/context.cc @@ -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; diff --git a/gcc/diagnostics/context.h b/gcc/diagnostics/context.h index b6ec85c..961a872 100644 --- a/gcc/diagnostics/context.h +++ b/gcc/diagnostics/context.h @@ -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; diff --git a/gcc/diagnostics/sarif-sink.cc b/gcc/diagnostics/sarif-sink.cc index 4738ae9..4b27acb 100644 --- a/gcc/diagnostics/sarif-sink.cc +++ b/gcc/diagnostics/sarif-sink.cc @@ -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), diff --git a/gcc/diagnostics/source-printing.cc b/gcc/diagnostics/source-printing.cc index 94b1c2d..aeda9ad 100644 --- a/gcc/diagnostics/source-printing.cc +++ b/gcc/diagnostics/source-printing.cc @@ -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)); diff --git a/gcc/opts.cc b/gcc/opts.cc index b6d25bf..1468b09 100644 --- a/gcc/opts.cc +++ b/gcc/opts.cc @@ -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: |