GCC-XML:GCC 的 XML 輸出
[
GCC-XML] 是個相當特別的專案,官方網頁的簡介如下:
Development tools that work with programming languages benefit from their ability to understand the code with which they work at a level comparable to a compiler. C++ has become a popular and powerful language, but parsing it is a very challenging problem. This has discouraged the development of tools meant to work directly with the language.
There is one open-source C++ parser, the C++ front-end to GCC, which is currently able to deal with the language in its entirety. The purpose of the GCC-XML extension is to generate an XML description of a C++ program from GCC's internal representation. Since XML is easy to parse, other development tools will be able to work with C++ programs without the burden of a complicated C++ parser.
依據 [
GCC-XML::Links] 所示,至少有兩個專案採用此成果:
- [pyste] - Boost.Python wrapper generator based on GCC-XML.
- [pygccxml] - Python parser for GCC-XML output
在概念上,[
GCC-XML] 並不算嶄新,比方說 SWIG 一類的 toolkit 就可以協助開發者跨越諸多程式語言的限制,然而,[
GCC-XML] 採用 XML 作為 GCC output 卻是相當特別。相較於 SWIG 以 callback 為基礎的途徑,[
GCC-XML] 可更有彈性,並且作為一個 portable intermediate representation,這允許更複雜的語法使用。
Debian 已經有收錄 [
Package: gccxml],我們實地寫一個 C++ 程式來測試:
$ cat example.cpp
#define _out_ __attribute((gccxml("out")))
class DummyClass {};
int single_function(_out_ float f, DummyClass e)
{
}
int main(void)
{
return 0;
}
依照 [
GCC-XML::Running] 的指示,產生對應的 GCC XML 輸出:
$ gccxml example.cpp -fxml=example.xml
以 mlview (GNOME-based XML viewer) 查看其內容:

我們可以從中窺見 GCC compiled symbols、builtins,以及 C++ mangled,比方說圖中的 mangled 即:
$ c++filt _ZN10DummyClassC1ERKS_
DummyClass::DummyClass(DummyClass const&)
也就是 G++ 所產生的 default copy constructor。
由 jserv 發表於 April 21, 2006 12:06 PM