【问题标题】:LibClang clang_getArgType() returns wrong typeLibClang clang_getArgType() 返回错误的类型
【发布时间】: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


【解决方案1】:

我知道这个问题已有 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);
    }
}

【讨论】:

  • 太棒了,我终于可以继续我的项目了
猜你喜欢
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多