how to make gcc spit out a mapping from flow graphs to source code line numbers

前端 未结 3 1979
迷失自我
迷失自我 2021-02-08 13:06

Can gcc spit out, given a C file, a list of the all function calls that occur, with filename and line number both for the call itself and for the function\'s declaration?

<
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-08 13:32

    Try gcc option -fdump-tree-fixupcfg-lineno.

    It will "pretty print" parsing AST (with line numbers) in a way that can easily be parsed using relatively simple lexer or any regex engine. Just find all non-keywords preceded by '=' and followed by '(' - it will be function calls.

    All complex expressions will be split into several lines so no two function calls will appear on one line.

    Take simple program:

    #include 
    #include 
    #include 
    
    #define PI (3.1415926536)
    
    int main(int argc, char *argv[]) {
        double  angle = PI / 2.0;
        printf("Sine = %lf, cosine = %lf\n", sin(angle), cos(angle));
        return EXIT_SUCCESS;
    }
    

    Compile it with -fdump-tree-fixupcfg-lineno and you get something like this:

    main (argc, argv)
    {
      double angle;
      int D.3381;
      double D.3380;
      double D.3379;
    
      # BLOCK 2, starting at line 8
      # PRED: ENTRY (fallthru)
      [test.c : 8] angle = 1.57079632680000003119857865385711193084716796875e+0;
      [test.c : 9] D.3379 = [test.c : 9] cos (angle);
      [test.c : 9] D.3380 = [test.c : 9] sin (angle);
      [test.c : 9] printf (&"Sine = %lf, cosine = %lf\n"[0], D.3380, D.3379);
      [test.c : 10] D.3381 = 0;
      return D.3381;
      # SUCC: EXIT
    
    }
    

    You won't get any complex expressions - just assignments and function call and no CPP macros, very easy to parse. Loops and conditionals don't make it much more difficult.

提交回复
热议问题