简体   繁体   中英

C++ header file parsing

I need to parse a header file. My goal is to search the specific structure from the header file and extract the values and offsets of the structure variables.

Can any one please suggest the best way for parsing the header file by omitting the comments and how to parse macros from header too?

Parsing C++ is tough. You'll likely want to use an existing parser. I know of 4 that are probably useful:

  • Edison Design Group front end
  • Clang's C++ parser
  • DMS Software Reengineering Toolkit and its C++14 front end
  • GCC (via Melt)

Most of these won't "parse" macros; they want to expand them using a preprocessor. So macros and PP conditionals disappear from the parse tree. DMS can do "limited" preprocessing, and collect/keep preprocessor directives and macros found in well structured places and a wide variety of places they commonly occur.

Parsing header files is really tough; they tend to be loaded with conditionals and junk from many previous versions of the software, and idioms from the specific vendor. (MS has some stunningly weird stuff in their headers). Unless you are talking about parsing your header files, make sure you check the tool you choose can handle the dialect of C++ that you are actually handling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM