C++ header file parsing

只愿长相守 提交于 2019-12-06 13:51:47

问题


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:


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.



来源:https://stackoverflow.com/questions/28825628/c-header-file-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!