【发布时间】:2020-05-25 15:30:02
【问题描述】:
我尝试使用 libClang 解析 C++ 方法,但是在尝试获取函数的参数/参数时,有时会给出错误的类型。
例子:
我有两种不同的方法
std::string Method::exportMethod(std::map<std::string, std::string> &defines) const
和
std::string Field::exportField(std::map<std::string, std::string> &defines) const
然后我打印 AST(用于调试目的)
CXChildVisitResult printer::printVisitor(CXCursor c, CXCursor parent, CXClientData clientData) {
recursivePrintData data = *static_cast<recursivePrintData *>(clientData);
*(data.stream) <<
data.indent <<
clang_getCursorKindSpelling(clang_getCursorKind(c)) <<
"; name: " << clang_getCursorSpelling(c) <<
", type: " << clang_getCursorType(c) <<
", arg0Type: " << clang_getArgType(clang_getCursorType(c), 0) <<
std::endl;
recursivePrintData newDat(data);
data.indent += " ";
clang_visitChildren(c, printVisitor, (void *) &data);
return CXChildVisit_Recurse;
}
(recursivePrintData 是一个包含输出流和当前缩进级别的结构)
这两种方法的输出如下:
导出方法:
CXXMethod; name: exportMethod, type: std::string (int &) const, arg0Type: int &
NamespaceRef; name: std, type: , arg0Type:
TypeRef; name: std::string, type: std::string, arg0Type:
ParmDecl; name: defines, type: int &, arg0Type:
导出字段:
CXXMethod; name: exportField, type: std::string (std::map<std::string, std::string> &) const, arg0Type: std::map<std::string, std::string> &
NamespaceRef; name: std, type: , arg0Type:
TypeRef; name: std::string, type: std::string, arg0Type:
ParmDecl; name: defines, type: std::map<std::string, std::string> &, arg0Type:
NamespaceRef; name: std, type: , arg0Type:
TemplateRef; name: map, type: , arg0Type:
NamespaceRef; name: std, type: , arg0Type:
TypeRef; name: std::string, type: std::string, arg0Type:
NamespaceRef; name: std, type: , arg0Type:
TypeRef; name: std::string, type: std::string, arg0Type:
NamespaceRef; name: std, type: , arg0Type:
TemplateRef; name: map, type: , arg0Type:
NamespaceRef; name: std, type: , arg0Type:
TypeRef; name: std::string, type: std::string, arg0Type:
NamespaceRef; name: std, type: , arg0Type:
TypeRef; name: std::string, type: std::string, arg0Type:
尽管这两种方法本质上是相同的(除了名称),但它会错误地将第一个方法的参数检测为整数引用,而正确处理第二个方法。这可能是什么原因造成的?
【问题讨论】:
-
exportMethod有没有超载? -
@cigien 不,到目前为止我的代码库中没有继承
-
不,即使没有继承,
Method是否还有另一个名为exportMethod的方法? -
@cigien 不,它甚至是唯一名为“exportMethod”的方法
标签: c++ clang abstract-syntax-tree libclang