[Lldb-commits] [PATCH] D61044: Fix infinite recursion when calling C++ template functions

Frédéric Riss via lldb-commits lldb-commits at lists.llvm.org
Tue Apr 23 15:51:56 PDT 2019



> On Apr 23, 2019, at 3:48 PM, Frederic Riss via Phabricator <reviews at reviews.llvm.org> wrote:
> 
> friss created this revision.
> friss added reviewers: shafik, clayborg, jingham.
> Herald added subscribers: kristof.beyls, javed.absar, aprantl.
> 
> When we encounter a templated function in the debug information, we
> were creating an AST that looked like this:
> 
> FunctionTemplateDecl 0x12980ab90 <<invalid sloc>> <invalid sloc> foo<int>
> 
> | -TemplateTypeParmDecl 0x12980aad0 <<invalid sloc>> <invalid sloc> class depth 0 index 0 T |
> | -FunctionDecl 0x12980aa30 <<invalid sloc>> <invalid sloc> foo<int> 'int (int)' extern     |
> |                                                                                           | -TemplateArgument type 'int' |
> | `-ParmVarDecl 0x12980a998 <<invalid sloc>> <invalid sloc> t1 'int'                        |
> |
> 
> `-FunctionDecl 0x12980aa30 <<invalid sloc>> <invalid sloc> foo<int> 'int (int)' extern
> 
>  |-TemplateArgument type 'int'
>  `-ParmVarDecl 0x12980a998 <<invalid sloc>> <invalid sloc> t1 'int'
> 
> Note that the FunctionTemplateDecl has 2 children which are identical (as
> in have the same address). This is not what Clang is doing:
> 
> FunctionTemplateDecl 0x7f89d206c6f8 </tmp/template.cpp:1:1, line:4:1> line:2:5 foo
> 
> | -TemplateTypeParmDecl 0x7f89d206c4a8 <line:1:10, col:19> col:19 referenced typename depth 0 index 0 T |
> | -FunctionDecl 0x7f89d206c660 <line:2:1, line:4:1> line:2:5 foo 'int (T)'                              |
> | `-ParmVarDecl 0x7f89d206c570 <col:9, col:11> col:11 t1 'T'                                            |
> |
> 
> `-FunctionDecl 0x7f89d206cb60 <line:2:1, line:4:1> line:2:5 used foo 'int (int)'
> 
>  |-TemplateArgument type 'int'
>  `-ParmVarDecl 0x7f89d206ca68 <col:9, col:11> col:11 t1 'int':’int'

I don’t know what Phabricator did to my commit message. Here it is (hopefully) un-mangled:

    Fix infinite recursion when calling C++ template functions
    
    Summary:
    When we encounter a templated function in the debug information, we
    were creating an AST that looked like this:
    
    FunctionTemplateDecl 0x12980ab90 <<invalid sloc>> <invalid sloc> foo<int>
    |-TemplateTypeParmDecl 0x12980aad0 <<invalid sloc>> <invalid sloc> class depth 0 index 0 T
    |-FunctionDecl 0x12980aa30 <<invalid sloc>> <invalid sloc> foo<int> 'int (int)' extern
    | |-TemplateArgument type 'int'
    | `-ParmVarDecl 0x12980a998 <<invalid sloc>> <invalid sloc> t1 'int'
    `-FunctionDecl 0x12980aa30 <<invalid sloc>> <invalid sloc> foo<int> 'int (int)' extern
      |-TemplateArgument type 'int'
      `-ParmVarDecl 0x12980a998 <<invalid sloc>> <invalid sloc> t1 'int'
    
    Note that the FunctionTemplateDecl has 2 children which are identical (as
    in have the same address). This is not what Clang is doing:
    
    FunctionTemplateDecl 0x7f89d206c6f8 </tmp/template.cpp:1:1, line:4:1> line:2:5 foo
    |-TemplateTypeParmDecl 0x7f89d206c4a8 <line:1:10, col:19> col:19 referenced typename depth 0 index 0 T
    |-FunctionDecl 0x7f89d206c660 <line:2:1, line:4:1> line:2:5 foo 'int (T)'
    | `-ParmVarDecl 0x7f89d206c570 <col:9, col:11> col:11 t1 'T'
    `-FunctionDecl 0x7f89d206cb60 <line:2:1, line:4:1> line:2:5 used foo 'int (int)'
      |-TemplateArgument type 'int'
      `-ParmVarDecl 0x7f89d206ca68 <col:9, col:11> col:11 t1 'int':'int'
    
    The 2 chidlren are different and actually repesent different things: the first
    one is the unspecialized version and the second one is specialized. (Just looking
    at the names shows another major difference which is that we create the parent
    with a name of "foo<int>" when it should be just "foo".)
    
    The fact that we have those 2 identical children confuses the ClangImporter
    and generates an infinite recursion (reported in https://llvm.org/pr41473).
    We cannot create the unspecialized version as the debug information doesn't
    contain a mapping from the template parameters to their use in the prototype.
    
    This patch just creates 2 different FunctionDecls for those 2 children of the
    FunctionTemplateDecl. This avoids the infinite recursion and allows us to
    call functions. As the XFAILs in the added test show, we've still got issues
    in our handling of templates. I believe they are mostly centered on the fact
    that we create do not register "foo" as a template, but "foo<int>". This is
    a bigger change that will need changes to the debug information generation.
    I believe this change makes sense on its own.
    
    Reviewers: shafik, clayborg, jingham
    
    Subscribers: aprantl, javed.absar, kristof.beyls, lldb-commits
    
    Differential Revision: https://reviews.llvm.org/D61044

Fred

> The 2 chidlren are different and actually repesent different things: the first
> one is the unspecialized version and the second one is specialized. (Just looking
> at the names shows another major difference which is that we create the parent
> with a name of "foo<int>" when it should be just "foo".)
> 
> The fact that we have those 2 identical children confuses the ClangImporter
> and generates an infinite recursion (reported in https://llvm.org/pr41473).
> We cannot create the unspecialized version as the debug information doesn't
> contain a mapping from the template parameters to their use in the prototype.
> 
> This patch just creates 2 different FunctionDecls for those 2 children of the
> FunctionTemplateDecl. This avoids the infinite recursion and allows us to
> call functions. As the XFAILs in the added test show, we've still got issues
> in our handling of templates. I believe they are mostly centered on the fact
> that we create do not register "foo" as a template, but "foo<int>". This is
> a bigger change that will need changes to the debug information generation.
> I believe this change makes sense on its own.
> 
> 
> https://reviews.llvm.org/D61044
> 
> Files:
>  packages/Python/lldbsuite/test/lang/cpp/template-function/Makefile
>  packages/Python/lldbsuite/test/lang/cpp/template-function/TestTemplateFunctions.py
>  packages/Python/lldbsuite/test/lang/cpp/template-function/main.cpp
>  source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
> 
> <D61044.196349.patch>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20190423/ca132c93/attachment-0001.html>


More information about the lldb-commits mailing list