1af526226Smrg /* Various diagnostic subroutines for the GNU C language.
2*dd083157Smrg Copyright (C) 2000-2020 Free Software Foundation, Inc.
3af526226Smrg Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
4af526226Smrg
5af526226Smrg This file is part of GCC.
6af526226Smrg
7af526226Smrg GCC is free software; you can redistribute it and/or modify it under
8af526226Smrg the terms of the GNU General Public License as published by the Free
9af526226Smrg Software Foundation; either version 3, or (at your option) any later
10af526226Smrg version.
11af526226Smrg
12af526226Smrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13af526226Smrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
14af526226Smrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15af526226Smrg for more details.
16af526226Smrg
17af526226Smrg You should have received a copy of the GNU General Public License
18af526226Smrg along with GCC; see the file COPYING3. If not see
19af526226Smrg <http://www.gnu.org/licenses/>. */
20af526226Smrg
21af526226Smrg #include "config.h"
22af526226Smrg #include "system.h"
23af526226Smrg #include "coretypes.h"
24af526226Smrg #include "tm.h"
25af526226Smrg #include "c-tree.h"
265ef59e75Smrg #include "opts.h"
27af526226Smrg
2881418a27Smrg /* Issue an ISO C11 pedantic warning MSGID if -pedantic outside C2X mode,
2981418a27Smrg otherwise issue warning MSGID if -Wc11-c2X-compat is specified.
3081418a27Smrg This function is supposed to be used for matters that are allowed in
3181418a27Smrg ISO C2X but not supported in ISO C11, thus we explicitly don't pedwarn
3281418a27Smrg when C2X is specified. */
3381418a27Smrg
3481418a27Smrg bool
pedwarn_c11(location_t location,int opt,const char * gmsgid,...)3581418a27Smrg pedwarn_c11 (location_t location, int opt, const char *gmsgid, ...)
3681418a27Smrg {
3781418a27Smrg diagnostic_info diagnostic;
3881418a27Smrg va_list ap;
3981418a27Smrg bool warned = false;
4081418a27Smrg rich_location richloc (line_table, location);
4181418a27Smrg
4281418a27Smrg va_start (ap, gmsgid);
4381418a27Smrg /* If desired, issue the C11/C2X compat warning, which is more specific
4481418a27Smrg than -pedantic. */
4581418a27Smrg if (warn_c11_c2x_compat > 0)
4681418a27Smrg {
4781418a27Smrg diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
4881418a27Smrg (pedantic && !flag_isoc2x)
4981418a27Smrg ? DK_PEDWARN : DK_WARNING);
5081418a27Smrg diagnostic.option_index = OPT_Wc11_c2x_compat;
5181418a27Smrg warned = diagnostic_report_diagnostic (global_dc, &diagnostic);
5281418a27Smrg }
5381418a27Smrg /* -Wno-c11-c2x-compat suppresses even the pedwarns. */
5481418a27Smrg else if (warn_c11_c2x_compat == 0)
5581418a27Smrg ;
5681418a27Smrg /* For -pedantic outside C2X, issue a pedwarn. */
5781418a27Smrg else if (pedantic && !flag_isoc2x)
5881418a27Smrg {
5981418a27Smrg diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_PEDWARN);
6081418a27Smrg diagnostic.option_index = opt;
6181418a27Smrg warned = diagnostic_report_diagnostic (global_dc, &diagnostic);
6281418a27Smrg }
6381418a27Smrg va_end (ap);
6481418a27Smrg return warned;
6581418a27Smrg }
6681418a27Smrg
675ef59e75Smrg /* Issue an ISO C99 pedantic warning MSGID if -pedantic outside C11 mode,
685ef59e75Smrg otherwise issue warning MSGID if -Wc99-c11-compat is specified.
695ef59e75Smrg This function is supposed to be used for matters that are allowed in
705ef59e75Smrg ISO C11 but not supported in ISO C99, thus we explicitly don't pedwarn
715ef59e75Smrg when C11 is specified. */
72af526226Smrg
735ef59e75Smrg bool
pedwarn_c99(location_t location,int opt,const char * gmsgid,...)74af526226Smrg pedwarn_c99 (location_t location, int opt, const char *gmsgid, ...)
75af526226Smrg {
76af526226Smrg diagnostic_info diagnostic;
77af526226Smrg va_list ap;
785ef59e75Smrg bool warned = false;
7963aace61Smrg rich_location richloc (line_table, location);
80af526226Smrg
81af526226Smrg va_start (ap, gmsgid);
825ef59e75Smrg /* If desired, issue the C99/C11 compat warning, which is more specific
835ef59e75Smrg than -pedantic. */
845ef59e75Smrg if (warn_c99_c11_compat > 0)
855ef59e75Smrg {
8663aace61Smrg diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
875ef59e75Smrg (pedantic && !flag_isoc11)
885ef59e75Smrg ? DK_PEDWARN : DK_WARNING);
895ef59e75Smrg diagnostic.option_index = OPT_Wc99_c11_compat;
903903d7f3Smrg warned = diagnostic_report_diagnostic (global_dc, &diagnostic);
915ef59e75Smrg }
925ef59e75Smrg /* -Wno-c99-c11-compat suppresses even the pedwarns. */
935ef59e75Smrg else if (warn_c99_c11_compat == 0)
945ef59e75Smrg ;
955ef59e75Smrg /* For -pedantic outside C11, issue a pedwarn. */
965ef59e75Smrg else if (pedantic && !flag_isoc11)
975ef59e75Smrg {
9863aace61Smrg diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_PEDWARN);
99af526226Smrg diagnostic.option_index = opt;
1003903d7f3Smrg warned = diagnostic_report_diagnostic (global_dc, &diagnostic);
1015ef59e75Smrg }
102af526226Smrg va_end (ap);
1035ef59e75Smrg return warned;
104af526226Smrg }
105af526226Smrg
1065ef59e75Smrg /* Issue an ISO C90 pedantic warning MSGID if -pedantic outside C99 mode,
1075ef59e75Smrg otherwise issue warning MSGID if -Wc90-c99-compat is specified, or if
1085ef59e75Smrg a specific option such as -Wlong-long is specified.
1095ef59e75Smrg This function is supposed to be used for matters that are allowed in
1105ef59e75Smrg ISO C99 but not supported in ISO C90, thus we explicitly don't pedwarn
1115ef59e75Smrg when C99 is specified. (There is no flag_c90.) */
112af526226Smrg
1136a5c9aabSmrg bool
pedwarn_c90(location_t location,int opt,const char * gmsgid,...)114af526226Smrg pedwarn_c90 (location_t location, int opt, const char *gmsgid, ...)
115af526226Smrg {
116af526226Smrg diagnostic_info diagnostic;
117af526226Smrg va_list ap;
1186a5c9aabSmrg bool warned = false;
11963aace61Smrg rich_location richloc (line_table, location);
120af526226Smrg
121af526226Smrg va_start (ap, gmsgid);
1225ef59e75Smrg /* Warnings such as -Wvla are the most specific ones. */
1235ef59e75Smrg if (opt != OPT_Wpedantic)
1245ef59e75Smrg {
1255ef59e75Smrg int opt_var = *(int *) option_flag_var (opt, &global_options);
1265ef59e75Smrg if (opt_var == 0)
1275ef59e75Smrg goto out;
1285ef59e75Smrg else if (opt_var > 0)
1295ef59e75Smrg {
13063aace61Smrg diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
1315ef59e75Smrg (pedantic && !flag_isoc99)
1325ef59e75Smrg ? DK_PEDWARN : DK_WARNING);
133af526226Smrg diagnostic.option_index = opt;
1343903d7f3Smrg diagnostic_report_diagnostic (global_dc, &diagnostic);
1356a5c9aabSmrg warned = true;
1365ef59e75Smrg goto out;
1375ef59e75Smrg }
1385ef59e75Smrg }
1395ef59e75Smrg /* Maybe we want to issue the C90/C99 compat warning, which is more
1405ef59e75Smrg specific than -pedantic. */
1415ef59e75Smrg if (warn_c90_c99_compat > 0)
1425ef59e75Smrg {
14363aace61Smrg diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
1445ef59e75Smrg (pedantic && !flag_isoc99)
1455ef59e75Smrg ? DK_PEDWARN : DK_WARNING);
1465ef59e75Smrg diagnostic.option_index = OPT_Wc90_c99_compat;
1473903d7f3Smrg diagnostic_report_diagnostic (global_dc, &diagnostic);
1485ef59e75Smrg }
1495ef59e75Smrg /* -Wno-c90-c99-compat suppresses the pedwarns. */
1505ef59e75Smrg else if (warn_c90_c99_compat == 0)
1515ef59e75Smrg ;
1525ef59e75Smrg /* For -pedantic outside C99, issue a pedwarn. */
1535ef59e75Smrg else if (pedantic && !flag_isoc99)
1545ef59e75Smrg {
15563aace61Smrg diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_PEDWARN);
1565ef59e75Smrg diagnostic.option_index = opt;
1573903d7f3Smrg diagnostic_report_diagnostic (global_dc, &diagnostic);
1586a5c9aabSmrg warned = true;
1595ef59e75Smrg }
1605ef59e75Smrg out:
161af526226Smrg va_end (ap);
1626a5c9aabSmrg return warned;
163af526226Smrg }
164