2011-03-04 41 views

回答

18

單向編譯器(依賴於編譯器)是在每個編譯器步驟之後使用轉儲。我寫了一個小程序:

template<class T> 
T square(T n) 
{ 
    return n * n; 
} 

int main(void) 
{ 
    square<int>(3); 
    square<float>(3.0); 
} 

則:

g++ -fdump-rtl-all test.cc 

這讓我一堆文件。看看(在我的情況下)test.cc.218.dfinish:

;; Function int main() (main) 
;; Function T square(T) [with T = int] (_Z6squareIiET_S0_) 
;; Function T square(T) [with T = float] (_Z6squareIfET_S0_) 
+0

有誰知道是否有類似的Visual Studio編譯器開關? – RedX 2012-05-30 07:53:55

+0

我唯一能想到的就是使用[Visual C++代碼模型](http://msdn.microsoft.com/zh-cn/library/t41260xs.aspx)。不知道這是否可以幫到你 – maverik 2012-05-30 08:01:56

+2

GCC編譯src' => [GENERIC](http://gcc.gnu.org/onlinedocs/gccint/GENERIC.html)=> [GIMPLE](http:// gcc .gnu.org/onlinedocs/gccint/GIMPLE.html)=> [RTL](http://gcc.gnu.org/onlinedocs/gccint/RTL.html)=>'asm'。要轉儲GENERIC/GIMPLE,請使用'-fdump-tree -...'([來自C/C++的GENERIC樹](http://gcc.gnu.org/onlinedocs/gccint/C-and-C_002b_002b-Trees.html)使用類似於源語言的語法)。有關[調試選項的GCC手冊部分](http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html)和[GCC內部手冊](http://gcc.gnu.org/)的更多信息onlinedocs/gccint/index.html的#頂部)。 – unthought 2012-12-06 10:43:08

7

我只有一半的屁股答案。

我不知道一種實際獲得C++源代碼的方法(已經存在),但CLang編譯器提供了一個選項-emit-ast,它轉儲用於表示解析語言的抽象語法樹。模板的各種實例將被表示。

AST是無論在內存和XML版本爲代表,所以您可以:

  • 只使用XML輸出
  • 解析它,然後產生一些C++代碼
  • 創建重寫器工具(在鐺直接支持)和消耗AST本身

對於大多數代碼檢查(包括檢查所選擇的過載)我發現,實際讀取的XML輸出(當然,grepping第粗糙它)足以滿足我的需求。

0

這是一個相當古老的問題,但我認爲在這方面已經有了顯着的改進,但尚未廣爲人知。

Metashell可以像模板實例化中的一種gdb一樣使用。這(據我所知)建立在鏗鏘的工具上。

enter image description here