Package: cppguts
cppgutsIf your project depends on some external C/C++ projects and
you want to make some changes in external functions/methods
and then copy/paste these changes automatically - this package may help you.There are two tools:editcppused to edit source code;dumpcppused to print some information about C/C++ translation unitsWe will discusseditcppas it is the objective tool.editcppdoesn't work with templates.The idea behindeditcpptooleditcppuseslibclangto find function/method definition start/end lines in text file (.c,.h,.hpp,.cppor whatever C/C++ extension).libclangparses eachdest.cppandsrc.cppand everything that is
included by#includepreprocessor directives. Theneditcpptool
selects all functions and methods defined indest.cppandsrc.cpp(it assumes that you know where old and new function/method definition resides) and tries to find matching functions/methods. After thateditcppcopies function/method definition fromsrc.cppand pastes it to thedest.cppwhile deleting old funcion/method definition.To find common functioneditcppchecks:are they bothdefinitions?are they bothconst?are they bothstatic?are they bothvitual?are they bothfunctions?are they bothmethods?do they both have the same name?do they both have the same return type and arg types?do they both have the same semantic parent (classname)? (for methods only)Notesyou are free to pass any clang argument aftereditcppotions but don't pass file without flag to clang (as we usually do when using clang)if you parse files that includes non standard headers then you probably need to pass-I<include_dir>flag to clang to include directories;if you parseC++files then it may be necessary to pass-std=c++03(or higher) flag to inform clang that the file isC++;if your new function/method definition uses external types then
these types must be preliminary declared (not necessary to define them);Remember that aftereditcppfinds common functions/methods
it will simply copy selected text lines from one file to anotherExampleoriginal function/method definition filedest.h:#include<iostream>// helper class that is used by the target class `Src`classSrcPrivate{public:SrcPrivate(){};voidadd(intv){val+=v;}voidsubstract(intv){val-=v;}private:intval;}// target classclassSrc{// method defined inside the classvoidadd(SrcPrivatep,intv){p.add(v);}// method defined outside the classvoidsubstract(SrcPrivatep,intv);// we won't tuch this methodvoiduntouched_print(intv){std::cout<<"The value is:\t"<<v<<std::endl;}}voidSrc::substract(SrcPrivatep,intv){p.substract(v);}// simple functionvoidfoo(int&v){v--;}namespacens{// function in namespacevoidbar(int&v){v++;}}new function/method definition filesrc.h:classSrcPrivate;// declare helper type// target classclassSrc{// NEW method definition inside classvoidadd(SrcPrivatep,intv){p.add(v)+10;}// NEW method definition ouside classvoidsubstract(SrcPrivatep,intv);}voidSrc::substract(SrcPrivatep,intv){p.substract(v)-10;}// NEW simple function definitionvoidfoo(int&v){v-=10;}// NEW function definition in namespacenamespacens{voidbar(int&v){v+=10;}}Run:editcpp --src-file=src.h --dest-file=dest.h --oldfile-keep -std=c++03The-std=c++03tells the clang to parse the files as C++. Also you may need to use any other clang flags like-Ito include directories that are required by the files.--oldfile-keep(default) is used to keep the original file (it will be renamed by adding_OLD_Nsuffix). Otherwise use--oldfile-deleteto delete the original file.Another option is to run the test (though the test deletes all the generated files so you better take a look in/testsdir):python -m unittest cppguts.tests.test_cppguts