Menu

cppcheck gives syntax error on "enum class"

2015-07-06
2016-04-03
  • Lieven de Cock

    Lieven de Cock - 2015-07-06

    consider the following header file (it has been completely stripped from other stuff, and the library it belongs too could also be stripped away to reproduce the problem.

    foo.h

    #ifndef FOO_H
    #define FOO_H
    
    namespace Foo
    {
    
    enum class Mode
    {
        readonly,
        readwrite
    };
    
    } // namespace Foo
    
    #endif // FOO_H
    

    call cppcheck (1.69) as :
    cppcheck --verbose --enable=all --enable=style --xml -I"." foo.h

    ==>

    <results> Checking foo.h... <error file="foo.h" line="7" id="syntaxError" severity="error" msg="syntax error"/> </results>

    It gives an error on the enum class, remove the word class and the problem is gone.

     
  • Daniel Marjamäki

    Thanks! This is a bug in Cppcheck.

    Can you create a ticket in our issue tracker?

    http://trac.cppcheck.net/

     
  • Lieven de Cock

    Lieven de Cock - 2015-07-08

    I will but ...

    Warning: Your permissions have been limited until you verify your email address.

    But I never received such email.

     
  • Daniel Marjamäki

    I don't know why.. but sometimes the verification email does not reach people.

    Please login. And goto your account settings. You should be allowed to enter a verification token somewhere..

    Your verification token is: AUQQuU1E

     
  • Alexander Mai

    Alexander Mai - 2015-07-12

    By default cppcheck does not expect C++ code from a header file.
    --language=c++ should fix that message.

     
  • John Hagen

    John Hagen - 2016-03-07

    According to this ticket, running cppcheck on .h files directly is not supported: http://trac.cppcheck.net/ticket/7383

     
  • pavithra rani

    pavithra rani - 2016-04-02

    Hi Daniel, Is this issue handled in latest release? i currently use cppcheck1.59 version. I could see similar error reported by cppcheck.

     
  • Alexander Mai

    Alexander Mai - 2016-04-03

    Please read the answers (and the ticket at trac) above to answer your question.

     
  • Maarten Hoes

    Maarten Hoes - 2017-06-27

    The answer is: 'don't run cppcheck on .h files". So when running cppcheck on an entire project, how do you tell it to skip all the .h files completely ? I cannot find this piece of information on the manual, man page, or command help.

     
    • Vladimir Liubimov

      The problem is cppcheck recognize content of the .h files as C language. You can force it to handle them as C++. Just pass the --language=c++ argument.

       
  • Daniel Marjamäki

    So when running cppcheck on an entire project, how do you tell it to skip all the .h files completely

    Cppcheck will normally skip the header files.

    # recursively check all source files in folder (skip headers)
    $ cppcheck folder
    # check source files in visual studio solution
    $ cppcheck --project=MyProject.sln
    # check source files according to compile database
    $ cppcheck --project=compile_commands.json
    
     

Log in to post a comment.