In the C frontend, c_parser_postfix_expression after parsing a primary expression passes "loc", the location of the *first token* in that expression to c_parser_postfix_expression_after_primary, which thus discards any range information we had for primary expressions containing more than one token; we get just the range of the first token.
An example of this can be seen in this testcase from: https://gcc.gnu.org/wiki/ClangDiagnosticsComparison void foo(char **argP, char **argQ) { (argP - argQ)(); argP(); } for which trunk currently gives these ranges: diagnostic-range-bad-called-object.c:7:3: error: called object is not a function or function pointer (argP - argQ)(); ^ diagnostic-range-bad-called-object.c:14:3: error: called object 'argP' is not a function or function pointer argP(); ^~~~ The second happens to be correct, but the first is missing range information. The following patch is a one-liner to preserve the expression's location, changing the first to: diagnostic-range-bad-called-object.c:7:9: error: called object is not a function or function pointer (argP - argQ)(); ~~~~~~^~~~~~~ and leaving the second unchanged. Applying this fix requires tweaking some column numbers for expected locations in gcc.dg/cast-function-1.c; the output of trunk was of the form: cast-function-1.c:21:7: warning: function called through a non-compatible type d = ((double (*) (int)) foo1) (i); ^ which the patch changes to: cast-function-1.c:21:8: warning: function called through a non-compatible type d = ((double (*) (int)) foo1) (i); ~^~~~~~~~~~~~~~~~~~~~~~~~ which I feel is an improvement. Successfully bootstrapped®rtested on x86_64-pc-linux-gnu. Adds 6 new PASS results to gcc.sum OK for trunk in stage 3? gcc/c/ChangeLog: * c-parser.c (c_parser_postfix_expression): Use EXPR_LOC_OR_LOC to preserve range information for the primary expression in the call to c_parser_postfix_expression_after_primary. gcc/testsuite/ChangeLog: * gcc.dg/cast-function-1.c (bar): Update column numbers. * gcc.dg/diagnostic-range-bad-called-object.c: New test case. --- gcc/c/c-parser.c | 3 ++- gcc/testsuite/gcc.dg/cast-function-1.c | 8 ++++---- .../gcc.dg/diagnostic-range-bad-called-object.c | 24 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/diagnostic-range-bad-called-object.c diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c index 5c32f45..e149e19 100644 --- a/gcc/c/c-parser.c +++ b/gcc/c/c-parser.c @@ -7954,7 +7954,8 @@ c_parser_postfix_expression (c_parser *parser) expr.value = error_mark_node; break; } - return c_parser_postfix_expression_after_primary (parser, loc, expr); + return c_parser_postfix_expression_after_primary + (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr); } /* Parse a postfix expression after a parenthesized type name: the diff --git a/gcc/testsuite/gcc.dg/cast-function-1.c b/gcc/testsuite/gcc.dg/cast-function-1.c index ab42db1..5228b55 100644 --- a/gcc/testsuite/gcc.dg/cast-function-1.c +++ b/gcc/testsuite/gcc.dg/cast-function-1.c @@ -18,14 +18,14 @@ typedef struct { void bar(double d, int i, str_t s) { - d = ((double (*) (int)) foo1) (i); /* { dg-warning "7:non-compatible|abort" } */ - i = ((int (*) (double)) foo1) (d); /* { dg-warning "7:non-compatible|abort" } */ - s = ((str_t (*) (int)) foo1) (i); /* { dg-warning "7:non-compatible|abort" } */ + d = ((double (*) (int)) foo1) (i); /* { dg-warning "8:non-compatible|abort" } */ + i = ((int (*) (double)) foo1) (d); /* { dg-warning "8:non-compatible|abort" } */ + s = ((str_t (*) (int)) foo1) (i); /* { dg-warning "8:non-compatible|abort" } */ ((void (*) (int)) foo1) (d); /* { dg-warning "non-compatible|abort" } */ i = ((int (*) (int)) foo1) (i); /* { dg-bogus "non-compatible|abort" } */ (void) foo1 (i); /* { dg-bogus "non-compatible|abort" } */ - d = ((double (*) (int)) foo2) (i); /* { dg-warning "7:non-compatible|abort" } */ + d = ((double (*) (int)) foo2) (i); /* { dg-warning "8:non-compatible|abort" } */ i = ((int (*) (double)) foo2) (d); /* { dg-bogus "non-compatible|abort" } */ s = ((str_t (*) (int)) foo2) (i); /* { dg-warning "non-compatible|abort" } */ ((void (*) (int)) foo2) (d); /* { dg-warning "non-compatible|abort" } */ diff --git a/gcc/testsuite/gcc.dg/diagnostic-range-bad-called-object.c b/gcc/testsuite/gcc.dg/diagnostic-range-bad-called-object.c new file mode 100644 index 0000000..95fb3e9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/diagnostic-range-bad-called-object.c @@ -0,0 +1,24 @@ +/* { dg-options "-fdiagnostics-show-caret" } */ + +/* Adapted from https://gcc.gnu.org/wiki/ClangDiagnosticsComparison */ + +void call_of_non_function_ptr (char **argP, char **argQ) +{ + (argP - argQ)(); /* { dg-error "called object is not a function or function pointer" } */ + +/* { dg-begin-multiline-output "" } + (argP - argQ)(); + ~~~~~~^~~~~~~ + { dg-end-multiline-output "" } */ + + argP(); /* { dg-error "called object 'argP' is not a function or function pointer" } */ + +/* { dg-begin-multiline-output "" } + argP(); + ^~~~ + { dg-end-multiline-output "" } + { dg-begin-multiline-output "" } + void call_of_non_function_ptr (char **argP, char **argQ) + ^~~~ + { dg-end-multiline-output "" } */ +} -- 1.8.5.3