Menu

Cppcheck crash; I wonder how this template usage works

2020-11-28
2020-11-29
  • Daniel Marjamäki

    Cppcheck crashes for this code:

    template < class Grid >
    void checkFatherLevel ( Grid & grid )
    {
        for ( int level = 0 ; level <= grid . maxLevel ( ) ; ++ level )
        {
            typedef typename Grid :: LevelGridView GridView ;
            typedef typename GridView :: template Codim < 0 > :: Iterator Iterator ;
            typedef typename Iterator :: Entity Entity ;
    
            GridView gv = grid . levelGridView ( level ) ;
            Iterator it = gv . template begin < 0 > ( ) ;
            Iterator end = gv . template end < 0 > ( ) ;
                if ( level == 0 )
                {
                }
        }
    }
    

    I wonder do you know what the template keyword does inside the template code?

    What would the proper AST be for the expression it = gv . template begin < 0 > ( ).

     

    Last edit: Daniel Marjamäki 2020-11-28
  • Daniel Marjamäki

    The clang AST for that expression:

              | `-VarDecl 0x18bab10 <col:9, col:50> col:18 end 'Iterator':'typename GridView::template Codim<0>::Iterator' cinit
              |   `-CallExpr 0x18bac60 <col:24, col:50> '<dependent type>'
              |     `-CXXDependentScopeMemberExpr 0x18babd8 <col:24, col:46> '<dependent type>' lvalue .end
              |       `-DeclRefExpr 0x18bab78 <col:24> 'GridView':'typename Grid::LevelGridView' lvalue Var 0x1894ba8 'gv' 'GridView':'typename Grid::LevelGridView'
    

    I guess if we remove template tokens in such code we will get a corresponding AST in Cppcheck.

    Does anybody disagree about that?

     

    Last edit: Daniel Marjamäki 2020-11-28
  • Daniel Marjamäki

    I will implement that simplification.. please feel free to comment if you think that will be wrong.

     
  • Paul Fultz

    Paul Fultz - 2020-11-28

    I wonder do you know what the template keyword does inside the template code?

    This is to disambiguate the function call. Since the compiler doesnt know if begin is a variable or a function. By default, it assumes begin is a variable so < and > are treated as operators instead of template brackets.

    Does anybody disagree about that?

    I dont necessarily disagree, but think we should preserve this information somehow as this seems like it would be useful in createLinks.

     
    • Daniel Marjamäki

      I dont necessarily disagree, but think we should preserve this information somehow as this seems like it would be useful in createLinks.

      ok good to know.. then I will try to add a attribute instead so this information is kept..

       
  • Robert Reif

    Robert Reif - 2020-11-28

    We already skip over the template token in the template simplifier.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.