Python拡張モジュールの静的コード解析にgcc-with-cpycheckerを使う

gccをPythonから使用できるプロジェクト GCC Python plugin というものがあります。 そのなかでPythonのC拡張モジュールを静的コード解析できるツール gcc-with-cpychecker というものがあります。 今日はその使い方を紹介します。

Instllation

$ sudo apt-get install gcc-4.7-plugin-dev
$ git clone git://git.fedorahosted.org/gcc-python-plugin.git
$ cd gcc-python-plugin
$ make plugin
$ make

gcc-python-plugin ごとPATHを通しておきます。

Usage

otamapy で試してみました。

$ git clone https://github.com/hhatto/otamapy
$ cd otamapy
$ CC=`which gcc-with-cpychecker` python setup.py build
$ ls build/temp.linux-i686-2.7/otamapy/otama.c.*.html
build/temp.linux-i686-2.7/otamapy/otama.c.OtamaObject_create_table-refcount-errors.html
build/temp.linux-i686-2.7/otamapy/otama.c.OtamaObject_drop_table-refcount-errors.html
build/temp.linux-i686-2.7/otamapy/otama.c.OtamaObject_exists-refcount-errors.html
build/temp.linux-i686-2.7/otamapy/otama.c.OtamaObject_feature_raw-refcount-errors.html
build/temp.linux-i686-2.7/otamapy/otama.c.OtamaObject_feature_string-refcount-errors.html
build/temp.linux-i686-2.7/otamapy/otama.c.OtamaObject_insert-refcount-errors.html
build/temp.linux-i686-2.7/otamapy/otama.c.OtamaObject_new-refcount-errors.html
build/temp.linux-i686-2.7/otamapy/otama.c.OtamaObject_pull-refcount-errors.html
build/temp.linux-i686-2.7/otamapy/otama.c.OtamaObject_remove-refcount-errors.html
build/temp.linux-i686-2.7/otamapy/otama.c.OtamaObject_similarity-refcount-errors.html
build/temp.linux-i686-2.7/otamapy/otama.c.make_results-refcount-errors.html
build/temp.linux-i686-2.7/otamapy/otama.c.setup_config-refcount-errors.html
build/temp.linux-i686-2.7/otamapy/otama.c.variant2pyobj-refcount-errors.html

build/temo.XX 配下にHTML形式の解析レポートが出力されます。 中身は以下のような感じ。

http://image.hexacosa.net/images/container/0000000000000461_l.png

問題が発見されました、チャンチャン。

Python3にも対応しているようなので、一度使ってみてはいかがでしょうか。でわ。