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?
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With