This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
RFA: PATCH to c_strlen for c++/35652 (wrong source location for string bounds warning)
- From: Jason Merrill <jason at redhat dot com>
- To: gcc-patches List <gcc-patches at gcc dot gnu dot org>
- Date: Wed, 03 Feb 2010 00:29:15 -0500
- Subject: RFA: PATCH to c_strlen for c++/35652 (wrong source location for string bounds warning)
Having source location information on most expressions makes this bug a
lot easier to fix.
Tested x86_64-pc-linux-gnu. OK for 4.5?
commit 3ca3ae93bd4b675ce3bc846ad6a5735d368b7bb6
Author: Jason Merrill <jason@redhat.com>
Date: Tue Feb 2 22:34:31 2010 -0500
PR c++/35652
* builtins.c (c_strlen): Use EXPR_LOCATION in diagnostics.
diff --git a/gcc/builtins.c b/gcc/builtins.c
index ac96934..b6fd1ea 100644
--- a/gcc/builtins.c
+++ b/gcc/builtins.c
@@ -433,6 +433,7 @@ c_strlen (tree src, int only_value)
HOST_WIDE_INT offset;
int max;
const char *ptr;
+ location_t loc;
STRIP_NOPS (src);
if (TREE_CODE (src) == COND_EXPR
@@ -450,6 +451,11 @@ c_strlen (tree src, int only_value)
&& (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
return c_strlen (TREE_OPERAND (src, 1), only_value);
+ if (EXPR_HAS_LOCATION (src))
+ loc = EXPR_LOCATION (src);
+ else
+ loc = input_location;
+
src = string_constant (src, &offset_node);
if (src == 0)
return NULL_TREE;
@@ -475,7 +481,7 @@ c_strlen (tree src, int only_value)
and return that. This would perhaps not be valid if we were dealing
with named arrays in addition to literal string constants. */
- return size_diffop_loc (input_location, size_int (max), offset_node);
+ return size_diffop_loc (loc, size_int (max), offset_node);
}
/* We have a known offset into the string. Start searching there for
@@ -494,7 +500,7 @@ c_strlen (tree src, int only_value)
/* Suppress multiple warnings for propagated constant strings. */
if (! TREE_NO_WARNING (src))
{
- warning (0, "offset outside bounds of constant string");
+ warning_at (loc, 0, "offset outside bounds of constant string");
TREE_NO_WARNING (src) = 1;
}
return NULL_TREE;
diff --git a/gcc/testsuite/g++.dg/warn/string1.C b/gcc/testsuite/g++.dg/warn/string1.C
new file mode 100644
index 0000000..3027727
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/string1.C
@@ -0,0 +1,18 @@
+// PR c++/35652
+// { dg-options "-O" }
+
+#include <string>
+int main() {
+ // blank line padding, could also be code...
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ std::string s = "";
+ s += 'x' + "y"; // { dg-warning "bounds of constant string" }
+}