Skip to content

Instantly share code, notes, and snippets.

@SirTony
Last active August 29, 2015 14:07
Show Gist options
  • Save SirTony/dec62f024acf03bbcc2b to your computer and use it in GitHub Desktop.
Save SirTony/dec62f024acf03bbcc2b to your computer and use it in GitHub Desktop.
Helpful user-defined attributes for marking TODOs and FIXMEs with code to be notified at compile time, rather than with comments and grepping for them later.
module annotations;
private {
import std.string;
import std.traits;
import std.array;
}
private template TargetInfo( alias target )
{
enum parent = __traits( identifier, __traits( parent, target ) );
enum isTopLevel = __traits( isSame, mixin( parent ), mixin( __MODULE__ ) );
enum name = __traits( identifier, target );
static if( isTopLevel )
enum prettyName = "%s.%s".format( __MODULE__, name );
else
enum prettyName = "%s.%s.%s".format( __MODULE__, parent, name );
}
private template AnnotationBase( alias target, string prefix, string message )
{
mixin TargetInfo!target;
pragma( msg, message.replace( "{MODULE}", __MODULE__ ).replace( "{NAME}", name ).replace( "{FULL_NAME}", prettyName ) );
}
package template FixMe( alias target, string message = null ) if( isSomeFunction!target || isCallable!target )
{
debug
{
mixin TargetInfo!target;
static if( message is null || message.length == 0 )
enum output = "FIXME: %s needs to be fixed in %s on line %u.".format( prettyName, __FILE__, __LINE__ );
else
enum output = "FIXME @ %s (%s:%u): %s".format( prettyName, __FILE__, __LINE__, message );
mixin AnnotationBase!( target, "FIXME", output );
}
}
package template ToDo( alias target, string message = null ) if( isSomeFunction!target || isCallable!target )
{
debug
{
mixin TargetInfo!target;
static if( message is null || message.length == 0 )
enum output = "TODO: %s needs to be taken care of in %s on line %u.".format( prettyName, __FILE__, __LINE__ );
else
enum output = "TODO @ %s (%s:%u): %s".format( prettyName, __FILE__, __LINE__, message );
mixin AnnotationBase!( target, "TODO", output );
}
}
module example;
import annotations;
/+
At compile time, the following will be printed to STDOUT (when in debug mode only):
FIXME @ example.randomNumber (example.d:11): The RNG is broken.
TODO @ example.main (example.d:17): Properly implement main().
+/
@FixMe!( randomNumber, "The RNG is broken." )
int randomNumber()
{
return 4;
}
@ToDo!( main, "Properly implement main()." )
void main()
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment