C++ header file parsing

2019-04-15 02:28发布

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?

1条回答
放荡不羁爱自由
2楼-- · 2019-04-15 03:12

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.

查看更多
登录 后发表回答