假设我有一些 C++ 函数:
void A(int i) {
/* some code */
}
void A(string s) {
/* some code */
}
void A(string s, int i) {
/* some code */
}
假设第一次调用占 A() 调用的 80%,第二次占 15%,最后一个占 5%。
我想静态跟踪调用。如果我对第一种调用感兴趣,没问题,“A(”的大多数字符串搜索结果都是类型 1,但如果我只想要类型 2 或类型 3,我将有很多不需要的来自类型 1 的噪声。
对于类型 3,如果我寻找在括号 A(*,*,*)
之间恰好有 2 个逗号的以下字符串,正则表达式会有所帮助(我实际上并不知道编程语法对于 RE)
但是对于类型 2 这将不起作用。
有什么技术可以用来通过签名查找函数调用吗?
编辑:我所说的“跟踪”是指通过查找所需函数的所有调用点来理解当前代码库。
最佳答案
For type 3, regular expressions can help if I look for a following string that has exactly 2 comas between parenthesis A(,,*) (I don't actually know the programming syntax for RE)
But for type 2 this won't work.
Is there any technique I can use to find a function call by its signature?
除了使用一些正则表达式(例如 Notepad++ 文件搜索、egrep 或类似工具)搜索文件之外,假设您能够更改声明/定义这些函数的源代码,您还可以使用一些编译器标准类似 [[deprecated]]
的功能属性:
void A(int i) {
/* some code */
}
[[deprecated]] void A(string s) {
// ^^^^^^^^^^^^^^
/* some code */
}
[[deprecated]] void A(string s, int i) {
// ^^^^^^^^^^^^^^
/* some code */
}
这将在使用这些功能时向您显示警告:
int main() {
A(5);
A("Hello");
A("Hello",42);
}
main.cpp:9:25: note: declared here [[deprecated]] void A(string s) { ^ main.cpp:20:18: warning: 'void A(std::__cxx11::string)' is deprecated [-Wdeprecated-declarations] A("Hello"); ^ main.cpp:9:25: note: declared here [[deprecated]] void A(string s) { ^ main.cpp:21:21: warning: 'void A(std::__cxx11::string, int)' is deprecated [-Wdeprecated-declarations] A("Hello",42); ^ main.cpp:13:25: note: declared here [[deprecated]] void A(string s, int i) { ^ main.cpp:21:21: warning: 'void A(std::__cxx11::string, int)' is deprecated [-Wdeprecated-declarations] A("Hello",42); ^ main.cpp:13:25: note: declared here [[deprecated]] void A(string s, int i) { ^
查看 online example用 g++ 编译。
您甚至可以为维护代码库的同事添加一条消息来装饰它:
[[deprecated("Get rid of these performance killing calls."
" Use A(A::getPrecomputedHash(s)) instead.")]]
void A(string s) {
}
关于c++ - 搜索具有特定签名调用的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55597428/