diff mbox

Fix PR objc/68438 (uninitialized source ranges)

Message ID 1448051277.19594.160.camel@surprise
State New
Headers show

Commit Message

David Malcolm Nov. 20, 2015, 8:27 p.m. UTC
The attached patch fixes some more places in c/c-parser.c where the
src_range field of a c_expr was being left uninitialized, this time for
various Objective C constructs.

The source ranges are verified using the same unit-testing plugin used
for C expressions.  This leads to a wart, which is that it means having
a .m test file lurking below gcc.dg/plugin.  A workaround would be to
create an objc.dg/plugin subdirectory, with a new plugin.exp for testing
Objective C plugins, and having it reference the existing plugin below
gcc.dg/plugin.  That seemed like excessive complication, so I went for
the simpler approach of simply putting the .m file below gcc.dg/plugin.

I manually verified that this fixes the specific valgrind warnings
reported in the PR, and visually inspected the code for
objective-c-specific instances of uninitialized c_expr src_range values.

Bootstrapped&regrtested on x86_64-pc-linux-gnu; adds 15 PASS results to
gcc.sum.

OK for trunk?

Comments

Joseph Myers Nov. 20, 2015, 11:28 p.m. UTC | #1
On Fri, 20 Nov 2015, David Malcolm wrote:

> The source ranges are verified using the same unit-testing plugin used
> for C expressions.  This leads to a wart, which is that it means having
> a .m test file lurking below gcc.dg/plugin.  A workaround would be to
> create an objc.dg/plugin subdirectory, with a new plugin.exp for testing
> Objective C plugins, and having it reference the existing plugin below
> gcc.dg/plugin.  That seemed like excessive complication, so I went for
> the simpler approach of simply putting the .m file below gcc.dg/plugin.

Have you made sure that this test is quietly not run if the 
--enable-languages configuration does not build the ObjC compiler?
David Malcolm Nov. 22, 2015, 4:13 p.m. UTC | #2
On Fri, 2015-11-20 at 23:28 +0000, Joseph Myers wrote:
> On Fri, 20 Nov 2015, David Malcolm wrote:
> 
> > The source ranges are verified using the same unit-testing plugin used
> > for C expressions.  This leads to a wart, which is that it means having
> > a .m test file lurking below gcc.dg/plugin.  A workaround would be to
> > create an objc.dg/plugin subdirectory, with a new plugin.exp for testing
> > Objective C plugins, and having it reference the existing plugin below
> > gcc.dg/plugin.  That seemed like excessive complication, so I went for
> > the simpler approach of simply putting the .m file below gcc.dg/plugin.
> 
> Have you made sure that this test is quietly not run if the 
> --enable-languages configuration does not build the ObjC compiler?

Good point; sadly, it does introduce FAILs for that configuration.

Is there (or could there be) a precanned dg- directive to ask if ObjC is
available?  

Otherwise I can look at creating an objc.dg/plugin subdirectory.

Thanks
Dave
Joseph Myers Nov. 23, 2015, 11:13 a.m. UTC | #3
On Sun, 22 Nov 2015, David Malcolm wrote:

> Is there (or could there be) a precanned dg- directive to ask if ObjC is
> available?  

I don't think so.  Normal practice is that each language's tests are in 
appropriate directories for that language, with runtest never called with 
a --tool option for that language if it wasn't built.
Jeff Law Nov. 23, 2015, 5:25 p.m. UTC | #4
On 11/23/2015 04:13 AM, Joseph Myers wrote:
> On Sun, 22 Nov 2015, David Malcolm wrote:
>
>> Is there (or could there be) a precanned dg- directive to ask if ObjC is
>> available?
>
> I don't think so.  Normal practice is that each language's tests are in
> appropriate directories for that language, with runtest never called with
> a --tool option for that language if it wasn't built.
Right.  Which argues that we really want to create a new test directory 
for objc plugin tests.

