预处理程序后的CDT IASTNode getRawSignature [英] CDT IASTNode getRawSignature after preprocessor

查看:105
本文介绍了预处理程序后的CDT IASTNode getRawSignature的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CDT核心插件中,接口 IASTNode 的方法 getRawSignature 被描述为:

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;"

但是我想在节点被处理后后得到字符串签名预处理程序

在上面的示例中,预期的字符串为: int x = 1

如何获取此字符串?我看过其他方法,但是没人能。

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.

推荐答案

好问题!

首先要注意的是,在CDT的处理过程中,没有任何代码以预处理字符串的形式存在。

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

   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.

我认为我们可以最接近的方法是重新预处理文件,从而获得整个文件的预处理令牌流。可以通过调用 AbstractCLikeLanguage.createScanner()来获得 IScanner (这是一种受保护的方法,因此您可以d需要从 GCCLanguage GPPLanguage 派生访问它),并调用 IScanner.nextToken ()反复获取预处理的令牌。

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.

这仍然不能完全满足您的需求,因为您想要的预处理令牌对应于特定的AST节点。我相信,您可以通过比较预处理标记的偏移量和长度(使用 IToken.getOffset() IToken.getLength())到AST节点的偏移量和长度(使用 ASTNode.getOffset() ASTNode.getLength()),我相信它们在相同的编号空间中。

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.

这篇关于预处理程序后的CDT IASTNode getRawSignature的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