如何使用libclang获得原始文字的值?
例如,如果我有一个光标类型的CXCursor CXCursor_IntegerLiteral,我如何提取文字值。
更新:
我在使用libclang时遇到了很多问题。我强烈建议完全避免使用clang提供的C++接口。C++接口具有很高的可用性,并且有很好的文档记录:http://clang.llvm.org/doxygen/annotated.html。
我现在看到libclang的唯一目的是像下面的代码那样为您生成ASTUnit对象(否则并不容易):
ASTUnit * astUnit;
{
index = clang_createIndex(0, 0);
tu = clang_parseTranslationUnit(
index, 0,
clangArgs, nClangArgs,
0, 0, CXTranslationUnit_None
);
astUnit = static_cast<ASTUnit *>(tu->TUData);
}现在,您可能会说libclang是稳定的,而C++接口则不稳定。这并不重要,因为您用libclang计算AST并使用它创建脚本的时间无论如何都会浪费很多时间。我宁愿花几个小时来修复那些在版本升级后没有编译的代码(如果需要的话)。
发布于 2012-06-19 19:58:06
在翻译单元中,您已经拥有了所需的所有信息,而不是对原作进行修复:
if (kind == CXCursor_IntegerLiteral)
{
CXSourceRange range = clang_getCursorExtent(cursor);
CXToken *tokens = 0;
unsigned int nTokens = 0;
clang_tokenize(tu, range, &tokens, &nTokens);
for (unsigned int i = 0; i < nTokens; i++)
{
CXString spelling = clang_getTokenSpelling(tu, tokens[i]);
printf("token = %s\n", clang_getCString(spelling));
clang_disposeString(spelling);
}
clang_disposeTokens(tu, tokens, nTokens);
}您将看到第一个标记是整数本身,下一个标记与此无关(例如。我是; for int i = 42;。
发布于 2020-09-12 11:47:54
如果您可以访问CXCursor,则可以使用clang_Cursor_Evaluate函数,例如:
CXChildVisitResult var_decl_visitor(
CXCursor cursor, CXCursor parent, CXClientData data) {
auto kind = clang_getCursorKind(cursor);
switch (kind) {
case CXCursor_IntegerLiteral: {
auto res = clang_Cursor_Evaluate(cursor);
auto value = clang_EvalResult_getAsInt(res);
clang_EvalResult_dispose(res);
std::cout << "IntegerLiteral " << value << std::endl;
break;
}
default:
break;
}
return CXChildVisit_Recurse;
}产出:
IntegerLiteral 42发布于 2012-05-21 21:09:30
通过引用原始文件,我找到了一种方法来做到这一点:
std::string getCursorText (CXCursor cur) {
CXSourceRange range = clang_getCursorExtent(cur);
CXSourceLocation begin = clang_getRangeStart(range);
CXSourceLocation end = clang_getRangeEnd(range);
CXFile cxFile;
unsigned int beginOff;
unsigned int endOff;
clang_getExpansionLocation(begin, &cxFile, 0, 0, &beginOff);
clang_getExpansionLocation(end, 0, 0, 0, &endOff);
ClangString filename = clang_getFileName(cxFile);
unsigned int textSize = endOff - beginOff;
FILE * file = fopen(filename.c_str(), "r");
if (file == 0) {
exit(ExitCode::CANT_OPEN_FILE);
}
fseek(file, beginOff, SEEK_SET);
char buff[4096];
char * pBuff = buff;
if (textSize + 1 > sizeof(buff)) {
pBuff = new char[textSize + 1];
}
pBuff[textSize] = '\0';
fread(pBuff, 1, textSize, file);
std::string res(pBuff);
if (pBuff != buff) {
delete [] pBuff;
}
fclose(file);
return res;
}https://stackoverflow.com/questions/10692015
复制相似问题