]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
diagnostics: Fix virtual location for -Wuninitialized [PR69543]
authorLewis Hyatt <lhyatt@gmail.com>
Fri, 30 Sep 2022 18:10:00 +0000 (14:10 -0400)
committerLewis Hyatt <lhyatt@gmail.com>
Fri, 30 Sep 2022 18:10:00 +0000 (14:10 -0400)
Warnings issued for -Wuninitialized have been using the spelling location of
the problematic usage, discarding any information on the location of the macro
expansion point if such usage was in a macro. This makes the warnings
impossible to control reliably with #pragma GCC diagnostic, and also discards
useful context in the diagnostic output. There seems to be no need to discard
the virtual location information, so this patch fixes that.

PR69543 was mostly about _Pragma issues which have been fixed for many years
now. The PR remains open because two of the testcases added in response to it
still have xfails, but those xfails have nothing to do with _Pragma and rather
just with the issue fixed by this patch, so the PR can be closed now as well.

The other testcase modified here, pragma-diagnostic-2.c, was explicitly
testing for the undesirable behavior that was xfailed in pr69543-3.c. I have
adjusted that and also added a new testcase verifying all 3 types of warning
that come from tree-ssa-uninit.cc get the proper location information now.

gcc/ChangeLog:

PR preprocessor/69543
* tree-ssa-uninit.cc (warn_uninit): Stop stripping macro tracking
information away from the diagnostic location.
(maybe_warn_read_write_only): Likewise.
(maybe_warn_operand): Likewise.

gcc/testsuite/ChangeLog:

PR preprocessor/69543
* c-c++-common/pr69543-3.c: Remove xfail.
* c-c++-common/pr69543-4.c: Likewise.
* gcc.dg/cpp/pragma-diagnostic-2.c: Adjust test for new behavior.
* c-c++-common/pragma-diag-16.c: New test.

gcc/testsuite/c-c++-common/pr69543-3.c
gcc/testsuite/c-c++-common/pr69543-4.c
gcc/testsuite/c-c++-common/pragma-diag-16.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-2.c
gcc/tree-ssa-uninit.cc

index fcf750cc05d37d87965dca5ed8ea8cb37d41a44c..6d4224f4af759b81b86cf37e1902b5d83fdba3fd 100644 (file)
@@ -3,15 +3,11 @@
 /* Verify disabling a warning, where the _Pragma is in regular code,
    but the affected code is within a macro.  */
 
-/* TODO: XFAIL: both C and C++ erroneously fail to suppress the warning
-   The warning is reported at the macro definition location, rather than
-   the macro expansion location.  */
-
-#define WARNABLE_CODE *++yyvsp = yylval; /* { dg-bogus "used uninitialized" "" { xfail *-*-* } } */
+#define WARNABLE_CODE *++yyvsp = yylval; /* { dg-bogus "used uninitialized" } */
 
 void test (char yylval)
 {
-  char *yyvsp; /* { dg-bogus "declared here" "" { xfail *-*-* } } */
+  char *yyvsp; /* { dg-bogus "declared here" } */
   _Pragma ("GCC diagnostic push")
   _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"")
   _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"")
index cd71e7e118822e7c5df4f3713a2f5515b63662e4..3db2eeb16eb67cb0c70945cec6df1b81e2479c72 100644 (file)
@@ -3,10 +3,6 @@
 /* Verify disabling a warning, where both the _Pragma and the
    affected code are within (different) macros.  */
 
-/* TODO: XFAIL: both C and C++ erroneously fail to suppress the warning
-   The warning is reported at the macro definition location, rather than
-   the macro expansion location.  */
-
 # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN   \
     _Pragma ("GCC diagnostic push") \
     _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"")\
 # define YY_IGNORE_MAYBE_UNINITIALIZED_END \
     _Pragma ("GCC diagnostic pop")
 
