2012-09-01 39 views
4

我正在調試/記憶工具上工作。我想顯示來自C++的符號,問題是它們非常冗長。目前我只使用__cxa_demangle,但由於包含了默認的模板參數,這通常會導致超過500個字符的巨大字符串。簡化複雜的C++模板符號

clang++當它報告符號時可以明確地做出聰明的事情,有什麼方法可以利用它嗎?

舉一個簡單的例子,讓我們:

std::map<std::string const, unsigned int, std::less<std::string const>, std::allocator<std::pair<std::string const, unsigned int> > >::find(std::string const&) 

其中明確可能被報告爲:

std::map<std::string const, unsigned int>::find(std::string const&) 

..如果我有一個足夠聰明的工具。很顯然,如果沒有額外的知識,就很難做到這一點(比如最初使用的包含內容 - 我可能會了解這些內容),但我會很感激任何提示。

到目前爲止,我一直指向libcxxabi,但除了沒有公共接口的分析樹(這不會阻止我自己),似乎我必須做的努力工作確定哪些參數是默認值。如果我能以某種方式欺騙鐺聲爲我做這件事,那將是非常棒的。

+4

我想我們都希望有更好的模板調試經驗。我記得不得不深入研究'boost :: ptr_map'進行調試,因爲所有這些默認參數突然顯示並填充頁面,所以它很糟糕。儘管如此,我的實用主義可能會爲一些類型的內置默認參數列表,以及基於此列表的elision。 –

+0

您可以編寫自己的邏輯:將參數列表解析爲參數,爲您關心的模板提供默認映射,並檢查參數是否與默認映射匹配(從後面開始)。 –

+0

不知何故,你的簡單例子已經很幸運了,因爲你顯示'std :: string const'而不是'std :: basic_string ,std :: allocator > > const'。看起來你已經利用了typedef和specializations? –

回答

5

STLFilt可以幫到你。有兩個perl腳本,STLFilt.pl(用於Visual C++)和gSTLFilt.p(用於gcc)。它旨在用於錯誤消息的簡化,但我已經將其用於後處理__cxa_demangle的輸出。

用在你的簡單的例子,不帶任何選項:

echo "std::map<std::string const, unsigned int, std::less<std::string const>, std::allocator<std::pair<std::string const, unsigned int> > >::find(std::string const&)" \ 
| perl ./gSTLFilt.pl 

獲取輸出:

BD Software STL Message Decryptor v3.10 for gcc 2/3/4 
map<string const, unsigned int>::find(string const &) 

如果你想用它的選擇玩,你應該能夠得到定製reformating(我的天堂沒有嘗試過)。