搜索具有特定签名呼叫的功能?

问题描述 投票:1回答:1

假设我有一些C ++函数:

void A(int i) {
/* some code */
}

void A(string s) {
/* some code */
}

void A(string s, int i) {
/* some code */
}

假设第一个调用占80%的A()调用,第二个调用占15%,最后调用占5%。

我想静态跟踪调用。如果我对第一种调用感兴趣,没问题,大多数字符串搜索结果“A(”将是类型1,但如果我只想要类型2或类型3,我会有很多不需要的1型噪音。

对于类型3,正则表达式可以帮助我找到一个在括号A(*,*,*)之间具有正好2个coma的后续字符串(我实际上不知道RE的编程语法)

但是对于类型2,这将不起作用。

是否有任何技术可以通过其签名找到函数调用?

编辑:我的意思是“跟踪”是通过查找所需函数的所有调用点来理解当前代码库。

c++ function debugging signature tracing
1个回答
2
投票

对于类型3,正则表达式可以帮助我找到一个后面的字符串,在括号A(,, *)之间恰好有2个逗号(我实际上不知道RE的编程语法)

但是对于类型2,这将不起作用。

是否有任何技术可以通过其签名找到函数调用?

除了你使用一些正则表达式搜索你的文件(例如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) {
                         ^

查看使用g ++编译的online example

您甚至可以通过消息为您的同事维护代码库来装饰它:

   [[deprecated("Get rid of these performance killing calls."
                " Use A(A::getPrecomputedHash(s)) instead.")]] 
      void A(string s) {
      }
© www.soinside.com 2019 - 2024. All rights reserved.