How do I add an override keyword to a large C ++ codebase?

I have a large C ++ database with thousands of source files. I want to add a keyword override

wherever needed. Some of my explicitly overridden functions don't actually override any function from the base class, and I would like to catch them, or at least highlight them.

I tried to do it manually, but the codebase is too big. I've tried using clang-modernize but it doesn't have any helpful instructions. I am also concerned that he will not be able to understand the code written for Visual Studio.

How can I add the override keyword to my codebase without spending man-weeks or more on the task?

+4


source to share


1 answer


It seems like clang-modernize has moved to clang-tidy which supports this.

Sample code (test.cpp):

struct Base {
    virtual void reimplementMe(int a) {}
};
struct Derived : public Base  {
    virtual void reimplementMe(int a) {}
};

      

Clang-Tidy Challenge (trial run):

clang-tidy-6.0 -checks='modernize-use-override' test.cpp -- -std=c++11

      

To actually fix your code (make sure you have a working backup):



clang-tidy-6.0 -checks='modernize-use-override' -fix test.cpp -- -std=c++11

      

Providing:

struct Base {
    virtual void reimplementMe(int a) {}
};
struct Derived : public Base  {
    void reimplementMe(int a) override {}
};

      


Note: I used an example from this great article - see the detailed explanation and check it out in the next part, which describes how to convert a larger project.

Good luck!

0


source







All Articles