Menu

cppcheck confuses template operator() call for "less than" comparison

VG
2021-08-14
2021-08-14
  • VG

    VG - 2021-08-14

    The following code triggers an error: "AST broken, binary operator '>' doesn't have two operands." This is obviously because the expression has been parsed incorrectly.

    template <std::size_t First, std::size_t... Indices, typename Functor>
    constexpr void constexpr_for_fold_impl([[maybe_unused]] Functor&& f, std::index_sequence<Indices...>) noexcept
    {
        (std::forward<Functor>(f).template operator() < First + Indices > (), ...);
    }
    
     
  • Paul Fultz

    Paul Fultz - 2021-08-14

    So cppcheck removes the template keyword which makes it harder to know what brackets should be linked. We should add a template flag in the token when its removed so we can later find templates.

    Alternatively, I wonder if we can try to link the brackets after the AST is built. Since we know there is no AST for the < it must mean it could be a template bracket.

     
  • VG

    VG - 2021-08-14

    I think the 'template' keyword here is absolutely necessary for disambiguation and you can't parse this correctly if it was removed (i. e. without taking this keyword into account).

     
  • Paul Fultz

    Paul Fultz - 2021-08-14

    Well, actually we dont remove the template keyword in the snippet above. However, we still dont check for a template keyword before ahead anyways. So I fixed this here:

    https://github.com/danmar/cppcheck/pull/3399

    It will remove the template keyword and then set the isTemplate flag on the name that follows, and then linking the brackets will use this flag.

     

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.