Skip to content

Commit 2ffc173

Browse files
committed
Add parsing and handling of #pragma GCC diagnostic [GH #182]
1 parent c37d30e commit 2ffc173

154 files changed

Lines changed: 275 additions & 1463 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/il_def.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6619,6 +6619,7 @@ enum a_gcc_pragma_kind : a_byte {
66196619
gcc_pk_push_options, /* #pragma GCC push_options */
66206620
gcc_pk_pop_options, /* #pragma GCC pop_options */
66216621
gcc_pk_reset_options, /* #pragma GCC reset_options */
6622+
gcc_pk_diagnostic, /* #pragma GCC diagnostic ... */
66226623
gcc_pk_last
66236624
};
66246625

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//allowed_words: Wunused
2+
[GH #182]
3+
"GCC diagnostic" pragmas
4+
5+
The "GCC diagnostic" pragmas were not recognized, so in GNU modes each one
6+
was given an "unrecognized GCC pragma" warning. For example, with --g++:
7+
8+
#pragma GCC diagnostic push // Previously warned about.
9+
#pragma GCC diagnostic ignored "-Wunused-parameter"
10+
int f(int unused) { return 0; }
11+
#pragma GCC diagnostic pop
12+
13+
This is now fixed. The "error", "warning", and "ignored" forms, the "push"
14+
and "pop" forms (when emulating GCC 4.6 or later), the "ignored_attributes"
15+
form (when emulating GCC 12 or later), and the "fatal" form (when emulating
16+
clang) are accepted without a warning, to match the behavior of those
17+
compilers. A missing or malformed option string is still diagnosed. The
18+
pragmas have no effect on the front end's own diagnostics.

‎src/preproc.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4287,6 +4287,48 @@ kind indicates which of the above is being processed.
42874287
} /* process_gnu_options_pragma */
42884288

42894289

