1

I have a C file with functions and calls. I'm searching for a way to inline all calls. The output has to be a C file with only one function implementation.

There are very few extern functions which should not be inlined (like scanf or functions marked with extern). It would be ok if I had to list these functions manually.

Impractical Solutions

  • Do the inlining by hand
    The files are too big. One file has 2'757 functions and 1'500'000 lines of code.

  • A self-written script doing plain text processing.
    The C standard can be quite complicated sometimes. I wouldn't trust such a script to get everything right.

  • Decompiling after compiling with inlining.
    Even with the inline keyword the compiler decides what to inline. There is no guarantee that all functions will be inlined. The output could be very messy.

I tagged this question as because I thought I had seen a compiler option for gcc or clang that did exactly what I wanted. But I cannot find said option anymore. I'm not even sure such an option did exist.

1

The static analysis tool Frama-C also has some preprocessing capabilities. From Frama-C's manual:

-inline-calls <f1,...,fn>
         inlines calls to functions f1,...,fn. [...]

I used the following command to inline functions.

frama-c -inline-calls ... -print -ocode output.c iutput.c

To list the functions to be inlined I wrote a small text processing script. First GNU indent is applied to the program, such that all function definitions are in some sort of normal form. Then the function names are extracted using grep -Po.

Frama-C does inlining very well. However, the inlined functions are still left in the program, even though they are neither called nor needed anymore. To remove all functions but main I wrote another text processing script.

Since Frama-C's preprocessing is only for its own purpose, things from stdio got a bit mangled. printf and scanf were renamed to functions like printf_va_34 and scanf_va_1. I used the following sed command to undo the renaming:

sed -Ei 's/(printf|scanf)_va_[0-9]*/\1/;s/__fc_(stdout|stderr)/\1/' inlined.c

The sed command is unsafe. For instance if printf_va_123 was not a function call but a substring of a variable name or string literal it would still be renamed.

Finally I used gcc -fsyntax-only -w inlined.c to check that all the plain text processing did not mangle the program.

  • Welcome aboard. This is a good answer, and I hope that it will be awarded soon. – Mawg Sep 27 '18 at 12:26
0

There is ctags. The output however is not a c file, but an index file for editors like vim. Maybe you can write something that translates that index file to your needs. Hope helps! Here link to wiki https://en.m.wikipedia.org/wiki/Ctags

  • 1
    I don't understand how ctags would achieve what the OP wants – Mawg Sep 26 '18 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.