jeff
Mike Stump Nov. 23, 2015, 9:04 p.m. UTC | #5
On Nov 23, 2015, at 3:13 AM, Joseph Myers <joseph@codesourcery.com> wrote:
> On Sun, 22 Nov 2015, David Malcolm wrote:
> 
>> Is there (or could there be) a precanned dg- directive to ask if ObjC is
>> available?  
> 
> I don't think so.  Normal practice is that each language's tests are in 
> appropriate directories for that language, with runtest never called with 
> a --tool option for that language if it wasn't built.

It is also reasonable to have a common to C/ObjC test directory if people want to do that and have them run if objc is present.  We do that for some tests in the C world.
diff mbox

Patch

From afdae8b15f71164d0d05e790078519b38bd674a4 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Fri, 20 Nov 2015 11:12:47 -0500
Subject: [PATCH] Fix PR objc/68438 (uninitialized source ranges)

gcc/c/ChangeLog:
	PR objc/68438
	* c-parser.c (c_parser_postfix_expression): Set up source ranges
	for various Objective-C constructs: Class.name syntax,
	@selector(), @protocol, @encode(), and [] message syntax.

gcc/testsuite/ChangeLog:
	PR objc/68438
	* gcc.dg/plugin/diagnostic-test-expressions-2.m: New test file.
	* gcc.dg/plugin/plugin.exp (plugin_test_list): Add the above file.
---
 gcc/c/c-parser.c                                   | 17 +++-
 .../gcc.dg/plugin/diagnostic-test-expressions-2.m  | 94 ++++++++++++++++++++++
 gcc/testsuite/gcc.dg/plugin/plugin.exp             |  3 +-
 3 files changed, 110 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/plugin/diagnostic-test-expressions-2.m

diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c
index 7b10764..18e9957 100644
--- a/gcc/c/c-parser.c
+++ b/gcc/c/c-parser.c
@@ -7338,10 +7338,13 @@  c_parser_postfix_expression (c_parser *parser)
 		expr.value = error_mark_node;
 		break;
 	      }
-	    component = c_parser_peek_token (parser)->value;
+	    c_token *component_tok = c_parser_peek_token (parser);
+	    component = component_tok->value;
+	    location_t end_loc = component_tok->get_finish ();
 	    c_parser_consume_token (parser);
 	    expr.value = objc_build_class_component_ref (class_name, 
 							 component);
+	    set_c_expr_source_range (&expr, loc, end_loc);
 	    break;
 	  }
 	default:
@@ -7816,9 +7819,11 @@  c_parser_postfix_expression (c_parser *parser)
 	    }
 	  {
 	    tree sel = c_parser_objc_selector_arg (parser);
+	    location_t close_loc = c_parser_peek_token (parser)->location;
 	    c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
 				       "expected %<)%>");
 	    expr.value = objc_build_selector_expr (loc, sel);
+	    set_c_expr_source_range (&expr, loc, close_loc);
 	  }
 	  break;
 	case RID_AT_PROTOCOL:
@@ -7839,9 +7844,11 @@  c_parser_postfix_expression (c_parser *parser)
 	  {
 	    tree id = c_parser_peek_token (parser)->value;
 	    c_parser_consume_token (parser);
+	    location_t close_loc = c_parser_peek_token (parser)->location;
 	    c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
 				       "expected %<)%>");
 	    expr.value = objc_build_protocol_expr (id);
+	    set_c_expr_source_range (&expr, loc, close_loc);
 	  }
 	  break;
 	case RID_AT_ENCODE:
@@ -7860,11 +7867,13 @@  c_parser_postfix_expression (c_parser *parser)
 	      c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
 	      break;
 	    }
-	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
-				     "expected %<)%>");
 	  {
+	    location_t close_loc = c_parser_peek_token (parser)->location;
+	    c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
+				     "expected %<)%>");
 	    tree type = groktypename (t1, NULL, NULL);
 	    expr.value = objc_build_encode_expr (type);
+	    set_c_expr_source_range (&expr, loc, close_loc);
 	  }
 	  break;
 	case RID_GENERIC:
@@ -7907,9 +7916,11 @@  c_parser_postfix_expression (c_parser *parser)
 	  c_parser_consume_token (parser);
 	  receiver = c_parser_objc_receiver (parser);
 	  args = c_parser_objc_message_args (parser);
