.logbook

学んだことを書き綴る、言わば航海日誌です。

Doxygenで関数コールツリーを出力する

以前の記事でC言語ソースコードを解析するためのVim環境を構築した。

C言語のソースコードを解析するため、Vimにプラグインを追加する - .logbook

しかし、大規模なS/Wともなるとエディタだけでは解析に限界がある。そこで今回、ソースコードからドキュメントをリバース・エンジニアリングするためのツール「Doxygen」をインストールすることにした。

Windows版のDoygenであればGUIで設定ができるが、今回はあえてUbuntu上に環境を構築し、コンソールベースで設定することとした。

Doxygenのインストール

毎度お馴染み、apt-getでさくっとインストールする。

$ sudo apt-get install doxygen

今回はC言語ソースコードから関数のコールツリーを出力させたい。そこで、ドキュメント情報をコールツリー等の図表に変換するため、もう一つツールをインストールする。

$ sudo apt-get install graphviz

これでインストールは完了である。

Doxygenの設定(C言語用)

Doxygenの設定を行うためにまずは設定ファイルを生成する必要がある。以下のコマンドで設定ファイルを作成できる。

$ doxygen -g test

コマンドを実行すると、カレントディレクトリにtestという設定ファイルが出来上がっていることがわかる。このファイルをエディタで開く。

$ vi test

下記の通り変更する。

# If the EXTRACT_ALL tag is set to YES doxygen will assume all entities in
# documentation are documented, even if no documentation was available.
# Private class members and static file members will be hidden unless
# the EXTRACT_PRIVATE respectively EXTRACT_STATIC tags are set to YES

EXTRACT_ALL            = YES

~省略~

# The INPUT tag can be used to specify the files and/or directories that contain
# documented source files. You may enter file names like "myfile.cpp" or
# directories like "/usr/src/myproject". Separate the files or directories
# with spaces.

INPUT                  = "ソース一式のルートディレクトリ"

~省略~

# If the value of the INPUT tag contains directories, you can use the
# FILE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp
# and *.h) to filter out the source-files in the directories. If left
# blank the following patterns are tested:
# *.c *.cc *.cxx *.cpp *.c++ *.d *.java *.ii *.ixx *.ipp *.i++ *.inl *.h *.hh
# *.hxx *.hpp *.h++ *.idl *.odl *.cs *.php *.php3 *.inc *.m *.mm *.dox *.py
# *.f90 *.f *.for *.vhd *.vhdl

FILE_PATTERNS          = *.c *.h *.cpp

# The RECURSIVE tag can be used to turn specify whether or not subdirectories
# should be searched for input files as well. Possible values are YES and NO.
# If left blank NO is used.

RECURSIVE              = YES


~省略~

# If you set the HAVE_DOT tag to YES then doxygen will assume the dot tool is
# available from the path. This tool is part of Graphviz, a graph visualization
# toolkit from AT&T and Lucent Bell Labs. The other options in this section
# have no effect if this option is set to NO (the default)

HAVE_DOT               = YES

~省略~

# If the CALL_GRAPH and HAVE_DOT options are set to YES then
# doxygen will generate a call dependency graph for every global function
# or class method. Note that enabling this option will significantly increase
# the time of a run. So in most cases it will be better to enable call graphs
# for selected functions only using the \callgraph command.

CALL_GRAPH             = YES

# If the CALLER_GRAPH and HAVE_DOT tags are set to YES then
# doxygen will generate a caller dependency graph for every global function
# or class method. Note that enabling this option will significantly increase
# the time of a run. So in most cases it will be better to enable caller
# graphs for selected functions only using the \callergraph command.

CALLER_GRAPH           = YES

Doxygenの実行

設定が終わったら仕様書を生成する。ソースの規模に応じて時間がかかる。

$ doxygen test

終わったら、カレントディレクトリに作成されたhtmlフォルダ配下のindex.htmlをブラウザで開く。

$ firefox ./html/index.html

このように関数毎に、「コールされる関数」と「コールする関数」をツリー化した状態で確認できる。

f:id:ylgbk:20131217195144p:plain