libclang返回有关函数声明的太多信息

 fuhuan9479184787 发布于 2023-02-13 20:07

我有以下使用clang-c API的代码.

#include 
#include 
#include 

CXChildVisitResult printVisitor(CXCursor cursor, CXCursor parent, CXClientData client_data) 
{       
    CXCursor cursor1 = clang_getCursorReferenced(cursor);

    CXType type = clang_getCursorType(cursor1);
    CXCursorKind kind = clang_getCursorKind(cursor1);
    CXString str = clang_getTypeSpelling(type);
    CXString str1 = clang_getCursorSpelling(cursor1);
    std::string cstr = clang_getCString(str);
    std::string cstr1 = clang_getCString(str1);

    if(type.kind != 0 && kind == CXCursorKind::CXCursor_FunctionDecl)
    {
        std::cout << "Declaration!\n" << "type is: " << cstr << std::endl;
        std::cout << "name is: " << cstr1 << std::endl;
    }

    return CXChildVisit_Recurse;
}

int main (int argc, char** argv)
{
 CXIndex index = clang_createIndex (
         false, // excludeDeclarationFromPCH
         true   // displayDiagnostics
 );
 CXTranslationUnit unit = clang_parseTranslationUnit (
         index,                           // CIdx
         "main1.cpp",                      // source_filename
         argv + 1 ,                        // command_line_args
         argc - 1 ,                        // num_command_line_args
         0,                                // unsave_files
         0,                                // num_unsaved_files
         CXTranslationUnit_None           // options
 );
 if (unit != 0 )
         std::cout << "Translation unit successfully created" << std::endl;
 else
         std::cout << "Translation unit was not created" << std::endl;

 CXCursor rootCursor = clang_getTranslationUnitCursor(unit);

    clang_visitChildren(rootCursor, printVisitor, NULL);


 clang_disposeTranslationUnit(unit);
 clang_disposeIndex(index);
}

此代码解析以下内容.

double getSum(double a, float b)
{
    return a + b;
}

int main(void)
{
    int a = 5;
    float b = 6;
    double c = a + b;
    return getSum(c, b);
}

程序运行时,我看到以下内容.

    Translation unit successfully created
    Declaration!
    type is: double (double, float)
    name is: getSum
    Declaration!
    type is: int ()
    name is: main
    Declaration!
    type is: double (double, float)
    name is: getSum
    Declaration!
    type is: double (double, float)
    name is: getSum
    Declaration!
    type is: double (double, float)
    name is: getSum
    Declaration!
    type is: double (double, float)
    name is: getSum

为什么我会getSum()在代码中得到如此多的声明?

1 个回答
  • 使用时clang_getCursorReferenced,您将获得CXCursor当前位置引用的内容.例如,函数声明引用自身,并由相应的函数调用引用.在您的示例中,您将因此获得对函数声明(函数声明本身或函数调用)的每个引用的正匹配.

    现在另一件事是每个都CXCursor代表AST的一部分,要么是叶子,要么是带有子部分的更复杂的部分.例如,在遍历AST时,您将依次找到以下游标:

    return getSum (c, b)

    getSum (c, b)

    getSum

    所有这些游标都引用了getSum函数声明,我的猜测是这些是触发多个引用的游标getSum.

    您可以通过调用来检查源代码的哪个部分对应于当前光标 clang_getCursorExtent

    2023-02-13 20:11 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有