我是 libtooling 的初学者,我尝试使用简单的 c++ 代码来学习。我尝试解析/打印 typedef 表达式,如下行:
namespace DEBUG {
typedef void(*function_pointer_t)(int&);
typedef int myInt;
}
clang++ -Xclang -ast-dump -fsyntax-only
输出:
`-NamespaceDecl 0x3e4fe331a8 <test.h:3:1, line:7:1> line:3:11 TESTS
|-TypedefDecl 0x3e4fe333b8 <line:4:2, col:42> col:18 function_pointer_t 'double (*)(int &)'
| `-PointerType 0x3e4fe33350 'double (*)(int &)'
| `-ParenType 0x3e4fe332f0 'double (int &)' sugar
| `-FunctionProtoType 0x3e4fe332b0 'double (int &)' cdecl
| |-BuiltinType 0x3e4fe32b60 'double'
| `-LValueReferenceType 0x3e4fe33210 'int &'
| `-BuiltinType 0x3e4fe32a40 'int'
`-TypedefDecl 0x3e4fe33420 <line:5:2, col:14> col:14 myInt 'int'
`-BuiltinType 0x3e4fe32a40 'int'
为了解析它,我创建了一个继承自MatchFinder::MatchCallback
和重载的类MatchFinder::MatchCallback::run
:
class TypdefDeclFinder : public MatchFinder::MatchCallback {
public:
virtual void run(const MatchFinder::MatchResult& result)
{
auto Item = result.Nodes.getNodeAs<clang::TypedefDecl>("typedefDeclMatch");
if (!Item) return;
if (!IsDeclFromInputFiles(Item, result.SourceManager)) return;
if (!Item->getIdentifier()) return;
if (IsInsideTemplateContext(Item)) return;
print(Item);
}
};
但是Item
指针等于null。我可以解析/打印函数、变量、类、结构模板、方法模板、枚举... with MacthFinder::MatchCallback
,但这种方式不能在 typedef 上运行。这段代码有什么问题?