[PATCH] D123319: Change how we handle auto return types for lambda operator() to be consistent with gcc

Adrian Prantl via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 17 17:13:47 PDT 2022


aprantl added a comment.

In D123319#3517966 <https://reviews.llvm.org/D123319#3517966>, @dblaikie wrote:

> In D123319#3506745 <https://reviews.llvm.org/D123319#3506745>, @shafik wrote:
>
>> 
>
> What does the linkage name do for your use case? Which cases are missing linkage names/where do they go missing?
>
>> I am happy to consider other approaches as well to solving lookup for local lambdas for D105564 <https://reviews.llvm.org/D105564>. Let me know what you think.
>
> Why does the return type help perform lookup? What kind of lookup?
>
> (again, my take is that "auto" return types probably shouldn't be described at all - we should just not describe functions where we don't know their return types because they're not very useful to know about (overload resolution, yes, but then you can't call them anyway) - but that's a broader argument to make/change to make)

IIUC, the motivating problem is (@shafik please correct me if this isn't it) this:

  $ cat /tmp/lambda.cpp 
  #include <cstdio>
  int main() {
    auto f = [](){ printf("hi from lambda\n"); return 1;} ;
    f();
    f();
    return f();
  }
  $ clang++ -g /tmp/lambda.cpp -o /tmp/a.out
  $ lldb /tmp/a.out
  (lldb) b 5
  (lldb) r
  (lldb) p f()
  hi from lambda
  (lldb) p auto val = f()
  error: expression failed to parse:
  error: <user expression 1>:1:6: variable has incomplete type 'void'
  auto val = f()
       ^

LLDB can't determine the return type of the lambda, because it has trouble matching up the abstract specification (with the `auto` return type) with the concrete definition (with the `int` return type):

  $ dwarfdump --name "operator()" /tmp/a.out.dSYM -p -c
  /tmp/a.out.dSYM/Contents/Resources/DWARF/a.out:	file format Mach-O arm64
  
  0x0000000b: DW_TAG_compile_unit
                DW_AT_producer	("Apple clang version 13.1.6 (clang-1316.0.21.2)")
                DW_AT_language	(DW_LANG_C_plus_plus_14)
                DW_AT_name	("/tmp/lambda.cpp")
                DW_AT_LLVM_sysroot	("/Library/Developer/CommandLineTools/SDKs/MacOSX13.0.sdk")
                DW_AT_APPLE_sdk	("MacOSX13.0.sdk")
                DW_AT_stmt_list	(0x00000000)
                DW_AT_comp_dir	("/Volumes/Data/swift")
                DW_AT_low_pc	(0x0000000100003f28)
                DW_AT_high_pc	(0x0000000100003f98)
  
  0x000007c6:   DW_TAG_subprogram
                  DW_AT_low_pc	(0x0000000100003f28)
                  DW_AT_high_pc	(0x0000000100003f6c)
                  DW_AT_frame_base	(DW_OP_reg29 W29)
                  DW_AT_name	("main")
                  DW_AT_decl_file	("/tmp/lambda.cpp")
                  DW_AT_decl_line	(2)
                  DW_AT_type	(0x000000000000029f "int")
                  DW_AT_external	(true)
  
  0x000007ed:     DW_TAG_class_type
                    DW_AT_calling_convention	(DW_CC_pass_by_value)
                    DW_AT_byte_size	(0x01)
                    DW_AT_decl_file	("/tmp/lambda.cpp")
                    DW_AT_decl_line	(3)
  
  0x000007f2:       DW_TAG_subprogram
                      DW_AT_name	("operator()")
                      DW_AT_decl_file	("/tmp/lambda.cpp")
                      DW_AT_decl_line	(3)
                      DW_AT_type	(0x0000000000000806 "auto")
                      DW_AT_declaration	(true)
                      DW_AT_accessibility	(DW_ACCESS_public)
  
  0x000007fe:         DW_TAG_formal_parameter
                        DW_AT_type	(0x000000000000080b "const class *")
                        DW_AT_artificial	(true)
  
  0x00000803:         NULL
  
  0x0000000b: DW_TAG_compile_unit
                DW_AT_producer	("Apple clang version 13.1.6 (clang-1316.0.21.2)")
                DW_AT_language	(DW_LANG_C_plus_plus_14)
                DW_AT_name	("/tmp/lambda.cpp")
                DW_AT_LLVM_sysroot	("/Library/Developer/CommandLineTools/SDKs/MacOSX13.0.sdk")
                DW_AT_APPLE_sdk	("MacOSX13.0.sdk")
                DW_AT_stmt_list	(0x00000000)
                DW_AT_comp_dir	("/Volumes/Data/swift")
                DW_AT_low_pc	(0x0000000100003f28)
                DW_AT_high_pc	(0x0000000100003f98)
  
  0x00000815:   DW_TAG_subprogram
                  DW_AT_low_pc	(0x0000000100003f6c)
                  DW_AT_high_pc	(0x0000000100003f98)
                  DW_AT_frame_base	(DW_OP_reg29 W29)
                  DW_AT_object_pointer	(0x00000834)
                  DW_AT_type	(0x000000000000029f "int")
                  DW_AT_linkage_name	("_ZZ4mainENK3$_0clEv")
                  DW_AT_specification	(0x00000000000007f2 "operator()")
  
  0x00000834:     DW_TAG_formal_parameter
                    DW_AT_location	(DW_OP_breg31 WSP+8)
                    DW_AT_name	("this")
                    DW_AT_type	(0x0000000000000841 "const class *")
                    DW_AT_artificial	(true)
  
  0x00000840:     NULL


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D123319/new/

https://reviews.llvm.org/D123319



More information about the cfe-commits mailing list