+	  location_t close_loc = c_parser_peek_token (parser)->location;
 	  c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
 				     "expected %<]%>");
 	  expr.value = objc_build_message_expr (receiver, args);
+	  set_c_expr_source_range (&expr, loc, close_loc);
 	  break;
 	}
       /* Else fall through to report error.  */
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-expressions-2.m b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-expressions-2.m
new file mode 100644
index 0000000..ed7aca3
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-expressions-2.m
@@ -0,0 +1,94 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O -fdiagnostics-show-caret" } */
+
+/* This file is similar to diagnostic-test-expressions-1.c
+   (see the notes in that file); this file adds test
+   coverage for various Objective C constructs. */
+
+extern void __emit_expression_range (int dummy, ...);
+
+@protocol prot
+@end
+
+@interface tests <prot>
+- (int) func0;
+- (int) func1:(int)i;
++ (int) func2;
+- (void) test_sending_messages;
++ (void) test_class_dot_name;
+- (void) test_at_selector;
+- (void) test_at_protocol;
+- (void) test_at_encode:(int)i;
+@end
+
+@implementation tests
+- (int) func0
+{
+  return 42;
+}
+- (int) func1:(int)i
+{
+  return i * i;
+}
++ (int) func2
+{
+  return 0;
+}
+- (void) test_sending_messages
+{
+  __emit_expression_range ( 0, [self func0] ); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range ( 0, [self func0] );
+                                ~~~~~~~~~~~~
+   { dg-end-multiline-output "" } */
+  __emit_expression_range ( 0, [self func1:5] ); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range ( 0, [self func1:5] );
+                                ~~~~~~~~~~~~~~
+   { dg-end-multiline-output "" } */
+}
++ (void) test_class_dot_name
+{
+  __emit_expression_range ( 0, tests.func2 ); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range ( 0, tests.func2 );
+                                ~~~~~~~~~~~
+   { dg-end-multiline-output "" } */
+}
+- (void) test_at_selector
+{
+  __emit_expression_range ( 0, @selector(func0) ); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range ( 0, @selector(func0) );
+                                ^~~~~~~~~~~~~~~~
+   { dg-end-multiline-output "" } */
+}
+- (void) test_at_protocol
+{
+  __emit_expression_range ( 0, @protocol(prot) ); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range ( 0, @protocol(prot) );
+                                ~~~~~~~~~~~~~~~
+   { dg-end-multiline-output "" } */
+}
+- (void) test_at_encode:(int)i
+{
+  /* @encode() generates a STRING_CST which doesn't retain a location
+     after parsing, so we need to access it via compound expressions
+     that can't be folded away.  */
+
+  /* Verify start.  */
+  __emit_expression_range ( 0, @encode(int) + i ); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range ( 0, @encode(int) + i );
+                                ~~~~~~~~~~~~~^~~
+   { dg-end-multiline-output "" } */
+
+  /* Verify finish.  */
+  __emit_expression_range ( 0, i + @encode(int) ); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range ( 0, i + @encode(int) );
+                                ~~^~~~~~~~~~~~~~
+   { dg-end-multiline-output "" } */
+}
+@end
diff --git a/gcc/testsuite/gcc.dg/plugin/plugin.exp b/gcc/testsuite/gcc.dg/plugin/plugin.exp
index 06080cc..6260a59 100644
--- a/gcc/testsuite/gcc.dg/plugin/plugin.exp
+++ b/gcc/testsuite/gcc.dg/plugin/plugin.exp
@@ -67,7 +67,8 @@  set plugin_test_list [list \
 	  diagnostic-test-show-locus-bw.c \
 	  diagnostic-test-show-locus-color.c } \
     { diagnostic_plugin_test_tree_expression_range.c \
-	  diagnostic-test-expressions-1.c } \
+	  diagnostic-test-expressions-1.c \
+	  diagnostic-test-expressions-2.m } \
     { diagnostic_plugin_show_trees.c \
 	  diagnostic-test-show-trees-1.c } \
     { levenshtein_plugin.c levenshtein-test-1.c } \
-- 
1.8.5.3