diff options
author | David Malcolm <dmalcolm@redhat.com> | 2017-04-24 19:12:52 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2017-04-24 19:12:52 +0000 |
commit | 412f61f0f6ee495f3dd076debd9db536aa386a26 (patch) | |
tree | c526d0dafa94cbbde49da875a81aa5371bb35d73 /gcc | |
parent | a8492d5ec1d8b90930a7dca04a104bcaec62d035 (diff) | |
download | gcc-412f61f0f6ee495f3dd076debd9db536aa386a26.zip gcc-412f61f0f6ee495f3dd076debd9db536aa386a26.tar.gz gcc-412f61f0f6ee495f3dd076debd9db536aa386a26.tar.bz2 |
Fix location of sizeof/alignof (PR c++/80016)
PR c++/80016 reports an issue with bizarre underlined range
for a complicated expression.
The root cause for the incorrect *starting* location of that range
is that alignof and sizeof expressions currently have
start == finish == caret at the opening parenthesis of the
expression.
This patch fixes this by generating appropriate start and finish
locations for alignof and sizeof expressions.
gcc/cp/ChangeLog:
PR c++/80016
* parser.c (cp_parser_unary_expression): Generate a location
range for alignof and sizeof expressions.
gcc/testsuite/ChangeLog:
PR c++/80016
* g++.dg/plugin/diagnostic-test-expressions-1.C (test_sizeof): New
test function.
(test_alignof): New test function.
From-SVN: r247108
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/cp/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/cp/parser.c | 19 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 7 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/plugin/diagnostic-test-expressions-1.C | 66 |
4 files changed, 94 insertions, 4 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 317415c..97bf27f 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2017-04-24 David Malcolm <dmalcolm@redhat.com> + + PR c++/80016 + * parser.c (cp_parser_unary_expression): Generate a location + range for alignof and sizeof expressions. + 2017-04-24 Volker Reichelt <v.reichelt@netcologne.de> * parser.c (cp_parser_cv_qualifier_seq_opt): Add fix-it info to diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 3efb28c..3a0e0cb 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -7805,12 +7805,11 @@ cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk, { tree operand, ret; enum tree_code op; - location_t first_loc; + location_t start_loc = token->location; op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR; /* Consume the token. */ cp_lexer_consume_token (parser->lexer); - first_loc = cp_lexer_peek_token (parser->lexer)->location; /* Parse the operand. */ operand = cp_parser_sizeof_operand (parser, keyword); @@ -7846,9 +7845,21 @@ cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk, TREE_SIDE_EFFECTS (ret) = 0; TREE_READONLY (ret) = 1; } - SET_EXPR_LOCATION (ret, first_loc); } - return ret; + + /* Construct a location e.g. : + alignof (expr) + ^~~~~~~~~~~~~~ + with start == caret at the start of the "alignof"/"sizeof" + token, with the endpoint at the final closing paren. */ + location_t finish_loc + = cp_lexer_previous_token (parser->lexer)->location; + location_t compound_loc + = make_location (start_loc, start_loc, finish_loc); + + cp_expr ret_expr (ret); + ret_expr.set_location (compound_loc); + return ret_expr; } case RID_NEW: diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index c8d79e3..a8381fa 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2017-04-24 David Malcolm <dmalcolm@redhat.com> + + PR c++/80016 + * g++.dg/plugin/diagnostic-test-expressions-1.C (test_sizeof): New + test function. + (test_alignof): New test function. + 2017-04-24 Marc Glisse <marc.glisse@inria.fr> * gcc.dg/tree-ssa/cmpexactdiv-2.c: New file. diff --git a/gcc/testsuite/g++.dg/plugin/diagnostic-test-expressions-1.C b/gcc/testsuite/g++.dg/plugin/diagnostic-test-expressions-1.C index 4d3b07c..2c004f3 100644 --- a/gcc/testsuite/g++.dg/plugin/diagnostic-test-expressions-1.C +++ b/gcc/testsuite/g++.dg/plugin/diagnostic-test-expressions-1.C @@ -101,6 +101,72 @@ int test_postfix_incdec (int i) /* Unary operators. ****************************************************/ +int test_sizeof (int i) +{ + __emit_expression_range (0, sizeof(int) + i); /* { dg-warning "range" } */ +/* { dg-begin-multiline-output "" } + __emit_expression_range (0, sizeof(int) + i); + ~~~~~~~~~~~~^~~ + { dg-end-multiline-output "" } */ + + __emit_expression_range (0, i + sizeof(int)); /* { dg-warning "range" } */ +/* { dg-begin-multiline-output "" } + __emit_expression_range (0, i + sizeof(int)); + ~~^~~~~~~~~~~~~ + { dg-end-multiline-output "" } */ + + __emit_expression_range (0, sizeof i + i); /* { dg-warning "range" } */ +/* { dg-begin-multiline-output "" } + __emit_expression_range (0, sizeof i + i); + ~~~~~~~~~^~~ + { dg-end-multiline-output "" } */ + + __emit_expression_range (0, i + sizeof i); /* { dg-warning "range" } */ +/* { dg-begin-multiline-output "" } + __emit_expression_range (0, i + sizeof i); + ~~^~~~~~~~~~ + { dg-end-multiline-output "" } */ +} + +int test_alignof (int i) +{ + __emit_expression_range (0, alignof(int) + i); /* { dg-warning "range" } */ +/* { dg-begin-multiline-output "" } + __emit_expression_range (0, alignof(int) + i); + ~~~~~~~~~~~~~^~~ + { dg-end-multiline-output "" } */ + + __emit_expression_range (0, i + alignof(int)); /* { dg-warning "range" } */ +/* { dg-begin-multiline-output "" } + __emit_expression_range (0, i + alignof(int)); + ~~^~~~~~~~~~~~~~ + { dg-end-multiline-output "" } */ + + __emit_expression_range (0, __alignof__(int) + i); /* { dg-warning "range" } */ +/* { dg-begin-multiline-output "" } + __emit_expression_range (0, __alignof__(int) + i); + ~~~~~~~~~~~~~~~~~^~~ + { dg-end-multiline-output "" } */ + + __emit_expression_range (0, i + __alignof__(int)); /* { dg-warning "range" } */ +/* { dg-begin-multiline-output "" } + __emit_expression_range (0, i + __alignof__(int)); + ~~^~~~~~~~~~~~~~~~~~ + { dg-end-multiline-output "" } */ + + __emit_expression_range (0, __alignof__ i + i); /* { dg-warning "range" } */ +/* { dg-begin-multiline-output "" } + __emit_expression_range (0, __alignof__ i + i); + ~~~~~~~~~~~~~~^~~ + { dg-end-multiline-output "" } */ + + __emit_expression_range (0, i + __alignof__ i); /* { dg-warning "range" } */ +/* { dg-begin-multiline-output "" } + __emit_expression_range (0, i + __alignof__ i); + ~~^~~~~~~~~~~~~~~ + { dg-end-multiline-output "" } */ +} + int test_prefix_incdec (int i) { __emit_expression_range (0, ++i ); /* { dg-warning "range" } */ |