# [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 ]
[0;34m|-[0m[0;1;32mFunctionDecl[0m[0;33m 0x7ffd5302ab10[0m <[0;33mkr.c:1:1[0m, [0;33mline:6:1[0m> [0;33mline:1:8[0m used[0;1;36m func[0m [0;32m'double (int, double)'[0m
[0;34m| |-[0m[0;1;32mParmVarDecl[0m[0;33m 0x7ffd5302a9d0[0m <[0;33mline:2:1[0m, [0;33mcol:5[0m> [0;33mcol:5[0m used[0;1;36m a[0m [0;32m'int'[0m
[0;34m| |-[0m[0;1;32mParmVarDecl[0m[0;33m 0x7ffd5302aa40[0m <[0;33mline:3:1[0m, [0;33mcol:7[0m> [0;33mcol:7[0m used[0;1;36m b[0m [0;32m'float'[0m
[0;34m| `-[0m[0;1;35mCompoundStmt[0m[0;33m 0x7ffd5302acb8[0m <[0;33mline:4:1[0m, [0;33mline:6:1[0m>
[0;34m|   `-[0m[0;1;35mReturnStmt[0m[0;33m 0x7ffd5302ac98[0m <[0;33mline:5:2[0m, [0;33mcol:13[0m>
[0;34m|     `-[0m[0;1;35mImplicitCastExpr[0m[0;33m 0x7ffd5302ac80[0m <[0;33mcol:9[0m, [0;33mcol:13[0m> [0;32m'double'[0m[0;36m[0m[0;36m[0m <[0;31mFloatingCast[0m>
[0;34m|       `-[0m[0;1;35mBinaryOperator[0m[0;33m 0x7ffd5302ac58[0m <[0;33mcol:9[0m, [0;33mcol:13[0m> [0;32m'float'[0m[0;36m[0m[0;36m[0m '*'
[0;34m|         |-[0m[0;1;35mImplicitCastExpr[0m[0;33m 0x7ffd5302ac40[0m <[0;33mcol:9[0m> [0;32m'float'[0m[0;36m[0m[0;36m[0m <[0;31mIntegralToFloating[0m>
[0;34m|         | `-[0m[0;1;35mImplicitCastExpr[0m[0;33m 0x7ffd5302ac10[0m <[0;33mcol:9[0m> [0;32m'int'[0m[0;36m[0m[0;36m[0m <[0;31mLValueToRValue[0m>
[0;34m|         |   `-[0m[0;1;35mDeclRefExpr[0m[0;33m 0x7ffd5302abc0[0m <[0;33mcol:9[0m> [0;32m'int'[0m[0;36m lvalue[0m[0;36m[0m [0;1;32mParmVar[0m[0;33m 0x7ffd5302a9d0[0m[0;1;36m 'a'[0m [0;32m'int'[0m
[0;34m|         `-[0m[0;1;35mImplicitCastExpr[0m[0;33m 0x7ffd5302ac28[0m <[0;33mcol:13[0m> [0;32m'float'[0m[0;36m[0m[0;36m[0m <[0;31mLValueToRValue[0m>
[0;34m|           `-[0m[0;1;35mDeclRefExpr[0m[0;33m 0x7ffd5302abe8[0m <[0;33mcol:13[0m> [0;32m'float'[0m[0;36m lvalue[0m[0;36m[0m [0;1;32mParmVar[0m[0;33m 0x7ffd5302aa40[0m[0;1;36m 'b'[0m [0;32m'float'[0m

[ ANSI ]
[0;34m|-[0m[0;1;32mFunctionDecl[0m[0;33m 0x7ff6d9829110[0m <[0;33mkr.c:8:1[0m, [0;33mline:11:1[0m> [0;33mline:8:8[0m used[0;1;36m func[0m [0;32m'double (int, float)'[0m
[0;34m| |-[0m[0;1;32mParmVarDecl[0m[0;33m 0x7ff6d9828fd0[0m <[0;33mcol:13[0m, [0;33mcol:17[0m> [0;33mcol:17[0m used[0;1;36m a[0m [0;32m'int'[0m
[0;34m| |-[0m[0;1;32mParmVarDecl[0m[0;33m 0x7ff6d9829040[0m <[0;33mcol:20[0m, [0;33mcol:26[0m> [0;33mcol:26[0m used[0;1;36m b[0m [0;32m'float'[0m
[0;34m| `-[0m[0;1;35mCompoundStmt[0m[0;33m 0x7ff6d98292b8[0m <[0;33mline:9:1[0m, [0;33mline:11:1[0m>
[0;34m|   `-[0m[0;1;35mReturnStmt[0m[0;33m 0x7ff6d9829298[0m <[0;33mline:10:2[0m, [0;33mcol:13[0m>
[0;34m|     `-[0m[0;1;35mImplicitCastExpr[0m[0;33m 0x7ff6d9829280[0m <[0;33mcol:9[0m, [0;33mcol:13[0m> [0;32m'double'[0m[0;36m[0m[0;36m[0m <[0;31mFloatingCast[0m>
[0;34m|       `-[0m[0;1;35mBinaryOperator[0m[0;33m 0x7ff6d9829258[0m <[0;33mcol:9[0m, [0;33mcol:13[0m> [0;32m'float'[0m[0;36m[0m[0;36m[0m '*'
[0;34m|         |-[0m[0;1;35mImplicitCastExpr[0m[0;33m 0x7ff6d9829240[0m <[0;33mcol:9[0m> [0;32m'float'[0m[0;36m[0m[0;36m[0m <[0;31mIntegralToFloating[0m>
[0;34m|         | `-[0m[0;1;35mImplicitCastExpr[0m[0;33m 0x7ff6d9829210[0m <[0;33mcol:9[0m> [0;32m'int'[0m[0;36m[0m[0;36m[0m <[0;31mLValueToRValue[0m>
[0;34m|         |   `-[0m[0;1;35mDeclRefExpr[0m[0;33m 0x7ff6d98291c0[0m <[0;33mcol:9[0m> [0;32m'int'[0m[0;36m lvalue[0m[0;36m[0m [0;1;32mParmVar[0m[0;33m 0x7ff6d9828fd0[0m[0;1;36m 'a'[0m [0;32m'int'[0m
[0;34m|         `-[0m[0;1;35mImplicitCastExpr[0m[0;33m 0x7ff6d9829228[0m <[0;33mcol:13[0m> [0;32m'float'[0m[0;36m[0m[0;36m[0m <[0;31mLValueToRValue[0m>
[0;34m|           `-[0m[0;1;35mDeclRefExpr[0m[0;33m 0x7ff6d98291e8[0m <[0;33mcol:13[0m> [0;32m'float'[0m[0;36m lvalue[0m[0;36m[0m [0;1;32mParmVar[0m[0;33m 0x7ff6d9829040[0m[0;1;36m 'b'[0m [0;32m'float'[0m