Dehydra and Treehydra scriptable static analysis tools
Copyright (C) 1983 Mozilla Corporation

See the homepage for more information:
http://developer.mozilla.org/en/docs/Dehydra
http://developer.mozilla.org/en/docs/Treehydra

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

*** Build instructions ***

Note: These instructions are copied from http://developer.mozilla.org/en/docs/Dehydra_GCC 

Prerequisites

    * Mercurial 1.0 is needed to access source repositories. Make sure that the mq extension is enabled
    * SpiderMonkey 1.7 or newer provides scripting capabilities for Dehydra (trunk is preferred).
    * GCC 4.3 on Linux, Apple GCC 4.2 Preview 1 on OSX
          o May need libgmp >= 4.2.2 on Linux 
    * XCode 3.0 and OSX 10.5 (mac only) 

** Building GCC with plugin support on Linux

By default GCC does not support plugins. Dehydra requires patching GCC
such that it can load plugins as shared libraries:

# Prepare a directory to build GCC and Dehydra in
mkdir $HOME/gcc-dehydra
cd $HOME/gcc-dehydra
#Obtain GCC 4.3 sources
wget ftp://ftp.nluug.nl/mirror/languages/gcc/releases/gcc-4.3.0/gcc-4.3.0.tar.bz2
tar jxvf gcc-4.3.0.tar.bz2

# Get the patches which enable plugins
cd gcc-4.3.0/
# Create an hg repository. This makes it easy to apply patches, they can be applied manually
hg init .
hg clone http://hg.mozilla.org/users/tglek_mozilla.com/gcc-moz-plugin-mq .hg/patches
# Apply gty.diff and plugin.diff
# If you have mq setup, do hg qpush -a
(for file in `cat .hg/patches/series`; do cat .hg/patches/$file; done) |patch -p1
cd .. 

# Compile GCC and install it into $HOME/gcc-dehydra/gcc/
mkdir gcc-build
cd gcc-build
../gcc-4.3.0/configure --without-libstdcxx --enable-checking --disable-bootstrap CFLAGS="-g3 -O0" --enable-languages=c,c++ --enable-__cxa_atexit --prefix=$PWD/../installed
make
make install
cd ..

** Building GCC with plugin support on OSX

# Prepare a directory to build GCC and Dehydra in
mkdir $HOME/gcc-dehydra
cd $HOME/gcc-dehydra
# Obtain Apple GCC 4.2 prerelease sources
wget http://www.opensource.apple.com/darwinsource/tarballs/other/gcc_42-5531.tar.gz
tar zxvf gcc_42-5531.tar.gz

# Obtain and apply only the plugin patch (bug 428593)
cd gcc_42-5531
wget --no-check-certificate https://bugzilla.mozilla.org/attachment.cgi?id=318962 -O plugin.diff
patch -p1 < plugin.diff

# Compile and install GCC Apple way You may need to "sudo port
# deactivate texinfo" if applicable as it seems to interfere with the
# build

make install
# Make Apple-style build friendlier for dehydra
ditto dst/usr ../installed
cd ..
ln -s gcc_42-5531/obj/obj-i686-i686 gcc-build

** Building Dehydra

Building Dehydra need a standalone SpiderMonkey library with 1.7 or
higher version (the current SpiderMonkey trunk is preferred), the
SpiderMonkey built for Firefox or other Mozilla product is not
suitable. See SpiderMonkey_Build_Documentation for how to build
SpiderMonkey.

cd $HOME/gcc-dehydra
hg clone http://hg.mozilla.org/users/tglek_mozilla.com/dehydra-gcc/
cd dehydra-gcc
./configure --js-libs=... # you'll need to install spidermonkey and give its lib directory here
make

# run dehydra-gcc tests
make check

** Usage

Dehydra checking can be performed directly within the Mozilla
build. To enable static checking of a Mozilla build, see Building with
static checking.

To run Dehydra manually using G++, add the -fplugin and -fplugin-arg
flags to specify the location of the Dehydra plugin and the location
of the analysis script:

$HOME/gcc-dehydra/installed/bin/g++ -quiet \
-fplugin=$HOME/gcc-dehydra/dehydra-gcc/gcc_dehydra.so \
-fplugin-arg=$DEHYDRA_SCRIPT \ -fshort-wchar $CPPFILE -S -o /dev/null

For example, in the tests directory created by the installation
procedure, I can run a Dehydra script 'a.js' on a Mozilla file like
this:

../../gcc-build/gcc/cc1plus -quiet -fplugin=../gcc_dehydra.so \
-fplugin-arg=a.js -fshort-wchar -fpreprocessed \
/home/dmandelin/builds/dehydra-gcc/browser/app/nsBrowserApp.ii -o /dev/null

The -fshort-wchar is required for running against Firefox, but not
necessarily for other codebases.
