Book Image

D Cookbook

By : Adam Ruppe
Book Image

D Cookbook

By: Adam Ruppe

Overview of this book

Table of Contents (21 chapters)
D Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. Define a plain enum called Virtual to use as the annotation to silence the warning.

  2. Find classes in your program to test, either manually or with reflection.

  3. Use __traits(derivedMembers, Class) to get all the members, excluding the base class members.

  4. Use __traits(isVirtualMethod, member) to determine whether it is a virtual function.

  5. Write a helper function that uses __traits(getAttributes, member) in a loop with static if(is(Virtual)) to look for the annotation.

  6. If it is not found, use pragma(msg) to warn the user.

  7. Optionally, return a failure flag from your check function. The user may...