gpt4 book ai didi

c++ - LibClang clang_getArgType() 返回错误的类型

转载 作者:行者123 更新时间:2023-12-02 19:29:33 24 4
gpt4 key购买 nike

我尝试使用 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:

尽管这两个方法本质上是相同的(除了名称之外),但它错误地将第一个方法的参数检测为整数引用,而它正确地处理了第二个方法。可能是什么原因造成的?

最佳答案

我知道这个问题已经有 9 个月了,但万一其他人偶然发现了这个问题……我也为此苦苦挣扎,直到我意识到我错过了 libclang 发出的诊断消息。 clang 中所有内容的默认类型都是 int,因此如果您对 clang_parseTranslationUnit 的调用缺少包含路径或其他原因失败(但不是致命失败),解析的 (但未定义)类型默认为 int。要解决此问题:在解析翻译单元后,调用这样的函数(警告可能没问题,这取决于您):

oid printDiagnostics(CXTranslationUnit translationUnit){
int nbDiag = clang_getNumDiagnostics(translationUnit);
printf("There are %i diagnostics:\n",nbDiag);

bool foundError = false;
for (unsigned int currentDiag = 0; currentDiag < nbDiag; ++currentDiag) {
CXDiagnostic diagnotic = clang_getDiagnostic(translationUnit, currentDiag);
CXString errorString = clang_formatDiagnostic(diagnotic,clang_defaultDiagnosticDisplayOptions());
std::string tmp{clang_getCString(errorString)};
clang_disposeString(errorString);
if (tmp.find("error:") != std::string::npos) {
foundError = true;
}
std::cerr << tmp << std::endl;
}
if (foundError) {
std::cerr << "Please resolve these issues and try again." <<std::endl;
exit(-1);
}
}

关于c++ - LibClang clang_getArgType() 返回错误的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62005698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com