From 7bab0ad0a868451d4c5916400e8a4ff29e845775 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 25 Nov 2015 03:48:38 -0500
Subject: [PATCH 08/20] FIXME: Support objc-c++ locations (has ChangeLog)

cp_parser_binary_expression is arguably an independent fix

gcc/cp/ChangeLog:
	* parser.c (cp_parser_primary_expression): Generate and set
	locations for Objective C 2.0 dot-syntax.
	(cp_parser_binary_expression): Always set location on result.
	(cp_parser_objc_expression): Convert return type from tree to
	cp_expr.
	(cp_parser_objc_message_expression): Generate and set locations.
	(cp_parser_objc_encode_expression): Convert return type from tree
	to cp_expr.  Generate a location and return it using cp_expr ctor.
	(cp_parser_objc_protocol_expression): Generate and set locations.
	(cp_parser_objc_selector_expression): Likewise.

gcc/testsuite/ChangeLog:
	* obj-c++.dg/plugin/diagnostic-test-expressions-1.mm: New file.
	* obj-c++.dg/plugin/plugin.exp: New file, based on
	objc.dg/plugin/plugin.exp.
---
 gcc/cp/parser.c                                    | 84 ++++++++++++++++---
 .../plugin/diagnostic-test-expressions-1.mm        | 94 ++++++++++++++++++++++
 gcc/testsuite/obj-c++.dg/plugin/plugin.exp         | 90 +++++++++++++++++++++
 3 files changed, 255 insertions(+), 13 deletions(-)
 create mode 100644 gcc/testsuite/obj-c++.dg/plugin/diagnostic-test-expressions-1.mm
 create mode 100644 gcc/testsuite/obj-c++.dg/plugin/plugin.exp

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 71fc4db..9e2f36f 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -2378,7 +2378,7 @@ static tree cp_parser_objc_message_args
   (cp_parser *);
 static tree cp_parser_objc_message_expression
   (cp_parser *);
-static tree cp_parser_objc_encode_expression
+static cp_expr cp_parser_objc_encode_expression
   (cp_parser *);
 static tree cp_parser_objc_defs_expression
   (cp_parser *);
@@ -2386,7 +2386,7 @@ static tree cp_parser_objc_protocol_expression
   (cp_parser *);
 static tree cp_parser_objc_selector_expression
   (cp_parser *);
-static tree cp_parser_objc_expression
+static cp_expr cp_parser_objc_expression
   (cp_parser *);
 static bool cp_parser_objc_selector_p
   (enum cpp_ttype);
@@ -5011,7 +5011,19 @@ cp_parser_primary_expression (cp_parser *parser,
 		if (component == error_mark_node)
 		  return error_mark_node;
 
-		return objc_build_class_component_ref (id_expression, component);
+		tree result = objc_build_class_component_ref (id_expression,
+							      component);
+		/* Build a location of the form:
+		     expr.component
+		     ~~~~~^~~~~~~~~
+		   with caret at the start of the component name (at
+		   input_location), ranging from the start of the id_expression
+		   to the end of the component name.  */
+		location_t combined_loc
+		  = make_location (input_location, id_expression.get_start (),
+				   get_finish (input_location));
+		protected_set_expr_location (result, combined_loc);
+		return result;
 	      }
 
 	    /* In Objective-C++, an instance variable (ivar) may be preferred
@@ -8683,10 +8695,14 @@ cp_parser_binary_expression (cp_parser* parser, bool cast_p,
 				  ? boolean_type_node : TREE_TYPE (current.lhs),
 				  current.lhs, rhs);
       else
-	current.lhs = build_x_binary_op (combined_loc, current.tree_type,
-					 current.lhs, current.lhs_type,
-					 rhs, rhs_type, &overload,
-					 complain_flags (decltype_p));
+        {
+          current.lhs = build_x_binary_op (combined_loc, current.tree_type,
+                                           current.lhs, current.lhs_type,
+                                           rhs, rhs_type, &overload,
+                                           complain_flags (decltype_p));
+          /* TODO: build_x_binary_op doesn't always honor the location.  */
+          current.lhs.set_location (combined_loc);
+        }
       current.lhs_type = current.tree_type;
 
       /* If the binary operator required the use of an overloaded operator,
@@ -27270,7 +27286,7 @@ cp_parser_allow_gnu_extensions_p (cp_parser* parser)
 
   Returns a tree representation of the expression.  */
 
-static tree
+static cp_expr
 cp_parser_objc_expression (cp_parser* parser)
 {
   /* Try to figure out what kind of declaration is present.  */
@@ -27322,12 +27338,23 @@ cp_parser_objc_message_expression (cp_parser* parser)
 {
   tree receiver, messageargs;
 
+  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
   receiver = cp_parser_objc_message_receiver (parser);
   messageargs = cp_parser_objc_message_args (parser);
+  location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
 
-  return objc_build_message_expr (receiver, messageargs);
+  tree result = objc_build_message_expr (receiver, messageargs);
+
+  /* Construct a location e.g.
+       [self func1:5]
+       ^~~~~~~~~~~~~~
+     ranging from the '[' to the ']', with the caret at the start.  */
+  location_t combined_loc = make_location (start_loc, start_loc, end_loc);
+  protected_set_expr_location (result, combined_loc);
+
+  return result;
 }
 
 /* Parse an objc-message-receiver.
@@ -27444,11 +27471,12 @@ cp_parser_objc_message_args (cp_parser* parser)
 
    Returns an encoded representation of the type argument.  */
 
-static tree
+static cp_expr
 cp_parser_objc_encode_expression (cp_parser* parser)
 {
   tree type;
   cp_token *token;
+  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
 
   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
@@ -27476,7 +27504,16 @@ cp_parser_objc_encode_expression (cp_parser* parser)
       return value;
     }
 
-  return objc_build_encode_expr (type);
+
+  /* Build a location of the form:
+       @encode(int)
+       ^~~~~~~~~~~~
+     with caret==start at the @ token, finishing at the close paren.  */
+  location_t combined_loc
+    = make_location (start_loc, start_loc,
+                     cp_lexer_previous_token (parser->lexer)->location);
+
+  return cp_expr (objc_build_encode_expr (type), combined_loc);
 }
 
 /* Parse an Objective-C @defs expression.  */
