| 1 | /* Language-independent diagnostic subroutines for the GNU Compiler |
| 2 | Collection that are only for use in the compilers proper and not |
| 3 | the driver or other programs. |
| 4 | Copyright (C) 1999-2025 Free Software Foundation, Inc. |
| 5 | |
| 6 | This file is part of GCC. |
| 7 | |
| 8 | GCC is free software; you can redistribute it and/or modify it under |
| 9 | the terms of the GNU General Public License as published by the Free |
| 10 | Software Foundation; either version 3, or (at your option) any later |
| 11 | version. |
| 12 | |
| 13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 16 | for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with GCC; see the file COPYING3. If not see |
| 20 | <http://www.gnu.org/licenses/>. */ |
| 21 | |
| 22 | #include "config.h" |
| 23 | #include "system.h" |
| 24 | #include "coretypes.h" |
| 25 | #include "tree.h" |
| 26 | #include "diagnostic.h" |
| 27 | #include "tree-pretty-print.h" |
| 28 | #include "gimple-pretty-print.h" |
| 29 | #include "tree-diagnostic.h" |
| 30 | #include "diagnostic-client-data-hooks.h" |
| 31 | #include "langhooks.h" |
| 32 | #include "intl.h" |
| 33 | #include "diagnostic-format-text.h" |
| 34 | |
| 35 | /* Prints out, if necessary, the name of the current function |
| 36 | that caused an error. */ |
| 37 | void |
| 38 | diagnostic_report_current_function (diagnostic_text_output_format &text_output, |
| 39 | const diagnostic_info *diagnostic) |
| 40 | { |
| 41 | location_t loc = diagnostic_location (diagnostic); |
| 42 | text_output.report_current_module (where: loc); |
| 43 | lang_hooks.print_error_function (text_output, LOCATION_FILE (loc), diagnostic); |
| 44 | } |
| 45 | |
| 46 | static void |
| 47 | default_tree_diagnostic_text_starter (diagnostic_text_output_format &text_output, |
| 48 | const diagnostic_info *diagnostic) |
| 49 | { |
| 50 | pretty_printer *const pp = text_output.get_printer (); |
| 51 | diagnostic_report_current_function (text_output, diagnostic); |
| 52 | pp_set_prefix (pp, prefix: text_output.build_prefix (*diagnostic)); |
| 53 | } |
| 54 | |
| 55 | /* Default tree printer. Handles declarations only. */ |
| 56 | bool |
| 57 | default_tree_printer (pretty_printer *pp, text_info *text, const char *spec, |
| 58 | int precision, bool wide, bool set_locus, bool hash, |
| 59 | bool *, pp_token_list &) |
| 60 | { |
| 61 | tree t; |
| 62 | |
| 63 | /* FUTURE: %+x should set the locus. */ |
| 64 | if (precision != 0 || wide || hash) |
| 65 | return false; |
| 66 | |
| 67 | switch (*spec) |
| 68 | { |
| 69 | case 'E': |
| 70 | t = va_arg (*text->m_args_ptr, tree); |
| 71 | if (TREE_CODE (t) == IDENTIFIER_NODE) |
| 72 | { |
| 73 | pp_identifier (pp, IDENTIFIER_POINTER (t)); |
| 74 | return true; |
| 75 | } |
| 76 | break; |
| 77 | |
| 78 | case 'D': |
| 79 | t = va_arg (*text->m_args_ptr, tree); |
| 80 | if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t)) |
| 81 | t = DECL_DEBUG_EXPR (t); |
| 82 | break; |
| 83 | |
| 84 | case 'F': |
| 85 | case 'T': |
| 86 | t = va_arg (*text->m_args_ptr, tree); |
| 87 | break; |
| 88 | |
| 89 | default: |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | if (set_locus) |
| 94 | text->set_location (idx: 0, DECL_SOURCE_LOCATION (t), range_display_kind: SHOW_RANGE_WITH_CARET); |
| 95 | |
| 96 | if (DECL_P (t)) |
| 97 | { |
| 98 | const char *n = DECL_NAME (t) |
| 99 | ? identifier_to_locale (lang_hooks.decl_printable_name (t, 2)) |
| 100 | : _("<anonymous>" ); |
| 101 | pp_string (pp, n); |
| 102 | } |
| 103 | else |
| 104 | dump_generic_node (pp, t, 0, TDF_SLIM, 0); |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | /* Set the locations of call sites along the inlining stack corresponding |
| 110 | to the DIAGNOSTIC location. */ |
| 111 | |
| 112 | static void |
| 113 | set_inlining_locations (diagnostic_context *, |
| 114 | diagnostic_info *diagnostic) |
| 115 | { |
| 116 | location_t loc = diagnostic_location (diagnostic); |
| 117 | tree block = LOCATION_BLOCK (loc); |
| 118 | |
| 119 | /* Count the number of locations in system headers. When all are, |
| 120 | warnings are suppressed by -Wno-system-headers. Otherwise, they |
| 121 | involve some user code, possibly inlined into a function in a system |
| 122 | header, and are not treated as coming from system headers. */ |
| 123 | unsigned nsyslocs = 0; |
| 124 | |
| 125 | /* Use a reference to the vector of locations for convenience. */ |
| 126 | auto &ilocs = diagnostic->m_iinfo.m_ilocs; |
| 127 | |
| 128 | while (block && TREE_CODE (block) == BLOCK |
| 129 | && BLOCK_ABSTRACT_ORIGIN (block)) |
| 130 | { |
| 131 | tree ao = BLOCK_ABSTRACT_ORIGIN (block); |
| 132 | if (TREE_CODE (ao) == FUNCTION_DECL) |
| 133 | { |
| 134 | if (!diagnostic->m_iinfo.m_ao) |
| 135 | diagnostic->m_iinfo.m_ao = block; |
| 136 | |
| 137 | location_t bsloc = BLOCK_SOURCE_LOCATION (block); |
| 138 | ilocs.safe_push (obj: bsloc); |
| 139 | if (in_system_header_at (loc: bsloc)) |
| 140 | ++nsyslocs; |
| 141 | } |
| 142 | else if (TREE_CODE (ao) != BLOCK) |
| 143 | break; |
| 144 | |
| 145 | block = BLOCK_SUPERCONTEXT (block); |
| 146 | } |
| 147 | |
| 148 | if (ilocs.length ()) |
| 149 | { |
| 150 | /* When there is an inlining context use the macro expansion |
| 151 | location for the original location and bump up NSYSLOCS if |
| 152 | it's in a system header since it's not counted above. */ |
| 153 | location_t sysloc = expansion_point_location_if_in_system_header (loc); |
| 154 | if (sysloc != loc) |
| 155 | { |
| 156 | loc = sysloc; |
| 157 | ++nsyslocs; |
| 158 | } |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | /* When there's no inlining context use the original location |
| 163 | and set NSYSLOCS accordingly. */ |
| 164 | nsyslocs = in_system_header_at (loc) != 0; |
| 165 | } |
| 166 | |
| 167 | ilocs.safe_push (obj: loc); |
| 168 | |
| 169 | /* Set if all locations are in a system header. */ |
| 170 | diagnostic->m_iinfo.m_allsyslocs = nsyslocs == ilocs.length (); |
| 171 | |
| 172 | if (tree *ao = pp_ti_abstract_origin (&diagnostic->message)) |
| 173 | *ao = (tree)diagnostic->m_iinfo.m_ao; |
| 174 | } |
| 175 | |
| 176 | /* Sets CONTEXT to use language independent diagnostics. */ |
| 177 | void |
| 178 | tree_diagnostics_defaults (diagnostic_context *context) |
| 179 | { |
| 180 | diagnostic_text_starter (context) = default_tree_diagnostic_text_starter; |
| 181 | diagnostic_text_finalizer (context) = default_diagnostic_text_finalizer; |
| 182 | context->set_format_decoder (default_tree_printer); |
| 183 | context->set_set_locations_callback (set_inlining_locations); |
| 184 | context->set_client_data_hooks (make_compiler_data_hooks ()); |
| 185 | } |
| 186 | |