Hi, I've reg-tested this patch on both the trunk and the releases/gcc-14 branches for x86_64-linux-gnu and arm-none-eabi and it no longer fails for any of the out-of-bounds-diagram* tests on any of the 2 platforms.
I'm a bit puzzled if the C++ part is enough, but I can't think of a way to trigger anything that show the wrong output after my change. Do you think that I need to add any additional tests? I think the existing test covers the problem well enough. Ok for trunk and releases/gcc-14? -- gcc/ChangeLog: PR c/116060 c/c-typeck.cc: Make sure left hand side and right hand side has identical named types to aid diagnostic output. cp/call.cc: Likewise. gcc/testsuite/ChangeLog: PR c/116060 c-c++-common/analyzer/out-of-bounds-diagram-8.c: Update to correct type. c-c++-common/analyzer/out-of-bounds-diagram-11.c: Likewise. gcc.dg/analyzer/out-of-bounds-diagram-10.c: Likewise. Signed-off-by: Torbjörn SVENSSON <torbjorn.svens...@foss.st.com> --- gcc/c/c-typeck.cc | 3 ++ gcc/cp/call.cc | 9 ++++++ .../analyzer/out-of-bounds-diagram-11.c | 28 +++++++++---------- .../analyzer/out-of-bounds-diagram-8.c | 28 +++++++++---------- .../analyzer/out-of-bounds-diagram-10.c | 28 +++++++++---------- 5 files changed, 54 insertions(+), 42 deletions(-) diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 902898d1944..e3e85d1ecde 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -7831,6 +7831,9 @@ convert_for_assignment (location_t location, location_t expr_loc, tree type, if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) { warn_for_address_of_packed_member (type, orig_rhs); + if (type != rhstype) + /* Convert RHS to TYPE in order to not loose TYPE in diagnostics. */ + rhs = convert (type, rhs); return rhs; } diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index c8420db568e..d859ce9a2d6 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -1319,6 +1319,9 @@ standard_conversion (tree to, tree from, tree expr, bool c_cast_p, { if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue) conv->type = qualified_to; + else if (from != to) + /* Use TO in order to not loose TO in diagnostics. */ + conv->type = to; return conv; } @@ -8741,6 +8744,12 @@ convert_like_internal (conversion *convs, tree expr, tree fn, int argnum, continue to warn about uses of EXPR as an integer, rather than as a pointer. */ expr = build_int_cst (totype, 0); + if (TREE_CODE (expr) == NON_LVALUE_EXPR && TREE_TYPE (expr) != totype) + { + /* Use TOTYPE in order to not loose TOTYPE in diagnostics. */ + expr = copy_node (expr); + TREE_TYPE (expr) = totype; + } return expr; case ck_ambig: /* We leave bad_p off ck_ambig because overload resolution considers diff --git a/gcc/testsuite/c-c++-common/analyzer/out-of-bounds-diagram-11.c b/gcc/testsuite/c-c++-common/analyzer/out-of-bounds-diagram-11.c index 63ae08347aa..048a1b9698f 100644 --- a/gcc/testsuite/c-c++-common/analyzer/out-of-bounds-diagram-11.c +++ b/gcc/testsuite/c-c++-common/analyzer/out-of-bounds-diagram-11.c @@ -47,20 +47,20 @@ void test7 (size_t size) /* { dg-begin-multiline-output "" } - ┌───────────────────────────────────────┐ - │ write of '(int) 42' │ - └───────────────────────────────────────┘ - │ │ - │ │ - v v - ┌──────────────────────────────────────────────────┐┌──────────────────┐ - │ buffer allocated on stack at (1) ││after valid range │ - └──────────────────────────────────────────────────┘└──────────────────┘ - ├────────────────────────┬─────────────────────────┤├────────┬─────────┤ - │ │ - ╭────────────────┴───────────────╮ ╭─────────┴────────╮ - │capacity: '(size * 4) + 3' bytes│ │overflow of 1 byte│ - ╰────────────────────────────────╯ ╰──────────────────╯ + ┌────────────────────────────────────────┐ + │ write of '(int32_t) 42' │ + └────────────────────────────────────────┘ + │ │ + │ │ + v v + ┌────────────────────────────────────────────────┐ ┌───────────────────┐ + │ buffer allocated on stack at (1) │ │ after valid range │ + └────────────────────────────────────────────────┘ └───────────────────┘ + ├───────────────────────┬────────────────────────┤ ├─────────┬─────────┤ + │ │ + ╭────────────────┴───────────────╮ ╭─────────┴────────╮ + │capacity: '(size * 4) + 3' bytes│ │overflow of 1 byte│ + ╰────────────────────────────────╯ ╰──────────────────╯ { dg-end-multiline-output "" } */ diff --git a/gcc/testsuite/c-c++-common/analyzer/out-of-bounds-diagram-8.c b/gcc/testsuite/c-c++-common/analyzer/out-of-bounds-diagram-8.c index c8ff2fd1e64..4d1f5227049 100644 --- a/gcc/testsuite/c-c++-common/analyzer/out-of-bounds-diagram-8.c +++ b/gcc/testsuite/c-c++-common/analyzer/out-of-bounds-diagram-8.c @@ -18,19 +18,19 @@ void test2 (size_t size) /* { dg-begin-multiline-output "" } - ┌───────────────────┐ - │write of '(int) 42'│ - └───────────────────┘ - │ - │ - v - ┌───────────────────────────────┐ ┌───────────────────┐ - │buffer allocated on heap at (1)│ │ after valid range │ - └───────────────────────────────┘ └───────────────────┘ - ├───────────────┬───────────────┤├───────┬────────┤├─────────┬─────────┤ - │ │ │ - ╭─────────────┴────────────╮ ╭───┴───╮ ╭─────────┴─────────╮ - │capacity: 'size * 4' bytes│ │4 bytes│ │overflow of 4 bytes│ - ╰──────────────────────────╯ ╰───────╯ ╰───────────────────╯ + ┌───────────────────────┐ + │write of '(int32_t) 42'│ + └───────────────────────┘ + │ + │ + v + ┌───────────────────────────────┐ ┌───────────────────────┐ + │buffer allocated on heap at (1)│ │ after valid range │ + └───────────────────────────────┘ └───────────────────────┘ + ├───────────────┬───────────────┤├─────┬──────┤├───────────┬───────────┤ + │ │ │ + ╭─────────────┴────────────╮ ╭───┴───╮ ╭─────────┴─────────╮ + │capacity: 'size * 4' bytes│ │4 bytes│ │overflow of 4 bytes│ + ╰──────────────────────────╯ ╰───────╯ ╰───────────────────╯ { dg-end-multiline-output "" } */ diff --git a/gcc/testsuite/gcc.dg/analyzer/out-of-bounds-diagram-10.c b/gcc/testsuite/gcc.dg/analyzer/out-of-bounds-diagram-10.c index 4a7b8e306de..f719c4801dd 100644 --- a/gcc/testsuite/gcc.dg/analyzer/out-of-bounds-diagram-10.c +++ b/gcc/testsuite/gcc.dg/analyzer/out-of-bounds-diagram-10.c @@ -11,19 +11,19 @@ int32_t int_vla_write_element_symbolic_before_start (int32_t x, size_t n) /* { dg-begin-multiline-output "" } - ┌───────────────────┐ - │write of '(int) 42'│ - └───────────────────┘ - │ - │ - v - ┌───────────────────┐ ┌────────────────────────────────┐ - │before valid range │ │buffer allocated on stack at (1)│ - └───────────────────┘ └────────────────────────────────┘ - ├─────────┬─────────┤├───────┬───────┤├───────────────┬────────────────┤ - │ │ │ - ╭─────────┴───────────╮ ╭───┴───╮ ╭───────────┴───────────╮ - │underwrite of 4 bytes│ │4 bytes│ │capacity: 'n * 4' bytes│ - ╰─────────────────────╯ ╰───────╯ ╰───────────────────────╯ + ┌───────────────────────┐ + │write of '(int32_t) 42'│ + └───────────────────────┘ + │ + │ + v + ┌───────────────────────┐ ┌────────────────────────────────┐ + │ before valid range │ │buffer allocated on stack at (1)│ + └───────────────────────┘ └────────────────────────────────┘ + ├───────────┬───────────┤├─────┬─────┤├───────────────┬────────────────┤ + │ │ │ + ╭──────────┴──────────╮ ╭───┴───╮ ╭───────────┴───────────╮ + │underwrite of 4 bytes│ │4 bytes│ │capacity: 'n * 4' bytes│ + ╰─────────────────────╯ ╰───────╯ ╰───────────────────────╯ { dg-end-multiline-output "" } */ -- 2.25.1