4290+
static a_boolean process_gnu_diagnostic_pragma(a_pending_pragma_ptr ppp)
4291+
/*
4292+
Handle "#pragma GCC diagnostic push/pop" (GCC 4.6 and later) and
4293+
"#pragma GCC diagnostic kind "option"", where kind is "error", "warning",
4294+
"ignored", "fatal" (clang only), or "ignored_attributes" (GCC 12 and later).
4295+
The pragmas are only accepted: they do not affect the front end's own
4296+
diagnostics, and (as in GCC) trailing text is ignored. Return FALSE if the
4297+
form is not recognized by the compiler being emulated.
4298+
*/
4299+
{
4300+
a_boolean recognized = FALSE;
4301+
4302+
/* Skip the "diagnostic" identifier. */
4303+
(void)get_token();
4304+
if (curr_token == tok_identifier) {
4305+
a_const_char *str = locator_for_curr_id.symbol_header->identifier;
4306+
if (gnu_version >= 40600 &&
4307+
(strcmp(str, "push") == 0 || strcmp(str, "pop") == 0)) {
4308+
recognized = TRUE;
4309+
} else if (strcmp(str, "error") == 0 ||
4310+
strcmp(str, "warning") == 0 ||
4311+
strcmp(str, "ignored") == 0 ||
4312+
(clang_mode && strcmp(str, "fatal") == 0) ||
4313+
(gnu_version >= 120000 &&
4314+
strcmp(str, "ignored_attributes") == 0)) {
4315+
recognized = TRUE;
4316+
(void)get_token();
4317+
if (curr_token != tok_string_literal ||
4318+
!is_normal_character_kind(const_for_curr_token.character_kind)) {
4319+
/* The option (or attribute list) is missing. Only a string literal
4320+
of plain char type is accepted. */
4321+
pos_warning(ec_exp_string_literal, &error_position);
4322+
} /* if */
4323+
} /* if */
4324+
} /* if */
4325+
if (recognized) {
4326+
ppp->variant.gcc.kind = (a_gcc_pragma_kind)gcc_pk_diagnostic;
4327+
} /* if */
4328+
return recognized;
4329+
} /* process_gnu_diagnostic_pragma */
4330+
4331+
42904332
void gcc_pragma(a_pending_pragma_ptr ppp)
42914333
/*
42924334
Process a "#pragma GCC ..." construct.
@@ -4318,6 +4360,9 @@ Process a "#pragma GCC ..." construct.
43184360
process_gnu_options_pragma(ppp, (a_gcc_pragma_kind)gcc_pk_pop_options);
43194361
} else if (gnu_version >= 40400 && strcmp(str, "reset_options") == 0) {
43204362
process_gnu_options_pragma(ppp, (a_gcc_pragma_kind)gcc_pk_reset_options);
4363+
} else if (strcmp(str, "diagnostic") == 0) {
4364+
ignore_in_back_end = FALSE;
4365+
recognized = process_gnu_diagnostic_pragma(ppp);
43214366
#if GNU_VECTOR_TYPES_ALLOWED && BUILTIN_FUNCTIONS_ENABLED
43224367
} else if (target_is_arm_based() && target_is_64_bits() &&
43234368
strcmp(str, "aarch64") == 0) {
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
11
fe_only -DTEST_NUMBER=1 --g++ Test_name.c
2-
"Test_name.c", line 13: warning: unrecognized GCC pragma
3-
_Pragma ("GCC diagnostic push")
4-
^
5-
6-
"Test_name.c", line 14: warning: unrecognized GCC pragma
7-
_Pragma ("GCC diagnostic pop")
8-
^
9-

‎tests/tests/imported/clang/c/Preprocessor/.pragma_diagnostic_output.rto/default.1.1.txt‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,3 @@ fe_only -DTEST_NUMBER=1 --clang -e 200 --no_wrap_diagnostics --clang_version 999
2323
#pragma clang diagnostic pop
2424
^
2525

26-
"Test_name.c", line 5: warning: unrecognized GCC pragma
27-
#pragma GCC diagnostic warning "-Wall"
28-
^
29-
30-
"Test_name.c", line 7: warning: unrecognized GCC pragma
31-
#pragma GCC diagnostic ignored "-Wall"
32-
^
33-
34-
"Test_name.c", line 9: warning: unrecognized GCC pragma
35-
#pragma GCC diagnostic error "-Wall"
36-
^
37-
38-
"Test_name.c", line 11: warning: unrecognized GCC pragma
39-
#pragma GCC diagnostic fatal "-Wall"
40-
^
41-
42-
"Test_name.c", line 13: warning: unrecognized GCC pragma
43-
#pragma GCC diagnostic push
44-
^
45-
46-
"Test_name.c", line 15: warning: unrecognized GCC pragma
47-
#pragma GCC diagnostic pop
48-
^
49-
Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1 @@
11
fe_only -DTEST_NUMBER=1 --clang -e 200 --no_wrap_diagnostics --clang_version 999999 --c Test_name.c
2-
"Test_name.c", line 5: warning: unrecognized GCC pragma
3-
#pragma GCC diagnostic warning "-Wall"
4-
^
5-
6-
"Test_name.c", line 7: warning: unrecognized GCC pragma
7-
#pragma GCC diagnostic ignored "-Wall"
8-
^
9-
10-
"Test_name.c", line 9: warning: unrecognized GCC pragma
11-
#pragma GCC diagnostic error "-Wall"
12-
^
13-
14-
"Test_name.c", line 11: warning: unrecognized GCC pragma
15-
#pragma GCC diagnostic fatal "-Wall"
16-
^
17-
18-
"Test_name.c", line 13: warning: unrecognized GCC pragma
19-
#pragma GCC diagnostic push
20-
^
21-
22-
"Test_name.c", line 15: warning: unrecognized GCC pragma
23-
#pragma GCC diagnostic pop
24-
^
25-
Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1 @@
11
fe_only -DTEST_NUMBER=1 --clang -e 200 --no_wrap_diagnostics --clang_version 999999 --c Test_name.c
2-
"Test_name.c", line 5: warning: unrecognized GCC pragma
3-
#pragma GCC diagnostic warning "-Wall"
4-
^
5-
6-
"Test_name.c", line 7: warning: unrecognized GCC pragma
7-
#pragma GCC diagnostic ignored "-Wall"
8-
^
9-
10-
"Test_name.c", line 9: warning: unrecognized GCC pragma
11-
#pragma GCC diagnostic error "-Wall"
12-
^
13-
14-
"Test_name.c", line 11: warning: unrecognized GCC pragma
15-
#pragma GCC diagnostic fatal "-Wall"
16-
^
17-
18-
"Test_name.c", line 13: warning: unrecognized GCC pragma
19-
#pragma GCC diagnostic push
20-
^
21-
22-
"Test_name.c", line 15: warning: unrecognized GCC pragma
23-
#pragma GCC diagnostic pop
24-
^
25-

‎tests/tests/imported/clang/c/Sema/.format-strings-signedness.rto/default.2.1.txt‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,4 @@ fe_only -DTEST_NUMBER=2 --clang -e 200 --no_wrap_diagnostics --clang_version 999
33
int_val = __INT_MAX__, // INT_MAX
44
^
55

6-
"Test_name.c", line 222: warning: unrecognized GCC pragma
7-
#pragma GCC diagnostic ignored "-Wformat"
8-
^
9-
106
1 error detected in the compilation of "Test_name.c".

‎tests/tests/imported/clang/c/Sema/.format-strings-signedness.rto/default.4.1.txt‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,4 @@ fe_only -DTEST_NUMBER=4 --clang -e 200 --no_wrap_diagnostics --clang_version 999
33
int_val = __INT_MAX__, // INT_MAX
44
^
55

6-
"Test_name.c", line 222: warning: unrecognized GCC pragma
7-
#pragma GCC diagnostic ignored "-Wformat"
8-
^
9-
106
1 error detected in the compilation of "Test_name.c".

‎tests/tests/imported/clang/c/Sema/.format-strings-signedness.rto/default.5.1.txt‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,4 @@ fe_only -DTEST_NUMBER=5 --clang -e 200 --no_wrap_diagnostics --clang_version 999
33
int_val = __INT_MAX__, // INT_MAX
44
^
55

6-
"Test_name.c", line 222: warning: unrecognized GCC pragma
7-
#pragma GCC diagnostic ignored "-Wformat"
8-
^
9-
106
1 error detected in the compilation of "Test_name.c".

0 commit comments

Comments
 (0)