Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add the override keyword to a large C++ codebase?

Tags:

c++

overriding

I have a large C++ codebase with thousands of source files. I want to add the override keyword wherever it's appropriate. Some of my apparently-overridden functions do not actually override any function from the base class, and I'd like to catch these or at least make them stand out.

I tried doing it manually, but the codebase is too large. I tried using clang-modernize, but it doesn't come with useful instructions. I'm also concerned that it won't be able to comprehend the codebase written for Visual Studio.

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

like image 591
Sophit Avatar asked Oct 31 '22 00:10

Sophit


1 Answers

It seems that clang-modernize has moved into clang-tidy which supports this.

Example code (test.cpp):

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

clang-tidy invocation (dry run):

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

To actually fix your code (be sure to have a working backup):

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

Giving:

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

Note: Example from this great article was used -- have a look for a detailed explanation and check it's next part which describes how to convert a larger project.

Good luck!

like image 81
vlp Avatar answered Nov 15 '22 05:11

vlp