Bug 21892 - value of constexpr variable is "<optimized out>" (no optimization)
Summary: value of constexpr variable is "<optimized out>" (no optimization)
Status: NEW
Alias: None
Product: gdb
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-08-02 19:55 UTC by Keith Seitz
Modified: 2023-08-28 04:52 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Keith Seitz 2017-08-02 19:55:05 UTC
Reported originally in c++/20020

----- snip -----

$ cat main.cpp 
struct A {
    static constexpr const char *a = "a";
    void foo() { }
};

int main(void) {
    A a;
    a.foo();
}

Compile with g++ 7.1 with C++17 semantics: g++-7 -std=c++17 -g -o main main.cpp

Now run it with gdb:

$ gdb main
GNU gdb (Debian 7.11.1-2~bpo8+1) 7.11.1
(...)
(gdb) b A::foo
Breakpoint 1 at 0x40055e: file main.cpp, line 6.
(gdb) r
Starting program: main 

Breakpoint 1, A::foo (this=0x7fffffffe09f) at main.cpp:6
6	    void foo() { }
(gdb) print *this
$1 = {static a = <optimized out>}

----- end -----
Comment 1 Guillaume Morin 2017-08-02 23:03:44 UTC
Just a note: I get *optimized out* with c++14 (the default), not c++17

So you want to compile with "g++-7 -g -o main main.cpp" to reproduce this bug.  If you add -std=c++17, you're reproducing the segfault in c++/20020
Comment 2 Guillaume Morin 2017-08-03 14:26:59 UTC
To be 100% clear with the c++/20020 segfault fixed and c++17, you get "{static a = <error reading variable: Missing ELF symbol "A::a".>}"

With c++14, you get "{static a = <optimized out>}"
Comment 3 Tom Tromey 2023-08-27 16:59:51 UTC
I needed -fno-eliminate-unused-debug-types for this.

This looks like a compiler bug to me.
The DWARF for A::a is:

 <2><35>: Abbrev Number: 5 (DW_TAG_variable)
    <36>   DW_AT_name        : a
    <38>   DW_AT_decl_file   : 1
    <39>   DW_AT_decl_line   : 2
    <3a>   DW_AT_decl_column : 34
    <3b>   DW_AT_linkage_name: (indirect string, offset: 0x0): _ZN1A1aE
    <3f>   DW_AT_type        : <0x60>
    <43>   DW_AT_external    : 1
    <43>   DW_AT_declaration : 1
    <43>   DW_AT_const_expr  : 1
    <43>   DW_AT_inline      : 1        (inlined)

There's no location here, just a linkage name.
However, that symbol isn't defined.