如何在 Clang AST 中的 SourceLocation 之后找到字符的 SourceLocation?
How can I find the SourceLocation of a character after a SourceLocation in a Clang AST?
我正在尝试使用 Clang Lexer 库查找标记的位置(特别是命名空间声明的左大括号)。我的想法(因为 NamespaceDecl 没有它的方法)是在命名空间声明开始后找到第一个左大括号的位置。但是,查看 Lexer API,我似乎找不到在访问 AST 时执行此操作的简短方法。任何 suggestions/alternatives 而不必做一些像重新解析代码这样激烈的事情?
这里是找到支架的方法。在我的测试中,它适用于命名和匿名命名空间。
// Assuming ns_decl is pointer to the NamespaceDecl
// sm is reference to the SourceManager
... in addition to the usual headers:
#include "clang/Basic/TokenKinds.h" // for clang::tok
#include "clang/Lex/Lexer.h" // for Lexer
...
clang::LangOptions lopt;
bool skipNewLines = false;
SourceLocation locToUse = ns_decl->isAnonymousNamespace() ?
ns_decl->getLocStart() : ns_decl->getLocation();
SourceLocation next_loc(
clang::Lexer::findLocationAfterToken(
locToUse,clang::tok::l_brace, sm,lopt,skipNewLines));
在像
这样的声明中
namespace NOPQ {
void f(int){}
}
namespace
ABCD
{
void g(float){}
}
namespace {
void h(int){}
}
next_loc 将对应于 NOPQ 的第 1 行,第 18 列;第 7 行,ABCD 的第 7 列;和第 11 行,第 12 列用于匿名 ns。
我正在尝试使用 Clang Lexer 库查找标记的位置(特别是命名空间声明的左大括号)。我的想法(因为 NamespaceDecl 没有它的方法)是在命名空间声明开始后找到第一个左大括号的位置。但是,查看 Lexer API,我似乎找不到在访问 AST 时执行此操作的简短方法。任何 suggestions/alternatives 而不必做一些像重新解析代码这样激烈的事情?
这里是找到支架的方法。在我的测试中,它适用于命名和匿名命名空间。
// Assuming ns_decl is pointer to the NamespaceDecl
// sm is reference to the SourceManager
... in addition to the usual headers:
#include "clang/Basic/TokenKinds.h" // for clang::tok
#include "clang/Lex/Lexer.h" // for Lexer
...
clang::LangOptions lopt;
bool skipNewLines = false;
SourceLocation locToUse = ns_decl->isAnonymousNamespace() ?
ns_decl->getLocStart() : ns_decl->getLocation();
SourceLocation next_loc(
clang::Lexer::findLocationAfterToken(
locToUse,clang::tok::l_brace, sm,lopt,skipNewLines));
在像
这样的声明中namespace NOPQ {
void f(int){}
}
namespace
ABCD
{
void g(float){}
}
namespace {
void h(int){}
}
next_loc 将对应于 NOPQ 的第 1 行,第 18 列;第 7 行,ABCD 的第 7 列;和第 11 行,第 12 列用于匿名 ns。