@@ -27505,13 +27542,23 @@ static tree
 cp_parser_objc_protocol_expression (cp_parser* parser)
 {
   tree proto;
+  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
 
   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
   proto = cp_parser_identifier (parser);
   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
 
-  return objc_build_protocol_expr (proto);
+  /* Build a location of the form:
+       @protocol(prot)
+       ^~~~~~~~~~~~~~~
+     with caret==start at the @ token, finishing at the close paren.  */
+  location_t combined_loc
+    = make_location (start_loc, start_loc,
+                     cp_lexer_previous_token (parser->lexer)->location);
+  tree result = objc_build_protocol_expr (proto);
+  protected_set_expr_location (result, combined_loc);
+  return result;
 }
 
 /* Parse an Objective-C selector expression.
@@ -27587,7 +27634,18 @@ cp_parser_objc_selector_expression (cp_parser* parser)
  finish_selector:
   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
 
-  return objc_build_selector_expr (loc, sel_seq);
+
+  /* Build a location of the form:
+       @selector(func)
+       ^~~~~~~~~~~~~~~
+     with caret==start at the @ token, finishing at the close paren.  */
+  location_t combined_loc
+    = make_location (loc, loc,
+                     cp_lexer_previous_token (parser->lexer)->location);
+  tree result = objc_build_selector_expr (combined_loc, sel_seq);
+  /* TODO: objc_build_selector_expr doesn't always honor the location.  */
+  protected_set_expr_location (result, combined_loc);
+  return result;
 }
 
 /* Parse a list of identifiers.
diff --git a/gcc/testsuite/obj-c++.dg/plugin/diagnostic-test-expressions-1.mm b/gcc/testsuite/obj-c++.dg/plugin/diagnostic-test-expressions-1.mm
new file mode 100644
index 0000000..609fe3d
--- /dev/null
+++ b/gcc/testsuite/obj-c++.dg/plugin/diagnostic-test-expressions-1.mm
@@ -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/obj-c++.dg/plugin/plugin.exp b/gcc/testsuite/obj-c++.dg/plugin/plugin.exp
new file mode 100644
index 0000000..d0d400b
--- /dev/null
+++ b/gcc/testsuite/obj-c++.dg/plugin/plugin.exp
@@ -0,0 +1,90 @@
+#   Copyright (C) 2009-2015 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+# Test the functionality of the GCC plugin support
+
+load_lib target-supports.exp
+load_lib obj-c++-dg.exp
+
+global TESTING_IN_BUILD_TREE
+global ENABLE_PLUGIN
+
+# The plugin testcases currently only work when the build tree is available.
+# Also check whether the host supports plugins.
+if { ![info exists TESTING_IN_BUILD_TREE] || ![info exists ENABLE_PLUGIN] } {
+    return
+}
+
+# If a testcase doesn't have special options, use these.
+global DEFAULT_CFLAGS
+if ![info exists DEFAULT_CFLAGS] then {
+    set DEFAULT_CFLAGS " -ansi -pedantic-errors"
+}
+
+# The procedures in plugin-support.exp need these parameters.
+set default_flags $DEFAULT_CFLAGS
+
+if $tracelevel then {
+    strace $tracelevel
+}
+
+# Load support procs.
+load_lib plugin-support.exp
+
+# These tests don't run runtest_file_p consistently if it
+# doesn't return the same values, so disable parallelization
+# of this *.exp file.  The first parallel runtest to reach
+# this will run all the tests serially.
+if ![gcc_parallel_test_run_p plugin] {
+    return
+}
+gcc_parallel_test_enable 0
+
+# Specify the plugin source file and the associated test files in a list.
+# plugin_test_list={ {plugin1 test1 test2 ...} {plugin2 test1 ...} ... }
+set plugin_test_list [list \
+    { ../../gcc.dg/plugin/diagnostic_plugin_test_tree_expression_range.c \
+	  diagnostic-test-expressions-1.mm } \
+]
+
+foreach plugin_test $plugin_test_list {
+    # Replace each source file with its full-path name
+    for {set i 0} {$i < [llength $plugin_test]} {incr i} {
+	set basename [lindex $plugin_test $i]
+	set plugin_test [lreplace $plugin_test $i $i $srcdir/$subdir/$basename]
+    }
+    set plugin_src [lindex $plugin_test 0]
+    # If we're only testing specific files and this isn't one of them, skip it.
+    if ![runtest_file_p $runtests $plugin_src] then {
+        continue
+    }
+    set plugin_input_tests [lreplace $plugin_test 0 0]
+    plugin-test-execute $plugin_src $plugin_input_tests
+}
+
+# run the plugindir tests
+
+# Initialize `dg'.
+dg-init
+
+# Main loop.
+dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/plugindir*.mm]] \
+	"" $DEFAULT_CFLAGS
+
+# All done.
+dg-finish
+
+gcc_parallel_test_enable 1
-- 
1.8.5.3

