Is there a program (or sed script) that will remove a function from a C file?

I need a script where I pass the C file and the name of the function and it will remove that C function from the file. Is there a utility that will do this?

In principle, I want to open a source library developed domestically and would like to keep its history. But there are certain functions that I cannot open. I use the git filter branch to delete entire files that cannot be opened, but now I have reached the point where I need to remove certain functions from the files, where parts of the file can be open, but others cannot, Because git filter-branch runs the command for each commit, I need something automatic that will remove this function from each commit of the repository.

+6
source share
4 answers

As lreeder, I would recommend code refactoring so that open and private files are in different files / libraries.

However, if this does not work for you, I think you should not resort to a complete analysis of the C code. Instead, you can do something simple, for example, put the personal parts of the file between comments like //PRIVATE and //END-PRIVATE . Then you can use ruby script to delete private sections:

 #!/usr/bin/ruby # remove_private.rb: Removes all lines between "PRIVATE" and "END-PRIVATE" # # Usage 1, output to STDIO: cat filename.c | ruby remove_private.rb # Usage 2, output to STDIO: ruby remove_private.rb filename.c # Usage 3, in-place editing of file (and creating a backup): ruby -i.bak remove_private.rb filename.c while line = ARGF.gets unless (line =~ /PRIVATE/)..(line =~ /END-PRIVATE/) puts line end end 

This uses some magic and convenient ruby ​​functions to make the code short, but I think it will be much easier to debug and maintain than the full C parser. In addition, developers can easily see which functions are private while they work on the code. I did something very similar to this for the project I was working on, where we wanted to release part of the Makefile, but not all. Perhaps you can rewrite it in your favorite scripting language.

+3
source

Can you reorganize all non-open C functions into a separate file / library? This is work, but it will be cleaner than changing the source code for each public release and will probably be easier to maintain in the long run.

If refactoring is not an option, I found the perl-based source code parser at http://search.cpan.org/~jtbraun/Parse-RecDescent-1.967009/demo/demo_another_Cgrammar.pl . It looks like with some hacks, it can filter out unwanted features.

+2
source

The path to action without parsing C code would be to canonize function definitions in a form that awk can easily remove, for example

 sometype_t *function (type_t arg, ...) { ... } 

and then remove from the function definition the correspondence '}' fixed at the beginning of the line. If you are already using the right indent, this should be easy. I saw encoding rules that for this reason require placing the function name below the return type, as in

 sometype_t * function (type_t arg, ...) { ... } 

which makes it even easier.

0
source

Emacs He already knows how to select a region of text containing a function definition, and it will be less dependent on a particular coding style than many other methods. This makes it less likely that something will be missed. With a few emacs lisp scripts, you can search the entire source tree non-interactively.

0
source

Source: https://habr.com/ru/post/946351/


All Articles