如何获取位置的变量名称在clang :: VarDecl [英] How to get location of variable name in clang::VarDecl

查看:614
本文介绍了如何获取位置的变量名称在clang :: VarDecl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用clang 3.0库对C / C ++代码进行一些分析,我需要获取变量声明的位置,我尝试了以下代码:

I'm using clang 3.0 library for some analysis of C/C++ code, and I need to get location of variable declaration, I tried this code:

clang::VarDecl * vd = ...;
clang::SourceManager & srcMgr = ...;

clang::SourceRange loc = vd->getSourceRange();
clang::PresumedLoc locStart = srcMgr.getPresumedLoc(loc.getBegin());
clang::PresumedLoc locEnd = srcMgr.getPresumedLoc(loc.getEnd());

locStart locEnd 开始(和结束)声明变量(具有类型,和可能的初始化)。例如:

But locStart and locEnd points to the beginning (and ending) of declaration variable (with type, and, possibly initialiser). For example:

const char * ptr = 0;
^            ^ ^   ^

locStart 会指向第一个指针( ^ ),locEnd将指向最后一个指针。如何获取第二个和第三个指针的位置(只有名称,没有类型和初始化器)?

locStart will point at the first pointer(^) , and locEnd will point at the last pointer. How can I get the location of the second and third pointers (only name, without type and initialiser)?

推荐答案

目前不在测试的位置,但我想你想提取 SourceLocation getLocation()从您的 VarDecl 。这个函数实际上是由 Decl 基类定义的。虽然它似乎只识别一个位置,似乎可用于识别整个名称(我没有尝试提取其各自的端点,但是,只是用它来表示变量)。

I'm currently not in the position to test it but I think you want to extract the SourceLocation obtained by getLocation() from your VarDecl. This function is actually defined by the Decl base class. Although it seems to identify just one location it seems to be usable to identify the entire name (I haven't tried to extract its individual ends, however, just used it to indicate the variable).

这篇关于如何获取位置的变量名称在clang :: VarDecl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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