Implementing a custom lint-style check for virtual functions
D's reflection capabilities can be used to perform some checks that the lint tools are needed to perform for the C or C++ code. Here, we'll implement the code to warn the user at compile time whether their class has an unmarked virtual function.
How to do it…
Let's execute the following steps to implement a custom lint-style check for virtual functions:
Define a plain
enum
calledVirtual
to use as the annotation to silence the warning.Find classes in your program to test, either manually or with reflection.
Use
__traits(derivedMembers, Class)
to get all the members, excluding the base class members.Use
__traits(isVirtualMethod, member)
to determine whether it is a virtual function.Write a helper function that uses
__traits(getAttributes, member)
in a loop withstatic if(is(Virtual))
to look for the annotation.If it is not found, use
pragma(msg)
to warn the user.Optionally, return a failure flag from your
check
function. The user may...