-#define WARNABLE_CODE *++yyvsp = yylval; /* { dg-bogus "used uninitialized" "" { xfail *-*-* } } */
+#define WARNABLE_CODE *++yyvsp = yylval; /* { dg-bogus "used uninitialized" } */
 
 void test (char yylval)
 {
-  char *yyvsp; /* { dg-bogus "declared here" "" { xfail *-*-* } } */
+  char *yyvsp; /* { dg-bogus "declared here" } */
   YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
   WARNABLE_CODE
   YY_IGNORE_MAYBE_UNINITIALIZED_END
diff --git a/gcc/testsuite/c-c++-common/pragma-diag-16.c b/gcc/testsuite/c-c++-common/pragma-diag-16.c
new file mode 100644 (file)
index 0000000..8cacd87
--- /dev/null
@@ -0,0 +1,63 @@
+/* Make sure that the 3 types of warnings generated from tree-ssa-uninit.cc have
+   proper virtual locations and so can be controlled by pragmas when they appear
+   in macros.  */
+
+/* { dg-do compile } */
+/* { dg-options "-Wuninitialized -Wmaybe-uninitialized" } */
+
+/* 1.  Check maybe_warn_read_write_only().  */
+#define DEREF1(p) (*p) /* { dg-warning {may be used uninitialized} } */
+__attribute__ ((access (write_only, 1)))
+int f1 (int* x) /* { dg-note {accessing argument 1} } */
+{
+  return DEREF1 (x); /* { dg-note {in expansion of macro 'DEREF1'} } */
+}
+
+#define DEREF2(p) (*p) /* { dg-bogus {may be used uninitialized} } */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
+__attribute__ ((access (write_only, 1)))
+int f2 (int* x) /* { dg-bogus {accessing argument 1} } */
+{
+  return DEREF2 (x); /* { dg-bogus {in expansion of macro 'DEREF1'} } */
+}
+#pragma GCC diagnostic pop
+
+/* 2.  Check warn_uninit().  */
+int g;
+#define SET3(a, b) ((a) = (b)) /* { dg-warning {'x' is used uninitialized} } */
+void f3 ()
+{
+  int x; /* { dg-note {'x' was declared here} } */
+  SET3 (g, x); /* { dg-note {in expansion of macro 'SET3'} } */
+}
+
+#define SET4(a, b) ((a) = (b)) /* { dg-bogus {'x' is used uninitialized} } */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wuninitialized"
+void f4 ()
+{
+  int x; /* { dg-bogus {'x' was declared here} } */
+  SET4 (g, x); /* { dg-bogus {in expansion of macro 'SET3'} } */
+}
+#pragma GCC diagnostic pop
+
+/* 3.  Check maybe_warn_operand().  */
+#define CALL5(func, arg) ((func) (arg)) /* { dg-warning {'c' may be used uninitialized} } */
+void f5a (const char *); /* { dg-note {by argument 1} } */
+void f5b ()
+{
+  char c; /* { dg-note {'c' declared here} } */
+  CALL5 (f5a, &c); /* { dg-note {in expansion of macro 'CALL5'} } */
+}
+
+#define CALL6(func, arg) ((func) (arg)) /* { dg-bogus {'c' may be used uninitialized} } */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
+void f6a (const char *); /* { dg-bogus {by argument 1} } */
+void f6b ()
+{
+  char c; /* { dg-bogus {'c' declared here} } */
+  CALL6 (f6a, &c); /* { dg-bogus {in expansion of macro 'CALL6'} } */
+}
+#pragma GCC diagnostic pop
index 38fc77c47baaaa4031ede54d28561c7b1446d621..89d2975ee7bd7cd8b18359dd1bc9fa2a8adc7c4a 100644 (file)
@@ -7,18 +7,17 @@ void f (unsigned);
 
 #define CODE_WITH_WARNING \
   int a; /* { dg-message "was declared here" } */       \
-  f (a)         /* { dg-warning "used uninitialized" } */
+  f (a)         /* { dg-error "used uninitialized" } */
 
 #pragma GCC diagnostic ignored "-Wuninitialized"
 
 void
 g (void)
 {
+  /* No warning expected here since the #pragma is in effect.  */
   CODE_WITH_WARNING;
 }
 
-#pragma GCC diagnostic push
-
 #pragma GCC diagnostic error "-Wuninitialized"
 
 void
@@ -26,3 +25,5 @@ h (void)
 {
   CODE_WITH_WARNING; /* { dg-message "in expansion of macro 'CODE_WITH_WARNING'" } */
 }
+
+/* { dg-regexp {.*some warnings being treated as errors} } */
index bde23997a0ada2b18403477609c56704ebac4459..bf2e50511af150b26ad6e09510868725b2a7e444 100644 (file)
@@ -274,9 +274,6 @@ warn_uninit (opt_code opt, tree t, tree var, gimple *context,
   else if (var_name_str)
     location = gimple_location (var_def_stmt);
 
-  location = linemap_resolve_location (line_table, location,
-                                      LRK_SPELLING_LOCATION, NULL);
-
   auto_diagnostic_group d;
   gcc_assert (opt == OPT_Wuninitialized || opt == OPT_Wmaybe_uninitialized);
   if (var)
@@ -424,10 +421,7 @@ maybe_warn_read_write_only (tree fndecl, gimple *stmt, tree arg, tree ptr)
          && access->mode != access_write_only)
        continue;
 
-      location_t stmtloc
-       = linemap_resolve_location (line_table, gimple_location (stmt),
-                                   LRK_SPELLING_LOCATION, NULL);
-
+      location_t stmtloc = gimple_location (stmt);
       if (!warning_at (stmtloc, OPT_Wmaybe_uninitialized,
                       "%qE may be used uninitialized", ptr))
        break;
@@ -722,9 +716,7 @@ maybe_warn_operand (ao_ref &ref, gimple *stmt, tree lhs, tree rhs,
   bool warned = false;
   /* We didn't find any may-defs so on all paths either
      reached function entry or a killing clobber.  */
-  location_t location
-    = linemap_resolve_location (line_table, gimple_location (stmt),
-                               LRK_SPELLING_LOCATION, NULL);
+  location_t location = gimple_location (stmt);
   if (wlims.always_executed)
     {
       if (warning_at (location, OPT_Wuninitialized,