Using Dehydra

As GCC compiles file, Dehydra calls functions in the user analysis script with information about the code being compiled. For more information, see the function reference and the object reference.

Example: printing the location of type declarations

Save the following C++ code dumptypes.cc:

typedef int MyInt;
struct Foo { int i; char *c; };

Save the following analysis script dumptypes.js:

function process_type(t)
{
  print("Type found: " + t.name + " location: " + t.loc);
}

function input_end()
{
  print("Hello, world!");
}

Compile using the following command:

$ g++ -fplugin=~/dehydra/gcc_dehydra.so -fplugin-arg=~/dumptypes.js -o/dev/null -c dumptypes.cc

Note:for g++4.5 and up

use -fplugin-arg-gcc_dehydra-script= rather than -fplugin-arg

It should print the following results:

Type found: Foo location: test.cc:2:12
Type found: MyInt location: test.cc:1:13
Hello, world!

See documentation for: process_type, input_end, print, .loc property

Example: using attributes to mark a class as "final"

Save the following code as final.cc:

// This class should not be subclassed!
class __attribute__((user("final"))) MyClass
{
};

// This subclass should be an error
class SubClass : public MyClass
{
};

Save the following analysis script final.js:

/**
 * Helper function: returns true if a class is marked with the "final" attribute.
 */
function isFinal(c)
{
  if (!c.attributes)
    return false;
  for each (let a in c.attributes)
    if (a.name == 'user' && a.value == 'final')
      return true;
  return false;
}

function process_type(t)
{
  if (t.bases)
    for each (let base in t.bases)
      if (isFinal(base.type))
        error("class " + t.name + " extends final class " + base.type.name, t.loc);
}

Compile using the following command:

$ g++ -fplugin=~/dehydra/gcc_dehydra.so -fplugin-arg=~/final.js -o/dev/null -c final.cc

It should print the following results and return with an error code:

final.cc:8: error: class SubClass extends final class MyClass

See documentation for: process_type, error, .bases property, .attributes property