Bug 43486 - Preserve variable-use locations
Summary: Preserve variable-use locations
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: David Malcolm
URL: https://gcc.gnu.org/ml/gcc-patches/20...
Keywords: diagnostic, patch
: 55173 66231 (view as bug list)
Depends on:
Blocks: 61534 69602 70730 77733 80036 71302 77777
  Show dependency treegraph
 
Reported: 2010-03-22 22:24 UTC by Taras Glek
Modified: 2024-04-04 18:25 UTC (History)
11 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-03-22 23:38:29


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Taras Glek 2010-03-22 22:24:45 UTC
Static analysis needs good location info in order to present useful errors. Unfortunately in GIMPLE when you have a statement like
func( x ); There is no way to get a location for x and often it's hard to get a location for func(ie if func takes form of some_namespace::func ( x )). 
In Mozilla, we work around this by walking the expression stack backwards and hope that an expression containing a reference to a variable has a location that's sufficiently close to the variable. Other C/C++ frontends indicate the coordinates of variable uses, not just declarations.
Comment 1 Andrew Pinski 2010-03-22 22:33:38 UTC
Which location do you want?   For function calls, it will be part of the call expression (or rather the call statement).  For variables, it is harder to keep track of that usage.  But most statements have a line/column info already.  
Comment 2 Taras Glek 2010-03-22 22:42:27 UTC
(In reply to comment #1)
> Which location do you want?   For function calls, it will be part of the call
> expression (or rather the call statement).  For variables, it is harder to keep
> track of that usage.  But most statements have a line/column info already.  
> 

Ideally I would want the beginning and ending of every expression in the AST, but I'd settle for ability to know the beginning of variables. For example
somenamespace::foo->call(
x,
y);

Right now there is no way to get locations for x, y or "call". CALL_EXPR just gives one a vague location, given that there can be a large number of other decls referenced within the CALL_EXPR.
Comment 3 Manuel López-Ibáñez 2010-03-22 23:38:28 UTC
I think we want this also for better diagnostics and in the long term for caret diagnostics. At a minimum, we should handle the case 

foo(
x, /* warning/error about x but location is that of either foo or the ';' */
y);

I am not sure how difficult is to implement this in the straightforward cases. More complex cases would be those affected by folding and constant arguments (which do not have locations).

A first step would be to propagate the correct locations when building expressions (adding location parameters to build_* functions and passing down the correct value).
Comment 4 Jason Merrill 2010-03-23 01:51:34 UTC
I suppose we could wrap rvalue uses in NOP_EXPR and lvalue uses in VIEW_CONVERT_EXPR.

Taras: You aren't actually trying to do this sort of analysis after lowering to GIMPLE, are you?
Comment 5 Taras Glek 2010-03-23 02:07:16 UTC
(In reply to comment #4)
> I suppose we could wrap rvalue uses in NOP_EXPR and lvalue uses in
> VIEW_CONVERT_EXPR.
> 
> Taras: You aren't actually trying to do this sort of analysis after lowering to
> GIMPLE, are you?

Ideally this would work in gimple too, but just having this in C++ fe would be enough.
Comment 6 Manuel López-Ibáñez 2012-10-14 18:28:32 UTC
I think there is a consensus that g++ wants this somehow. Recent work:

http://gcc.gnu.org/ml/gcc-patches/2012-09/msg01222.html
Comment 7 Paolo Carlini 2012-11-01 23:06:54 UTC
*** Bug 55173 has been marked as a duplicate of this bug. ***
Comment 8 Matthew Woehlke 2015-03-30 14:55:30 UTC
Can this *please* get fixed? This really hurts the ability to use -Wzero-as-null-ptr in particular. See https://bugreports.qt.io/browse/QTBUG-45291 for an example of the pain this causes.
Comment 9 Paolo Carlini 2015-05-21 22:05:06 UTC
*** Bug 66231 has been marked as a duplicate of this bug. ***
Comment 10 Manuel López-Ibáñez 2015-05-21 23:14:10 UTC
(In reply to Matthew Woehlke from comment #8)
> Can this *please* get fixed? This really hurts the ability to use
> -Wzero-as-null-ptr in particular. See
> https://bugreports.qt.io/browse/QTBUG-45291 for an example of the pain this
> causes.

Fixing this is hard. There are several ideas floating around but all of them have their disadvantages (https://gcc.gnu.org/ml/gcc-patches/2012-09/msg01373.html and https://gcc.gnu.org/ml/gcc-patches/2012-11/msg00164.html). Unless some new brilliant contributor appears and starts passionately working on this, this is not going to be fixed in the near (or medium) term. If you know such a person, please let us know.
Comment 11 David Malcolm 2018-01-10 19:41:27 UTC
Author: dmalcolm
Date: Wed Jan 10 19:40:55 2018
New Revision: 256448

URL: https://gcc.gnu.org/viewcvs?rev=256448&root=gcc&view=rev
Log:
Preserving locations for variable-uses and constants (PR c++/43486)

This patch implements location wrapper nodes, preserving source locations
of the uses of variables and constants in various places in the
C++ frontend: at the arguments at callsites, and for typeid, alignof,
sizeof, and offsetof.

For example, it allows the C++ FE to underline the pertinent argument
for mismatching calls, for such expressions, improving:

extern int callee (int one, const char *two, float three);

int caller (int first, int second, float third)
{
  return callee (first, second, third);
}

from

test.cc: In function 'int caller(int, int, float)':
test.cc:5:38: error: invalid conversion from 'int' to 'const char*' [-fpermissive]
   return callee (first, second, third);
                                      ^
test.cc:1:41: note:   initializing argument 2 of 'int callee(int, const char*, float)'
 extern int callee (int one, const char *two, float three);
                             ~~~~~~~~~~~~^~~

to:

test.cc: In function 'int caller(int, int, float)':
test.cc:5:25: error: invalid conversion from 'int' to 'const char*' [-fpermissive]
   return callee (first, second, third);
                         ^~~~~~
test.cc:1:41: note:   initializing argument 2 of 'int callee(int, const char*, float)'
 extern int callee (int one, const char *two, float three);
                             ~~~~~~~~~~~~^~~

This is the combination of the following patches:

  "[PATCH 01/14] C++: preserve locations within build_address"
     https://gcc.gnu.org/ml/gcc-patches/2017-11/msg00883.html

  "[PATCH v2.4 of 02/14] Support for adding and stripping location_t wrapper nodes"
    https://gcc.gnu.org/ml/gcc-patches/2018-01/msg00591.html

  "[PATCH] Eliminate location wrappers in tree_nop_conversion/STRIP_NOPS"
    https://gcc.gnu.org/ml/gcc-patches/2017-12/msg01330.html

  "[PATCH v4 of 03/14] C++: add location_t wrapper nodes during parsing (minimal impl)"
    https://gcc.gnu.org/ml/gcc-patches/2018-01/msg00660.html

  "[PATCH 04/14] Update testsuite to show improvements"
    https://gcc.gnu.org/ml/gcc-patches/2017-11/msg00891.html

  "[v3 of 05/14] C++: handle locations wrappers when calling warn_for_memset"
    https://gcc.gnu.org/ml/gcc-patches/2017-12/msg01378.html

  "[PATCH 07/14] reject_gcc_builtin: strip any location wrappers"
    https://gcc.gnu.org/ml/gcc-patches/2017-11/msg00886.html

  "[v3 of PATCH 08/14] cp/tree.c: strip location wrappers in lvalue_kind"
    https://gcc.gnu.org/ml/gcc-patches/2017-12/msg01433.html

  "[PATCH 09/14] Strip location wrappers in null_ptr_cst_p"
    https://gcc.gnu.org/ml/gcc-patches/2017-11/msg00888.html

  "[PATCH 11/14] Handle location wrappers in string_conv_p"
    https://gcc.gnu.org/ml/gcc-patches/2017-11/msg00890.html

  "[PATCH 12/14] C++: introduce null_node_p"
    https://gcc.gnu.org/ml/gcc-patches/2017-11/msg00894.html

  "[v3 of PATCH 13/14] c-format.c: handle location wrappers"
    https://gcc.gnu.org/ml/gcc-patches/2017-12/msg01494.html

  "[PATCH 14/14] pp_c_cast_expression: don't print casts for location wrappers"
    https://gcc.gnu.org/ml/gcc-patches/2017-11/msg00893.html

  "[v3 of PATCH 15/14] Use fold_for_warn in get_atomic_generic_size"
    https://gcc.gnu.org/ml/gcc-patches/2017-12/msg01380.html

  "[PATCH] Add selftest for "fold_for_warn (error_mark_node)""
    https://gcc.gnu.org/ml/gcc-patches/2017-12/msg01385.html

gcc/c-family/ChangeLog:
	PR c++/43486
	* c-common.c: Include "selftest.h".
	(get_atomic_generic_size): Perform the test for integral type
	before the range test for any integer constant, fixing indentation
	of braces.  Call fold_for_warn before testing for an INTEGER_CST.
	(reject_gcc_builtin): Strip any location wrapper from EXPR.
	(selftest::test_fold_for_warn): New function.
	(selftest::c_common_c_tests): New function.
	(selftest::c_family_tests): Call it, and
	selftest::c_pretty_print_c_tests.
	* c-common.h (selftest::c_pretty_print_c_tests): New decl.
	* c-format.c (check_format_arg): Convert VAR_P check to a
	fold_for_warn.
	* c-pretty-print.c: Include "selftest.h".
	(pp_c_cast_expression): Don't print casts for location wrappers.
	(selftest::assert_c_pretty_printer_output): New function.
	(ASSERT_C_PRETTY_PRINTER_OUTPUT): New macro.
	(selftest::test_location_wrappers): New function.
	(selftest::c_pretty_print_c_tests): New function.
	* c-warn.c (warn_for_memset): Call fold_for_warn on the arguments.

gcc/cp/ChangeLog:
	PR c++/43486
	* call.c (null_ptr_cst_p): Strip location wrappers when
	converting from '0' to a pointer type in C++11 onwards.
	(conversion_null_warnings): Replace comparison with null_node with
	call to null_node_p.
	(build_over_call): Likewise.
	* cp-gimplify.c (cp_fold): Remove the early bailout when
	processing_template_decl.
	* cp-lang.c (selftest::run_cp_tests): Call
	selftest::cp_pt_c_tests and selftest::cp_tree_c_tests.
	* cp-tree.h (cp_expr::maybe_add_location_wrapper): New method.
	(selftest::run_cp_tests): Move decl to bottom of file.
	(null_node_p): New inline function.
	(selftest::cp_pt_c_tests): New decl.
	(selftest::cp_tree_c_tests): New decl.
	* cvt.c (build_expr_type_conversion): Replace comparison with
	null_node with call to null_node_p.
	* error.c (args_to_string): Likewise.
	* except.c (build_throw): Likewise.
	* mangle.c (write_expression): Skip location wrapper nodes.
	* parser.c (literal_integer_zerop): New function.
	(cp_parser_postfix_expression): Call maybe_add_location_wrapper on
	the result for RID_TYPEID. Pass true for new "wrap_locations_p"
	param of cp_parser_parenthesized_expression_list.  When calling
	warn_for_memset, replace integer_zerop calls with
	literal_integer_zerop, eliminating the double logical negation
	cast to bool.  Eliminate the special-casing for CONST_DECL in
	favor of the fold_for_warn within warn_for_memset.
	(cp_parser_parenthesized_expression_list): Add "wrap_locations_p"
	param, defaulting to false.  Convert "expr" to a cp_expr, and call
	maybe_add_location_wrapper on it when wrap_locations_p is true.
	(cp_parser_unary_expression): Call maybe_add_location_wrapper on
	the result for RID_ALIGNOF and RID_SIZEOF.
	(cp_parser_builtin_offsetof): Likewise.
	* pt.c: Include "selftest.h".
	(tsubst_copy): Handle location wrappers.
	(tsubst_copy_and_build): Likewise.
	(build_non_dependent_expr): Likewise.
	(selftest::test_build_non_dependent_expr): New function.
	(selftest::cp_pt_c_tests): New function.
	* tree.c: Include "selftest.h".
	(lvalue_kind): Handle VIEW_CONVERT_EXPR location wrapper nodes.
	(selftest::test_lvalue_kind): New function.
	(selftest::cp_tree_c_tests): New function.
	* typeck.c (string_conv_p): Strip any location wrapper from "exp".
	(cp_build_binary_op): Replace comparison with null_node with call
	to null_node_p.
	(build_address): Use location of operand when building address
	expression.

gcc/testsuite/ChangeLog:
	PR c++/43486
	* g++.dg/diagnostic/param-type-mismatch.C: Update expected results
	to reflect that the arguments are correctly underlined.
	* g++.dg/plugin/diagnostic-test-expressions-1.C: Add test coverage
	for globals, params, locals and literals.
	(test_sizeof): Directly test the location of "sizeof", rather than
	when used in compound expressions.
	(test_alignof): Likewise for "alignof".
	(test_string_literals): Likewise for string literals.
	(test_numeric_literals): Likewise for numeric literals.
	(test_builtin_offsetof): Likewise for "__builtin_offsetof".
	(test_typeid): Likewise for typeid.
	(test_unary_plus): New.
	* g++.dg/warn/Wformat-1.C: Add tests of pointer arithmetic on
	format strings.

gcc/ChangeLog:
	PR c++/43486
	* tree-core.h: Document EXPR_LOCATION_WRAPPER_P's usage of
	"public_flag".
	* tree.c (tree_nop_conversion): Return true for location wrapper
	nodes.
	(maybe_wrap_with_location): New function.
	(selftest::check_strip_nops): New function.
	(selftest::test_location_wrappers): New function.
	(selftest::tree_c_tests): Call it.
	* tree.h (STRIP_ANY_LOCATION_WRAPPER): New macro.
	(maybe_wrap_with_location): New decl.
	(EXPR_LOCATION_WRAPPER_P): New macro.
	(location_wrapper_p): New inline function.
	(tree_strip_any_location_wrapper): New inline function.


Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-family/ChangeLog
    trunk/gcc/c-family/c-common.c
    trunk/gcc/c-family/c-common.h
    trunk/gcc/c-family/c-format.c
    trunk/gcc/c-family/c-pretty-print.c
    trunk/gcc/c-family/c-warn.c
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/call.c
    trunk/gcc/cp/cp-gimplify.c
    trunk/gcc/cp/cp-lang.c
    trunk/gcc/cp/cp-tree.h
    trunk/gcc/cp/cvt.c
    trunk/gcc/cp/error.c
    trunk/gcc/cp/except.c
    trunk/gcc/cp/mangle.c
    trunk/gcc/cp/parser.c
    trunk/gcc/cp/pt.c
    trunk/gcc/cp/tree.c
    trunk/gcc/cp/typeck.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/g++.dg/diagnostic/param-type-mismatch.C
    trunk/gcc/testsuite/g++.dg/plugin/diagnostic-test-expressions-1.C
    trunk/gcc/testsuite/g++.dg/warn/Wformat-1.C
    trunk/gcc/tree-core.h
    trunk/gcc/tree.c
    trunk/gcc/tree.h
Comment 12 David Malcolm 2018-01-10 19:58:24 UTC
(In reply to David Malcolm from comment #11)
> Author: dmalcolm
> Date: Wed Jan 10 19:40:55 2018
> New Revision: 256448
> 
> URL: https://gcc.gnu.org/viewcvs?rev=256448&root=gcc&view=rev
> Log:
> Preserving locations for variable-uses and constants (PR c++/43486)

This patch is a step towards fixing this bug, but we're not there yet:

The patch adds machinery for adding location wrapper nodes for those expressions that don't have them, and adds such wrappers to the C++ FE for callsite arguments and at a few other places. This is a minimal approach, which improves various diagnostics in the C++ FE, but in theory we should add wrappers throughout the C++ FE for *all* uses-of-decls and uses-of-constants.  See:
  https://gcc.gnu.org/ml/gcc-patches/2017-11/msg01588.html
for an earlier work-in-progress attempt at this (probably bit-rotted by now).

Similarly, the patch doesn't add any wrapper nodes to other frontends, in particular, the C FE.

Finally, I believe, all of these wrappers are discarded at some point at or before gimplification, so it doesn't help the middle-end yet; we'd need some way to retain the location information there.
Comment 13 David Malcolm 2018-01-11 19:39:23 UTC
Author: dmalcolm
Date: Thu Jan 11 19:38:52 2018
New Revision: 256552

URL: https://gcc.gnu.org/viewcvs?rev=256552&root=gcc&view=rev
Log:
Add some reproducers for issues found developing the location-wrappers patch

gcc/testsuite/ChangeLog:
	PR c++/43486
	* g++.dg/wrappers: New subdirectory.
	* g++.dg/wrappers/README: New file.
	* g++.dg/wrappers/alloc.C: New test case.
	* g++.dg/wrappers/cow-istream-string.C: New test case.
	* g++.dg/wrappers/cp-stdlib.C: New test case.
	* g++.dg/wrappers/sanitizer_coverage_libcdep_new.C: New test case.
	* g++.dg/wrappers/wrapper-around-type-pack-expansion.C: New test
	case.


Added:
    trunk/gcc/testsuite/g++.dg/wrappers/
    trunk/gcc/testsuite/g++.dg/wrappers/README
    trunk/gcc/testsuite/g++.dg/wrappers/alloc.C
    trunk/gcc/testsuite/g++.dg/wrappers/cow-istream-string.C
    trunk/gcc/testsuite/g++.dg/wrappers/cp-stdlib.C
    trunk/gcc/testsuite/g++.dg/wrappers/sanitizer_coverage_libcdep_new.C
    trunk/gcc/testsuite/g++.dg/wrappers/wrapper-around-type-pack-expansion.C
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 14 David Malcolm 2018-11-05 19:50:30 UTC
Candidate followup patch:
  https://gcc.gnu.org/ml/gcc-patches/2018-11/msg00304.html
Comment 15 David Malcolm 2018-12-19 15:08:53 UTC
Author: dmalcolm
Date: Wed Dec 19 15:08:21 2018
New Revision: 267272

URL: https://gcc.gnu.org/viewcvs?rev=267272&root=gcc&view=rev
Log:
C++: more location wrapper nodes (PR c++/43064, PR c++/43486)

This is v6 of the patch, as posted to:
  https://gcc.gnu.org/ml/gcc-patches/2018-12/msg01331.html


The C++ frontend gained various location wrapper nodes in r256448 (GCC 8).
That patch:
  https://gcc.gnu.org/ml/gcc-patches/2018-01/msg00799.html
added wrapper nodes around all nodes with !CAN_HAVE_LOCATION_P for:

* arguments at callsites, and for

* typeid, alignof, sizeof, and offsetof.

This is a followup to that patch, adding many more location wrappers
to the C++ frontend.  It adds location wrappers for nodes with
!CAN_HAVE_LOCATION_P to:

* all literal nodes (in cp_parser_primary_expression)

* all id-expression nodes (in finish_id_expression), except within a
  decltype.

* all mem-initializer nodes within a mem-initializer-list
  (in cp_parser_mem_initializer)

However, the patch also adds some suppressions: regions in the parser
for which wrapper nodes will not be created:

* within a template-parameter-list or template-argument-list (in
  cp_parser_template_parameter_list and cp_parser_template_argument_list
  respectively), to avoid encoding the spelling location of the nodes
  in types.  For example, "array<10>" and "array<10>" are the same type,
  despite the fact that the two different "10" tokens are spelled in
  different locations in the source.

* within a gnu-style attribute (none of are handlers are set up to cope
  with location wrappers yet)

* within various OpenMP clauses

The patch enables various improvements to locations for bad
initializations, for -Wchar-subscripts, and enables various other
improvements in the followup patch.

For example, given the followup buggy mem-initializer:

class X {
  X() : bad(42),
        good(42)
  { }
  void* bad;
  int good;
};

previously, our diagnostic was on the final close parenthesis of the
mem-initializer-list, leaving it unclear where the problem is:

t.cc: In constructor 'X::X()':
t.cc:3:16: error: invalid conversion from 'int' to 'void*' [-fpermissive]
    3 |         good(42)
      |                ^
      |                |
      |                int

whereas with the patch we highlight which expression is bogus:

t.cc: In constructor 'X::X()':
t.cc:2:13: error: invalid conversion from 'int' to 'void*' [-fpermissive]
    2 |   X() : bad(42),
      |             ^~
      |             |
      |             int

Similarly, the diagnostic for this bogus initialization:

i.cc:1:44: error: initializer-string for array of chars is too long [-fpermissive]
    1 | char test[3][4] = { "ok", "too long", "ok" };
      |                                            ^

is improved by the patch so that it indicates which string is too long:

i.cc:1:27: error: initializer-string for array of chars is too long [-fpermissive]
    1 | char test[3][4] = { "ok", "too long", "ok" };
      |                           ^~~~~~~~~~


gcc/c-family/ChangeLog:
	PR c++/43064
	PR c++/43486
	* c-common.c (unsafe_conversion_p): Fold any location wrapper.
	(verify_tree): Handle location wrappers.
	(c_common_truthvalue_conversion): Strip any location wrapper.
	Handle CONST_DECL.
	(fold_offsetof): Strip any location wrapper.
	(complete_array_type): Likewise for initial_value.
	(convert_vector_to_array_for_subscript): Call fold_for_warn on the
	index before checking for INTEGER_CST.
	* c-pretty-print.c (c_pretty_printer::primary_expression): Don't
	print parentheses around location wrappers.
	* c-warn.c (warn_logical_operator): Call fold_for_warn on op_right
	before checking for INTEGER_CST.
	(warn_tautological_bitwise_comparison): Call
	tree_strip_any_location_wrapper on lhs, rhs, and bitop's operand
	before checking for INTEGER_CST.
	(readonly_error): Strip any location wrapper.
	(warn_array_subscript_with_type_char): Strip location wrappers
	before checking for INTEGER_CST.  Use the location of the index if
	available.

gcc/ChangeLog:
	PR c++/43064
	PR c++/43486
	* convert.c: Include "selftest.h".
	(preserve_any_location_wrapper): New function.
	(convert_to_pointer_maybe_fold): Update to handle location
	wrappers.
	(convert_to_real_maybe_fold): Likewise.
	(convert_to_integer_1): Strip expr when using TREE_OVERFLOW.
	Handle location wrappers when checking for INTEGER_CST.
	(convert_to_integer_maybe_fold): Update to handle location
	wrappers.
	(convert_to_complex_maybe_fold): Likewise.
	(selftest::test_convert_to_integer_maybe_fold): New functions.
	(selftest::convert_c_tests): New function.
	* convert.h (preserve_any_location_wrapper): New decl.
	* fold-const.c (size_binop_loc): Strip location wrappers when
	using TREE_OVERFLOW.
	(operand_equal_p): Strip any location wrappers.
	(integer_valued_real_p): Strip any location wrapper.
	* selftest-run-tests.c (selftest::run_tests): Call
	selftest::convert_c_tests.
	* selftest.h (selftest::convert_c_tests): New decl.
	* tree.c (build_complex): Assert that REAL and IMAG are constants.
	(integer_zerop): Look through location wrappers.
	(integer_onep): Likewise.
	(integer_each_onep): Likewise.
	(integer_all_onesp): Likewise.
	(integer_minus_onep): Likewise.
	(integer_pow2p): Likewise.
	(integer_nonzerop): Likewise.
	(integer_truep): Likewise.
	(fixed_zerop): Likewise.
	(real_zerop): Likewise.
	(real_onep): Likewise.
	(real_minus_onep): Likewise.
	(tree_int_cst_equal): Likewise.
	(simple_cst_equal): Treat location wrappers with non-equal source
	locations as being unequal.
	(uniform_integer_cst_p): Look through location wrappers.
	(maybe_wrap_with_location): Don't create wrappers if any
	auto_suppress_location_wrappers are active.
	(suppress_location_wrappers): New variable.
	(selftest::test_predicates): New test.
	(selftest::tree_c_tests): Call it.
	* tree.h (CONSTANT_CLASS_OR_WRAPPER_P): New macro.
	(suppress_location_wrappers): New decl.
	(class auto_suppress_location_wrappers): New class.

gcc/cp/ChangeLog:
	PR c++/43064
	PR c++/43486
	* call.c (build_conditional_expr_1): Strip location wrappers when
	checking for CONST_DECL.
	(conversion_null_warnings): Use location of "expr" if available.
	* class.c (fixed_type_or_null): Handle location wrappers.
	* constexpr.c (potential_constant_expression_1): Likewise.
	* cvt.c (ignore_overflows): Strip location wrappers when
	checking for INTEGER_CST, and re-wrap the result if present.
	(ocp_convert): Call fold_for_warn before checking for INTEGER_CST.
	* decl.c (reshape_init_r): Strip any location wrapper.
	(undeduced_auto_decl): Likewise.
	* expr.c (mark_discarded_use): Likewise for expr.
	* init.c (build_aggr_init): Likewise before checking init for
	DECL_P.
	(warn_placement_new_too_small): Call fold_for_warn on adj before
	checking for CONSTANT_CLASS_P, and on nelts.  Strip any location
	wrapper from op0 and on oper before checking for VAR_P.
	* parser.c (cp_parser_primary_expression): Call
	maybe_add_location_wrapper on numeric and string literals.
	(cp_parser_postfix_expression): Strip any location wrapper when
	checking for DECL_IS_BUILTIN_CONSTANT_P.
	(cp_parser_unary_expression): Ensure that folding of NEGATE_EXPR
	around a constant happens in the presence of location wrappers and
	returns a wrapped result.
	(cp_parser_has_attribute_expression): Strip any location wrapper
	from "oper".
	(cp_parser_binary_expression): Strip any location wrapper when
	checking for DECL_P on the lhs.
	(cp_parser_decltype): Strip any location wrapper from result of
	cp_parser_decltype_expr.
	(cp_parser_mem_initializer): Add location wrappers to the
	parenthesized expression list.
	(cp_parser_template_parameter_list): Don't create wrapper nodes
	within a template-parameter-list.
	(cp_parser_template_argument_list): Don't create wrapper nodes
	within a template-argument-list.
	(cp_parser_parameter_declaration): Strip location wrappers from
	default arguments.
	(cp_parser_gnu_attribute_list): Don't create wrapper nodes.
	(cp_parser_std_attribute_spec_seq): Likewise.
	(cp_parser_omp_all_clauses): Don't create wrapper nodes within
	OpenMP clauses.
	(cp_parser_omp_for_loop): Likewise.
	(cp_parser_omp_declare_reduction_exprs): Likewise.
	* pt.c (convert_nontype_argument_function): Strip location
	wrappers from fn_no_ptr before checking for FUNCTION_DECL.
	(tsubst_default_argument): Move note about which callsite led to
	instantiation to after the check_default_argument call.
	(do_auto_deduction): Likewise from init before checking for
	DECL_P.
	* semantics.c (force_paren_expr): Likewise from expr before
	checking for DECL_P.
	(finish_parenthesized_expr): Likewise from expr before
	checking for STRING_CST.
	(perform_koenig_lookup): Likewise from fn.
	(finish_call_expr): Likewise.
	(finish_id_expression): Rename to...
	(finish_id_expression_1): ...this, calling
	maybe_add_location_wrapper on the result.
	(capture_decltype): Use lookup_name_real rather than value_member
	when looking up decl within the capture-list.
	* tree.c (cp_stabilize_reference): Strip any location wrapper.
	(builtin_valid_in_constant_expr_p): Likewise.
	(strip_typedefs_expr): Strip any location wrapper before checking
	for decls or constants.
	(is_overloaded_fn): Likewise.
	(maybe_get_fns): Likewise.
	(selftest::test_lvalue_kind): Verify lvalue_p.
	* typeck.c (cxx_sizeof_expr): Strip any location wrapper.
	(cxx_alignof_expr): Likewise.
	(is_bitfield_expr_with_lowered_type): Handle location wrappers.
	(cp_build_array_ref): Call maybe_constant_value on "idx".
	(cp_build_binary_op): Strip location wrapper from first_arg before
	checking for PARM_DECL.  Likewise for op1 before checking for
	INTEGER_CST in two places.  Likewise for orig_op0 and orig_op1
	when checking for STRING_CST.
	(cp_build_addr_expr_1): Likewise for arg when checking for
	FUNCTION_DECL.
	(cp_build_modify_expr): Likewise for newrhs when checking for
	STRING_CST.
	(convert_for_assignment): Don't strip location wrappers when
	stripping NON_LVALUE_EXPR.
	(maybe_warn_about_returning_address_of_local): Strip location
	wrapper from whats_returned before checking for DECL_P.
	(can_do_nrvo_p): Strip location wrapper from retval.
	(treat_lvalue_as_rvalue_p): Likewise.
	(check_return_expr): Likewise.
	* typeck2.c (cxx_incomplete_type_diagnostic): Strip location
	wrapper from value before checking for VAR_P or PARM_DECL.
	(digest_init_r): Strip location wrapper from init.  When
	copying "init", also copy the wrapped node.

gcc/objc/ChangeLog:
	PR c++/43064
	PR c++/43486
	* objc-act.c (objc_maybe_build_component_ref): Strip any location
	wrapper before checking for UOBJC_SUPER_decl and self_decl.
	(objc_finish_message_expr): Strip any location wrapper.
	(gen_declaration): Strip location wrappers from "w".

gcc/testsuite/ChangeLog:
	PR c++/43064
	PR c++/43486
	* c-c++-common/pr51712.c (valid2): Mark xfail as passing on C++.
	* g++.dg/cpp0x/constexpr-47969.C: Update column of expected error.
	* g++.dg/cpp0x/constexpr-ex2.C: Likewise.
	* g++.dg/cpp0x/scoped_enum2.C: Likewise.
	* g++.dg/cpp1z/decomp48.C: Update expected location of warning
	for named local variables to use that of the local variable.
	* g++.dg/ext/vla1.C: Update column.
	* g++.dg/init/array43.C: Update expected column to be that of the
	initializer.
	* g++.dg/init/initializer-string-too-long.C: New test.
	* g++.dg/init/new44.C: Add "-ftrack-macro-expansion=0".
	* g++.dg/init/pr43064-1.C: New test.
	* g++.dg/init/pr43064-2.C: New test.
	* g++.dg/init/pr43064-3.C: New test.
	* g++.dg/other/fold1.C: Update column of expected error.
	* g++.dg/parse/crash36.C: Likewise.
	* g++.dg/plugin/diagnostic-test-expressions-1.C: Add negative
	integer and float expressions.
	* g++.dg/template/defarg6.C: Move expected error to the default
	argument; add expected message about where instantiated.
	* g++.dg/wrappers/Wparentheses.C: New test.
	* g++.old-deja/g++.bugs/900402_02.C: Update column of expected
	error.


Added:
    trunk/gcc/testsuite/g++.dg/init/initializer-string-too-long.C
    trunk/gcc/testsuite/g++.dg/init/pr43064-1.C
    trunk/gcc/testsuite/g++.dg/init/pr43064-2.C
    trunk/gcc/testsuite/g++.dg/init/pr43064-3.C
    trunk/gcc/testsuite/g++.dg/wrappers/Wparentheses.C
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-family/ChangeLog
    trunk/gcc/c-family/c-common.c
    trunk/gcc/c-family/c-pretty-print.c
    trunk/gcc/c-family/c-warn.c
    trunk/gcc/convert.c
    trunk/gcc/convert.h
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/call.c
    trunk/gcc/cp/class.c
    trunk/gcc/cp/constexpr.c
    trunk/gcc/cp/cvt.c
    trunk/gcc/cp/decl.c
    trunk/gcc/cp/expr.c
    trunk/gcc/cp/init.c
    trunk/gcc/cp/parser.c
    trunk/gcc/cp/pt.c
    trunk/gcc/cp/semantics.c
    trunk/gcc/cp/tree.c
    trunk/gcc/cp/typeck.c
    trunk/gcc/cp/typeck2.c
    trunk/gcc/fold-const.c
    trunk/gcc/objc/ChangeLog
    trunk/gcc/objc/objc-act.c
    trunk/gcc/selftest-run-tests.c
    trunk/gcc/selftest.h
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/c-c++-common/pr51712.c
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-47969.C
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-ex2.C
    trunk/gcc/testsuite/g++.dg/cpp0x/scoped_enum2.C
    trunk/gcc/testsuite/g++.dg/cpp1z/decomp48.C
    trunk/gcc/testsuite/g++.dg/ext/vla1.C
    trunk/gcc/testsuite/g++.dg/init/array43.C
    trunk/gcc/testsuite/g++.dg/init/new44.C
    trunk/gcc/testsuite/g++.dg/other/fold1.C
    trunk/gcc/testsuite/g++.dg/parse/crash36.C
    trunk/gcc/testsuite/g++.dg/plugin/diagnostic-test-expressions-1.C
    trunk/gcc/testsuite/g++.dg/template/defarg6.C
    trunk/gcc/testsuite/g++.old-deja/g++.bugs/900402_02.C
    trunk/gcc/tree.c
    trunk/gcc/tree.h
Comment 16 David Malcolm 2018-12-19 15:27:09 UTC
Should be greatly improved for C++ for gcc 9 by r267272.
Comment 17 Eric Gallager 2018-12-20 03:48:53 UTC
(In reply to David Malcolm from comment #16)
> Should be greatly improved for C++ for gcc 9 by r267272.

...but not fixed?
Comment 18 GCC Commits 2020-11-26 09:05:15 UTC
The master branch has been updated by Thomas Schwinge <tschwinge@gcc.gnu.org>:

https://gcc.gnu.org/g:c0c7270cc4efd896fe99f8ad5409dbef089a407f

commit r11-5430-gc0c7270cc4efd896fe99f8ad5409dbef089a407f
Author: Thomas Schwinge <thomas@codesourcery.com>
Date:   Wed Nov 25 20:36:55 2020 +0100

    Don't create location wrapper nodes within OpenACC clauses
    
    This fixes a GCC 11, 10, 9 regression introduced by commit
    dfd7fdca2ac17d8b823a16700525824ca312ade0 (Subversion r267272) "C++: more
    location wrapper nodes (PR c++/43064, PR c++/43486)".  But: this isn't
    intending to blame David, because back then, the problem hasn't been visible in
    the testsuite (or else I'm sure would've been addressed right away) because of
    our all dear friend: missing testsuite coverage.  Thus, for GCC 8, I'm likewise
    enhancing the testsuite, without the C++ front end code changes.
    
    I actually had presumed that there may be an issue for OpenACC:
    <http://mid.mail-archive.com/874lb9qr2u.fsf@euler.schwinge.homeip.net>, so here
    we are, two years (and many "wasted" hours...) later...
    
            gcc/cp/
            * parser.c (cp_parser_omp_var_list_no_open): Assert that array
            section's 'low_bound', 'length' are not location wrapper nodes.
            (cp_parser_oacc_all_clauses, cp_parser_oacc_cache): Instantiate
            'auto_suppress_location_wrappers'.
            gcc/testsuite/
            * c-c++-common/goacc/cache-3-1.c: New.
            * c-c++-common/goacc/cache-3-2.c: Likewise.
            * c-c++-common/goacc/data-clause-1.c: Likewise.
            * c-c++-common/goacc/data-clause-2.c: Likewise.
            * c-c++-common/gomp/map-1.c: Adjust.
            * c-c++-common/gomp/map-2.c: Likewise.
            * g++.dg/goacc/cache-3-1.C: New.
            * g++.dg/goacc/cache-3-2.C: Likewise.
            * g++.dg/goacc/data-clause-1.C: Likewise.
            * g++.dg/goacc/data-clause-2.C: Likewise.
            * g++.dg/gomp/map-1.C: Adjust.
            * g++.dg/gomp/map-2.C: Likewise.
    
    Reported-by: Sandra Loosemore <sandra@codesourcery.com>
Comment 19 GCC Commits 2020-11-26 09:07:00 UTC
The releases/gcc-10 branch has been updated by Thomas Schwinge <tschwinge@gcc.gnu.org>:

https://gcc.gnu.org/g:e8e0357d129187b24085ce52172c87dbf6c2ecae

commit r10-9086-ge8e0357d129187b24085ce52172c87dbf6c2ecae
Author: Thomas Schwinge <thomas@codesourcery.com>
Date:   Wed Nov 25 20:36:55 2020 +0100

    Don't create location wrapper nodes within OpenACC clauses
    
    This fixes a GCC 11, 10, 9 regression introduced by commit
    dfd7fdca2ac17d8b823a16700525824ca312ade0 (Subversion r267272) "C++: more
    location wrapper nodes (PR c++/43064, PR c++/43486)".  But: this isn't
    intending to blame David, because back then, the problem hasn't been visible in
    the testsuite (or else I'm sure would've been addressed right away) because of
    our all dear friend: missing testsuite coverage.  Thus, for GCC 8, I'm likewise
    enhancing the testsuite, without the C++ front end code changes.
    
    I actually had presumed that there may be an issue for OpenACC:
    <http://mid.mail-archive.com/874lb9qr2u.fsf@euler.schwinge.homeip.net>, so here
    we are, two years (and many "wasted" hours...) later...
    
            gcc/cp/
            * parser.c (cp_parser_omp_var_list_no_open): Assert that array
            section's 'low_bound', 'length' are not location wrapper nodes.
            (cp_parser_oacc_all_clauses, cp_parser_oacc_cache): Instantiate
            'auto_suppress_location_wrappers'.
            gcc/testsuite/
            * c-c++-common/goacc/cache-3-1.c: New.
            * c-c++-common/goacc/cache-3-2.c: Likewise.
            * c-c++-common/goacc/data-clause-1.c: Likewise.
            * c-c++-common/goacc/data-clause-2.c: Likewise.
            * c-c++-common/gomp/map-1.c: Adjust.
            * c-c++-common/gomp/map-2.c: Likewise.
            * g++.dg/goacc/cache-3-1.C: New.
            * g++.dg/goacc/cache-3-2.C: Likewise.
            * g++.dg/goacc/data-clause-1.C: Likewise.
            * g++.dg/goacc/data-clause-2.C: Likewise.
            * g++.dg/gomp/map-1.C: Adjust.
            * g++.dg/gomp/map-2.C: Likewise.
    
    Reported-by: Sandra Loosemore <sandra@codesourcery.com>
    (cherry picked from commit c0c7270cc4efd896fe99f8ad5409dbef089a407f)
Comment 20 GCC Commits 2020-11-26 09:07:35 UTC
The releases/gcc-9 branch has been updated by Thomas Schwinge <tschwinge@gcc.gnu.org>:

https://gcc.gnu.org/g:25b61f935a8eca56c68c8587fc8915797250bb30

commit r9-9073-g25b61f935a8eca56c68c8587fc8915797250bb30
Author: Thomas Schwinge <thomas@codesourcery.com>
Date:   Wed Nov 25 20:36:55 2020 +0100

    Don't create location wrapper nodes within OpenACC clauses
    
    This fixes a GCC 11, 10, 9 regression introduced by commit
    dfd7fdca2ac17d8b823a16700525824ca312ade0 (Subversion r267272) "C++: more
    location wrapper nodes (PR c++/43064, PR c++/43486)".  But: this isn't
    intending to blame David, because back then, the problem hasn't been visible in
    the testsuite (or else I'm sure would've been addressed right away) because of
    our all dear friend: missing testsuite coverage.  Thus, for GCC 8, I'm likewise
    enhancing the testsuite, without the C++ front end code changes.
    
    I actually had presumed that there may be an issue for OpenACC:
    <http://mid.mail-archive.com/874lb9qr2u.fsf@euler.schwinge.homeip.net>, so here
    we are, two years (and many "wasted" hours...) later...
    
            gcc/cp/
            * parser.c (cp_parser_omp_var_list_no_open): Assert that array
            section's 'low_bound', 'length' are not location wrapper nodes.
            (cp_parser_oacc_all_clauses, cp_parser_oacc_cache): Instantiate
            'auto_suppress_location_wrappers'.
            gcc/testsuite/
            * c-c++-common/goacc/cache-3-1.c: New.
            * c-c++-common/goacc/cache-3-2.c: Likewise.
            * c-c++-common/goacc/data-clause-1.c: Likewise.
            * c-c++-common/goacc/data-clause-2.c: Likewise.
            * c-c++-common/gomp/map-1.c: Adjust.
            * c-c++-common/gomp/map-2.c: Likewise.
            * g++.dg/goacc/cache-3-1.C: New.
            * g++.dg/goacc/cache-3-2.C: Likewise.
            * g++.dg/goacc/data-clause-1.C: Likewise.
            * g++.dg/goacc/data-clause-2.C: Likewise.
            * g++.dg/gomp/map-1.C: Adjust.
            * g++.dg/gomp/map-2.C: Likewise.
    
    Reported-by: Sandra Loosemore <sandra@codesourcery.com>
    (cherry picked from commit c0c7270cc4efd896fe99f8ad5409dbef089a407f)
Comment 21 GCC Commits 2020-11-26 09:10:13 UTC
The releases/gcc-8 branch has been updated by Thomas Schwinge <tschwinge@gcc.gnu.org>:

https://gcc.gnu.org/g:23ec71d91e3044108a557dace573d3e60ff1c07e

commit r8-10649-g23ec71d91e3044108a557dace573d3e60ff1c07e
Author: Thomas Schwinge <thomas@codesourcery.com>
Date:   Wed Nov 25 20:36:55 2020 +0100

    Don't create location wrapper nodes within OpenACC clauses (testsuite changes only)
    
    This fixes a GCC 11, 10, 9 regression introduced by commit
    dfd7fdca2ac17d8b823a16700525824ca312ade0 (Subversion r267272) "C++: more
    location wrapper nodes (PR c++/43064, PR c++/43486)".  But: this isn't
    intending to blame David, because back then, the problem hasn't been visible in
    the testsuite (or else I'm sure would've been addressed right away) because of
    our all dear friend: missing testsuite coverage.  Thus, for GCC 8, I'm likewise
    enhancing the testsuite, without the C++ front end code changes.
    
    I actually had presumed that there may be an issue for OpenACC:
    <http://mid.mail-archive.com/874lb9qr2u.fsf@euler.schwinge.homeip.net>, so here
    we are, two years (and many "wasted" hours...) later...
    
            gcc/testsuite/
            * c-c++-common/goacc/cache-3-1.c: New.
            * c-c++-common/goacc/cache-3-2.c: Likewise.
            * c-c++-common/goacc/data-clause-1.c: Likewise.
            * c-c++-common/goacc/data-clause-2.c: Likewise.
            * c-c++-common/gomp/map-1.c: Adjust.
            * c-c++-common/gomp/map-2.c: Likewise.
            * g++.dg/goacc/cache-3-1.C: New.
            * g++.dg/goacc/cache-3-2.C: Likewise.
            * g++.dg/goacc/data-clause-1.C: Likewise.
            * g++.dg/goacc/data-clause-2.C: Likewise.
            * g++.dg/gomp/map-1.C: Adjust.
            * g++.dg/gomp/map-2.C: Likewise.
    
    Reported-by: Sandra Loosemore <sandra@codesourcery.com>
    (cherry picked from commit c0c7270cc4efd896fe99f8ad5409dbef089a407f (testsuite changes only))
Comment 22 Marek Polacek 2024-01-24 01:42:14 UTC
Fixed, I suppose.