Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc for parsing code

Tags:

c++

c

parsing

gcc

I would like to know how to use GCC as a library to parse C/C++/Java/Objective C/Ada code for my program. I want to bypass prepocessing and prefix all the functions that are user written with a prefix My. like so Print(); becomes MyPrint(); I also wish to do this with the variables.

like image 703
Taylor Ramirez Avatar asked Nov 15 '11 23:11

Taylor Ramirez


1 Answers

You can look here:
http://codesynthesis.com/~boris/blog/2010/05/03/parsing-cxx-with-gcc-plugin-part-1/

This is description of how to use gcc plugin interface to parse C++ code. Other language should be handled in the same manner.

Also you can try pork from mozilla:
https://wiki.mozilla.org/Pork

When I tried it (pork), I spend hour or so to fix compile problems, but then I can write scripts like this:

rewrite SyncPrimitiveUpgrade {
  type PRLock* => Mutex*
  call PR_NewLock() => new Mutex()
  call PR_Lock(lock) => lock->Lock()
  call PR_Unlock(lock) => lock->Unlock()
  call PR_DestroyLock(lock) => delete lock
}

so it found all type PRLock and replate it with Mutex, also it search call of functions like PR_NewLock and replace it with "new Mutex".

like image 189
fghj Avatar answered Sep 18 '22 02:09

fghj