gccxml を試してみた。
C++の静的解析をやりたいなーって思っていたら、gccxmlというソフトがあるらしい。
debian なら apt-get で一発ではいるのでさくっと試してみた。
//インストール
#apt-get install gccxml
//テスト用のプログラム
#vi test.c
#include <stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
//ためしにコンパイルしてみる。
#gcc test.c
//実行
#./a.out
Hello World!
//gccxmlでxml化してみる。
gccxml test.c -fxml=test.xmlできたxmlを超長いので抜粋
<Function id="_91" name="main" returns="_50" context="_1" location="f6:3" file="f6" line="3" endline="0"/>
あれ、、関数の宣言しか入っていない。。。
関数の中身がスルーされる。そういうものなのか?
関数の中身こそが解析したいとこなんだけどなぁ。。。
gcc なので、いろいろな言語に対応しているらしい。
と、いうわけで OpenCOBOL で試してみたw
OpenCOBOLはcobolを Cに変換して gccを通しているはず。。。
参考:OpenCOBOL マニュアル
http://www12.ocn.ne.jp/~peg/1_1.html
//OpenCOBOLのインストール。
#apt-get install open-cobol
//サンプル 左に余白をあけるのがCOBOL流
vi hello.cob
* Sample COBOL program
IDENTIFICATION DIVISION.
PROGRAM-ID. hello.
PROCEDURE DIVISION.
DISPLAY "Hello World!".
STOP RUN.
//コンパイル
cobc -x hello.cob
//実行
./hello
Hello World!
//gccxmlにかけてみる。 動かん
gccxml hello.cob
hello.cob:1: error: expected constructor, destructor, or type conversion before 'COBOL'
//コンパイラを指定してみる やっぱりダメ
gccxml hello.cob --gccxml-compiler cobc
/tmp/gccxml_identify_compiler20085.cc:1: Warning: Invalid indicator 'f' at column 7
/tmp/gccxml_identify_compiler20085.cc:4: Warning: Invalid indicator '_' at column 7
/tmp/gccxml_identify_compiler20085.cc:6: Warning: Invalid indicator '_' at column 7
/tmp/gccxml_identify_compiler20085.cc:8: Warning: Invalid indicator '_' at column 7
/tmp/gccxml_identify_compiler20085.pp: line 2: syntax error near unexpected token `__GNUC__'
/tmp/gccxml_identify_compiler20085.pp: line 2: `fined(__GNUC__)'
Error executing "/usr/share/gccxml-0.9/gccxml_find_flags cobc -g -O2 -g -Wall -O2 "
Could not determine GCCXML_FLAGS setting.
//オプションが悪いのか?と思って指定してみる
gccxml hello.cob --gccxml-compiler cobc --gccxml-cxxflags "-x hello.cob"
/tmp/gccxml_identify_compiler20123.cc:1: Warning: Invalid indicator 'f' at column 7
/tmp/gccxml_identify_compiler20123.cc:4: Warning: Invalid indicator '_' at column 7
/tmp/gccxml_identify_compiler20123.cc:6: Warning: Invalid indicator '_' at column 7
/tmp/gccxml_identify_compiler20123.cc:8: Warning: Invalid indicator '_' at column 7
/tmp/gccxml_identify_compiler20123.pp: line 3: IDENTIFICATIONDIVISION.: command not found
/tmp/gccxml_identify_compiler20123.pp: line 4: PROGRAM-ID.hello.: command not found
/tmp/gccxml_identify_compiler20123.pp: line 5: PROCEDUREDIVISION.: command not found
/tmp/gccxml_identify_compiler20123.pp: line 6: DISPLAYHelloWorld!.: command not found
/tmp/gccxml_identify_compiler20123.pp: line 7: STOPRUN.: command not found
/tmp/gccxml_identify_compiler20123.pp: line 10: syntax error near unexpected token `__GNUC__'
/tmp/gccxml_identify_compiler20123.pp: line 10: `fined(__GNUC__)'
Error executing "/usr/share/gccxml-0.9/gccxml_find_flags cobc -x hello.cob"
Could not determine GCCXML_FLAGS setting.
//動かんのでデバッグしてみる。
//最終的にこのshellが動いてなんかやるらしい。中を見てみる。
vi /usr/share/gccxml-0.9/gccxml_find_flags
-------抜粋--------
# Use the compiler's preprocessor to identify the compiler.
GCCXML_PID="$$"
cat > "/tmp/gccxml_identify_compiler$GCCXML_PID.cc" <<!
#if defined(__GNUC__)
GCCXML_SUPPORT="GCC"
#elif defined(__sgi) && defined(_COMPILER_VERSION)
GCCXML_SUPPORT="MIPSpro"
#elif defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 700)
GCCXML_SUPPORT="Intel"
#elif defined(__SUNPRO_CC)
GCCXML_SUPPORT="Sun"
#else
GCCXML_SUPPORT=""
#endif
!
# Run the compiler's preprocessor on the above code to identify it.
# (workaround for CC whitespace:)
if (${GCCXML_COMPILER} ${GCCXML_CXXFLAGS} \
-E "/tmp/gccxml_identify_compiler$GCCXML_PID.cc" \
| sed -e 's/[ ^]//g' \
> "/tmp/gccxml_identify_compiler$GCCXML_PID.pp";
rm -f "/tmp/gccxml_identify_compiler$GCCXML_PID.cc"
) ; then : ; else
echo "Error running \"${GCCXML_COMPILER} -E\""
exit 1
fi
-------抜粋-------- ようするに、コンパイラに食べさせる何かを作って、それを -E オプションで gcc とかのコンパイラに食べさせるらしい。
ただ、今回は、 ${GCCXML_COMPILER} の部分が OpenCOBOL の cobc になっているため、そんなの知らんといわれているみたい。
ただ、ためしにここを消してもエラーになって動作しない。
仮に動作させたとしても、関数本体が出力されるわけではないので、静的解析には使えないと思われる。
動作させる方法があったら誰か教えてください。
んなわけで、残念ですが (ノ`Д´)ノ彡┻━┻ することにしました。