Menu

Override Issue

2019-03-12
2019-03-18
  • Sourabh Joshi

    Sourabh Joshi - 2019-03-12

    Consider the following Code snippet:

    include <iostream></iostream>

    using namespace std;
    class MyClass : public std::basic_streambuf<char>
    {
    public :
    int_type overflow(int_type c = traits_type::eof());
    };</char>

    CppCheck Does not detects that the function 'overflow' is virtual in the std::basic_streambuf class and
    does not hits the predefined rule which is
    "The function '$symbol' overrides a function in a base class but is not marked with a 'override' specifier."

    It Happens because the class that is inherited is in a lib function and the Code File does not exits in the folder path specified.
    Can any one tell me what should I do to hit the Rule.

    Any help would be appreciated..

    Note : The Corresponding Function is in the checkclass.cpp - void CheckClass::checkOverride()

     
  • Daniel Marjamäki

    I don't know if I can help you immediately with this. there is no good quick answer.

    If you want this check to work today then I am afraid my recommendation is to use another tool. I believe there are a few compilers and tools that will warn about missing override. I can't recommend a specific one. But it seems that gcc/clang/msvc/clang-tidy has this warning.

    I really do not recommend that system headers are included in Cppcheck but you could try to see what happens if you use -I /usr/include or some such.

    A proper fix in Cppcheck would be nice. However;
    - Putting this info in the cfg files would be very labour intensive
    - I do not want that system headers are included

    We could create some kind of method that will automatically parse out the type declarations from the system headers and put it in some kind of database.

     
    • Robert Reif

      Robert Reif - 2019-03-13

      The main problem (besides performance) in checking header files is setting all the builtin system specific defines necessary for the code to make sense. Since we don't automatically add these magic defines in cppcheck (I believe there was a patch to do this) we could run system headers through the system cpp before simplecpp. Unfortunately this will only work if the platform type is native.

       
  • Sourabh Joshi

    Sourabh Joshi - 2019-03-13

    @Daniel Marjamäki Thanks for your quick feedback.
    I tried using the -I option but still the rule did not got hit.

    The Following code does produces the desired output That the
    "The function 'f' overrides a function in a base class but is not marked with a 'override'

    class A{ 
        public: 
         virtual int f(int a=42); 
        }; 
    
        class B: public A{ 
        public: 
         int f(int a=43); 
        }; 
    

    Wonder why the below does not works tried by giving the include dir paths to it as well but no Luck.

    using namespace std;
    class MyClass : public std::basic_streambuf<char>
    {
    public : 
    int_type overflow(int_type c = traits_type::eof());
    }
    
     
  • Daniel Marjamäki

    gcc:

    $ g++ -fsyntax-only -Wsuggest-override override.cpp
    override.cpp:6:10: warning: ‘virtual std::basic_streambuf<char>::int_type MyClass::overflow(std::basic_streambuf<char>::int_type)’ can be marked override [-Wsuggest-override]
     int_type overflow(int_type c = traits_type::eof());
              ^~~~~~~~
    

    clang-tidy:

    $ clang-tidy -checks=*override override.cpp --
    142 warnings generated.
    /home/danielmarjamaki/cppcheck/override.cpp:6:10: warning: annotate this function with 'override' or (rarely) 'final' [hicpp-use-override]
    int_type overflow(int_type c = traits_type::eof());
             ^                                        ~
                                                       override
    
     
  • Sourabh Joshi

    Sourabh Joshi - 2019-03-14

    I gave the Following command and it did not work as you gave:

    cppcheck.exe --enable=all "AboveCodeFilePath"
    
     
  • Daniel Marjamäki

    so use a compiler or other tool. for now, cppcheck does not do this well.

     
  • Daniel Marjamäki

    I am sure that visual studio also has such a warning.. I just don't know how you configure it.

     
    • versat

      versat - 2019-03-18

      I have searched a bit, but it seems that Visual Studio does not have such a warning. It can warn when override is specified but now allowed. But not if there is an override missing.
      If someone finds information how to warn about missing override in Visual Studio i would be interested also.

       

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.