I am desiring a command-line tool to extract a definition or declaration (typedef, structure, enumeration, variable, or function) from a C or C++ source file. Also a way to replace an existing definition/declaration would be handy (after transforming the extracted definition by a user-submitted script). Is there such generic tool available, or is some resonably close approximation of such a tool?
Scriptability and ability to hook-up with user created scripts or programs is of importance here, although I am academically curious of GUI programs too. Open source solutions for Unix/Linux camp are preferred (although I am curious of Windows and OS X tools too). Primary language interests are C and C++ but more generic solution would be even better (I think we do not need super accurate parsing capabilities for finding, extracting and replacing a definition in a program source file).
Sample Use Cases (extra - for the curious mind):
- Given deeply nested
struct
s and variable (array) initializations of these types, suppose there is a need to change a struct definition by adding or reordering fields or rewriting the variable/array definitions in more readable format without introducing errors resulting from manual labor. This would work by extracting the old initializations, then using a script/program to write the new initializations to replace the old ones. - For implementing a code browsing tool - extract a definition.
- Decorative code generation (e.g. logging function entries / returns).
- Scripted code structuring (e.g. extract this and that thing and put in different place without change - version control commit comment could document the command to perform this operation to make it evident and verifiable that nothing changed).
Alternative problem: If there is a tool to tell the location of the definition (beginning and end line would suffice - we could even assume all the definitions/declarations we are interested in are in their own line), then it would a simply exercise of finger dexterity to to write a program to
- extract definitions,
- replace definitions, or even
extract a definition, run a program specified by command line options (or an editor) to
- receive the desired extracted definitions from
stdin
(or from a temporary file), - perform the transformation (editing), and
- output the new definitions to
stdout
(or save them to the given temporary file)
to be replaced by the executing program.
- receive the desired extracted definitions from
So the major, more challenging problem would be finding the begin and end line of the definition.
Note about tags: More accurate tag than code-generation
would be code-transformation
but it does not exist.