[cfe-users] Matching K&R style function definitions?

Marshall Clow mclow.lists at gmail.com
Thu Oct 16 08:51:10 PDT 2014

Suppose I have a function def like this:

	double func(a, b)
	int a;
	float b;
		return a * b;

and I would like to rewrite it as:

	double func(int a, float b)
		return a * b;

What’s the best way to write a matcher for the first form?

I dumped the AST for both of them, and didn’t see any obvious sign:

[ K&R ]
|-FunctionDecl 0x7ffd5302ab10 <kr.c:1:1, line:6:1> line:1:8 used func 'double (int, double)'
| |-ParmVarDecl 0x7ffd5302a9d0 <line:2:1, col:5> col:5 used a 'int'
| |-ParmVarDecl 0x7ffd5302aa40 <line:3:1, col:7> col:7 used b 'float'
| `-CompoundStmt 0x7ffd5302acb8 <line:4:1, line:6:1>
|   `-ReturnStmt 0x7ffd5302ac98 <line:5:2, col:13>
|     `-ImplicitCastExpr 0x7ffd5302ac80 <col:9, col:13> 'double' <FloatingCast>
|       `-BinaryOperator 0x7ffd5302ac58 <col:9, col:13> 'float' '*'
|         |-ImplicitCastExpr 0x7ffd5302ac40 <col:9> 'float' <IntegralToFloating>
|         | `-ImplicitCastExpr 0x7ffd5302ac10 <col:9> 'int' <LValueToRValue>
|         |   `-DeclRefExpr 0x7ffd5302abc0 <col:9> 'int' lvalue ParmVar 0x7ffd5302a9d0 'a' 'int'
|         `-ImplicitCastExpr 0x7ffd5302ac28 <col:13> 'float' <LValueToRValue>
|           `-DeclRefExpr 0x7ffd5302abe8 <col:13> 'float' lvalue ParmVar 0x7ffd5302aa40 'b' 'float'

[ ANSI ]
|-FunctionDecl 0x7ff6d9829110 <kr.c:8:1, line:11:1> line:8:8 used func 'double (int, float)'
| |-ParmVarDecl 0x7ff6d9828fd0 <col:13, col:17> col:17 used a 'int'
| |-ParmVarDecl 0x7ff6d9829040 <col:20, col:26> col:26 used b 'float'
| `-CompoundStmt 0x7ff6d98292b8 <line:9:1, line:11:1>
|   `-ReturnStmt 0x7ff6d9829298 <line:10:2, col:13>
|     `-ImplicitCastExpr 0x7ff6d9829280 <col:9, col:13> 'double' <FloatingCast>
|       `-BinaryOperator 0x7ff6d9829258 <col:9, col:13> 'float' '*'
|         |-ImplicitCastExpr 0x7ff6d9829240 <col:9> 'float' <IntegralToFloating>
|         | `-ImplicitCastExpr 0x7ff6d9829210 <col:9> 'int' <LValueToRValue>
|         |   `-DeclRefExpr 0x7ff6d98291c0 <col:9> 'int' lvalue ParmVar 0x7ff6d9828fd0 'a' 'int'
|         `-ImplicitCastExpr 0x7ff6d9829228 <col:13> 'float' <LValueToRValue>
|           `-DeclRefExpr 0x7ff6d98291e8 <col:13> 'float' lvalue ParmVar 0x7ff6d9829040 'b' 'float'

Thanks in advance!

— Marshall

More information about the cfe-users mailing list