Menu

Cppcheck show syntax error when analyzing template

顾涛明
2018-10-25
2018-10-25
  • 顾涛明

    顾涛明 - 2018-10-25

    For example:

     template<typename Word, size_t NumWords,
              typename Enable = typename std::enable_if<sizeof(Word) < sizeof(word_t)>::type >
    

    Cppcheck will calculate the number of '<' and '>'. However, this symbol in 'sizeof(Word) < sizeof(word_t)' means less-than sign, and cppcheck still think it is 'garbage templates'.

    /cppcheck-1.85/lib/tokenize.cpp
    void Tokenizer::findGarbageCode() const
    {
        ...
        // Garbage templates..
        if (isCPP()) {
            for (const Token *tok = tokens(); tok; tok = tok->next()) {
                if (!Token::simpleMatch(tok, "template <"))
                    continue;
                if (tok->previous() && !Token::Match(tok->previous(), "[:;{})>]"))
                    syntaxError(tok);
                const Token * const tok1 = tok;
                tok = tok->next()->findClosingBracket();
                if (!tok)
                    syntaxError(tok1);
                if (!Token::Match(tok, ">|>> ::| %name%") &&
                    !Token::Match(tok, ">|>> [ [ %name%"))
                    syntaxError(tok->next() ? tok->next() : tok1);
            }
        }
    }
    

    if I add brances on both sides of 'sizeof(Word) < sizeof(word_t)' or add '>' in the end, the probrem will not appear.

     template<typename Word, size_t NumWords,
              typename Enable = typename std::enable_if<(sizeof(Word) < sizeof(word_t))>::type >
    
     template<typename Word, size_t NumWords,
              typename Enable = typename std::enable_if<sizeof(Word) < sizeof(word_t)>::type >>
    
     
  • Daniel Marjamäki

    Thanks! I have created a ticket: https://trac.cppcheck.net/ticket/8812

     

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.