CDT IASTNode getRawSignature after preprocessor

好久不见. 提交于 2019-12-12 20:27:43

问题


In CDT core plugin, there is a method getRawSignature of interface IASTNode that was described as:

Returns the raw signature of the IASTNode before it is processed by the preprocessor.
Example:
#define ONE 1
int x=ONE; // getRawSignature() for this declaration would return "int x=ONE;"

But I want to get the String signature after the node is processed by the preprocessor
In the above example, the expected string is: int x=1
How to get this string? I have looked at some other methods, but no one can.


回答1:


Great question!

The first thing to note is that, at no point during CDT's processing does code exist in the form of a preprocessed string.

The processing flow is this:

   Unpreprocessed string
-> [Lexer]
-> Unpreprocessed token stream
-> [Preprocessor]
-> Preprocessed token stream
-> [Parser]
-> Abstract syntax tree

Notice that in preprocessed form, the code only exists as a token stream, not as a string.

That said, if you had the preprocessed token stream, you could probably use it to construct a preprocessed string.

Unfortunately, I'm not aware of an easy way to obtain preprocessed tokens, and this mailing list thread suggests there may not be one.

The closest I think we can come is to re-preprocess the file, and thereby obtain preprocessed token stream for the entire file. This can be done by invoking AbstractCLikeLanguage.createScanner() to obtain an IScanner (it's a protected method, so you'd need to derive from GCCLanguage or GPPLanguage to access it), and calling IScanner.nextToken() repeatedly to obtain the preprocessed tokens.

That still doesn't quite give you what you want, since you want preprocessed tokens corresponding to a particular AST node. I believe you can compute this by comparing the offset and length of the preprocessed tokens (obtained using IToken.getOffset() and IToken.getLength()) to the offset and length of the AST node (obtained using ASTNode.getOffset() and ASTNode.getLength()), which I believe are in the same numbering space.



来源:https://stackoverflow.com/questions/37605685/cdt-iastnode-getrawsignature-after-preprocessor

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