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.
c++
but maximum number of tags is 5. There are no tagscode-extraction
orcode-transformation
... and any way the maximum number of tags was already reached. :-) – Fromascripting
originally but had to remove due to limit. – Fromaautomation
is described as follows: Automation is the process of having a computer do a repetitive task or a task that requires great precision or multiple steps, without requiring human intervention. This seems what I am craving for. I do not trust myself as much as I trust a program to do such transformation (in my case I wanted to transform an array of complex data type of about 100 entries - I did it already programmatically but am dreaming of a more generic solution). – Fromagcc-melt
andsparse
links. Seem very interesting although by quick reading not useful for my use case now. I will take time to study in more detail. – Froma#ifdef
...#elif
...#else
...#endif
) at least due to architecture differences - data sizes - and predefined macros. However, by very quick experiment I could not make use of the output files with output files resulting from use of-fdump-tree-*
options. In my recall, the output seemed to have more information about the code than data structures which I was for my current use case interested in. – Froma