GCC Middle and Back End API Reference
format_string_diagnostic_t Class Reference

#include <substring-locations.h>

Collaboration diagram for format_string_diagnostic_t:

Public Member Functions

 format_string_diagnostic_t (const substring_loc &fmt_loc, const range_label *fmt_label, location_t param_loc, const range_label *param_label, const char *corrected_substring)
 
bool emit_warning_va (diagnostic_option_id option_id, const char *gmsgid, va_list *ap) const ATTRIBUTE_GCC_DIAG(3
 
bool bool emit_warning_n_va (diagnostic_option_id option_id, unsigned HOST_WIDE_INT n, const char *singular_gmsgid, const char *plural_gmsgid, va_list *ap) const ATTRIBUTE_GCC_DIAG(4
 
bool bool ATTRIBUTE_GCC_DIAG (5, 0)
 
bool emit_warning (diagnostic_option_id option_id, const char *gmsgid,...) const ATTRIBUTE_GCC_DIAG(3
 
bool bool emit_warning_n (diagnostic_option_id option_id, unsigned HOST_WIDE_INT n, const char *singular_gmsgid, const char *plural_gmsgid,...) const ATTRIBUTE_GCC_DIAG(4
 
bool bool ATTRIBUTE_GCC_DIAG (5, 6)
 

Static Public Attributes

static const char *const highlight_color_format_string = highlight_colors::expected
 
static const char *const highlight_color_param = highlight_colors::actual
 

Private Attributes

const substring_locm_fmt_loc
 
const range_label * m_fmt_label
 
location_t m_param_loc
 
const range_label * m_param_label
 
const char * m_corrected_substring
 

Detailed Description

A bundle of state for emitting a diagnostic relating to a format string.

Constructor & Destructor Documentation

◆ format_string_diagnostic_t()

format_string_diagnostic_t::format_string_diagnostic_t ( const substring_loc & fmt_loc,
const range_label * fmt_label,
location_t param_loc,
const range_label * param_label,
const char * corrected_substring )
format_string_diagnostic_t's ctor, giving information for use by the emit_warning* member functions, as follows: They attempt to obtain precise location information within a string literal from FMT_LOC. Case 1: if substring location is available, and is within the range of the format string itself, the primary location of the diagnostic is the substring range obtained from FMT_LOC, with the caret at the *end* of the substring range. For example: test.c:90:10: warning: problem with '%i' here [-Wformat=] printf ("hello %i", msg); ~^ Case 2: if the substring location is available, but is not within the range of the format string, the primary location is that of the format string, and a note is emitted showing the substring location. For example: test.c:90:10: warning: problem with '%i' here [-Wformat=] printf("hello " INT_FMT " world", msg); ^~~~~~~~~~~~~~~~~~~~~~~~~ test.c:19: note: format string is defined here #define INT_FMT "%i" ~^ Case 3: if precise substring information is unavailable, the primary location is that of the whole string passed to FMT_LOC's constructor. For example: test.c:90:10: warning: problem with '%i' here [-Wformat=] printf(fmt, msg); ^~~ For each of cases 1-3, if param_loc is not UNKNOWN_LOCATION, then it is used as a secondary range within the warning. For example, here it is used with case 1: test.c:90:16: warning: '%s' here but arg 2 has 'long' type [-Wformat=] printf ("foo %s bar", long_i + long_j); ~^ ~~~~~~~~~~~~~~~ and here with case 2: test.c:90:16: warning: '%s' here but arg 2 has 'long' type [-Wformat=] printf ("foo " STR_FMT " bar", long_i + long_j); ^~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ test.c:89:16: note: format string is defined here #define STR_FMT "%s" ~^ and with case 3: test.c:90:10: warning: '%i' here, but arg 2 is "const char *' [-Wformat=] printf(fmt, msg); ^~~ ~~~ If non-NULL, then FMT_LABEL will be used to label the location within the string for cases 1 and 2; if non-NULL, then PARAM_LABEL will be used to label the parameter. For example with case 1: test.c:90:16: warning: '%s' here but arg 2 has 'long' type [-Wformat=] printf ("foo %s bar", long_i + long_j); ~^ ~~~~~~~~~~~~~~~ | int and with case 2: test.c:90:10: warning: problem with '%i' here [-Wformat=] printf("hello " INT_FMT " world", msg); ^~~~~~~~~~~~~~~~~~~~~~~~~ test.c:19: note: format string is defined here #define INT_FMT "%i" ~^ | int If CORRECTED_SUBSTRING is non-NULL, use it for cases 1 and 2 to provide a fix-it hint, suggesting that it should replace the text within the substring range. For example: test.c:90:10: warning: problem with '%i' here [-Wformat=] printf ("hello %i", msg); ~^ %s

References m_corrected_substring, m_fmt_label, m_fmt_loc, m_param_label, and m_param_loc.

Member Function Documentation

◆ ATTRIBUTE_GCC_DIAG() [1/2]

bool bool format_string_diagnostic_t::ATTRIBUTE_GCC_DIAG ( 5 ,
0  )

◆ ATTRIBUTE_GCC_DIAG() [2/2]

bool bool format_string_diagnostic_t::ATTRIBUTE_GCC_DIAG ( 5 ,
6  )

References ATTRIBUTE_GCC_DIAG().

◆ emit_warning()

bool format_string_diagnostic_t::emit_warning ( diagnostic_option_id option_id,
const char * gmsgid,
... ) const
Variadic version of the above (singular only).

References ap, and emit_warning_va().

Referenced by ATTRIBUTE_GCC_DIAG().

◆ emit_warning_n()

bool format_string_diagnostic_t::emit_warning_n ( diagnostic_option_id option_id,
unsigned HOST_WIDE_INT n,
const char * singular_gmsgid,
const char * plural_gmsgid,
... ) const
Variadic version of the above (singular vs plural).

References ap, and emit_warning_n_va().

Referenced by ATTRIBUTE_GCC_DIAG().

◆ emit_warning_n_va()

bool format_string_diagnostic_t::emit_warning_n_va ( diagnostic_option_id option_id,
unsigned HOST_WIDE_INT n,
const char * singular_gmsgid,
const char * plural_gmsgid,
va_list * ap ) const
Emit a warning governed by option OPTION_ID, using SINGULAR_GMSGID as the format string (or if PLURAL_GMSGID is different from SINGULAR_GMSGID, using SINGULAR_GMSGID, PLURAL_GMSGID and N as arguments to ngettext) and AP as its arguments. Return true if a warning was emitted, false otherwise.

References ap, diagnostic_report_diagnostic(), diagnostic_set_info(), diagnostic_set_info_translated(), global_dc, highlight_color_format_string, highlight_color_param, inform(), line_table, m_corrected_substring, m_fmt_label, m_fmt_loc, m_param_label, m_param_loc, ngettext, NULL, ULONG_MAX, and UNKNOWN_LOCATION.

Referenced by emit_warning_n(), and emit_warning_va().

◆ emit_warning_va()

bool format_string_diagnostic_t::emit_warning_va ( diagnostic_option_id option_id,
const char * gmsgid,
va_list * ap ) const
Singular-only version of the above.

References ap, and emit_warning_n_va().

Referenced by emit_warning().

Field Documentation

◆ highlight_color_format_string

const char *const format_string_diagnostic_t::highlight_color_format_string = highlight_colors::expected
static
Source locations within string literals. Copyright (C) 2016-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/>.

Referenced by emit_warning_n_va().

◆ highlight_color_param

const char *const format_string_diagnostic_t::highlight_color_param = highlight_colors::actual
static

Referenced by emit_warning_n_va().

◆ m_corrected_substring

const char* format_string_diagnostic_t::m_corrected_substring
private

◆ m_fmt_label

const range_label* format_string_diagnostic_t::m_fmt_label
private

◆ m_fmt_loc

const substring_loc& format_string_diagnostic_t::m_fmt_loc
private

◆ m_param_label

const range_label* format_string_diagnostic_t::m_param_label
private

◆ m_param_loc

location_t format_string_diagnostic_t::m_param_loc
private

The documentation for this class was generated from the following files: