if (c_parser_next_token_is (parser, CPP_NAME))
{
c_token *comp_tok = c_parser_peek_token (parser);
- offsetof_ref = build_component_ref
- (loc, offsetof_ref, comp_tok->value, comp_tok->location);
+ offsetof_ref
+ = build_component_ref (loc, offsetof_ref, comp_tok->value,
+ comp_tok->location, UNKNOWN_LOCATION);
c_parser_consume_token (parser);
while (c_parser_next_token_is (parser, CPP_DOT)
|| c_parser_next_token_is (parser,
break;
}
c_token *comp_tok = c_parser_peek_token (parser);
- offsetof_ref = build_component_ref
- (loc, offsetof_ref, comp_tok->value,
- comp_tok->location);
+ offsetof_ref
+ = build_component_ref (loc, offsetof_ref,
+ comp_tok->value,
+ comp_tok->location,
+ UNKNOWN_LOCATION);
c_parser_consume_token (parser);
}
else
finish = c_parser_peek_token (parser)->get_finish ();
c_parser_consume_token (parser);
expr.value = build_component_ref (op_loc, expr.value, ident,
- comp_loc);
+ comp_loc, UNKNOWN_LOCATION);
set_c_expr_source_range (&expr, start, finish);
expr.original_code = ERROR_MARK;
if (TREE_CODE (expr.value) != COMPONENT_REF)
build_indirect_ref (op_loc,
expr.value,
RO_ARROW),
- ident, comp_loc);
+ ident, comp_loc,
+ expr.get_location ());
set_c_expr_source_range (&expr, start, finish);
expr.original_code = ERROR_MARK;
if (TREE_CODE (expr.value) != COMPONENT_REF)
&& c_parser_next_token_is (parser, CPP_DEREF)))
{
location_t op_loc = c_parser_peek_token (parser)->location;
+ location_t arrow_loc = UNKNOWN_LOCATION;
if (c_parser_next_token_is (parser, CPP_DEREF))
{
c_expr t_expr;
t_expr = convert_lvalue_to_rvalue (op_loc, t_expr,
true, false);
t = build_indirect_ref (op_loc, t_expr.value, RO_ARROW);
+ arrow_loc = t_expr.get_location ();
}
c_parser_consume_token (parser);
if (!c_parser_next_token_is (parser, CPP_NAME))
tree ident = comp_tok->value;
location_t comp_loc = comp_tok->location;
c_parser_consume_token (parser);
- t = build_component_ref (op_loc, t, ident, comp_loc);
+ t = build_component_ref (op_loc, t, ident, comp_loc,
+ arrow_loc);
}
/* FALLTHROUGH */
case OMP_CLAUSE_AFFINITY:
extern tree decl_constant_value_1 (tree, bool);
extern void mark_exp_read (tree);
extern tree composite_type (tree, tree);
-extern tree build_component_ref (location_t, tree, tree, location_t);
+extern tree build_component_ref (location_t, tree, tree, location_t,
+ location_t);
extern tree build_array_ref (location_t, tree, tree);
extern tree build_external_ref (location_t, tree, bool, tree *);
extern void pop_maybe_used (bool);
/* Make an expression to refer to the COMPONENT field of structure or
union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
location of the COMPONENT_REF. COMPONENT_LOC is the location
- of COMPONENT. */
+ of COMPONENT. ARROW_LOC is the location of the first -> operand if
+ it is from -> operator. */
tree
build_component_ref (location_t loc, tree datum, tree component,
- location_t component_loc)
+ location_t component_loc, location_t arrow_loc)
{
tree type = TREE_TYPE (datum);
enum tree_code code = TREE_CODE (type);
/* Special-case the error message for "ptr.field" for the case
where the user has confused "." vs "->". */
rich_location richloc (line_table, loc);
- /* "loc" should be the "." token. */
- richloc.add_fixit_replace ("->");
- error_at (&richloc,
- "%qE is a pointer; did you mean to use %<->%>?",
- datum);
+ if (TREE_CODE (datum) == INDIRECT_REF && arrow_loc != UNKNOWN_LOCATION)
+ {
+ richloc.add_fixit_insert_before (arrow_loc, "(*");
+ richloc.add_fixit_insert_after (arrow_loc, ")");
+ error_at (&richloc,
+ "%qE is a pointer to pointer; did you mean to dereference "
+ "it before applying %<->%> to it?",
+ TREE_OPERAND (datum, 0));
+ }
+ else
+ {
+ /* "loc" should be the "." token. */
+ richloc.add_fixit_replace ("->");
+ error_at (&richloc,
+ "%qE is a pointer; did you mean to use %<->%>?",
+ datum);
+ }
return error_mark_node;
}
else if (code != ERROR_MARK)
finish = c_parser_peek_token (parser)->get_finish ();
c_parser_consume_token (parser);
expr.value = build_component_ref (op_loc, expr.value, ident,
- comp_loc);
+ comp_loc, UNKNOWN_LOCATION);
set_c_expr_source_range (&expr, start, finish);
expr.original_code = ERROR_MARK;
if (TREE_CODE (expr.value) != COMPONENT_REF)
expr.value = build_component_ref (op_loc,
build_simple_mem_ref_loc
(op_loc, expr.value),
- ident, comp_loc);
+ ident, comp_loc,
+ expr.get_location ());
set_c_expr_source_range (&expr, start, finish);
expr.original_code = ERROR_MARK;
if (TREE_CODE (expr.value) != COMPONENT_REF)
tf_warning_or_error);
#else
return build_component_ref (input_location, datum, component,
- UNKNOWN_LOCATION);
+ UNKNOWN_LOCATION, UNKNOWN_LOCATION);
#endif
}
--- /dev/null
+/* PR c/91134 */
+/* { dg-options "-fdiagnostics-show-caret" } */
+
+struct X { int member; } x;
+struct Y { struct X **x; } y;
+
+int
+foo (void)
+{
+ struct X *pointer = &x;
+ struct Y *yp = &y;
+ struct X **pointerpointer = &pointer;
+ int i = *pointerpointer->member; /* { dg-error "'pointerpointer' is a pointer to pointer; did you mean to dereference it before applying '->' to it\\\?" } */
+/* { dg-begin-multiline-output "" }
+ int i = *pointerpointer->member;
+ ^~
+ (* )
+ { dg-end-multiline-output "" } */
+ int j = pointer.member; /* { dg-error "'pointer' is a pointer; did you mean to use '->'\\\?" } */
+/* { dg-begin-multiline-output "" }
+ int j = pointer.member;
+ ^
+ ->
+ { dg-end-multiline-output "" } */
+ int k = yp->x->member; /* { dg-error "'yp->x' is a pointer to pointer; did you mean to dereference it before applying '->' to it\\\?" } */
+/* { dg-begin-multiline-output "" }
+ int k = yp->x->member;
+ ^~
+ (* )
+ { dg-end-multiline-output "" } */
+ return i + j + k;
+}