Skip to content

Instantly share code, notes, and snippets.

@dpeek
Created June 26, 2014 14:36
Show Gist options
  • Save dpeek/15fec53d7befe1984f04 to your computer and use it in GitHub Desktop.
Save dpeek/15fec53d7befe1984f04 to your computer and use it in GitHub Desktop.
-x Test
--macro Test.Macro.keep()
--next
-x Test
-dce full
--macro Test.Macro.keep()
import haxe.macro.Type;
using haxe.macro.Tools;
using Lambda;
class Test
{
static function main() new Test();
function new()
{
check("build");
check("dead");
check("keep");
}
function check(field:String) trace('$field: ' + (Reflect.field(this, field) != null));
function build() null;
function dead() null;
@:keep function keep() null;
}
class Macro
{
static function keep() haxe.macro.Context.onGenerate(process);
static function process(types:Array<Type>) types.iter(processType);
static function processType(type:Type) switch (type)
{
case TInst(t, _):
if (t.get().name == 'Test') processFields(t.get().fields.get());
case _:
}
static function processFields(fields:Array<ClassField>)
{
for (field in fields)
if (field.name == 'build')
field.meta.add('@:keep', [], haxe.macro.Context.currentPos());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment