Extent for templated functions seems off

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Extent for templated functions seems off

Eric Fiselier via cfe-dev
I'm trying to use libclang to extract some code, and I hit a case that is really irking me.

Look at the Foo::foo definition on lines 7 through 11:

     1 template <typename ClassType>
     2 struct Foo {
     3     template <typename T>
     4     int foo();
     5 };
     6
     7 template <typename ClassType>
     8 template <typename T>
     9 int Foo<ClassType>::foo() {
    10     return 42;
    11 }
    
The reported extent of the definition FunctionTemplateDecl is lines 8 to 11, when I was expecting it to start at line 7. Therefore, when I use that extent in order to extract the function definition, it leaves behind the class template line on line 7.

Note that when you look the second child element, the CXXMethodDecl, that one has the full extent from lines 7 to 11, which is what I really want.

Is this a bug? It seems odd that the children would have an extent not strictly inside the parent element.

If this is not a bug, what's the best way to find the full extent of the entire definition? I tried a hack to use the min/max extent of the node and its children, but libclang does not happen to return that child.

Thanks!
Jared

===== clang AST dump =====

|-ClassTemplateDecl 0x7fa70d058da8 <temp.cpp:1:1, line:5:1> line:2:8 Foo
| |-TemplateTypeParmDecl 0x7fa70d058c58 <line:1:11, col:20> col:20 typename ClassType
| `-CXXRecordDecl 0x7fa70d058d10 <line:2:1, line:5:1> line:2:8 struct Foo definition
|   |-CXXRecordDecl 0x7fa70d059018 <col:1, col:8> col:8 implicit struct Foo
|   `-FunctionTemplateDecl 0x7fa70d059280 <line:3:5, line:4:13> col:9 foo
|     |-TemplateTypeParmDecl 0x7fa70d0590b0 <line:3:15, col:24> col:24 typename T
|     `-CXXMethodDecl 0x7fa70d0591e8 <line:4:5, col:13> col:9 foo 'int (void)'
|-FunctionTemplateDecl 0x7fa70d059648 parent 0x7fa70d058d10 prev 0x7fa70d059280 <line:8:1, line:11:1> line:9:21 foo
  |-TemplateTypeParmDecl 0x7fa70d059370 <line:8:11, col:20> col:20 typename T
  `-CXXMethodDecl 0x7fa70d059578 parent 0x7fa70d058d10 prev 0x7fa70d0591e8 <line:7:1, line:11:1> line:9:21 foo 'int (void)'
    `-CompoundStmt 0x7fa70d0596e8 <col:27, line:11:1>
      `-ReturnStmt 0x7fa70d0596d0 <line:10:5, col:12>
        `-IntegerLiteral 0x7fa70d0596b0 <col:12> 'int' 42


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Extent for templated functions seems off

Eric Fiselier via cfe-dev
Jared Grubb via cfe-dev <[hidden email]> writes:

> I'm trying to use libclang to extract some code, and I hit a case that is really irking me.
>
> Look at the Foo::foo definition on lines 7 through 11:
>
>      1 template <typename ClassType>
>      2 struct Foo {
>      3    template <typename T>
>      4    int foo();
>      5 };
>      6
>      7 template <typename ClassType>
>      8 template <typename T>
>      9 int Foo<ClassType>::foo() {
>     10    return 42;
>     11 }
>    
> The reported extent of the definition FunctionTemplateDecl is lines 8 to 11, when I was expecting it to start at line 7. Therefore, when I use that extent in order to extract the function definition, it leaves behind the class template line on line 7.
>
> Note that when you look the second child element, the CXXMethodDecl, that one has the full extent from lines 7 to 11, which is what I really want.
>
> Is this a bug? It seems odd that the children would have an extent not
> strictly inside the parent element.

I think it's a bug, I am not sure how to fix it though.

>
> If this is not a bug, what's the best way to find the full extent of the entire definition? I tried a hack to use the min/max extent of the node and its children, but libclang does not happen to return that child.
>
> Thanks!
> Jared
>
> ===== clang AST dump =====
>
> |-ClassTemplateDecl 0x7fa70d058da8 <temp.cpp:1:1, line:5:1> line:2:8 Foo
> | |-TemplateTypeParmDecl 0x7fa70d058c58 <line:1:11, col:20> col:20 typename ClassType
> | `-CXXRecordDecl 0x7fa70d058d10 <line:2:1, line:5:1> line:2:8 struct Foo definition
> |   |-CXXRecordDecl 0x7fa70d059018 <col:1, col:8> col:8 implicit struct Foo
> |   `-FunctionTemplateDecl 0x7fa70d059280 <line:3:5, line:4:13> col:9 foo
> |     |-TemplateTypeParmDecl 0x7fa70d0590b0 <line:3:15, col:24> col:24 typename T
> |     `-CXXMethodDecl 0x7fa70d0591e8 <line:4:5, col:13> col:9 foo 'int (void)'
> |-FunctionTemplateDecl 0x7fa70d059648 parent 0x7fa70d058d10 prev 0x7fa70d059280 <line:8:1, line:11:1> line:9:21 foo
>   |-TemplateTypeParmDecl 0x7fa70d059370 <line:8:11, col:20> col:20 typename T
>   `-CXXMethodDecl 0x7fa70d059578 parent 0x7fa70d058d10 prev 0x7fa70d0591e8 <line:7:1, line:11:1> line:9:21 foo 'int (void)'
>     `-CompoundStmt 0x7fa70d0596e8 <col:27, line:11:1>
>       `-ReturnStmt 0x7fa70d0596d0 <line:10:5, col:12>
>         `-IntegerLiteral 0x7fa70d0596b0 <col:12> 'int' 42
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev