I'm currently trying to parse a C header file AND retain the name of some macro-defined values.
As Tim notes in the comments below the preprocessor does its job and ultimately derives that raw value that will be used in the code. However, I was hoping to accomplish both generating the AST of the header file AND extracting, or retaining easy access to the name of the macro-defined values.
All of that to say, and ask, is there any way of utilizing pycparser to extract raw macro defined values, or is this out of the scope or not how the tool was intended to be used?
My code is rather simple, and as mentioned earlier, is outputting most of what I would expect to be there minus macro defined values.
ast = parse_file(filename, use_cpp=True,
cpp_path=CC, # CC = gcc
cpp_args=[
'-E',
r'-I/path/to/fake_libc_include',
r'-I/other/includes'
]
)
ast.show()
Say for example I make a main_file.c, and I include the header I want to parse.
#include <target_header.h>
int main() {
int i = foobar; // #define foobar 0x3
return 0;
}
I then do the same process of parsing the C file rather than header file using pycparser. I will get the following:
FuncDef:
Decl: main, [], [], [], []
FuncDecl:
TypeDecl: main, [], None
IdentifierType: ['int']
Compound:
Decl: i, [], [], [], []
TypeDecl: i, [], None
IdentifierType: ['int']
Constant: int, 0x0003
Return:
Constant: int, 0
So the information of the macro value of the defined macro and the preprocessor only cares about the value, as expected. Ultimately I was hoping for a helper function from pycparser that does "pre-preprocessor" lifting so to speak of the actual name of the macro-define values but I think I might be running into a wall of pycparser not being built for that purpose.
Perhaps just using a separate approach or tool such as the one listed here might be the best bet but let me know if anyone has done something similar with pycparsrer so as to avoid using more than one library:
Use C preprocessor macros (just the constants) as Python variables
UPDATE: this question is based on a required capability that is outside of the scope of pycparser but I am keeping this question and answering using my approach in case anyone else runs into the same need.