If you want to do this reliably, you will need the full C interface and the ability to modify collapsible code.
Our DMS Software Reengineering Toolkit with its C Front End will probably do what you want. DMS can analyze, build AST and perform custom transformations in the source text, either procedural or as surface syntactic transformations.
There are some problems with macros and preprocessor directives; if you parse and save them, in many cases it can do, but they expand such directives when they are not “structured”. Saving means you are not getting a character table. If you expand all the directives, after parsing you can get a character table with the same contents as the C compiler.
For a specific OP task, he would like to write a source for the source, transforming something like the following:
rule decorate_function_definition_with_pragma(fh:function_head, b: block): declaration -> declaration = " \fh \b " -> " \fh #pragma my_pragma \b " if some_condiiton(fh);
where "my_pragma" is replaced with essentially the pragmatic text that it wants, and some_condition is a regular predicate that filters the matchingheader_functions for which the pragma must be inserted.
The pattern maps to the syntax tree, so it cannot have a mismatch, such as sed or a regular expression. The secret is that template variables refer to grammar rules in C Front End; a function variable of type function_head can only match trees that can satisfy the grammar rules of function_head.
To perform this transformation, only once to determine each detected function requires some trivial control logic.
source share