Skip to content

Commit 44ba7bc

Browse files
author
Lewis Hyatt
committed
diagnostics: Fix behavior of permerror options after diagnostic pop [PR111918]
When a diagnostic pragma changes the classification of a given diagnostic, the global options flags (such as warn_narrowing, etc.) may get changed too. Specifically, if a warning was not enabled initially and was later enabled by a pragma, then the corresponding global flag will change from false to true when the pragma is processed. That change is permanent and is not undone by a subsequent `#pragma GCC diagnostic pop'; the warning flag needs to remain enabled since a diagnostic could be generated later on for a source location prior to the pop. So in order to support popping to the initial classification, given that the global options flags no longer reflect that state, the diagnostic_context object itself remembers the way things were before it changed anything. The current implementation works fine for diagnostics that are always errors or always warnings, but it doesn't do the right thing for diagnostics that could be either, such as -Wnarrowing. The classification of that diagnostic (or any permerror diagnostic) depends on the state of -fpermissive; for the particular case of -Wnarrowing it also matters whether a compile-time or run-time narrowing is being diagnosed. The problem is that the current implementation insists on recording whether an enabled diagnostic should be a DK_WARNING or a DK_ERROR, and then, after popping to the initial state, it overrides it always to that type only. Fix that up by adding a new internal diagnostic type DK_ANY. This just indicates that the diagnostic is enabled without mandating exactly what type of diagnostic it should be. Then the diagnostic can be emitted with whatever type the frontend asks for. Incidentally, while making this change, I noticed that classify_diagnostic() spends some time computing a return value (the old classification kind) that is not used anywhere. The computed value seems to have some problems, mainly that it does not take into account `#pragma GCC diagnostic pop' at all, and so the returned value doesn't seem like it could make sense in many contexts. Given it would also not be desirable to leak the new internal-only DK_ANY type to outside callers, I think it would make sense in a subsequent cleanup patch to remove the return value altogether. gcc/ChangeLog: PR c++/111918 * diagnostic-core.h (enum diagnostic_t): Add DK_ANY special flag. * diagnostic.cc (diagnostic_option_classifier::classify_diagnostic): Make use of DK_ANY to indicate a diagnostic was initially enabled. (diagnostic_context::diagnostic_enabled): Do not change the type of a diagnostic if the saved classification is type DK_ANY. gcc/testsuite/ChangeLog: PR c++/111918 * g++.dg/cpp0x/Wnarrowing21a.C: New test. * g++.dg/cpp0x/Wnarrowing21b.C: New test. * g++.dg/cpp0x/Wnarrowing21c.C: New test. * g++.dg/cpp0x/Wnarrowing21d.C: New test.
1 parent cc606a9 commit 44ba7bc

File tree

6 files changed

+55
-4
lines changed

6 files changed

+55
-4
lines changed

gcc/diagnostic-core.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ typedef enum
3333
DK_LAST_DIAGNOSTIC_KIND,
3434
/* This is used for tagging pragma pops in the diagnostic
3535
classification history chain. */
36-
DK_POP
36+
DK_POP,
37+
/* This is used internally to note that a diagnostic is enabled
38+
without mandating any specific type. */
39+
DK_ANY,
3740
} diagnostic_t;
3841

3942
/* RAII-style class for grouping related diagnostics. */

gcc/diagnostic.cc

+10-3
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,7 @@ classify_diagnostic (const diagnostic_context *context,
11361136
if (old_kind == DK_UNSPECIFIED)
11371137
{
11381138
old_kind = !context->option_enabled_p (option_index)
1139-
? DK_IGNORED : (context->warning_as_error_requested_p ()
1140-
? DK_ERROR : DK_WARNING);
1139+
? DK_IGNORED : DK_ANY;
11411140
m_classify_diagnostic[option_index] = old_kind;
11421141
}
11431142

@@ -1472,7 +1471,15 @@ diagnostic_context::diagnostic_enabled (diagnostic_info *diagnostic)
14721471
option. */
14731472
if (diag_class == DK_UNSPECIFIED
14741473
&& !option_unspecified_p (diagnostic->option_index))
1475-
diagnostic->kind = m_option_classifier.get_current_override (diagnostic->option_index);
1474+
{
1475+
const diagnostic_t new_kind
1476+
= m_option_classifier.get_current_override (diagnostic->option_index);
1477+
if (new_kind != DK_ANY)
1478+
/* DK_ANY means the diagnostic is not to be ignored, but we don't want
1479+
to change it specifically to DK_ERROR or DK_WARNING; we want to
1480+
preserve whatever the caller has specified. */
1481+
diagnostic->kind = new_kind;
1482+
}
14761483

14771484
/* This allows for future extensions, like temporarily disabling
14781485
warnings for ranges of source code. */
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* PR c++/111918 */
2+
/* { dg-do compile { target c++11 } } */
3+
/* { dg-options "" } Suppress default -pedantic-errors so we can test permerror functionality. */
4+
extern int g ();
5+
float f1{123456789}; /* { dg-error "-Wnarrowing" } */
6+
float f2{g ()}; /* { dg-warning "-Wnarrowing" } */
7+
#pragma GCC diagnostic push
8+
#pragma GCC diagnostic ignored "-Wnarrowing"
9+
float f3{123456789}; /* { dg-bogus "-Wnarrowing" } */
10+
float f4{g ()}; /* { dg-bogus "-Wnarrowing" } */
11+
#pragma GCC diagnostic pop
12+
float f5{123456789}; /* { dg-bogus "warning" "warning in place of error" } */
13+
/* { dg-error "-Wnarrowing" "" { target *-*-* } .-1 } */
14+
float f6{g ()}; /* { dg-warning "-Wnarrowing" } */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* PR c++/111918 */
2+
/* { dg-do compile { target c++11 } } */
3+
/* { dg-options "-Wno-error=narrowing" } */
4+
float f1{123456789}; /* { dg-warning "-Wnarrowing" } */
5+
#pragma GCC diagnostic push
6+
#pragma GCC diagnostic ignored "-Wnarrowing"
7+
float f2{123456789}; /* { dg-bogus "-Wnarrowing" } */
8+
#pragma GCC diagnostic pop
9+
float f3{123456789}; /* { dg-warning "-Wnarrowing" } */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* PR c++/111918 */
2+
/* { dg-do compile { target c++11 } } */
3+
/* { dg-options "-fpermissive" } */
4+
float f1{123456789}; /* { dg-warning "-Wnarrowing" } */
5+
#pragma GCC diagnostic push
6+
#pragma GCC diagnostic ignored "-Wnarrowing"
7+
float f2{123456789}; /* { dg-bogus "-Wnarrowing" } */
8+
#pragma GCC diagnostic pop
9+
float f3{123456789}; /* { dg-warning "-Wnarrowing" } */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* PR c++/111918 */
2+
/* { dg-do compile { target c++11 } } */
3+
/* { dg-options "-Wno-narrowing" } */
4+
float f1{123456789}; /* { dg-bogus "-Wnarrowing" } */
5+
#pragma GCC diagnostic push
6+
#pragma GCC diagnostic warning "-Wnarrowing"
7+
float f2{123456789}; /* { dg-warning "-Wnarrowing" } */
8+
#pragma GCC diagnostic pop
9+
float f3{123456789}; /* { dg-bogus "-Wnarrowing" } */

0 commit comments

Comments
 (0)