开发者

extract arguments from function with scripting

开发者 https://www.devze.com 2023-01-09 05:05 出处:网络
I have a file with function prototypes like this: int func1(type1 arg, int x); type2 funct2(int z, char* buffer);

I have a file with function prototypes like this:

int func1(type1 arg, int x);

type2 funct2(int z, char* buffer);

I want to create a script (bash, sed, awk, whatever) that will print

function = func1 // first argument type = type1// second argument type = int
function = func1 // first argument type = int// second argument type = char*

In other words, tokenize every line and print the function names and arguments. Additionally I would like to hold these tokens as variable开发者_运维百科s to print them later, eg echo $4.


An alternative approach would be to compile with "-g" and read the debug information.
This answer may help you read the debug information and figure out function parameters (it's Python, not bash, but I'd recommend using Python or Perl instead of bash anyway).

The resulting solution would be much more robust than anything based on text parsing. It will deal with all the different ways in which a function might be defined, and even with crazy things like functions defined in macros.

To convince you better (or help you get it right if you're not convinced), here's a list of test cases that may break your parsing:

// Many lines
const
char
*

f
(
int
x
)
{
}

// Count parenthesis!
void f(void (*f)(void *f)) {}

// Old style
void f(a, b)
int a;
char *b
{
}

// Not a function
int f=sizeof(int);

// Nesting
int f() {
    int g() { return 1; }
    return g();
}

// Just one
void f(int x /*, int y */) { }

// what if?
void (int x
#ifdef ALSO_Y
     , int y
#endif
) { }

// A function called __attribute__?
static int __attribute__((always_inline)) f(int x) {}


here's a start.

#!/bin/bash
#bash 3.2+
while read -r line
do
  line="${line#* }"
  [[ $line =~ "^(.*)\((.*)\)" ]]
  echo  "function: ${BASH_REMATCH[1]}"
  echo  "args: ${BASH_REMATCH[2]}"
  ARGS=${BASH_REMATCH[2]}
  FUNCTION=${BASH_REMATCH[1]}
  # break down the arguments further.
  set -- $ARGS
  echo "first arg type:$1 , second arg type: $2"
done <"file"

output

$ ./shell.sh
function: func1
args: type1 arg, int x
first arg type:type1 , second arg type: arg,
function: funct2
args: int z, char* buffer
first arg type:int , second arg type: z,
0

精彩评论

暂无评论...
验证码 换一张
取 消